@@ -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 } ;
@@ -24,6 +24,7 @@ use crate::attributes::codegen_attrs::{
2424 UsedParser ,
2525} ;
2626use crate :: attributes:: confusables:: ConfusablesParser ;
27+ use crate :: attributes:: crate_level:: CrateNameParser ;
2728use crate :: attributes:: deprecation:: DeprecationParser ;
2829use crate :: attributes:: dummy:: DummyParser ;
2930use crate :: attributes:: inline:: { InlineParser , RustcForceInlineParser } ;
@@ -166,6 +167,7 @@ attribute_parsers!(
166167
167168 // tidy-alphabetical-start
168169 Single <CoverageParser >,
170+ Single <CrateNameParser >,
169171 Single <CustomMirParser >,
170172 Single <DeprecationParser >,
171173 Single <DummyParser >,
@@ -263,11 +265,7 @@ impl Stage for Early {
263265 sess : & ' sess Session ,
264266 diag : impl for < ' x > Diagnostic < ' x > ,
265267 ) -> ErrorGuaranteed {
266- if self . emit_errors . should_emit ( ) {
267- sess. dcx ( ) . emit_err ( diag)
268- } else {
269- sess. dcx ( ) . create_err ( diag) . delay_as_bug ( )
270- }
268+ self . should_emit ( ) . emit_err ( sess. dcx ( ) . create_err ( diag) )
271269 }
272270
273271 fn should_emit ( & self ) -> ShouldEmit {
@@ -314,7 +312,9 @@ pub struct AcceptContext<'f, 'sess, S: Stage> {
314312 /// The span of the attribute currently being parsed
315313 pub ( crate ) attr_span : Span ,
316314
315+ /// Whether it is an inner or outer attribute
317316 pub ( crate ) attr_style : AttrStyle ,
317+
318318 /// The expected structure of the attribute.
319319 ///
320320 /// Used in reporting errors to give a hint to users what the attribute *should* look like.
@@ -333,7 +333,7 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
333333 /// must be delayed until after HIR is built. This method will take care of the details of
334334 /// that.
335335 pub ( crate ) fn emit_lint ( & mut self , lint : AttributeLintKind , span : Span ) {
336- if ! self . stage . should_emit ( ) . should_emit ( ) {
336+ if matches ! ( self . stage. should_emit( ) , ShouldEmit :: Nothing ) {
337337 return ;
338338 }
339339 let id = self . target_id ;
@@ -651,8 +651,13 @@ pub enum OmitDoc {
651651 Skip ,
652652}
653653
654- #[ derive( Copy , Clone ) ]
654+ #[ derive( Copy , Clone , Debug ) ]
655655pub enum ShouldEmit {
656+ /// The operations will emit errors, and lints, and errors are fatal.
657+ ///
658+ /// Only relevant when early parsing, in late parsing equivalent to `ErrorsAndLints`.
659+ /// Late parsing is never fatal, and instead tries to emit as many diagnostics as possible.
660+ EarlyFatal ,
656661 /// The operation will emit errors and lints.
657662 /// This is usually what you need.
658663 ErrorsAndLints ,
@@ -662,10 +667,12 @@ pub enum ShouldEmit {
662667}
663668
664669impl ShouldEmit {
665- pub fn should_emit ( & self ) -> bool {
670+ pub ( crate ) fn emit_err ( & self , diag : Diag < ' _ > ) -> ErrorGuaranteed {
666671 match self {
667- ShouldEmit :: ErrorsAndLints => true ,
668- ShouldEmit :: Nothing => false ,
672+ ShouldEmit :: EarlyFatal if diag. level ( ) == Level :: DelayedBug => diag. emit ( ) ,
673+ ShouldEmit :: EarlyFatal => diag. upgrade_to_fatal ( ) . emit ( ) ,
674+ ShouldEmit :: ErrorsAndLints => diag. emit ( ) ,
675+ ShouldEmit :: Nothing => diag. delay_as_bug ( ) ,
669676 }
670677 }
671678}
0 commit comments