about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-10-14 06:04:30 +0200
committerGitHub <noreply@github.com>2024-10-14 06:04:30 +0200
commitd34b9324c0557abbcd52257fdc544e8a8ff77d4d (patch)
tree30c1c26d8b1cc5f70f8bead17ee8328e68ca5393
parent5d63a3db9c7c3a2543fbeb3fe8f30d0173acdcf8 (diff)
parentb5e91a00c89a065f618693873ad7074a13ad6b90 (diff)
downloadrust-d34b9324c0557abbcd52257fdc544e8a8ff77d4d.tar.gz
rust-d34b9324c0557abbcd52257fdc544e8a8ff77d4d.zip
Rollup merge of #131660 - Urgau:non_local_def-131643, r=jieyouxu
Also use outermost const-anon for impl items in `non_local_defs` lint

This PR update the logic for the impl paths (items) in the `non_local_definitions` lint to also consider const-anon in case the impl definition is wrapped inside const-anon it-self wrapped into a const-anon where the items are.

r? `@jieyouxu` *(since you interacted on the issue)*
Fixes *(after beta-backport)* #131643
-rw-r--r--compiler/rustc_lint/src/non_local_def.rs10
-rw-r--r--tests/ui/lint/non-local-defs/convoluted-locals-131474-with-mods.rs11
-rw-r--r--tests/ui/lint/non-local-defs/convoluted-locals-131474.rs9
3 files changed, 27 insertions, 3 deletions
diff --git a/compiler/rustc_lint/src/non_local_def.rs b/compiler/rustc_lint/src/non_local_def.rs
index 6fecddb3319..3c31b879bd6 100644
--- a/compiler/rustc_lint/src/non_local_def.rs
+++ b/compiler/rustc_lint/src/non_local_def.rs
@@ -301,9 +301,13 @@ fn did_has_local_parent(
         return false;
     };
 
-    peel_parent_while(tcx, parent_did, |tcx, did| tcx.def_kind(did) == DefKind::Mod)
-        .map(|parent_did| parent_did == impl_parent || Some(parent_did) == outermost_impl_parent)
-        .unwrap_or(false)
+    peel_parent_while(tcx, parent_did, |tcx, did| {
+        tcx.def_kind(did) == DefKind::Mod
+            || (tcx.def_kind(did) == DefKind::Const
+                && tcx.opt_item_name(did) == Some(kw::Underscore))
+    })
+    .map(|parent_did| parent_did == impl_parent || Some(parent_did) == outermost_impl_parent)
+    .unwrap_or(false)
 }
 
 /// Given a `DefId` checks if it satisfies `f` if it does check with it's parent and continue
diff --git a/tests/ui/lint/non-local-defs/convoluted-locals-131474-with-mods.rs b/tests/ui/lint/non-local-defs/convoluted-locals-131474-with-mods.rs
index cef0f0205e8..72fd056d461 100644
--- a/tests/ui/lint/non-local-defs/convoluted-locals-131474-with-mods.rs
+++ b/tests/ui/lint/non-local-defs/convoluted-locals-131474-with-mods.rs
@@ -31,4 +31,15 @@ const _: () = {
     };
 };
 
+// https://github.com/rust-lang/rust/issues/131643
+const _: () = {
+    const _: () = {
+        impl tmp::InnerTest {}
+    };
+
+    mod tmp {
+        pub(super) struct InnerTest;
+    }
+};
+
 fn main() {}
diff --git a/tests/ui/lint/non-local-defs/convoluted-locals-131474.rs b/tests/ui/lint/non-local-defs/convoluted-locals-131474.rs
index 4881723f13b..8e738544a71 100644
--- a/tests/ui/lint/non-local-defs/convoluted-locals-131474.rs
+++ b/tests/ui/lint/non-local-defs/convoluted-locals-131474.rs
@@ -21,4 +21,13 @@ const _: () = {
     };
 };
 
+// https://github.com/rust-lang/rust/issues/131643
+const _: () = {
+    const _: () = {
+        impl InnerTest {}
+    };
+
+    struct InnerTest;
+};
+
 fn main() {}