Skip to content

Commit 4b8149f

Browse files
Urgaucuviper
authored andcommitted
Consider inner modules to be local in the non_local_definitions lint
(cherry picked from commit 21c688a)
1 parent f5d04ca commit 4b8149f

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

compiler/rustc_lint/src/non_local_def.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -356,16 +356,22 @@ fn path_has_local_parent(
356356
}
357357

358358
/// Given a def id and a parent impl def id, this checks if the parent
359-
/// def id correspond to the def id of the parent impl definition.
359+
/// def id (modulo modules) correspond to the def id of the parent impl definition.
360360
#[inline]
361361
fn did_has_local_parent(
362362
did: DefId,
363363
tcx: TyCtxt<'_>,
364364
impl_parent: DefId,
365365
impl_parent_parent: Option<DefId>,
366366
) -> bool {
367-
did.is_local() && {
368-
let res_parent = tcx.parent(did);
369-
res_parent == impl_parent || Some(res_parent) == impl_parent_parent
370-
}
367+
did.is_local()
368+
&& if let Some(did_parent) = tcx.opt_parent(did) {
369+
did_parent == impl_parent
370+
|| Some(did_parent) == impl_parent_parent
371+
|| !did_parent.is_crate_root()
372+
&& tcx.def_kind(did_parent) == DefKind::Mod
373+
&& did_has_local_parent(did_parent, tcx, impl_parent, impl_parent_parent)
374+
} else {
375+
false
376+
}
371377
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//! This test checks that module are treated as if they were local
2+
//!
3+
//! https://github.com/rust-lang/rust/issues/124396
4+
5+
//@ check-pass
6+
7+
trait JoinTo {}
8+
9+
fn simple_one() {
10+
mod posts {
11+
#[allow(non_camel_case_types)]
12+
pub struct table {}
13+
}
14+
15+
impl JoinTo for posts::table {}
16+
}
17+
18+
fn simple_two() {
19+
mod posts {
20+
pub mod posts {
21+
#[allow(non_camel_case_types)]
22+
pub struct table {}
23+
}
24+
}
25+
26+
impl JoinTo for posts::posts::table {}
27+
}
28+
29+
struct Global;
30+
fn trait_() {
31+
mod posts {
32+
pub trait AdjecentTo {}
33+
}
34+
35+
impl posts::AdjecentTo for Global {}
36+
}
37+
38+
fn main() {}

0 commit comments

Comments
 (0)