@@ -10,7 +10,9 @@ use crate::{BuiltinMacroState, Determinacy, MacroData};
10
10
use crate :: { DeriveData , Finalize , ParentScope , ResolutionError , Resolver , ScopeSet } ;
11
11
use crate :: { ModuleKind , ModuleOrUniformRoot , NameBinding , PathResult , Segment , ToNameBinding } ;
12
12
use rustc_ast:: expand:: StrippedCfgItem ;
13
- use rustc_ast:: { self as ast, attr, Crate , Inline , ItemKind , ModKind , NodeId } ;
13
+ use rustc_ast:: {
14
+ self as ast, attr, AttrStyle , Attribute , Crate , Inline , ItemKind , ModKind , NodeId ,
15
+ } ;
14
16
use rustc_ast_pretty:: pprust;
15
17
use rustc_attr:: StabilityLevel ;
16
18
use rustc_data_structures:: intern:: Interned ;
@@ -35,7 +37,7 @@ use rustc_span::edition::Edition;
35
37
use rustc_span:: hygiene:: { self , ExpnData , ExpnKind , LocalExpnId } ;
36
38
use rustc_span:: hygiene:: { AstPass , MacroKind } ;
37
39
use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
38
- use rustc_span:: { Span , DUMMY_SP } ;
40
+ use rustc_span:: { BytePos , Span , DUMMY_SP } ;
39
41
use std:: cell:: Cell ;
40
42
use std:: mem;
41
43
@@ -695,6 +697,32 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
695
697
res. map ( |res| ( self . get_macro ( res) . map ( |macro_data| macro_data. ext . clone ( ) ) , res) )
696
698
}
697
699
700
+ fn report_invalid_crate_level_attr ( & mut self , attrs : & [ Attribute ] , name : Symbol ) -> bool {
701
+ for attr in attrs. iter ( ) . filter ( |attr| attr. style == AttrStyle :: Inner ) {
702
+ if attr. has_name ( name) {
703
+ let tcx = self . tcx ;
704
+ tcx. dcx ( ) . emit_err ( rustc_attr:: InvalidAttrAtCrateLevel {
705
+ span : attr. span ,
706
+ sugg_span : tcx
707
+ . sess
708
+ . source_map ( )
709
+ . span_to_snippet ( attr. span )
710
+ . ok ( )
711
+ . filter ( |src| src. starts_with ( "#![" ) )
712
+ . map ( |_| {
713
+ attr. span
714
+ . with_lo ( attr. span . lo ( ) + BytePos ( 1 ) )
715
+ . with_hi ( attr. span . lo ( ) + BytePos ( 2 ) )
716
+ } ) ,
717
+ name,
718
+ item : None ,
719
+ } ) ;
720
+ return true ;
721
+ }
722
+ }
723
+ false
724
+ }
725
+
698
726
pub ( crate ) fn finalize_macro_resolutions ( & mut self , krate : & Crate ) {
699
727
let check_consistency = |this : & mut Self ,
700
728
path : & [ Segment ] ,
@@ -714,15 +742,29 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
714
742
// expanded into a dummy fragment for recovery during expansion.
715
743
// Now, post-expansion, the resolution may succeed, but we can't change the
716
744
// past and need to report an error.
717
- // However, non-speculative `resolve_path` can successfully return private items
745
+ // Special cases:
746
+ // 1. non-speculative `resolve_path` can successfully return private items
718
747
// even if speculative `resolve_path` returned nothing previously, so we skip this
719
748
// less informative error if the privacy error is reported elsewhere.
749
+ // 2. issue #118455, unresolved top level attribute error didn't imported prelude and
750
+ // already have emitted an error, report builtin macro and attributes error by the way,
751
+ // so `check_invalid_crate_level_attr` in can ignore them.
752
+
720
753
if this. privacy_errors . is_empty ( ) {
721
- this. dcx ( ) . emit_err ( CannotDetermineMacroResolution {
722
- span,
723
- kind : kind. descr ( ) ,
724
- path : Segment :: names_to_string ( path) ,
725
- } ) ;
754
+ if this. is_builtin_macro ( res)
755
+ || this. builtin_attrs_bindings . values ( ) . any ( |b| b. res ( ) == res)
756
+ {
757
+ if !this. report_invalid_crate_level_attr ( & krate. attrs , path[ 0 ] . ident . name ) {
758
+ return ;
759
+ }
760
+ if this. tcx . dcx ( ) . has_errors ( ) . is_none ( ) {
761
+ this. dcx ( ) . emit_err ( CannotDetermineMacroResolution {
762
+ span,
763
+ kind : kind. descr ( ) ,
764
+ path : Segment :: names_to_string ( path) ,
765
+ } ) ;
766
+ }
767
+ }
726
768
}
727
769
}
728
770
} ;
0 commit comments