@@ -5,7 +5,7 @@ use crate::errors::CannotDetermineMacroResolution;
5
5
use crate :: errors:: { self , AddAsNonDerive , CannotFindIdentInThisScope } ;
6
6
use crate :: errors:: { MacroExpectedFound , RemoveSurroundingDerive } ;
7
7
use crate :: Namespace :: * ;
8
- use crate :: { BuiltinMacroState , Determinacy , MacroData , Used } ;
8
+ use crate :: { BuiltinMacroState , Determinacy , MacroData , NameBindingKind , Used } ;
9
9
use crate :: { DeriveData , Finalize , ParentScope , ResolutionError , Resolver , ScopeSet } ;
10
10
use crate :: { ModuleKind , ModuleOrUniformRoot , NameBinding , PathResult , Segment , ToNameBinding } ;
11
11
use rustc_ast:: expand:: StrippedCfgItem ;
@@ -18,15 +18,18 @@ use rustc_errors::{Applicability, StashKey};
18
18
use rustc_expand:: base:: { Annotatable , DeriveResolution , Indeterminate , ResolverExpand } ;
19
19
use rustc_expand:: base:: { SyntaxExtension , SyntaxExtensionKind } ;
20
20
use 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
+ } ;
22
24
use rustc_hir:: def:: { self , DefKind , Namespace , NonMacroAttrKind } ;
23
25
use rustc_hir:: def_id:: { CrateNum , DefId , LocalDefId } ;
24
26
use rustc_middle:: middle:: stability;
25
27
use rustc_middle:: ty:: RegisteredTools ;
26
28
use 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
+ } ;
30
33
use rustc_session:: lint:: BuiltinLintDiag ;
31
34
use rustc_session:: parse:: feature_err;
32
35
use rustc_span:: edit_distance:: edit_distance;
@@ -277,6 +280,15 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
277
280
let parent_scope = & ParentScope { derives, ..parent_scope } ;
278
281
let supports_macro_expansion = invoc. fragment_kind . supports_macro_expansion ( ) ;
279
282
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
+ } ) ;
280
292
let ( ext, res) = self . smart_resolve_macro_path (
281
293
path,
282
294
kind,
@@ -286,6 +298,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
286
298
node_id,
287
299
force,
288
300
soft_custom_inner_attributes_gate ( path, invoc) ,
301
+ invoc_in_mod_inert_attr,
289
302
) ?;
290
303
291
304
let span = invoc. span ( ) ;
@@ -366,6 +379,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
366
379
& parent_scope,
367
380
true ,
368
381
force,
382
+ None ,
369
383
) {
370
384
Ok ( ( Some ( ext) , _) ) => {
371
385
if !ext. helper_attrs . is_empty ( ) {
@@ -468,9 +482,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
468
482
node_id : NodeId ,
469
483
force : bool ,
470
484
soft_custom_inner_attributes_gate : bool ,
485
+ invoc_in_mod_inert_attr : Option < LocalDefId > ,
471
486
) -> 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
+ ) {
474
495
Ok ( ( Some ( ext) , res) ) => ( ext, res) ,
475
496
Ok ( ( None , res) ) => ( self . dummy_ext ( kind) , res) ,
476
497
Err ( Determinacy :: Determined ) => ( self . dummy_ext ( kind) , Res :: Err ) ,
@@ -600,14 +621,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
600
621
601
622
pub ( crate ) fn resolve_macro_path (
602
623
& mut self ,
603
- path : & ast:: Path ,
624
+ ast_path : & ast:: Path ,
604
625
kind : Option < MacroKind > ,
605
626
parent_scope : & ParentScope < ' a > ,
606
627
trace : bool ,
607
628
force : bool ,
629
+ invoc_in_mod_inert_attr : Option < ( LocalDefId , NodeId ) > ,
608
630
) -> 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 ) ;
611
633
612
634
// Possibly apply the macro helper hack
613
635
if kind == Some ( MacroKind :: Bang )
@@ -667,6 +689,37 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
667
689
668
690
let res = binding. map ( |binding| binding. res ( ) ) ;
669
691
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
+ }
670
723
res
671
724
} ;
672
725
0 commit comments