use body's param-env when checking if type needs drop

This commit is contained in:
Michael Goulet 2022-07-17 10:56:12 -07:00
parent c2ecd3af87
commit 6d2bd541e0
3 changed files with 25 additions and 7 deletions

View File

@ -431,9 +431,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
let lhs = &self.thir[lhs]; let lhs = &self.thir[lhs];
if let ty::Adt(adt_def, _) = lhs.ty.kind() && adt_def.is_union() { if let ty::Adt(adt_def, _) = lhs.ty.kind() && adt_def.is_union() {
if let Some((assigned_ty, assignment_span)) = self.assignment_info { 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. // 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 { } else {
self.requires_unsafe(expr.span, AccessToUnionField); self.requires_unsafe(expr.span, AccessToUnionField);

View File

@ -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 // We have to check the actual type of the assignment, as that determines if the
// old value is being dropped. // old value is being dropped.
let assigned_ty = place.ty(&self.body.local_decls, self.tcx).ty; let assigned_ty = place.ty(&self.body.local_decls, self.tcx).ty;
if assigned_ty.needs_drop( if assigned_ty.needs_drop(self.tcx, self.param_env) {
self.tcx,
self.tcx.param_env(base_ty.ty_adt_def().unwrap().did()),
) {
// This would be unsafe, but should be outright impossible since we reject such unions. // This would be unsafe, but should be outright impossible since we reject such unions.
self.tcx.sess.delay_span_bug( self.tcx.sess.delay_span_bug(
self.source_info.span, self.source_info.span,
"union fields that need dropping should be impossible", format!("union fields that need dropping should be impossible: {assigned_ty}")
); );
} }
} else { } else {

View File

@ -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() {}