Skip to content

Commit

Permalink
Fix a variant index and variant discriminant confusion
Browse files Browse the repository at this point in the history
Previously for enums using the `Variants::Single` layout, the variant
index was being confused with its discriminant. For example, in the case
of `enum E { A = 1 }`.

Use `discriminant_for_variant` to avoid the issue.
  • Loading branch information
tmiasko committed Nov 20, 2021
1 parent d5a91f3 commit c3e71d8
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ fn variant_discriminants<'tcx>(
match &layout.variants {
Variants::Single { index } => {
let mut res = FxHashSet::default();
res.insert(index.as_u32() as u128);
res.insert(
ty.discriminant_for_variant(tcx, *index)
.map_or(index.as_u32() as u128, |discr| discr.val),
);
res
}
Variants::Multiple { variants, .. } => variants
Expand Down

0 comments on commit c3e71d8

Please sign in to comment.