@@ -5,7 +5,7 @@ use std::sync::LazyLock;
55
66use private:: Sealed ;
77use rustc_ast:: { AttrStyle , MetaItemLit , NodeId } ;
8- use rustc_errors:: Diagnostic ;
8+ use rustc_errors:: { Diag , Diagnostic , Level } ;
99use rustc_feature:: AttributeTemplate ;
1010use rustc_hir:: attrs:: AttributeKind ;
1111use rustc_hir:: lints:: { AttributeLint , AttributeLintKind } ;
@@ -23,6 +23,7 @@ use crate::attributes::codegen_attrs::{
2323 NoMangleParser , OptimizeParser , TargetFeatureParser , TrackCallerParser , UsedParser ,
2424} ;
2525use crate :: attributes:: confusables:: ConfusablesParser ;
26+ use crate :: attributes:: crate_level:: CrateNameParser ;
2627use crate :: attributes:: deprecation:: DeprecationParser ;
2728use crate :: attributes:: dummy:: DummyParser ;
2829use crate :: attributes:: inline:: { InlineParser , RustcForceInlineParser } ;
@@ -165,6 +166,7 @@ attribute_parsers!(
165166
166167 // tidy-alphabetical-start
167168 Single <CoverageParser >,
169+ Single <CrateNameParser >,
168170 Single <CustomMirParser >,
169171 Single <DeprecationParser >,
170172 Single <DummyParser >,
@@ -261,11 +263,7 @@ impl Stage for Early {
261263 sess : & ' sess Session ,
262264 diag : impl for < ' x > Diagnostic < ' x > ,
263265 ) -> ErrorGuaranteed {
264- if self . emit_errors . should_emit ( ) {
265- sess. dcx ( ) . emit_err ( diag)
266- } else {
267- sess. dcx ( ) . create_err ( diag) . delay_as_bug ( )
268- }
266+ self . should_emit ( ) . emit_err ( sess. dcx ( ) . create_err ( diag) )
269267 }
270268
271269 fn should_emit ( & self ) -> ShouldEmit {
@@ -312,7 +310,9 @@ pub struct AcceptContext<'f, 'sess, S: Stage> {
312310 /// The span of the attribute currently being parsed
313311 pub ( crate ) attr_span : Span ,
314312
313+ /// Whether it is an inner or outer attribute
315314 pub ( crate ) attr_style : AttrStyle ,
315+
316316 /// The expected structure of the attribute.
317317 ///
318318 /// Used in reporting errors to give a hint to users what the attribute *should* look like.
@@ -331,7 +331,7 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
331331 /// must be delayed until after HIR is built. This method will take care of the details of
332332 /// that.
333333 pub ( crate ) fn emit_lint ( & mut self , lint : AttributeLintKind , span : Span ) {
334- if ! self . stage . should_emit ( ) . should_emit ( ) {
334+ if matches ! ( self . stage. should_emit( ) , ShouldEmit :: Nothing ) {
335335 return ;
336336 }
337337 let id = self . target_id ;
@@ -649,8 +649,13 @@ pub enum OmitDoc {
649649 Skip ,
650650}
651651
652- #[ derive( Copy , Clone ) ]
652+ #[ derive( Copy , Clone , Debug ) ]
653653pub enum ShouldEmit {
654+ /// The operations will emit errors, and lints, and errors are fatal.
655+ ///
656+ /// Only relevant when early parsing, in late parsing equivalent to `ErrorsAndLints`.
657+ /// Late parsing is never fatal, and instead tries to emit as many diagnostics as possible.
658+ EarlyFatal ,
654659 /// The operation will emit errors and lints.
655660 /// This is usually what you need.
656661 ErrorsAndLints ,
@@ -660,10 +665,12 @@ pub enum ShouldEmit {
660665}
661666
662667impl ShouldEmit {
663- pub fn should_emit ( & self ) -> bool {
668+ pub ( crate ) fn emit_err ( & self , diag : Diag < ' _ > ) -> ErrorGuaranteed {
664669 match self {
665- ShouldEmit :: ErrorsAndLints => true ,
666- ShouldEmit :: Nothing => false ,
670+ ShouldEmit :: EarlyFatal if diag. level ( ) == Level :: DelayedBug => diag. emit ( ) ,
671+ ShouldEmit :: EarlyFatal => diag. upgrade_to_fatal ( ) . emit ( ) ,
672+ ShouldEmit :: ErrorsAndLints => diag. emit ( ) ,
673+ ShouldEmit :: Nothing => diag. delay_as_bug ( ) ,
667674 }
668675 }
669676}
0 commit comments