|
1 | 1 | mod helpers;
|
2 | 2 |
|
3 |
| - |
4 | 3 | use aster;
|
5 | 4 |
|
6 | 5 | use ir::annotations::FieldAccessorKind;
|
@@ -1667,7 +1666,22 @@ impl CodeGenerator for Enum {
|
1667 | 1666 | Some(&name)
|
1668 | 1667 | };
|
1669 | 1668 |
|
1670 |
| - for variant in self.variants().iter() { |
| 1669 | + // NB: We defer the creation of constified variants, in case we find |
| 1670 | + // another variant with the same value (which is the common thing to |
| 1671 | + // do). |
| 1672 | + let mut constified_variants = vec![]; |
| 1673 | + |
| 1674 | + let mut iter = self.variants().iter().peekable(); |
| 1675 | + |
| 1676 | + while iter.peek().is_some() || !constified_variants.is_empty() { |
| 1677 | + let variant = iter.next() |
| 1678 | + .unwrap_or_else(|| constified_variants.remove(0)); |
| 1679 | + |
| 1680 | + if variant.force_constification() && iter.peek().is_some() { |
| 1681 | + constified_variants.push(variant); |
| 1682 | + continue; |
| 1683 | + } |
| 1684 | + |
1671 | 1685 | match seen_values.entry(variant.val()) {
|
1672 | 1686 | Entry::Occupied(ref entry) => {
|
1673 | 1687 | if is_rust_enum {
|
@@ -1707,9 +1721,11 @@ impl CodeGenerator for Enum {
|
1707 | 1721 |
|
1708 | 1722 | let variant_name = ctx.rust_mangle(variant.name());
|
1709 | 1723 |
|
1710 |
| - // If it's an unnamed enum, we also generate a constant so |
1711 |
| - // it can be properly accessed. |
1712 |
| - if is_rust_enum && enum_ty.name().is_none() { |
| 1724 | + // If it's an unnamed enum, or constification is enforced, |
| 1725 | + // we also generate a constant so it can be properly |
| 1726 | + // accessed. |
| 1727 | + if (is_rust_enum && enum_ty.name().is_none()) || |
| 1728 | + variant.force_constification() { |
1713 | 1729 | let mangled_name = if is_toplevel {
|
1714 | 1730 | variant_name.clone()
|
1715 | 1731 | } else {
|
|
0 commit comments