Skip to content

Commit

Permalink
Use the correct place for enum variants.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Feb 18, 2023
1 parent 7213eaa commit efb4688
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 14 additions & 6 deletions compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,21 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
AggregateKind::Adt(def_id, variant_index, ..) => {
match self.tcx.def_kind(def_id) {
DefKind::Struct => (Some(target_idx), None),
DefKind::Enum => (Some(target_idx), Some(variant_index)),
DefKind::Enum => (
self.map.apply(target_idx, TrackElem::Variant(variant_index)),
Some(variant_index),
),
_ => (None, None),
}
}
_ => (None, None),
};
if let Some(target) = variant_target {
if let Some(variant_target_idx) = variant_target {
for (field_index, operand) in operands.iter().enumerate() {
if let Some(field) = self
.map()
.apply(target, TrackElem::Field(Field::from_usize(field_index)))
{
if let Some(field) = self.map().apply(
variant_target_idx,
TrackElem::Field(Field::from_usize(field_index)),
) {
let result = self.handle_operand(operand, state);
state.insert_idx(field, result, self.map());
}
Expand All @@ -154,6 +157,11 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
if let Some(variant_index) = variant_index
&& let Some(discr_idx) = self.map().apply(target_idx, TrackElem::Discriminant)
{
// We are assigning the discriminant as part of an aggregate.
// This discriminant can only alias a variant field's value if the operand
// had an invalid value for that type.
// Using invalid values is UB, so we are allowed to perform the assignment
// without extra flooding.
let enum_ty = target.ty(self.local_decls, self.tcx).ty;
if let Some(discr_val) = self.eval_discriminant(enum_ty, variant_index) {
state.insert_value_idx(discr_idx, FlatSet::Elem(discr_val), &self.map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@

bb3: {
StorageLive(_4); // scope 1 at $DIR/enum.rs:+2:29: +2:30
_4 = ((_1 as V1).0: i32); // scope 1 at $DIR/enum.rs:+2:29: +2:30
_2 = _4; // scope 3 at $DIR/enum.rs:+2:35: +2:36
- _4 = ((_1 as V1).0: i32); // scope 1 at $DIR/enum.rs:+2:29: +2:30
- _2 = _4; // scope 3 at $DIR/enum.rs:+2:35: +2:36
+ _4 = const 0_i32; // scope 1 at $DIR/enum.rs:+2:29: +2:30
+ _2 = const 0_i32; // scope 3 at $DIR/enum.rs:+2:35: +2:36
StorageDead(_4); // scope 1 at $DIR/enum.rs:+2:35: +2:36
goto -> bb4; // scope 1 at $DIR/enum.rs:+2:35: +2:36
}
Expand Down

0 comments on commit efb4688

Please sign in to comment.