@@ -10,7 +10,9 @@ use crate::{BuiltinMacroState, Determinacy, MacroData};
1010use crate :: { DeriveData , Finalize , ParentScope , ResolutionError , Resolver , ScopeSet } ;
1111use crate :: { ModuleKind , ModuleOrUniformRoot , NameBinding , PathResult , Segment , ToNameBinding } ;
1212use 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+ } ;
1416use rustc_ast_pretty:: pprust;
1517use rustc_attr:: StabilityLevel ;
1618use rustc_data_structures:: intern:: Interned ;
@@ -35,7 +37,7 @@ use rustc_span::edition::Edition;
3537use rustc_span:: hygiene:: { self , ExpnData , ExpnKind , LocalExpnId } ;
3638use rustc_span:: hygiene:: { AstPass , MacroKind } ;
3739use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
38- use rustc_span:: { Span , DUMMY_SP } ;
40+ use rustc_span:: { BytePos , Span , DUMMY_SP } ;
3941use std:: cell:: Cell ;
4042use std:: mem;
4143
@@ -689,6 +691,32 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
689691 res. map ( |res| ( self . get_macro ( res) . map ( |macro_data| macro_data. ext . clone ( ) ) , res) )
690692 }
691693
694+ fn report_invalid_crate_level_attr ( & mut self , attrs : & [ Attribute ] , name : Symbol ) -> bool {
695+ for attr in attrs. iter ( ) . filter ( |attr| attr. style == AttrStyle :: Inner ) {
696+ if attr. has_name ( name) {
697+ let tcx = self . tcx ;
698+ tcx. dcx ( ) . emit_err ( rustc_attr:: InvalidAttrAtCrateLevel {
699+ span : attr. span ,
700+ sugg_span : tcx
701+ . sess
702+ . source_map ( )
703+ . span_to_snippet ( attr. span )
704+ . ok ( )
705+ . filter ( |src| src. starts_with ( "#![" ) )
706+ . map ( |_| {
707+ attr. span
708+ . with_lo ( attr. span . lo ( ) + BytePos ( 1 ) )
709+ . with_hi ( attr. span . lo ( ) + BytePos ( 2 ) )
710+ } ) ,
711+ name,
712+ item : None ,
713+ } ) ;
714+ return true ;
715+ }
716+ }
717+ false
718+ }
719+
692720 pub ( crate ) fn finalize_macro_resolutions ( & mut self , krate : & Crate ) {
693721 let check_consistency = |this : & mut Self ,
694722 path : & [ Segment ] ,
@@ -708,15 +736,29 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
708736 // expanded into a dummy fragment for recovery during expansion.
709737 // Now, post-expansion, the resolution may succeed, but we can't change the
710738 // past and need to report an error.
711- // However, non-speculative `resolve_path` can successfully return private items
739+ // Special cases:
740+ // 1. non-speculative `resolve_path` can successfully return private items
712741 // even if speculative `resolve_path` returned nothing previously, so we skip this
713742 // less informative error if the privacy error is reported elsewhere.
743+ // 2. issue #118455, unresolved top level attribute error didn't imported prelude and
744+ // already have emitted an error, report builtin macro and attributes error by the way,
745+ // so `check_invalid_crate_level_attr` in can ignore them.
746+
714747 if this. privacy_errors . is_empty ( ) {
715- this. dcx ( ) . emit_err ( CannotDetermineMacroResolution {
716- span,
717- kind : kind. descr ( ) ,
718- path : Segment :: names_to_string ( path) ,
719- } ) ;
748+ if this. is_builtin_macro ( res)
749+ || this. builtin_attrs_bindings . values ( ) . any ( |b| b. res ( ) == res)
750+ {
751+ if !this. report_invalid_crate_level_attr ( & krate. attrs , path[ 0 ] . ident . name ) {
752+ return ;
753+ }
754+ if this. tcx . dcx ( ) . has_errors ( ) . is_none ( ) {
755+ this. dcx ( ) . emit_err ( CannotDetermineMacroResolution {
756+ span,
757+ kind : kind. descr ( ) ,
758+ path : Segment :: names_to_string ( path) ,
759+ } ) ;
760+ }
761+ }
720762 }
721763 }
722764 } ;
0 commit comments