@@ -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
@@ -695,6 +697,32 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
695697 res. map ( |res| ( self . get_macro ( res) . map ( |macro_data| macro_data. ext . clone ( ) ) , res) )
696698 }
697699
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+
698726 pub ( crate ) fn finalize_macro_resolutions ( & mut self , krate : & Crate ) {
699727 let check_consistency = |this : & mut Self ,
700728 path : & [ Segment ] ,
@@ -714,15 +742,29 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
714742 // expanded into a dummy fragment for recovery during expansion.
715743 // Now, post-expansion, the resolution may succeed, but we can't change the
716744 // 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
718747 // even if speculative `resolve_path` returned nothing previously, so we skip this
719748 // 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+
720753 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+ }
726768 }
727769 }
728770 } ;
0 commit comments