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](https://github.com/rust-lang/rust/issues/124396#issuecomment-2079553642) 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](https://github.com/rust-lang/rust/issues/124396#issuecomment-2079692820) will need to be removed before this can be merged. Fixes *(nearly, needs backport)* https://github.com/rust-lang/rust/issues/124396
This commit is contained in:
commit
378a43a065
2 changed files with 49 additions and 5 deletions
|
@ -357,7 +357,7 @@ fn path_has_local_parent(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given a def id and a parent impl def id, this checks if the parent
|
/// Given a def id and a parent impl def id, this checks if the parent
|
||||||
/// def id correspond to the def id of the parent impl definition.
|
/// def id (modulo modules) correspond to the def id of the parent impl definition.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn did_has_local_parent(
|
fn did_has_local_parent(
|
||||||
did: DefId,
|
did: DefId,
|
||||||
|
@ -365,8 +365,14 @@ fn did_has_local_parent(
|
||||||
impl_parent: DefId,
|
impl_parent: DefId,
|
||||||
impl_parent_parent: Option<DefId>,
|
impl_parent_parent: Option<DefId>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
did.is_local() && {
|
did.is_local()
|
||||||
let res_parent = tcx.parent(did);
|
&& if let Some(did_parent) = tcx.opt_parent(did) {
|
||||||
res_parent == impl_parent || Some(res_parent) == impl_parent_parent
|
did_parent == impl_parent
|
||||||
}
|
|| Some(did_parent) == impl_parent_parent
|
||||||
|
|| !did_parent.is_crate_root()
|
||||||
|
&& tcx.def_kind(did_parent) == DefKind::Mod
|
||||||
|
&& did_has_local_parent(did_parent, tcx, impl_parent, impl_parent_parent)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
38
tests/ui/lint/non-local-defs/module_as_local.rs
Normal file
38
tests/ui/lint/non-local-defs/module_as_local.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
//! This test checks that module are treated as if they were local
|
||||||
|
//!
|
||||||
|
//! https://github.com/rust-lang/rust/issues/124396
|
||||||
|
|
||||||
|
//@ check-pass
|
||||||
|
|
||||||
|
trait JoinTo {}
|
||||||
|
|
||||||
|
fn simple_one() {
|
||||||
|
mod posts {
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
pub struct table {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl JoinTo for posts::table {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn simple_two() {
|
||||||
|
mod posts {
|
||||||
|
pub mod posts {
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
pub struct table {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl JoinTo for posts::posts::table {}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Global;
|
||||||
|
fn trait_() {
|
||||||
|
mod posts {
|
||||||
|
pub trait AdjecentTo {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl posts::AdjecentTo for Global {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
Loading…
Add table
Add a link
Reference in a new issue