3
3
use rustc_ast:: { self as ast, attr} ;
4
4
use rustc_ast:: { Attribute , LitKind , MetaItem , MetaItemKind , MetaItemLit , NestedMetaItem , NodeId } ;
5
5
use rustc_ast_pretty:: pprust;
6
+ use rustc_errors:: ErrorGuaranteed ;
6
7
use rustc_feature:: { find_gated_cfg, is_builtin_attr_name, Features , GatedCfg } ;
7
8
use rustc_macros:: HashStable_Generic ;
8
9
use rustc_session:: config:: ExpectedValues ;
@@ -48,33 +49,27 @@ pub(crate) enum UnsupportedLiteralReason {
48
49
DeprecatedKvPair ,
49
50
}
50
51
51
- fn handle_errors ( sess : & ParseSess , span : Span , error : AttrError ) {
52
+ fn handle_errors ( sess : & ParseSess , span : Span , error : AttrError ) -> ErrorGuaranteed {
52
53
match error {
53
54
AttrError :: MultipleItem ( item) => {
54
- sess. emit_err ( session_diagnostics:: MultipleItem { span, item } ) ;
55
+ sess. emit_err ( session_diagnostics:: MultipleItem { span, item } )
55
56
}
56
57
AttrError :: UnknownMetaItem ( item, expected) => {
57
- sess. emit_err ( session_diagnostics:: UnknownMetaItem { span, item, expected } ) ;
58
- }
59
- AttrError :: MissingSince => {
60
- sess. emit_err ( session_diagnostics:: MissingSince { span } ) ;
61
- }
62
- AttrError :: NonIdentFeature => {
63
- sess. emit_err ( session_diagnostics:: NonIdentFeature { span } ) ;
64
- }
65
- AttrError :: MissingFeature => {
66
- sess. emit_err ( session_diagnostics:: MissingFeature { span } ) ;
58
+ sess. emit_err ( session_diagnostics:: UnknownMetaItem { span, item, expected } )
67
59
}
60
+ AttrError :: MissingSince => sess. emit_err ( session_diagnostics:: MissingSince { span } ) ,
61
+ AttrError :: NonIdentFeature => sess. emit_err ( session_diagnostics:: NonIdentFeature { span } ) ,
62
+ AttrError :: MissingFeature => sess. emit_err ( session_diagnostics:: MissingFeature { span } ) ,
68
63
AttrError :: MultipleStabilityLevels => {
69
- sess. emit_err ( session_diagnostics:: MultipleStabilityLevels { span } ) ;
64
+ sess. emit_err ( session_diagnostics:: MultipleStabilityLevels { span } )
70
65
}
71
66
AttrError :: UnsupportedLiteral ( reason, is_bytestr) => {
72
67
sess. emit_err ( session_diagnostics:: UnsupportedLiteral {
73
68
span,
74
69
reason,
75
70
is_bytestr,
76
71
start_point_span : sess. source_map ( ) . start_point ( span) ,
77
- } ) ;
72
+ } )
78
73
}
79
74
}
80
75
}
@@ -411,19 +406,23 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
411
406
since = Some ( rust_version_symbol ( ) ) ;
412
407
}
413
408
409
+ let feature = match feature {
410
+ Some ( feature) if rustc_lexer:: is_ident ( feature. as_str ( ) ) => Ok ( feature) ,
411
+ Some ( _bad_feature) => {
412
+ Err ( handle_errors ( & sess. parse_sess , attr. span , AttrError :: NonIdentFeature ) )
413
+ }
414
+ None => Err ( handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingFeature ) ) ,
415
+ } ;
416
+
417
+ let since =
418
+ since. ok_or_else ( || handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingSince ) ) ;
419
+
414
420
match ( feature, since) {
415
- ( Some ( feature) , Some ( since) ) => {
421
+ ( Ok ( feature) , Ok ( since) ) => {
416
422
let level = StabilityLevel :: Stable { since, allowed_through_unstable_modules : false } ;
417
423
Some ( ( feature, level) )
418
424
}
419
- ( None , _) => {
420
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingFeature ) ;
421
- None
422
- }
423
- _ => {
424
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingSince ) ;
425
- None
426
- }
425
+ ( Err ( ErrorGuaranteed { .. } ) , _) | ( _, Err ( ErrorGuaranteed { .. } ) ) => None ,
427
426
}
428
427
}
429
428
@@ -497,12 +496,19 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
497
496
}
498
497
}
499
498
500
- match ( feature, reason, issue) {
501
- ( Some ( feature) , reason, Some ( _) ) => {
502
- if !rustc_lexer:: is_ident ( feature. as_str ( ) ) {
503
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: NonIdentFeature ) ;
504
- return None ;
505
- }
499
+ let feature = match feature {
500
+ Some ( feature) if rustc_lexer:: is_ident ( feature. as_str ( ) ) => Ok ( feature) ,
501
+ Some ( _bad_feature) => {
502
+ Err ( handle_errors ( & sess. parse_sess , attr. span , AttrError :: NonIdentFeature ) )
503
+ }
504
+ None => Err ( handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingFeature ) ) ,
505
+ } ;
506
+
507
+ let issue =
508
+ issue. ok_or_else ( || sess. emit_err ( session_diagnostics:: MissingIssue { span : attr. span } ) ) ;
509
+
510
+ match ( feature, issue) {
511
+ ( Ok ( feature) , Ok ( _) ) => {
506
512
let level = StabilityLevel :: Unstable {
507
513
reason : UnstableReason :: from_opt_reason ( reason) ,
508
514
issue : issue_num,
@@ -511,14 +517,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
511
517
} ;
512
518
Some ( ( feature, level) )
513
519
}
514
- ( None , _, _) => {
515
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingFeature ) ;
516
- return None ;
517
- }
518
- _ => {
519
- sess. emit_err ( session_diagnostics:: MissingIssue { span : attr. span } ) ;
520
- return None ;
521
- }
520
+ ( Err ( ErrorGuaranteed { .. } ) , _) | ( _, Err ( ErrorGuaranteed { .. } ) ) => None ,
522
521
}
523
522
}
524
523
0 commit comments