@@ -31,54 +31,13 @@ pub fn is_builtin_attr(attr: &Attribute) -> bool {
31
31
attr. is_doc_comment ( ) || attr. ident ( ) . is_some_and ( |ident| is_builtin_attr_name ( ident. name ) )
32
32
}
33
33
34
- enum AttrError {
35
- MultipleItem ( String ) ,
36
- UnknownMetaItem ( String , & ' static [ & ' static str ] ) ,
37
- MissingSince ,
38
- NonIdentFeature ,
39
- MissingFeature ,
40
- MultipleStabilityLevels ,
41
- UnsupportedLiteral ( UnsupportedLiteralReason , /* is_bytestr */ bool ) ,
42
- }
43
-
44
34
pub ( crate ) enum UnsupportedLiteralReason {
45
35
Generic ,
46
36
CfgString ,
47
37
DeprecatedString ,
48
38
DeprecatedKvPair ,
49
39
}
50
40
51
- fn handle_errors ( sess : & ParseSess , span : Span , error : AttrError ) {
52
- match error {
53
- AttrError :: MultipleItem ( item) => {
54
- sess. emit_err ( session_diagnostics:: MultipleItem { span, item } ) ;
55
- }
56
- 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 } ) ;
67
- }
68
- AttrError :: MultipleStabilityLevels => {
69
- sess. emit_err ( session_diagnostics:: MultipleStabilityLevels { span } ) ;
70
- }
71
- AttrError :: UnsupportedLiteral ( reason, is_bytestr) => {
72
- sess. emit_err ( session_diagnostics:: UnsupportedLiteral {
73
- span,
74
- reason,
75
- is_bytestr,
76
- start_point_span : sess. source_map ( ) . start_point ( span) ,
77
- } ) ;
78
- }
79
- }
80
- }
81
-
82
41
#[ derive( Copy , Clone , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
83
42
pub enum InlineAttr {
84
43
None ,
@@ -241,7 +200,7 @@ pub fn find_stability(
241
200
sym:: rustc_allowed_through_unstable_modules => allowed_through_unstable_modules = true ,
242
201
sym:: unstable => {
243
202
if stab. is_some ( ) {
244
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MultipleStabilityLevels ) ;
203
+ sess. emit_err ( session_diagnostics :: MultipleStabilityLevels { span : attr. span } ) ;
245
204
break ;
246
205
}
247
206
@@ -251,7 +210,7 @@ pub fn find_stability(
251
210
}
252
211
sym:: stable => {
253
212
if stab. is_some ( ) {
254
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MultipleStabilityLevels ) ;
213
+ sess. emit_err ( session_diagnostics :: MultipleStabilityLevels { span : attr. span } ) ;
255
214
break ;
256
215
}
257
216
if let Some ( ( feature, level) ) = parse_stability ( sess, attr) {
@@ -295,7 +254,7 @@ pub fn find_const_stability(
295
254
sym:: rustc_promotable => promotable = true ,
296
255
sym:: rustc_const_unstable => {
297
256
if const_stab. is_some ( ) {
298
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MultipleStabilityLevels ) ;
257
+ sess. emit_err ( session_diagnostics :: MultipleStabilityLevels { span : attr. span } ) ;
299
258
break ;
300
259
}
301
260
@@ -306,7 +265,7 @@ pub fn find_const_stability(
306
265
}
307
266
sym:: rustc_const_stable => {
308
267
if const_stab. is_some ( ) {
309
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MultipleStabilityLevels ) ;
268
+ sess. emit_err ( session_diagnostics :: MultipleStabilityLevels { span : attr. span } ) ;
310
269
break ;
311
270
}
312
271
if let Some ( ( feature, level) ) = parse_stability ( sess, attr) {
@@ -340,7 +299,7 @@ pub fn find_body_stability(
340
299
for attr in attrs {
341
300
if attr. has_name ( sym:: rustc_default_body_unstable) {
342
301
if body_stab. is_some ( ) {
343
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MultipleStabilityLevels ) ;
302
+ sess. emit_err ( session_diagnostics :: MultipleStabilityLevels { span : attr. span } ) ;
344
303
break ;
345
304
}
346
305
@@ -355,11 +314,10 @@ pub fn find_body_stability(
355
314
356
315
fn insert_or_error ( sess : & Session , meta : & MetaItem , item : & mut Option < Symbol > ) -> Option < ( ) > {
357
316
if item. is_some ( ) {
358
- handle_errors (
359
- & sess. parse_sess ,
360
- meta. span ,
361
- AttrError :: MultipleItem ( pprust:: path_to_string ( & meta. path ) ) ,
362
- ) ;
317
+ sess. emit_err ( session_diagnostics:: MultipleItem {
318
+ span : meta. span ,
319
+ item : pprust:: path_to_string ( & meta. path ) ,
320
+ } ) ;
363
321
None
364
322
} else if let Some ( v) = meta. value_str ( ) {
365
323
* item = Some ( v) ;
@@ -380,26 +338,24 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
380
338
let mut since = None ;
381
339
for meta in metas {
382
340
let Some ( mi) = meta. meta_item ( ) else {
383
- handle_errors (
384
- & sess. parse_sess ,
385
- meta. span ( ) ,
386
- AttrError :: UnsupportedLiteral ( UnsupportedLiteralReason :: Generic , false ) ,
387
- ) ;
341
+ sess. emit_err ( session_diagnostics:: UnsupportedLiteral {
342
+ span : meta. span ( ) ,
343
+ reason : UnsupportedLiteralReason :: Generic ,
344
+ is_bytestr : false ,
345
+ start_point_span : sess. source_map ( ) . start_point ( meta. span ( ) ) ,
346
+ } ) ;
388
347
return None ;
389
348
} ;
390
349
391
350
match mi. name_or_empty ( ) {
392
351
sym:: feature => insert_or_error ( sess, mi, & mut feature) ?,
393
352
sym:: since => insert_or_error ( sess, mi, & mut since) ?,
394
353
_ => {
395
- handle_errors (
396
- & sess. parse_sess ,
397
- meta. span ( ) ,
398
- AttrError :: UnknownMetaItem (
399
- pprust:: path_to_string ( & mi. path ) ,
400
- & [ "feature" , "since" ] ,
401
- ) ,
402
- ) ;
354
+ sess. emit_err ( session_diagnostics:: UnknownMetaItem {
355
+ span : meta. span ( ) ,
356
+ item : pprust:: path_to_string ( & mi. path ) ,
357
+ expected : & [ "feature" , "since" ] ,
358
+ } ) ;
403
359
return None ;
404
360
}
405
361
}
@@ -417,11 +373,11 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
417
373
Some ( ( feature, level) )
418
374
}
419
375
( None , _) => {
420
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingFeature ) ;
376
+ sess. emit_err ( session_diagnostics :: MissingFeature { span : attr. span } ) ;
421
377
None
422
378
}
423
379
_ => {
424
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingSince ) ;
380
+ sess. emit_err ( session_diagnostics :: MissingSince { span : attr. span } ) ;
425
381
None
426
382
}
427
383
}
@@ -441,11 +397,12 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
441
397
let mut implied_by = None ;
442
398
for meta in metas {
443
399
let Some ( mi) = meta. meta_item ( ) else {
444
- handle_errors (
445
- & sess. parse_sess ,
446
- meta. span ( ) ,
447
- AttrError :: UnsupportedLiteral ( UnsupportedLiteralReason :: Generic , false ) ,
448
- ) ;
400
+ sess. emit_err ( session_diagnostics:: UnsupportedLiteral {
401
+ span : meta. span ( ) ,
402
+ reason : UnsupportedLiteralReason :: Generic ,
403
+ is_bytestr : false ,
404
+ start_point_span : sess. source_map ( ) . start_point ( meta. span ( ) ) ,
405
+ } ) ;
449
406
return None ;
450
407
} ;
451
408
@@ -484,14 +441,11 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
484
441
}
485
442
sym:: implied_by => insert_or_error ( sess, mi, & mut implied_by) ?,
486
443
_ => {
487
- handle_errors (
488
- & sess. parse_sess ,
489
- meta. span ( ) ,
490
- AttrError :: UnknownMetaItem (
491
- pprust:: path_to_string ( & mi. path ) ,
492
- & [ "feature" , "reason" , "issue" , "soft" , "implied_by" ] ,
493
- ) ,
494
- ) ;
444
+ sess. emit_err ( session_diagnostics:: UnknownMetaItem {
445
+ span : meta. span ( ) ,
446
+ item : pprust:: path_to_string ( & mi. path ) ,
447
+ expected : & [ "feature" , "reason" , "issue" , "soft" , "implied_by" ] ,
448
+ } ) ;
495
449
return None ;
496
450
}
497
451
}
@@ -500,7 +454,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
500
454
match ( feature, reason, issue) {
501
455
( Some ( feature) , reason, Some ( _) ) => {
502
456
if !rustc_lexer:: is_ident ( feature. as_str ( ) ) {
503
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: NonIdentFeature ) ;
457
+ sess. emit_err ( session_diagnostics :: NonIdentFeature { span : attr. span } ) ;
504
458
return None ;
505
459
}
506
460
let level = StabilityLevel :: Unstable {
@@ -512,7 +466,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
512
466
Some ( ( feature, level) )
513
467
}
514
468
( None , _, _) => {
515
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingFeature ) ;
469
+ sess. emit_err ( session_diagnostics :: MissingFeature { span : attr. span } ) ;
516
470
return None ;
517
471
}
518
472
_ => {
@@ -659,11 +613,12 @@ pub fn eval_condition(
659
613
ast:: MetaItemKind :: List ( mis) => {
660
614
for mi in mis. iter ( ) {
661
615
if !mi. is_meta_item ( ) {
662
- handle_errors (
663
- sess,
664
- mi. span ( ) ,
665
- AttrError :: UnsupportedLiteral ( UnsupportedLiteralReason :: Generic , false ) ,
666
- ) ;
616
+ sess. emit_err ( session_diagnostics:: UnsupportedLiteral {
617
+ span : mi. span ( ) ,
618
+ reason : UnsupportedLiteralReason :: Generic ,
619
+ is_bytestr : false ,
620
+ start_point_span : sess. source_map ( ) . start_point ( mi. span ( ) ) ,
621
+ } ) ;
667
622
return false ;
668
623
}
669
624
}
@@ -731,14 +686,12 @@ pub fn eval_condition(
731
686
true
732
687
}
733
688
MetaItemKind :: NameValue ( lit) if !lit. kind . is_str ( ) => {
734
- handle_errors (
735
- sess,
736
- lit. span ,
737
- AttrError :: UnsupportedLiteral (
738
- UnsupportedLiteralReason :: CfgString ,
739
- lit. kind . is_bytestr ( ) ,
740
- ) ,
741
- ) ;
689
+ sess. emit_err ( session_diagnostics:: UnsupportedLiteral {
690
+ span : lit. span ,
691
+ reason : UnsupportedLiteralReason :: CfgString ,
692
+ is_bytestr : lit. kind . is_bytestr ( ) ,
693
+ start_point_span : sess. source_map ( ) . start_point ( lit. span ) ,
694
+ } ) ;
742
695
true
743
696
}
744
697
ast:: MetaItemKind :: Word | ast:: MetaItemKind :: NameValue ( ..) => {
@@ -795,26 +748,23 @@ pub fn find_deprecation(
795
748
MetaItemKind :: List ( list) => {
796
749
let get = |meta : & MetaItem , item : & mut Option < Symbol > | {
797
750
if item. is_some ( ) {
798
- handle_errors (
799
- & sess. parse_sess ,
800
- meta. span ,
801
- AttrError :: MultipleItem ( pprust:: path_to_string ( & meta. path ) ) ,
802
- ) ;
751
+ sess. emit_err ( session_diagnostics:: MultipleItem {
752
+ span : meta. span ,
753
+ item : pprust:: path_to_string ( & meta. path ) ,
754
+ } ) ;
803
755
return false ;
804
756
}
805
757
if let Some ( v) = meta. value_str ( ) {
806
758
* item = Some ( v) ;
807
759
true
808
760
} else {
809
761
if let Some ( lit) = meta. name_value_literal ( ) {
810
- handle_errors (
811
- & sess. parse_sess ,
812
- lit. span ,
813
- AttrError :: UnsupportedLiteral (
814
- UnsupportedLiteralReason :: DeprecatedString ,
815
- lit. kind . is_bytestr ( ) ,
816
- ) ,
817
- ) ;
762
+ sess. emit_err ( session_diagnostics:: UnsupportedLiteral {
763
+ span : lit. span ,
764
+ reason : UnsupportedLiteralReason :: DeprecatedString ,
765
+ is_bytestr : lit. kind . is_bytestr ( ) ,
766
+ start_point_span : sess. source_map ( ) . start_point ( lit. span ) ,
767
+ } ) ;
818
768
} else {
819
769
sess. emit_err ( session_diagnostics:: IncorrectMetaItem {
820
770
span : meta. span ,
@@ -852,30 +802,25 @@ pub fn find_deprecation(
852
802
}
853
803
}
854
804
_ => {
855
- handle_errors (
856
- & sess. parse_sess ,
857
- meta. span ( ) ,
858
- AttrError :: UnknownMetaItem (
859
- pprust:: path_to_string ( & mi. path ) ,
860
- if features. deprecated_suggestion {
861
- & [ "since" , "note" , "suggestion" ]
862
- } else {
863
- & [ "since" , "note" ]
864
- } ,
865
- ) ,
866
- ) ;
805
+ sess. emit_err ( session_diagnostics:: UnknownMetaItem {
806
+ span : meta. span ( ) ,
807
+ item : pprust:: path_to_string ( & mi. path ) ,
808
+ expected : if features. deprecated_suggestion {
809
+ & [ "since" , "note" , "suggestion" ]
810
+ } else {
811
+ & [ "since" , "note" ]
812
+ } ,
813
+ } ) ;
867
814
continue ' outer;
868
815
}
869
816
} ,
870
817
NestedMetaItem :: Lit ( lit) => {
871
- handle_errors (
872
- & sess. parse_sess ,
873
- lit. span ,
874
- AttrError :: UnsupportedLiteral (
875
- UnsupportedLiteralReason :: DeprecatedKvPair ,
876
- false ,
877
- ) ,
878
- ) ;
818
+ sess. emit_err ( session_diagnostics:: UnsupportedLiteral {
819
+ span : lit. span ,
820
+ reason : UnsupportedLiteralReason :: DeprecatedKvPair ,
821
+ is_bytestr : false ,
822
+ start_point_span : sess. source_map ( ) . start_point ( lit. span ) ,
823
+ } ) ;
879
824
continue ' outer;
880
825
}
881
826
}
@@ -885,7 +830,7 @@ pub fn find_deprecation(
885
830
886
831
if is_rustc {
887
832
if since. is_none ( ) {
888
- handle_errors ( & sess. parse_sess , attr. span , AttrError :: MissingSince ) ;
833
+ sess. emit_err ( session_diagnostics :: MissingSince { span : attr. span } ) ;
889
834
continue ;
890
835
}
891
836
0 commit comments