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;
@@ -695,6 +698,35 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
695
698
res. map ( |res| ( self . get_macro ( res) . map ( |macro_data| macro_data. ext . clone ( ) ) , res) )
696
699
}
697
700
701
+ fn report_invalid_crate_level_attr ( & mut self , attrs : & [ Attribute ] , name : Symbol ) -> bool {
702
+ for attr in attrs. iter ( ) . filter ( |attr| attr. style == AttrStyle :: Inner ) {
703
+ if attr. has_name ( name) {
704
+ let tcx = self . tcx ;
705
+ tcx. sess . emit_err ( errors:: InvalidAttrAtCrateLevel {
706
+ name,
707
+ span : attr. span ,
708
+ sugg : errors:: InvalidAttrSugg {
709
+ span : tcx
710
+ . sess
711
+ . source_map ( )
712
+ . span_to_snippet ( attr. span )
713
+ . ok ( )
714
+ . filter ( |src| src. starts_with ( "#![" ) )
715
+ . map ( |_| {
716
+ attr. span
717
+ . with_lo ( attr. span . lo ( ) + BytePos ( 1 ) )
718
+ . with_hi ( attr. span . lo ( ) + BytePos ( 2 ) )
719
+ } )
720
+ . unwrap ( ) ,
721
+ name,
722
+ } ,
723
+ } ) ;
724
+ return true ;
725
+ }
726
+ }
727
+ false
728
+ }
729
+
698
730
pub ( crate ) fn finalize_macro_resolutions ( & mut self , krate : & Crate ) {
699
731
let check_consistency = |this : & mut Self ,
700
732
path : & [ Segment ] ,
@@ -714,15 +746,28 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
714
746
// expanded into a dummy fragment for recovery during expansion.
715
747
// Now, post-expansion, the resolution may succeed, but we can't change the
716
748
// past and need to report an error.
717
- // However, non-speculative `resolve_path` can successfully return private items
749
+ // Special cases:
750
+ // 1. non-speculative `resolve_path` can successfully return private items
718
751
// even if speculative `resolve_path` returned nothing previously, so we skip this
719
752
// less informative error if the privacy error is reported elsewhere.
753
+ // 2. issue #118455, unresolved top level attribute error didn't imported prelude and
754
+ // already have emitted an error, report builtin macro and attributes error by the way,
755
+ // so `check_invalid_crate_level_attr` in can ignore them.
756
+
757
+ let is_builtin = res. opt_def_id ( ) . map_or ( false , |_| this. is_builtin ( res) ) ;
720
758
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
- } ) ;
759
+ let mut fallback = !is_builtin;
760
+ if is_builtin && krate. id == ast:: CRATE_NODE_ID {
761
+ fallback =
762
+ !this. report_invalid_crate_level_attr ( & krate. attrs , path[ 0 ] . ident . name ) ;
763
+ }
764
+ if fallback && this. tcx . sess . has_errors ( ) . is_none ( ) {
765
+ this. dcx ( ) . emit_err ( CannotDetermineMacroResolution {
766
+ span,
767
+ kind : kind. descr ( ) ,
768
+ path : Segment :: names_to_string ( path) ,
769
+ } ) ;
770
+ }
726
771
}
727
772
}
728
773
} ;
0 commit comments