Skip to content

Commit

Permalink
Rollup merge of rust-lang#99384 - compiler-errors:issue-99375, r=oli-obk
Browse files Browse the repository at this point in the history
use body's param-env when checking if type needs drop

The type comes from the body, so we should be using the body's param-env, as opposed to the ADT's param env, because we know less in the latter compared to the former.

Fixes rust-lang#99375
  • Loading branch information
matthiaskrgr authored Jul 18, 2022
2 parents 517a9a7 + 6d2bd54 commit ab0b75d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
let lhs = &self.thir[lhs];
if let ty::Adt(adt_def, _) = lhs.ty.kind() && adt_def.is_union() {
if let Some((assigned_ty, assignment_span)) = self.assignment_info {
if assigned_ty.needs_drop(self.tcx, self.tcx.param_env(adt_def.did())) {
if assigned_ty.needs_drop(self.tcx, self.param_env) {
// This would be unsafe, but should be outright impossible since we reject such unions.
self.tcx.sess.delay_span_bug(assignment_span, "union fields that need dropping should be impossible");
self.tcx.sess.delay_span_bug(assignment_span, format!("union fields that need dropping should be impossible: {assigned_ty}"));
}
} else {
self.requires_unsafe(expr.span, AccessToUnionField);
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_mir_transform/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,11 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
// We have to check the actual type of the assignment, as that determines if the
// old value is being dropped.
let assigned_ty = place.ty(&self.body.local_decls, self.tcx).ty;
if assigned_ty.needs_drop(
self.tcx,
self.tcx.param_env(base_ty.ty_adt_def().unwrap().did()),
) {
if assigned_ty.needs_drop(self.tcx, self.param_env) {
// This would be unsafe, but should be outright impossible since we reject such unions.
self.tcx.sess.delay_span_bug(
self.source_info.span,
"union fields that need dropping should be impossible",
format!("union fields that need dropping should be impossible: {assigned_ty}")
);
}
} else {
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/union/issue-99375.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// check-pass

union URes<R: Copy> {
uninit: (),
init: R,
}

struct Params<F, R: Copy> {
function: F,
result: URes<R>,
}

unsafe extern "C" fn do_call<F, R>(params: *mut Params<F, R>)
where
R: Copy,
F: Fn() -> R,
{
(*params).result.init = ((*params).function)();
}

fn main() {}

0 comments on commit ab0b75d

Please sign in to comment.