@@ -25,13 +25,14 @@ use crate::source_map::Spanned;
25
25
use crate :: edition:: { ALL_EDITIONS , Edition } ;
26
26
use crate :: visit:: { self , FnKind , Visitor } ;
27
27
use crate :: parse:: { token, ParseSess } ;
28
+ use crate :: parse:: parser:: Parser ;
28
29
use crate :: symbol:: { Symbol , sym} ;
29
30
use crate :: tokenstream:: TokenTree ;
30
31
31
32
use errors:: { Applicability , DiagnosticBuilder , Handler } ;
32
33
use rustc_data_structures:: fx:: FxHashMap ;
33
34
use rustc_target:: spec:: abi:: Abi ;
34
- use syntax_pos:: { Span , DUMMY_SP } ;
35
+ use syntax_pos:: { Span , DUMMY_SP , MultiSpan } ;
35
36
use log:: debug;
36
37
use lazy_static:: lazy_static;
37
38
@@ -566,6 +567,9 @@ declare_features! (
566
567
// #[repr(transparent)] on unions.
567
568
( active, transparent_unions, "1.37.0" , Some ( 60405 ) , None ) ,
568
569
570
+ // Allows explicit discriminants on non-unit enum variants.
571
+ ( active, arbitrary_enum_discriminant, "1.37.0" , Some ( 60553 ) , None ) ,
572
+
569
573
// -------------------------------------------------------------------------
570
574
// feature-group-end: actual feature gates
571
575
// -------------------------------------------------------------------------
@@ -1705,20 +1709,20 @@ pub fn emit_feature_err(
1705
1709
feature_err ( sess, feature, span, issue, explain) . emit ( ) ;
1706
1710
}
1707
1711
1708
- pub fn feature_err < ' a > (
1712
+ pub fn feature_err < ' a , S : Into < MultiSpan > > (
1709
1713
sess : & ' a ParseSess ,
1710
1714
feature : Symbol ,
1711
- span : Span ,
1715
+ span : S ,
1712
1716
issue : GateIssue ,
1713
1717
explain : & str ,
1714
1718
) -> DiagnosticBuilder < ' a > {
1715
1719
leveled_feature_err ( sess, feature, span, issue, explain, GateStrength :: Hard )
1716
1720
}
1717
1721
1718
- fn leveled_feature_err < ' a > (
1722
+ fn leveled_feature_err < ' a , S : Into < MultiSpan > > (
1719
1723
sess : & ' a ParseSess ,
1720
1724
feature : Symbol ,
1721
- span : Span ,
1725
+ span : S ,
1722
1726
issue : GateIssue ,
1723
1727
explain : & str ,
1724
1728
level : GateStrength ,
@@ -2033,6 +2037,29 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
2033
2037
}
2034
2038
}
2035
2039
2040
+ ast:: ItemKind :: Enum ( ast:: EnumDef { ref variants, ..} , ..) => {
2041
+ for variant in variants {
2042
+ match ( & variant. node . data , & variant. node . disr_expr ) {
2043
+ ( ast:: VariantData :: Unit ( ..) , _) => { } ,
2044
+ ( _, Some ( disr_expr) ) =>
2045
+ gate_feature_post ! (
2046
+ & self ,
2047
+ arbitrary_enum_discriminant,
2048
+ disr_expr. value. span,
2049
+ "discriminants on non-unit variants are experimental" ) ,
2050
+ _ => { } ,
2051
+ }
2052
+ }
2053
+
2054
+ let has_feature = self . context . features . arbitrary_enum_discriminant ;
2055
+ if !has_feature && !i. span . allows_unstable ( sym:: arbitrary_enum_discriminant) {
2056
+ Parser :: maybe_report_invalid_custom_discriminants (
2057
+ self . context . parse_sess ,
2058
+ & variants,
2059
+ ) ;
2060
+ }
2061
+ }
2062
+
2036
2063
ast:: ItemKind :: Impl ( _, polarity, defaultness, _, _, _, _) => {
2037
2064
if polarity == ast:: ImplPolarity :: Negative {
2038
2065
gate_feature_post ! ( & self , optin_builtin_traits,
0 commit comments