Skip to content

Commit 6d2bd54

Browse files
use body's param-env when checking if type needs drop
1 parent c2ecd3a commit 6d2bd54

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
431431
let lhs = &self.thir[lhs];
432432
if let ty::Adt(adt_def, _) = lhs.ty.kind() && adt_def.is_union() {
433433
if let Some((assigned_ty, assignment_span)) = self.assignment_info {
434-
if assigned_ty.needs_drop(self.tcx, self.tcx.param_env(adt_def.did())) {
434+
if assigned_ty.needs_drop(self.tcx, self.param_env) {
435435
// This would be unsafe, but should be outright impossible since we reject such unions.
436-
self.tcx.sess.delay_span_bug(assignment_span, "union fields that need dropping should be impossible");
436+
self.tcx.sess.delay_span_bug(assignment_span, format!("union fields that need dropping should be impossible: {assigned_ty}"));
437437
}
438438
} else {
439439
self.requires_unsafe(expr.span, AccessToUnionField);

compiler/rustc_mir_transform/src/check_unsafety.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,11 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
219219
// We have to check the actual type of the assignment, as that determines if the
220220
// old value is being dropped.
221221
let assigned_ty = place.ty(&self.body.local_decls, self.tcx).ty;
222-
if assigned_ty.needs_drop(
223-
self.tcx,
224-
self.tcx.param_env(base_ty.ty_adt_def().unwrap().did()),
225-
) {
222+
if assigned_ty.needs_drop(self.tcx, self.param_env) {
226223
// This would be unsafe, but should be outright impossible since we reject such unions.
227224
self.tcx.sess.delay_span_bug(
228225
self.source_info.span,
229-
"union fields that need dropping should be impossible",
226+
format!("union fields that need dropping should be impossible: {assigned_ty}")
230227
);
231228
}
232229
} else {

src/test/ui/union/issue-99375.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// check-pass
2+
3+
union URes<R: Copy> {
4+
uninit: (),
5+
init: R,
6+
}
7+
8+
struct Params<F, R: Copy> {
9+
function: F,
10+
result: URes<R>,
11+
}
12+
13+
unsafe extern "C" fn do_call<F, R>(params: *mut Params<F, R>)
14+
where
15+
R: Copy,
16+
F: Fn() -> R,
17+
{
18+
(*params).result.init = ((*params).function)();
19+
}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)