@@ -14,6 +14,8 @@ use rustc_span::hygiene::Transparency;
14
14
use rustc_span:: { symbol:: sym, symbol:: Symbol , Span } ;
15
15
use std:: num:: NonZeroU32 ;
16
16
17
+ use crate :: session_diagnostics;
18
+
17
19
pub fn is_builtin_attr ( attr : & Attribute ) -> bool {
18
20
attr. is_doc_comment ( ) || attr. ident ( ) . filter ( |ident| is_builtin_attr_name ( ident. name ) ) . is_some ( )
19
21
}
@@ -25,46 +27,38 @@ enum AttrError {
25
27
NonIdentFeature ,
26
28
MissingFeature ,
27
29
MultipleStabilityLevels ,
28
- UnsupportedLiteral ( & ' static str , /* is_bytestr */ bool ) ,
30
+ UnsupportedLiteral ( UnsupportedLiteralReason , /* is_bytestr */ bool ) ,
31
+ }
32
+
33
+ pub ( crate ) enum UnsupportedLiteralReason {
34
+ Generic ,
35
+ CfgString ,
36
+ DeprecatedString ,
37
+ DeprecatedKvPair ,
29
38
}
30
39
31
40
fn handle_errors ( sess : & ParseSess , span : Span , error : AttrError ) {
32
- let diag = & sess. span_diagnostic ;
33
41
match error {
34
42
AttrError :: MultipleItem ( item) => {
35
- struct_span_err ! ( diag , span , E0538 , "multiple '{}' items" , item) . emit ( ) ;
43
+ sess . emit_err ( session_diagnostics :: MultipleItem { span , item } ) ;
36
44
}
37
45
AttrError :: UnknownMetaItem ( item, expected) => {
38
- let expected = expected. iter ( ) . map ( |name| format ! ( "`{}`" , name) ) . collect :: < Vec < _ > > ( ) ;
39
- struct_span_err ! ( diag, span, E0541 , "unknown meta item '{}'" , item)
40
- . span_label ( span, format ! ( "expected one of {}" , expected. join( ", " ) ) )
41
- . emit ( ) ;
46
+ sess. emit_err ( session_diagnostics:: UnknownMetaItem { span, item, expected } ) ;
42
47
}
43
48
AttrError :: MissingSince => {
44
- struct_span_err ! ( diag , span, E0542 , "missing 'since'" ) . emit ( ) ;
49
+ sess . emit_err ( session_diagnostics :: MissingSince { span } ) ;
45
50
}
46
51
AttrError :: NonIdentFeature => {
47
- struct_span_err ! ( diag , span, E0546 , "'feature' is not an identifier" ) . emit ( ) ;
52
+ sess . emit_err ( session_diagnostics :: NonIdentFeature { span } ) ;
48
53
}
49
54
AttrError :: MissingFeature => {
50
- struct_span_err ! ( diag , span, E0546 , "missing 'feature'" ) . emit ( ) ;
55
+ sess . emit_err ( session_diagnostics :: MissingFeature { span } ) ;
51
56
}
52
57
AttrError :: MultipleStabilityLevels => {
53
- struct_span_err ! ( diag , span, E0544 , "multiple stability levels" ) . emit ( ) ;
58
+ sess . emit_err ( session_diagnostics :: MultipleStabilityLevels { span } ) ;
54
59
}
55
- AttrError :: UnsupportedLiteral ( msg, is_bytestr) => {
56
- let mut err = struct_span_err ! ( diag, span, E0565 , "{}" , msg) ;
57
- if is_bytestr {
58
- if let Ok ( lint_str) = sess. source_map ( ) . span_to_snippet ( span) {
59
- err. span_suggestion (
60
- span,
61
- "consider removing the prefix" ,
62
- & lint_str[ 1 ..] ,
63
- Applicability :: MaybeIncorrect ,
64
- ) ;
65
- }
66
- }
67
- err. emit ( ) ;
60
+ AttrError :: UnsupportedLiteral ( reason, is_bytestr) => {
61
+ sess. emit_err ( session_diagnostics:: UnsupportedLiteral { span, reason, is_bytestr } ) ;
68
62
}
69
63
}
70
64
}
@@ -326,7 +320,7 @@ where
326
320
handle_errors (
327
321
& sess. parse_sess ,
328
322
meta. span ( ) ,
329
- AttrError :: UnsupportedLiteral ( "unsupported literal" , false ) ,
323
+ AttrError :: UnsupportedLiteral ( UnsupportedLiteralReason :: Generic , false ) ,
330
324
) ;
331
325
continue ' outer;
332
326
} ;
@@ -494,7 +488,10 @@ where
494
488
handle_errors (
495
489
& sess. parse_sess ,
496
490
lit. span ,
497
- AttrError :: UnsupportedLiteral ( "unsupported literal" , false ) ,
491
+ AttrError :: UnsupportedLiteral (
492
+ UnsupportedLiteralReason :: Generic ,
493
+ false ,
494
+ ) ,
498
495
) ;
499
496
continue ' outer;
500
497
}
@@ -711,7 +708,7 @@ pub fn eval_condition(
711
708
handle_errors (
712
709
sess,
713
710
mi. span ( ) ,
714
- AttrError :: UnsupportedLiteral ( "unsupported literal" , false ) ,
711
+ AttrError :: UnsupportedLiteral ( UnsupportedLiteralReason :: Generic , false ) ,
715
712
) ;
716
713
return false ;
717
714
}
@@ -790,7 +787,7 @@ pub fn eval_condition(
790
787
sess,
791
788
lit. span ,
792
789
AttrError :: UnsupportedLiteral (
793
- "literal in `cfg` predicate value must be a string" ,
790
+ UnsupportedLiteralReason :: CfgString ,
794
791
lit. kind . is_bytestr ( ) ,
795
792
) ,
796
793
) ;
@@ -870,8 +867,7 @@ where
870
867
& sess. parse_sess ,
871
868
lit. span ,
872
869
AttrError :: UnsupportedLiteral (
873
- "literal in `deprecated` \
874
- value must be a string",
870
+ UnsupportedLiteralReason :: DeprecatedString ,
875
871
lit. kind . is_bytestr ( ) ,
876
872
) ,
877
873
) ;
@@ -934,7 +930,7 @@ where
934
930
& sess. parse_sess ,
935
931
lit. span ,
936
932
AttrError :: UnsupportedLiteral (
937
- "item in `deprecated` must be a key/value pair" ,
933
+ UnsupportedLiteralReason :: DeprecatedKvPair ,
938
934
false ,
939
935
) ,
940
936
) ;
0 commit comments