1
1
//! A bunch of methods and structures more or less related to resolving macros and
2
2
//! interface provided by `Resolver` to macro expander.
3
3
4
+ use crate :: ast:: AttrStyle ;
5
+ use crate :: ast:: Attribute ;
4
6
use crate :: errors:: {
5
7
self , AddAsNonDerive , CannotDetermineMacroResolution , CannotFindIdentInThisScope ,
6
8
MacroExpectedFound , RemoveSurroundingDerive ,
@@ -35,6 +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 } ;
40
+ use rustc_span:: BytePos ;
38
41
use rustc_span:: { Span , DUMMY_SP } ;
39
42
use std:: cell:: Cell ;
40
43
use std:: mem;
@@ -698,6 +701,35 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
698
701
res. map ( |res| ( self . get_macro ( res) . map ( |macro_data| macro_data. ext . clone ( ) ) , res) )
699
702
}
700
703
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
+ name,
710
+ span : attr. span ,
711
+ sugg : errors:: InvalidAttrSugg {
712
+ span : tcx
713
+ . sess
714
+ . source_map ( )
715
+ . span_to_snippet ( attr. span )
716
+ . ok ( )
717
+ . filter ( |src| src. starts_with ( "#![" ) )
718
+ . map ( |_| {
719
+ attr. span
720
+ . with_lo ( attr. span . lo ( ) + BytePos ( 1 ) )
721
+ . with_hi ( attr. span . lo ( ) + BytePos ( 2 ) )
722
+ } )
723
+ . unwrap ( ) ,
724
+ name,
725
+ } ,
726
+ } ) ;
727
+ return true ;
728
+ }
729
+ }
730
+ false
731
+ }
732
+
701
733
pub ( crate ) fn finalize_macro_resolutions ( & mut self , krate : & Crate ) {
702
734
let check_consistency = |this : & mut Self ,
703
735
path : & [ Segment ] ,
@@ -717,15 +749,28 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
717
749
// expanded into a dummy fragment for recovery during expansion.
718
750
// Now, post-expansion, the resolution may succeed, but we can't change the
719
751
// past and need to report an error.
720
- // However, non-speculative `resolve_path` can successfully return private items
752
+ // Special cases:
753
+ // 1. non-speculative `resolve_path` can successfully return private items
721
754
// even if speculative `resolve_path` returned nothing previously, so we skip this
722
755
// less informative error if the privacy error is reported elsewhere.
756
+ // 2. issue #118455, unresolved top level attribute error didn't imported prelude and
757
+ // already have emitted an error, report builtin macro and attributes error by the way,
758
+ // so `check_invalid_crate_level_attr` in can ignore them.
759
+
760
+ let is_builtin = res. opt_def_id ( ) . map_or ( false , |_| this. is_builtin ( res) ) ;
723
761
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
- } ) ;
762
+ let mut fallback = !is_builtin;
763
+ if is_builtin && krate. id == ast:: CRATE_NODE_ID {
764
+ fallback =
765
+ !this. report_invalid_crate_level_attr ( & krate. attrs , path[ 0 ] . ident . name ) ;
766
+ }
767
+ if fallback && this. tcx . sess . has_errors ( ) . is_none ( ) {
768
+ this. tcx . sess . emit_err ( CannotDetermineMacroResolution {
769
+ span,
770
+ kind : kind. descr ( ) ,
771
+ path : Segment :: names_to_string ( path) ,
772
+ } ) ;
773
+ }
729
774
}
730
775
}
731
776
} ;
0 commit comments