@@ -15,7 +15,7 @@ use rustc_ast_pretty::pprust;
1515use rustc_attr:: StabilityLevel ;
1616use rustc_data_structures:: intern:: Interned ;
1717use rustc_data_structures:: sync:: Lrc ;
18- use rustc_errors:: { struct_span_err, Applicability } ;
18+ use rustc_errors:: { struct_span_err, Applicability , FatalError } ;
1919use rustc_expand:: base:: { Annotatable , DeriveResolutions , Indeterminate , ResolverExpand } ;
2020use rustc_expand:: base:: { SyntaxExtension , SyntaxExtensionKind } ;
2121use rustc_expand:: compile_declarative_macro;
@@ -268,15 +268,19 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
268268 }
269269 } ;
270270
271- let ( path, kind, inner_attr , derives) = match invoc. kind {
271+ let ( path, kind, attr_kind , derives) = match invoc. kind {
272272 InvocationKind :: Attr { ref attr, ref derives, .. } => (
273273 & attr. get_normal_item ( ) . path ,
274274 MacroKind :: Attr ,
275- attr. style == ast :: AttrStyle :: Inner ,
275+ attr. style ,
276276 self . arenas . alloc_ast_paths ( derives) ,
277277 ) ,
278- InvocationKind :: Bang { ref mac, .. } => ( & mac. path , MacroKind :: Bang , false , & [ ] [ ..] ) ,
279- InvocationKind :: Derive { ref path, .. } => ( path, MacroKind :: Derive , false , & [ ] [ ..] ) ,
278+ InvocationKind :: Bang { ref mac, .. } => {
279+ ( & mac. path , MacroKind :: Bang , ast:: AttrStyle :: Outer , & [ ] [ ..] )
280+ }
281+ InvocationKind :: Derive { ref path, .. } => {
282+ ( path, MacroKind :: Derive , ast:: AttrStyle :: Outer , & [ ] [ ..] )
283+ }
280284 } ;
281285
282286 // Derives are not included when `invocations` are collected, so we have to add them here.
@@ -287,7 +291,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
287291 path,
288292 kind,
289293 supports_macro_expansion,
290- inner_attr ,
294+ attr_kind ,
291295 parent_scope,
292296 node_id,
293297 force,
@@ -373,6 +377,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
373377 match self . resolve_macro_path (
374378 path,
375379 Some ( MacroKind :: Derive ) ,
380+ ast:: AttrStyle :: Outer ,
376381 & parent_scope,
377382 true ,
378383 force,
@@ -498,19 +503,19 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
498503 path : & ast:: Path ,
499504 kind : MacroKind ,
500505 supports_macro_expansion : SupportsMacroExpansion ,
501- inner_attr : bool ,
506+ attr_kind : ast :: AttrStyle ,
502507 parent_scope : & ParentScope < ' a > ,
503508 node_id : NodeId ,
504509 force : bool ,
505510 soft_custom_inner_attributes_gate : bool ,
506511 ) -> Result < ( Lrc < SyntaxExtension > , Res ) , Indeterminate > {
507- let ( ext, res) = match self . resolve_macro_path ( path , Some ( kind ) , parent_scope , true , force )
508- {
509- Ok ( ( Some ( ext) , res) ) => ( ext, res) ,
510- Ok ( ( None , res) ) => ( self . dummy_ext ( kind) , res) ,
511- Err ( Determinacy :: Determined ) => ( self . dummy_ext ( kind) , Res :: Err ) ,
512- Err ( Determinacy :: Undetermined ) => return Err ( Indeterminate ) ,
513- } ;
512+ let ( ext, res) =
513+ match self . resolve_macro_path ( path , Some ( kind ) , attr_kind , parent_scope , true , force ) {
514+ Ok ( ( Some ( ext) , res) ) => ( ext, res) ,
515+ Ok ( ( None , res) ) => ( self . dummy_ext ( kind) , res) ,
516+ Err ( Determinacy :: Determined ) => ( self . dummy_ext ( kind) , Res :: Err ) ,
517+ Err ( Determinacy :: Undetermined ) => return Err ( Indeterminate ) ,
518+ } ;
514519
515520 // Report errors for the resolved macro.
516521 for segment in & path. segments {
@@ -549,7 +554,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
549554 match supports_macro_expansion {
550555 SupportsMacroExpansion :: No => Some ( ( "a" , "non-macro attribute" ) ) ,
551556 SupportsMacroExpansion :: Yes { supports_inner_attrs } => {
552- if inner_attr && !supports_inner_attrs {
557+ if let ast:: AttrStyle :: Inner = attr_kind
558+ && !supports_inner_attrs
559+ {
553560 Some ( ( "a" , "non-macro inner attribute" ) )
554561 } else {
555562 None
@@ -588,7 +595,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
588595 }
589596
590597 // We are trying to avoid reporting this error if other related errors were reported.
591- if res != Res :: Err && inner_attr && !self . tcx . features ( ) . custom_inner_attributes {
598+ if res != Res :: Err
599+ && let ast:: AttrStyle :: Inner = attr_kind
600+ && !self . tcx . features ( ) . custom_inner_attributes
601+ {
592602 let msg = match res {
593603 Res :: Def ( ..) => "inner macro attributes are unstable" ,
594604 Res :: NonMacroAttr ( ..) => "custom inner attributes are unstable" ,
@@ -627,6 +637,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
627637 & mut self ,
628638 path : & ast:: Path ,
629639 kind : Option < MacroKind > ,
640+ attr_kind : ast:: AttrStyle ,
630641 parent_scope : & ParentScope < ' a > ,
631642 trace : bool ,
632643 force : bool ,
@@ -685,6 +696,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
685696 self . single_segment_macro_resolutions . push ( (
686697 path[ 0 ] . ident ,
687698 kind,
699+ attr_kind,
688700 * parent_scope,
689701 binding. ok ( ) ,
690702 ) ) ;
@@ -794,7 +806,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
794806 }
795807
796808 let macro_resolutions = mem:: take ( & mut self . single_segment_macro_resolutions ) ;
797- for ( ident, kind, parent_scope, initial_binding) in macro_resolutions {
809+ let mut has_reported_inner_attr_error = false ;
810+ let mut raise_fatal_error = false ;
811+ for ( ident, kind, attr_kind, parent_scope, initial_binding) in macro_resolutions {
798812 match self . early_resolve_ident_in_lexical_scope (
799813 ident,
800814 ScopeSet :: Macro ( kind) ,
@@ -810,7 +824,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
810824 } ) ;
811825 let res = binding. res ( ) ;
812826 let seg = Segment :: from_ident ( ident) ;
813- check_consistency ( self , & [ seg] , ident. span , kind, initial_res, res) ;
827+ if has_reported_inner_attr_error
828+ && let Res :: Def ( DefKind :: Macro ( MacroKind :: Attr ) , _) = res
829+ && let None = initial_res
830+ {
831+ // Do not emit an indeterminate resolution and later errors when an outer
832+ // attribute wasn't found, as this can be knock down effects. #118455
833+ raise_fatal_error = true ;
834+ } else {
835+ let res = binding. res ( ) ;
836+ check_consistency ( self , & [ seg] , ident. span , kind, initial_res, res) ;
837+ } ;
814838 if res == Res :: NonMacroAttr ( NonMacroAttrKind :: DeriveHelperCompat ) {
815839 let node_id = self
816840 . invocation_parents
@@ -825,7 +849,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
825849 ) ;
826850 }
827851 }
828- Err ( ..) => {
852+ Err ( _) => {
853+ if let ast:: AttrStyle :: Inner = attr_kind {
854+ has_reported_inner_attr_error = true ;
855+ }
829856 let expected = kind. descr_expected ( ) ;
830857
831858 let mut err = self . tcx . sess . create_err ( CannotFindIdentInThisScope {
@@ -851,6 +878,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
851878 None ,
852879 ) ;
853880 }
881+
882+ if raise_fatal_error {
883+ // When we encounter an inner attribute failure, and subsequent successful macro
884+ // resolutions following early resolution failures. This is so when an outer attribute
885+ // isn't found, and we encounter `derive` attributes, we won't raise errors caused by
886+ // any code that relies on those derives having been evaluated. We don't attempt to
887+ // recover because the behavior of those derives could have been modified by the outer
888+ // attribute, causing *other* errors, so it is safest to just stop early instead.
889+ FatalError . raise ( ) ;
890+ }
854891 }
855892
856893 fn check_stability_and_deprecation (
0 commit comments