11//! A bunch of methods and structures more or less related to resolving macros and
22//! interface provided by `Resolver` to macro expander.
33
4+ use crate :: ast:: AttrStyle ;
5+ use crate :: ast:: Attribute ;
46use crate :: errors:: {
57 self , AddAsNonDerive , CannotDetermineMacroResolution , CannotFindIdentInThisScope ,
68 MacroExpectedFound , RemoveSurroundingDerive ,
@@ -35,6 +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 } ;
40+ use rustc_span:: BytePos ;
3841use rustc_span:: { Span , DUMMY_SP } ;
3942use std:: cell:: Cell ;
4043use std:: mem;
@@ -698,6 +701,31 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
698701 res. map ( |res| ( self . get_macro ( res) . map ( |macro_data| macro_data. ext . clone ( ) ) , res) )
699702 }
700703
704+ fn report_invalid_crate_level_attr ( & mut self , attrs : & [ Attribute ] , name : Symbol ) -> bool {
705+ for attr in attrs. iter ( ) . filter ( |attr| attr. style == AttrStyle :: Inner ) {
706+ if attr. has_name ( name) {
707+ let tcx = self . tcx ;
708+ tcx. sess . emit_err ( errors:: InvalidAttrAtCrateLevel {
709+ span : attr. span ,
710+ sugg_span : tcx
711+ . sess
712+ . source_map ( )
713+ . span_to_snippet ( attr. span )
714+ . ok ( )
715+ . filter ( |src| src. starts_with ( "#![" ) )
716+ . map ( |_| {
717+ attr. span
718+ . with_lo ( attr. span . lo ( ) + BytePos ( 1 ) )
719+ . with_hi ( attr. span . lo ( ) + BytePos ( 2 ) )
720+ } ) ,
721+ name,
722+ } ) ;
723+ return true ;
724+ }
725+ }
726+ false
727+ }
728+
701729 pub ( crate ) fn finalize_macro_resolutions ( & mut self , krate : & Crate ) {
702730 let check_consistency = |this : & mut Self ,
703731 path : & [ Segment ] ,
@@ -717,15 +745,28 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
717745 // expanded into a dummy fragment for recovery during expansion.
718746 // Now, post-expansion, the resolution may succeed, but we can't change the
719747 // past and need to report an error.
720- // However, non-speculative `resolve_path` can successfully return private items
748+ // Special cases:
749+ // 1. non-speculative `resolve_path` can successfully return private items
721750 // even if speculative `resolve_path` returned nothing previously, so we skip this
722751 // less informative error if the privacy error is reported elsewhere.
752+ // 2. issue #118455, unresolved top level attribute error didn't imported prelude and
753+ // already have emitted an error, report builtin macro and attributes error by the way,
754+ // so `check_invalid_crate_level_attr` in can ignore them.
755+
756+ let is_builtin = res. opt_def_id ( ) . map_or ( false , |_| this. is_builtin ( res) ) ;
723757 if this. privacy_errors . is_empty ( ) {
724- this. tcx . sess . emit_err ( CannotDetermineMacroResolution {
725- span,
726- kind : kind. descr ( ) ,
727- path : Segment :: names_to_string ( path) ,
728- } ) ;
758+ let mut fallback = !is_builtin;
759+ if is_builtin && krate. id == ast:: CRATE_NODE_ID {
760+ fallback =
761+ !this. report_invalid_crate_level_attr ( & krate. attrs , path[ 0 ] . ident . name ) ;
762+ }
763+ if fallback && this. tcx . sess . has_errors ( ) . is_none ( ) {
764+ this. tcx . sess . emit_err ( CannotDetermineMacroResolution {
765+ span,
766+ kind : kind. descr ( ) ,
767+ path : Segment :: names_to_string ( path) ,
768+ } ) ;
769+ }
729770 }
730771 }
731772 } ;
0 commit comments