1
Fork 0

Remove redundant union check in `KnownPanicsLint const prop

because we are already marking unions `NoPropagation` in
`CanConstProp::check()`. That is enough to prevent any attempts
at const propagating unions and this second check is not needed.

Also improve a comment in `CanConstProp::check()`
This commit is contained in:
Gurinder Singh 2024-04-30 15:17:47 +05:30
parent f9dca46218
commit 741d40f327

View file

@ -583,21 +583,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
val.into() val.into()
} }
Aggregate(ref kind, ref fields) => { Aggregate(ref kind, ref fields) => Value::Aggregate {
// Do not const prop union fields as they can be
// made to produce values that don't match their
// underlying layout's type (see ICE #121534).
// If the last element of the `Adt` tuple
// is `Some` it indicates the ADT is a union
if let AggregateKind::Adt(_, _, _, _, Some(_)) = **kind {
return None;
};
Value::Aggregate {
fields: fields fields: fields
.iter() .iter()
.map(|field| { .map(|field| self.eval_operand(field).map_or(Value::Uninit, Value::Immediate))
self.eval_operand(field).map_or(Value::Uninit, Value::Immediate)
})
.collect(), .collect(),
variant: match **kind { variant: match **kind {
AggregateKind::Adt(_, variant, _, _, _) => variant, AggregateKind::Adt(_, variant, _, _, _) => variant,
@ -608,8 +597,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
| AggregateKind::Coroutine(_, _) | AggregateKind::Coroutine(_, _)
| AggregateKind::CoroutineClosure(_, _) => VariantIdx::ZERO, | AggregateKind::CoroutineClosure(_, _) => VariantIdx::ZERO,
}, },
} },
}
Repeat(ref op, n) => { Repeat(ref op, n) => {
trace!(?op, ?n); trace!(?op, ?n);
@ -897,8 +885,9 @@ impl CanConstProp {
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() { for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
let ty = body.local_decls[local].ty; let ty = body.local_decls[local].ty;
if ty.is_union() { if ty.is_union() {
// Do not const prop unions as they can // Unions are incompatible with the current implementation of
// ICE during layout calc // const prop because Rust has no concept of an active
// variant of a union
*val = ConstPropMode::NoPropagation; *val = ConstPropMode::NoPropagation;
} else { } else {
match tcx.layout_of(param_env.and(ty)) { match tcx.layout_of(param_env.and(ty)) {