@@ -5,7 +5,7 @@ use crate::errors::CannotDetermineMacroResolution;
55use crate :: errors:: { self , AddAsNonDerive , CannotFindIdentInThisScope } ;
66use crate :: errors:: { MacroExpectedFound , RemoveSurroundingDerive } ;
77use crate :: Namespace :: * ;
8- use crate :: { BuiltinMacroState , Determinacy , MacroData , Used } ;
8+ use crate :: { BuiltinMacroState , Determinacy , MacroData , NameBindingKind , Used } ;
99use crate :: { DeriveData , Finalize , ParentScope , ResolutionError , Resolver , ScopeSet } ;
1010use crate :: { ModuleKind , ModuleOrUniformRoot , NameBinding , PathResult , Segment , ToNameBinding } ;
1111use rustc_ast:: expand:: StrippedCfgItem ;
@@ -18,15 +18,18 @@ use rustc_errors::{Applicability, StashKey};
1818use rustc_expand:: base:: { Annotatable , DeriveResolution , Indeterminate , ResolverExpand } ;
1919use rustc_expand:: base:: { SyntaxExtension , SyntaxExtensionKind } ;
2020use rustc_expand:: compile_declarative_macro;
21- use rustc_expand:: expand:: { AstFragment , Invocation , InvocationKind , SupportsMacroExpansion } ;
21+ use rustc_expand:: expand:: {
22+ AstFragment , AstFragmentKind , Invocation , InvocationKind , SupportsMacroExpansion ,
23+ } ;
2224use rustc_hir:: def:: { self , DefKind , Namespace , NonMacroAttrKind } ;
2325use rustc_hir:: def_id:: { CrateNum , DefId , LocalDefId } ;
2426use rustc_middle:: middle:: stability;
2527use rustc_middle:: ty:: RegisteredTools ;
2628use rustc_middle:: ty:: { TyCtxt , Visibility } ;
27- use rustc_session:: lint:: builtin:: UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES ;
28- use rustc_session:: lint:: builtin:: { LEGACY_DERIVE_HELPERS , SOFT_UNSTABLE } ;
29- use rustc_session:: lint:: builtin:: { UNUSED_MACROS , UNUSED_MACRO_RULES } ;
29+ use rustc_session:: lint:: builtin:: {
30+ LEGACY_DERIVE_HELPERS , OUT_OF_SCOPE_MACRO_CALLS , SOFT_UNSTABLE ,
31+ UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES , UNUSED_MACROS , UNUSED_MACRO_RULES ,
32+ } ;
3033use rustc_session:: lint:: BuiltinLintDiag ;
3134use rustc_session:: parse:: feature_err;
3235use rustc_span:: edit_distance:: edit_distance;
@@ -277,6 +280,15 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
277280 let parent_scope = & ParentScope { derives, ..parent_scope } ;
278281 let supports_macro_expansion = invoc. fragment_kind . supports_macro_expansion ( ) ;
279282 let node_id = invoc. expansion_data . lint_node_id ;
283+ let invoc_in_mod_inert_attr = self
284+ . invocation_parents
285+ . get ( & invoc_id)
286+ . or_else ( || self . invocation_parents . get ( & eager_expansion_root) )
287+ . map ( |& ( mod_def_id, _) | mod_def_id)
288+ . filter ( |& mod_def_id| {
289+ invoc. fragment_kind == AstFragmentKind :: Expr
290+ && self . tcx . def_kind ( mod_def_id) == DefKind :: Mod
291+ } ) ;
280292 let ( ext, res) = self . smart_resolve_macro_path (
281293 path,
282294 kind,
@@ -286,6 +298,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
286298 node_id,
287299 force,
288300 soft_custom_inner_attributes_gate ( path, invoc) ,
301+ invoc_in_mod_inert_attr,
289302 ) ?;
290303
291304 let span = invoc. span ( ) ;
@@ -366,6 +379,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
366379 & parent_scope,
367380 true ,
368381 force,
382+ None ,
369383 ) {
370384 Ok ( ( Some ( ext) , _) ) => {
371385 if !ext. helper_attrs . is_empty ( ) {
@@ -468,9 +482,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
468482 node_id : NodeId ,
469483 force : bool ,
470484 soft_custom_inner_attributes_gate : bool ,
485+ invoc_in_mod_inert_attr : Option < LocalDefId > ,
471486 ) -> Result < ( Lrc < SyntaxExtension > , Res ) , Indeterminate > {
472- let ( ext, res) = match self . resolve_macro_path ( path, Some ( kind) , parent_scope, true , force)
473- {
487+ let ( ext, res) = match self . resolve_macro_path (
488+ path,
489+ Some ( kind) ,
490+ parent_scope,
491+ true ,
492+ force,
493+ invoc_in_mod_inert_attr. map ( |def_id| ( def_id, node_id) ) ,
494+ ) {
474495 Ok ( ( Some ( ext) , res) ) => ( ext, res) ,
475496 Ok ( ( None , res) ) => ( self . dummy_ext ( kind) , res) ,
476497 Err ( Determinacy :: Determined ) => ( self . dummy_ext ( kind) , Res :: Err ) ,
@@ -600,14 +621,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
600621
601622 pub ( crate ) fn resolve_macro_path (
602623 & mut self ,
603- path : & ast:: Path ,
624+ ast_path : & ast:: Path ,
604625 kind : Option < MacroKind > ,
605626 parent_scope : & ParentScope < ' a > ,
606627 trace : bool ,
607628 force : bool ,
629+ invoc_in_mod_inert_attr : Option < ( LocalDefId , NodeId ) > ,
608630 ) -> Result < ( Option < Lrc < SyntaxExtension > > , Res ) , Determinacy > {
609- let path_span = path . span ;
610- let mut path = Segment :: from_path ( path ) ;
631+ let path_span = ast_path . span ;
632+ let mut path = Segment :: from_path ( ast_path ) ;
611633
612634 // Possibly apply the macro helper hack
613635 if kind == Some ( MacroKind :: Bang )
@@ -667,6 +689,37 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
667689
668690 let res = binding. map ( |binding| binding. res ( ) ) ;
669691 self . prohibit_imported_non_macro_attrs ( binding. ok ( ) , res. ok ( ) , path_span) ;
692+ if let Ok ( binding) = binding
693+ && matches ! ( binding. kind, NameBindingKind :: Res ( ..) )
694+ && let Some ( ( mod_def_id, node_id) ) = invoc_in_mod_inert_attr
695+ && let Ok ( Res :: Def ( DefKind :: Macro ( MacroKind :: Bang ) , def_id) ) = res
696+ && self . tcx . parent ( def_id) == mod_def_id. to_def_id ( )
697+ {
698+ let tralala = self . early_resolve_ident_in_lexical_scope (
699+ path[ 0 ] . ident ,
700+ scope_set,
701+ & ParentScope {
702+ macro_rules : self . arenas . alloc_macro_rules_scope ( MacroRulesScope :: Empty ) ,
703+ ..* parent_scope
704+ } ,
705+ None ,
706+ false ,
707+ None ,
708+ ) ;
709+
710+ if tralala. ok ( ) . and_then ( |binding| binding. res ( ) . opt_def_id ( ) ) == Some ( def_id) {
711+ // Nothing
712+ } else {
713+ self . tcx . sess . psess . buffer_lint (
714+ OUT_OF_SCOPE_MACRO_CALLS ,
715+ path_span,
716+ node_id,
717+ BuiltinLintDiag :: OutOfScopeMacroCalls {
718+ path : pprust:: path_to_string ( ast_path) ,
719+ } ,
720+ ) ;
721+ }
722+ }
670723 res
671724 } ;
672725
0 commit comments