Skip to content

Commit 378a43a

Browse files
committed
Auto merge of #124539 - Urgau:non-local-defs_modulo_modules, r=lcnr
Consider inner modules to be local in the `non_local_definitions` lint This PR implements the [proposed fix](#124396 (comment)) for #124396, that is to consider inner modules to be local in the `non_local_definitions` lint. This PR is voluntarily kept as minimal as possible so it can be backported easily. T-lang [nomination](#124396 (comment)) will need to be removed before this can be merged. Fixes *(nearly, needs backport)* #124396
2 parents 5e469eb + 21c688a commit 378a43a

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

Diff for: compiler/rustc_lint/src/non_local_def.rs

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

359359
/// Given a def id and a parent impl def id, this checks if the parent
360-
/// def id correspond to the def id of the parent impl definition.
360+
/// def id (modulo modules) correspond to the def id of the parent impl definition.
361361
#[inline]
362362
fn did_has_local_parent(
363363
did: DefId,
364364
tcx: TyCtxt<'_>,
365365
impl_parent: DefId,
366366
impl_parent_parent: Option<DefId>,
367367
) -> bool {
368-
did.is_local() && {
369-
let res_parent = tcx.parent(did);
370-
res_parent == impl_parent || Some(res_parent) == impl_parent_parent
371-
}
368+
did.is_local()
369+
&& if let Some(did_parent) = tcx.opt_parent(did) {
370+
did_parent == impl_parent
371+
|| Some(did_parent) == impl_parent_parent
372+
|| !did_parent.is_crate_root()
373+
&& tcx.def_kind(did_parent) == DefKind::Mod
374+
&& did_has_local_parent(did_parent, tcx, impl_parent, impl_parent_parent)
375+
} else {
376+
false
377+
}
372378
}

Diff for: tests/ui/lint/non-local-defs/module_as_local.rs

+38
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)