Skip to content

Commit 6ba80a9

Browse files
committed
Auto merge of #126987 - petrochenkov:atvisord2, r=pnkfelix
out_of_scope_macro_calls: Detect calls inside attributes more precisely Fixes #126984.
2 parents ed7e35f + 83cf471 commit 6ba80a9

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

compiler/rustc_resolve/src/def_collector.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ use rustc_hir::def_id::LocalDefId;
88
use rustc_span::hygiene::LocalExpnId;
99
use rustc_span::symbol::{kw, sym, Symbol};
1010
use rustc_span::Span;
11+
use std::mem;
1112
use tracing::debug;
1213

1314
pub(crate) fn collect_definitions(
1415
resolver: &mut Resolver<'_, '_>,
1516
fragment: &AstFragment,
1617
expansion: LocalExpnId,
1718
) {
18-
let (parent_def, impl_trait_context) = resolver.invocation_parents[&expansion];
19-
fragment.visit_with(&mut DefCollector { resolver, parent_def, expansion, impl_trait_context });
19+
let (parent_def, impl_trait_context, in_attr) = resolver.invocation_parents[&expansion];
20+
let mut visitor = DefCollector { resolver, parent_def, expansion, impl_trait_context, in_attr };
21+
fragment.visit_with(&mut visitor);
2022
}
2123

2224
/// Creates `DefId`s for nodes in the AST.
2325
struct DefCollector<'a, 'b, 'tcx> {
2426
resolver: &'a mut Resolver<'b, 'tcx>,
2527
parent_def: LocalDefId,
2628
impl_trait_context: ImplTraitContext,
29+
in_attr: bool,
2730
expansion: LocalExpnId,
2831
}
2932

@@ -53,7 +56,7 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
5356
}
5457

5558
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
56-
let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def);
59+
let orig_parent_def = mem::replace(&mut self.parent_def, parent_def);
5760
f(self);
5861
self.parent_def = orig_parent_def;
5962
}
@@ -63,7 +66,7 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
6366
impl_trait_context: ImplTraitContext,
6467
f: F,
6568
) {
66-
let orig_itc = std::mem::replace(&mut self.impl_trait_context, impl_trait_context);
69+
let orig_itc = mem::replace(&mut self.impl_trait_context, impl_trait_context);
6770
f(self);
6871
self.impl_trait_context = orig_itc;
6972
}
@@ -105,8 +108,10 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
105108

106109
fn visit_macro_invoc(&mut self, id: NodeId) {
107110
let id = id.placeholder_to_expn_id();
108-
let old_parent =
109-
self.resolver.invocation_parents.insert(id, (self.parent_def, self.impl_trait_context));
111+
let old_parent = self
112+
.resolver
113+
.invocation_parents
114+
.insert(id, (self.parent_def, self.impl_trait_context, self.in_attr));
110115
assert!(old_parent.is_none(), "parent `LocalDefId` is reset for an invocation");
111116
}
112117
}
@@ -413,4 +418,10 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
413418
visit::walk_crate(self, krate)
414419
}
415420
}
421+
422+
fn visit_attribute(&mut self, attr: &'a Attribute) -> Self::Result {
423+
let orig_in_attr = mem::replace(&mut self.in_attr, true);
424+
visit::walk_attribute(self, attr);
425+
self.in_attr = orig_in_attr;
426+
}
416427
}

compiler/rustc_resolve/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ pub struct Resolver<'a, 'tcx> {
11391139
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
11401140
/// we know what parent node that fragment should be attached to thanks to this table,
11411141
/// and how the `impl Trait` fragments were introduced.
1142-
invocation_parents: FxHashMap<LocalExpnId, (LocalDefId, ImplTraitContext)>,
1142+
invocation_parents: FxHashMap<LocalExpnId, (LocalDefId, ImplTraitContext, bool /*in_attr*/)>,
11431143

11441144
/// Some way to know that we are in a *trait* impl in `visit_assoc_item`.
11451145
/// FIXME: Replace with a more general AST map (together with some other fields).
@@ -1371,7 +1371,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13711371
node_id_to_def_id.insert(CRATE_NODE_ID, crate_feed);
13721372

13731373
let mut invocation_parents = FxHashMap::default();
1374-
invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential));
1374+
invocation_parents
1375+
.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential, false));
13751376

13761377
let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> = tcx
13771378
.sess

compiler/rustc_resolve/src/macros.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,12 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
297297
.invocation_parents
298298
.get(&invoc_id)
299299
.or_else(|| self.invocation_parents.get(&eager_expansion_root))
300-
.map(|&(mod_def_id, _)| mod_def_id)
301-
.filter(|&mod_def_id| {
302-
invoc.fragment_kind == AstFragmentKind::Expr
300+
.filter(|&&(mod_def_id, _, in_attr)| {
301+
in_attr
302+
&& invoc.fragment_kind == AstFragmentKind::Expr
303303
&& self.tcx.def_kind(mod_def_id) == DefKind::Mod
304-
});
304+
})
305+
.map(|&(mod_def_id, ..)| mod_def_id);
305306
let (ext, res) = self.smart_resolve_macro_path(
306307
path,
307308
kind,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ check-pass
2+
//@ needs-asm-support
3+
4+
macro_rules! mac { () => { "" } }
5+
macro_rules! mac2 { () => { "auxiliary/issue-40469.rs" } }
6+
7+
std::arch::global_asm!(mac!()); // OK
8+
include!(mac2!()); // OK
9+
10+
fn main() {}

0 commit comments

Comments
 (0)