@@ -15,7 +15,7 @@ use rustc_ast_pretty::pprust;
15
15
use rustc_attr:: StabilityLevel ;
16
16
use rustc_data_structures:: intern:: Interned ;
17
17
use rustc_data_structures:: sync:: Lrc ;
18
- use rustc_errors:: { struct_span_err, Applicability } ;
18
+ use rustc_errors:: { struct_span_err, Applicability , FatalError } ;
19
19
use rustc_expand:: base:: { Annotatable , DeriveResolutions , Indeterminate , ResolverExpand } ;
20
20
use rustc_expand:: base:: { SyntaxExtension , SyntaxExtensionKind } ;
21
21
use rustc_expand:: compile_declarative_macro;
@@ -268,15 +268,19 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
268
268
}
269
269
} ;
270
270
271
- let ( path, kind, inner_attr , derives) = match invoc. kind {
271
+ let ( path, kind, attr_kind , derives) = match invoc. kind {
272
272
InvocationKind :: Attr { ref attr, ref derives, .. } => (
273
273
& attr. get_normal_item ( ) . path ,
274
274
MacroKind :: Attr ,
275
- attr. style == ast :: AttrStyle :: Inner ,
275
+ attr. style ,
276
276
self . arenas . alloc_ast_paths ( derives) ,
277
277
) ,
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
+ }
280
284
} ;
281
285
282
286
// 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> {
287
291
path,
288
292
kind,
289
293
supports_macro_expansion,
290
- inner_attr ,
294
+ attr_kind ,
291
295
parent_scope,
292
296
node_id,
293
297
force,
@@ -373,6 +377,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
373
377
match self . resolve_macro_path (
374
378
path,
375
379
Some ( MacroKind :: Derive ) ,
380
+ ast:: AttrStyle :: Outer ,
376
381
& parent_scope,
377
382
true ,
378
383
force,
@@ -498,19 +503,19 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
498
503
path : & ast:: Path ,
499
504
kind : MacroKind ,
500
505
supports_macro_expansion : SupportsMacroExpansion ,
501
- inner_attr : bool ,
506
+ attr_kind : ast :: AttrStyle ,
502
507
parent_scope : & ParentScope < ' a > ,
503
508
node_id : NodeId ,
504
509
force : bool ,
505
510
soft_custom_inner_attributes_gate : bool ,
506
511
) -> 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
+ } ;
514
519
515
520
// Report errors for the resolved macro.
516
521
for segment in & path. segments {
@@ -549,7 +554,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
549
554
match supports_macro_expansion {
550
555
SupportsMacroExpansion :: No => Some ( ( "a" , "non-macro attribute" ) ) ,
551
556
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
+ {
553
560
Some ( ( "a" , "non-macro inner attribute" ) )
554
561
} else {
555
562
None
@@ -588,7 +595,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
588
595
}
589
596
590
597
// 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
+ {
592
602
let msg = match res {
593
603
Res :: Def ( ..) => "inner macro attributes are unstable" ,
594
604
Res :: NonMacroAttr ( ..) => "custom inner attributes are unstable" ,
@@ -627,6 +637,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
627
637
& mut self ,
628
638
path : & ast:: Path ,
629
639
kind : Option < MacroKind > ,
640
+ attr_kind : ast:: AttrStyle ,
630
641
parent_scope : & ParentScope < ' a > ,
631
642
trace : bool ,
632
643
force : bool ,
@@ -685,6 +696,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
685
696
self . single_segment_macro_resolutions . push ( (
686
697
path[ 0 ] . ident ,
687
698
kind,
699
+ attr_kind,
688
700
* parent_scope,
689
701
binding. ok ( ) ,
690
702
) ) ;
@@ -794,7 +806,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
794
806
}
795
807
796
808
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 {
798
812
match self . early_resolve_ident_in_lexical_scope (
799
813
ident,
800
814
ScopeSet :: Macro ( kind) ,
@@ -810,7 +824,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
810
824
} ) ;
811
825
let res = binding. res ( ) ;
812
826
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
+ } ;
814
838
if res == Res :: NonMacroAttr ( NonMacroAttrKind :: DeriveHelperCompat ) {
815
839
let node_id = self
816
840
. invocation_parents
@@ -825,7 +849,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
825
849
) ;
826
850
}
827
851
}
828
- Err ( ..) => {
852
+ Err ( _) => {
853
+ if let ast:: AttrStyle :: Inner = attr_kind {
854
+ has_reported_inner_attr_error = true ;
855
+ }
829
856
let expected = kind. descr_expected ( ) ;
830
857
831
858
let mut err = self . tcx . sess . create_err ( CannotFindIdentInThisScope {
@@ -851,6 +878,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
851
878
None ,
852
879
) ;
853
880
}
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
+ }
854
891
}
855
892
856
893
fn check_stability_and_deprecation (
0 commit comments