Skip to content

Commit 54c46c0

Browse files
committed
Also treat impl definition parent as transparent regarding modules
1 parent a0d98ff commit 54c46c0

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

compiler/rustc_lint/src/non_local_def.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ fn path_has_local_parent(
284284
.is_some_and(|did| did_has_local_parent(did, cx.tcx, impl_parent, outermost_impl_parent))
285285
}
286286

287-
/// Given a def id this checks if the parent def id (modulo modules) correspond to
288-
/// the def id of the parent impl definition (the direct one and the outermost one).
287+
/// Given a def if we check if the parent def id (modulo modules and const-anons) correspond
288+
/// to the def id of the parent impl definition (the direct parent or the outermost one).
289289
#[inline]
290290
fn did_has_local_parent(
291291
did: DefId,
@@ -301,13 +301,21 @@ fn did_has_local_parent(
301301
return false;
302302
};
303303

304-
peel_parent_while(tcx, parent_did, |tcx, did| {
305-
tcx.def_kind(did) == DefKind::Mod
306-
|| (tcx.def_kind(did) == DefKind::Const
307-
&& tcx.opt_item_name(did) == Some(kw::Underscore))
308-
})
309-
.map(|parent_did| parent_did == impl_parent || Some(parent_did) == outermost_impl_parent)
310-
.unwrap_or(false)
304+
// Peel modules
305+
peel_parent_while(tcx, parent_did, |tcx, did| tcx.def_kind(did) == DefKind::Mod)
306+
// Peel const-anons
307+
.map(|parent_did| {
308+
peel_parent_while(tcx, parent_did, |tcx, did| {
309+
tcx.def_kind(did) == DefKind::Const
310+
&& tcx.opt_item_name(did) == Some(kw::Underscore)
311+
})
312+
})
313+
.flatten()
314+
// Check if parent def if is either equal to:
315+
// - the `impl` parent
316+
// - or the outermost `impl` parent
317+
.map(|parent_did| parent_did == impl_parent || Some(parent_did) == outermost_impl_parent)
318+
.unwrap_or(false)
311319
}
312320

313321
/// Given a `DefId` checks if it satisfies `f` if it does check with it's parent and continue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Regression tests for https://github.com/rust-lang/rust/issues/132427
2+
3+
//@ check-pass
4+
5+
// original
6+
mod auth {
7+
const _: () = {
8+
pub enum ArbitraryContext {}
9+
10+
const _: () = {
11+
impl ArbitraryContext {}
12+
};
13+
};
14+
}
15+
16+
const _: () = {
17+
mod auth {
18+
const _: () = {
19+
pub enum ArbitraryContext {}
20+
21+
const _: () = {
22+
impl ArbitraryContext {}
23+
};
24+
};
25+
}
26+
};
27+
28+
mod a {
29+
mod b {
30+
const _: () = {
31+
pub enum ArbitraryContext {}
32+
33+
const _: () = {
34+
impl ArbitraryContext {}
35+
};
36+
};
37+
}
38+
}
39+
40+
mod foo {
41+
const _: () = {
42+
mod auth {
43+
const _: () = {
44+
pub enum ArbitraryContext {}
45+
46+
const _: () = {
47+
impl ArbitraryContext {}
48+
};
49+
};
50+
}
51+
};
52+
}
53+
54+
fn main() {}

0 commit comments

Comments
 (0)