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 {
- // To avoid semver hazard, we only consider `Copy` and `ManuallyDrop` non-dropping.
- if !(assigned_ty
- .ty_adt_def()
- .map_or(false, |adt| adt.is_manually_drop())
- || assigned_ty
- .is_copy_modulo_regions(self.tcx.at(expr.span), self.param_env))
- {
- self.requires_unsafe(assignment_span, AssignToDroppingUnionField);
- } else {
- // write to non-drop union field, safe
+ 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, format!("union fields that need dropping should be impossible: {assigned_ty}"));
}
} else {
self.requires_unsafe(expr.span, AccessToUnionField);
UseOfMutableStatic,
UseOfExternStatic,
DerefOfRawPointer,
- AssignToDroppingUnionField,
AccessToUnionField,
MutationOfLayoutConstrainedField,
BorrowOfLayoutConstrainedField,
UseOfMutableStatic => "use of mutable static",
UseOfExternStatic => "use of extern static",
DerefOfRawPointer => "dereference of raw pointer",
- AssignToDroppingUnionField => "assignment to union field that might need dropping",
AccessToUnionField => "access to union field",
MutationOfLayoutConstrainedField => "mutation of layout constrained field",
BorrowOfLayoutConstrainedField => {
"raw pointers may be null, dangling or unaligned; they can violate aliasing rules \
and cause data races: all of these are undefined behavior",
),
- AssignToDroppingUnionField => (
- Cow::Borrowed(self.simple_description()),
- "the previous content of the field will be dropped, which causes undefined \
- behavior if the field was not properly initialized",
- ),
AccessToUnionField => (
Cow::Borrowed(self.simple_description()),
"the field may not be properly initialized: using uninitialized data will cause \
pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalDefId>) {
// THIR unsafeck is gated under `-Z thir-unsafeck`
- if !tcx.sess.opts.debugging_opts.thir_unsafeck {
+ if !tcx.sess.opts.unstable_opts.thir_unsafeck {
return;
}