Skip to content

Commit 4d7a3eb

Browse files
committed
fresh binding should shallow the def after expand
1 parent b63223c commit 4d7a3eb

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
330330

331331
module = match rib.kind {
332332
RibKind::Module(module) => module,
333-
RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
333+
RibKind::MacroDefinition(def) | RibKind::LookAheadMacroDefinition(def)
334+
if def == self.macro_def(ident.span.ctxt()) =>
335+
{
334336
// If an invocation of this macro created `ident`, give up on `ident`
335337
// and switch to `ident`'s source from the macro definition.
336338
ident.span.remove_mark();
@@ -1141,6 +1143,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11411143
| RibKind::FnOrCoroutine
11421144
| RibKind::Module(..)
11431145
| RibKind::MacroDefinition(..)
1146+
| RibKind::LookAheadMacroDefinition(..)
11441147
| RibKind::ForwardGenericParamBan(_) => {
11451148
// Nothing to do. Continue.
11461149
}
@@ -1233,6 +1236,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12331236
| RibKind::FnOrCoroutine
12341237
| RibKind::Module(..)
12351238
| RibKind::MacroDefinition(..)
1239+
| RibKind::LookAheadMacroDefinition(..)
12361240
| RibKind::InlineAsmSym
12371241
| RibKind::AssocItem
12381242
| RibKind::ForwardGenericParamBan(_) => {
@@ -1326,6 +1330,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13261330
| RibKind::FnOrCoroutine
13271331
| RibKind::Module(..)
13281332
| RibKind::MacroDefinition(..)
1333+
| RibKind::LookAheadMacroDefinition(..)
13291334
| RibKind::InlineAsmSym
13301335
| RibKind::AssocItem
13311336
| RibKind::ForwardGenericParamBan(_) => continue,

compiler/rustc_resolve/src/late.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl IntoDiagArg for PatternSource {
103103
/// Denotes whether the context for the set of already bound bindings is a `Product`
104104
/// or `Or` context. This is used in e.g., `fresh_binding` and `resolve_pattern_inner`.
105105
/// See those functions for more information.
106-
#[derive(PartialEq)]
106+
#[derive(PartialEq, Debug)]
107107
enum PatBoundCtx {
108108
/// A product pattern context, e.g., `Variant(a, b)`.
109109
Product,
@@ -217,6 +217,8 @@ pub(crate) enum RibKind<'ra> {
217217
/// We passed through a `macro_rules!` statement
218218
MacroDefinition(DefId),
219219

220+
LookAheadMacroDefinition(DefId),
221+
220222
/// All bindings in this rib are generic parameters that can't be used
221223
/// from the default of a generic parameter because they're not declared
222224
/// before said generic parameter. Also see the `visit_generics` override.
@@ -247,6 +249,7 @@ impl RibKind<'_> {
247249
| RibKind::ConstantItem(..)
248250
| RibKind::Module(_)
249251
| RibKind::MacroDefinition(_)
252+
| RibKind::LookAheadMacroDefinition(_)
250253
| RibKind::InlineAsmSym => false,
251254
RibKind::ConstParamTy
252255
| RibKind::AssocItem
@@ -258,7 +261,9 @@ impl RibKind<'_> {
258261
/// This rib forbids referring to labels defined in upwards ribs.
259262
fn is_label_barrier(self) -> bool {
260263
match self {
261-
RibKind::Normal | RibKind::MacroDefinition(..) => false,
264+
RibKind::Normal
265+
| RibKind::MacroDefinition(..)
266+
| RibKind::LookAheadMacroDefinition(..) => false,
262267

263268
RibKind::AssocItem
264269
| RibKind::FnOrCoroutine
@@ -3793,17 +3798,21 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
37933798

37943799
/// Apply the bindings from a pattern to the innermost rib of the current scope.
37953800
fn apply_pattern_bindings(&mut self, mut pat_bindings: PatternBindings) {
3796-
let rib_bindings = self.innermost_rib_bindings(ValueNS);
37973801
let Some((_, pat_bindings)) = pat_bindings.pop() else {
37983802
bug!("tried applying nonexistent bindings from pattern");
37993803
};
3800-
3801-
if rib_bindings.is_empty() {
3802-
// Often, such as for match arms, the bindings are introduced into a new rib.
3803-
// In this case, we can move the bindings over directly.
3804-
*rib_bindings = pat_bindings;
3805-
} else {
3806-
rib_bindings.extend(pat_bindings);
3804+
for rib in self.ribs[ValueNS].iter_mut().rev() {
3805+
let stop = !matches!(rib.kind, RibKind::LookAheadMacroDefinition(_));
3806+
if rib.bindings.is_empty() {
3807+
// Often, such as for match arms, the bindings are introduced into a new rib.
3808+
// In this case, we can move the bindings over directly.
3809+
rib.bindings = pat_bindings.clone();
3810+
} else {
3811+
rib.bindings.extend(pat_bindings.clone());
3812+
}
3813+
if stop {
3814+
break;
3815+
}
38073816
}
38083817
}
38093818

@@ -4680,11 +4689,19 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46804689
&& let ItemKind::MacroDef(..) = item.kind
46814690
{
46824691
num_macro_definition_ribs += 1;
4692+
let res = self.r.local_def_id(item.id).to_def_id();
4693+
self.ribs[ValueNS].push(Rib::new(RibKind::LookAheadMacroDefinition(res)));
4694+
}
4695+
}
4696+
4697+
for stmt in &block.stmts {
4698+
if let StmtKind::Item(ref item) = stmt.kind
4699+
&& let ItemKind::MacroDef(..) = item.kind
4700+
{
46834701
let res = self.r.local_def_id(item.id).to_def_id();
46844702
self.ribs[ValueNS].push(Rib::new(RibKind::MacroDefinition(res)));
46854703
self.label_ribs.push(Rib::new(RibKind::MacroDefinition(res)));
46864704
}
4687-
46884705
self.visit_stmt(stmt);
46894706
}
46904707

@@ -4694,6 +4711,10 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46944711
self.ribs[ValueNS].pop();
46954712
self.label_ribs.pop();
46964713
}
4714+
for _ in 0..num_macro_definition_ribs {
4715+
// pop `RibKind::LookAheadMacroDefinition`
4716+
self.ribs[ValueNS].pop();
4717+
}
46974718
self.last_block_rib = self.ribs[ValueNS].pop();
46984719
if anonymous_module.is_some() {
46994720
self.ribs[TypeNS].pop();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//@ check-pass
2+
//@ edition:2018
3+
4+
// issue#95237
5+
6+
fn f0() {
7+
fn f() -> i8 { 42 }
8+
let f = || -> i16 { 42 };
9+
10+
let a: i16 = m!();
11+
macro_rules! m {() => ( f() )}
12+
use m;
13+
let b: i16 = m!();
14+
}
15+
16+
fn f1() {
17+
fn f() -> i8 { 42 }
18+
19+
let a: i8 = m!();
20+
let f = || -> i16 { 42 };
21+
macro_rules! m {() => ( f() )}
22+
use m;
23+
let b: i16 = m!();
24+
}
25+
26+
fn f2() {
27+
fn f() -> i8 { 42 }
28+
29+
let a: i8 = m!();
30+
macro_rules! m {() => ( f() )}
31+
use m;
32+
let b: i8 = m!();
33+
34+
let f = || -> i16 { 42 };
35+
}
36+
37+
fn f3() {
38+
let f = || -> i16 { 42 };
39+
40+
let a: i16 = m!();
41+
macro_rules! m {() => ( f() )}
42+
use m;
43+
let b: i16 = m!();
44+
}
45+
46+
fn main () {}

0 commit comments

Comments
 (0)