From 301ce3610f5a40b33162e38b30e0b7ee47c805c1 Mon Sep 17 00:00:00 2001 From: Urgau Date: Mon, 26 Feb 2024 07:59:44 +0100 Subject: [PATCH 1/4] Limit non local defs lint logic to only impls and macros --- compiler/rustc_lint/src/non_local_def.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_lint/src/non_local_def.rs b/compiler/rustc_lint/src/non_local_def.rs index 6cb6fd1cbd550..9ba04ca9ea434 100644 --- a/compiler/rustc_lint/src/non_local_def.rs +++ b/compiler/rustc_lint/src/non_local_def.rs @@ -67,6 +67,10 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { return; } + if !matches!(item.kind, ItemKind::Impl(_) | ItemKind::Macro(_, _)) { + return; + } + let parent = cx.tcx.parent(item.owner_id.def_id.into()); let parent_def_kind = cx.tcx.def_kind(parent); let parent_opt_item_name = cx.tcx.opt_item_name(parent); From 2b512024c031142a9a8765a15f6fc5171c3c6690 Mon Sep 17 00:00:00 2001 From: Urgau Date: Mon, 26 Feb 2024 20:07:21 +0100 Subject: [PATCH 2/4] Use nearly exclusively HIR and avoid at all cost unnecessary query calls --- compiler/rustc_lint/src/non_local_def.rs | 102 ++++++++++++------ tests/ui/lint/non_local_definitions.stderr | 100 ++++++++--------- tests/ui/proc-macro/nested-macro-rules.stderr | 2 +- 3 files changed, 119 insertions(+), 85 deletions(-) diff --git a/compiler/rustc_lint/src/non_local_def.rs b/compiler/rustc_lint/src/non_local_def.rs index 9ba04ca9ea434..974a9bd8ca8f6 100644 --- a/compiler/rustc_lint/src/non_local_def.rs +++ b/compiler/rustc_lint/src/non_local_def.rs @@ -1,8 +1,6 @@ -use rustc_hir::{def::DefKind, Body, Item, ItemKind, Node, Path, QPath, TyKind}; +use rustc_hir::{Body, Item, ItemKind, OwnerNode, Path, QPath, TyKind}; use rustc_span::def_id::{DefId, LOCAL_CRATE}; -use rustc_span::{sym, symbol::kw, ExpnKind, MacroKind}; - -use smallvec::{smallvec, SmallVec}; +use rustc_span::{sym, symbol::kw, symbol::Ident, ExpnKind, MacroKind}; use crate::lints::{NonLocalDefinitionsCargoUpdateNote, NonLocalDefinitionsDiag}; use crate::{LateContext, LateLintPass, LintContext}; @@ -71,15 +69,20 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { return; } - let parent = cx.tcx.parent(item.owner_id.def_id.into()); - let parent_def_kind = cx.tcx.def_kind(parent); - let parent_opt_item_name = cx.tcx.opt_item_name(parent); + let Some((_, parent_node)) = cx.tcx.hir().parent_owner_iter(item.hir_id()).next() else { + return; + }; + let parent_is_anon_const = matches!( + parent_node, + OwnerNode::Item(Item { + ident: Ident { name: kw::Underscore, .. }, + kind: ItemKind::Const(..), + .. + }) + ); // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module. - if self.body_depth == 1 - && parent_def_kind == DefKind::Const - && parent_opt_item_name == Some(kw::Underscore) - { + if self.body_depth == 1 && parent_is_anon_const { return; } @@ -119,23 +122,34 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { // We also ignore anon-const in item by including the anon-const // parent as well; and since it's quite uncommon, we use smallvec // to avoid unnecessary heap allocations. - let local_parents: SmallVec<[DefId; 1]> = if parent_def_kind == DefKind::Const - && parent_opt_item_name == Some(kw::Underscore) - { - smallvec![parent, cx.tcx.parent(parent)] - } else { - smallvec![parent] + let mut local_parent = { + let mut local_parent_cache = None; + move || { + *local_parent_cache + .get_or_insert_with(|| cx.tcx.parent(item.owner_id.to_def_id())) + } + }; + let mut extra_local_parent = { + let mut extra_parent_cache = None; + move |did| { + *extra_parent_cache + .get_or_insert_with(|| parent_is_anon_const.then(|| cx.tcx.parent(did))) + } }; let self_ty_has_local_parent = match impl_.self_ty.kind { - TyKind::Path(QPath::Resolved(_, ty_path)) => { - path_has_local_parent(ty_path, cx, &*local_parents) - } + TyKind::Path(QPath::Resolved(_, ty_path)) => path_has_local_parent( + ty_path, + cx, + &mut local_parent, + &mut extra_local_parent, + ), TyKind::TraitObject([principle_poly_trait_ref, ..], _, _) => { path_has_local_parent( principle_poly_trait_ref.trait_ref.path, cx, - &*local_parents, + &mut local_parent, + &mut extra_local_parent, ) } TyKind::TraitObject([], _, _) @@ -157,17 +171,21 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { let of_trait_has_local_parent = impl_ .of_trait - .map(|of_trait| path_has_local_parent(of_trait.path, cx, &*local_parents)) + .map(|of_trait| { + path_has_local_parent( + of_trait.path, + cx, + &mut local_parent, + &mut extra_local_parent, + ) + }) .unwrap_or(false); // If none of them have a local parent (LOGICAL NOR) this means that // this impl definition is a non-local definition and so we lint on it. if !(self_ty_has_local_parent || of_trait_has_local_parent) { let const_anon = if self.body_depth == 1 - && parent_def_kind == DefKind::Const - && parent_opt_item_name != Some(kw::Underscore) - && let Some(parent) = parent.as_local() - && let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent) + && let OwnerNode::Item(item) = parent_node && let ItemKind::Const(ty, _, _) = item.kind && let TyKind::Tup(&[]) = ty.kind { @@ -181,9 +199,10 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { item.span, NonLocalDefinitionsDiag::Impl { depth: self.body_depth, - body_kind_descr: cx.tcx.def_kind_descr(parent_def_kind, parent), - body_name: parent_opt_item_name - .map(|s| s.to_ident_string()) + body_kind_descr: "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */, + body_name: parent_node + .ident() + .map(|s| s.name.to_ident_string()) .unwrap_or_else(|| "".to_string()), cargo_update: cargo_update(), const_anon, @@ -199,9 +218,10 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { item.span, NonLocalDefinitionsDiag::MacroRules { depth: self.body_depth, - body_kind_descr: cx.tcx.def_kind_descr(parent_def_kind, parent), - body_name: parent_opt_item_name - .map(|s| s.to_ident_string()) + body_kind_descr: "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */, + body_name: parent_node + .ident() + .map(|s| s.name.to_ident_string()) .unwrap_or_else(|| "".to_string()), cargo_update: cargo_update(), }, @@ -221,6 +241,20 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { /// std::convert::PartialEq> /// ^^^^^^^^^^^^^^^^^^^^^^^ /// ``` -fn path_has_local_parent(path: &Path<'_>, cx: &LateContext<'_>, local_parents: &[DefId]) -> bool { - path.res.opt_def_id().is_some_and(|did| local_parents.contains(&cx.tcx.parent(did))) +fn path_has_local_parent( + path: &Path<'_>, + cx: &LateContext<'_>, + local_parent: &mut impl FnMut() -> DefId, + extra_local_parent: &mut impl FnMut(DefId) -> Option, +) -> bool { + if let Some(did) = path.res.opt_def_id() { + if !did.is_local() { + false + } else { + let res_parent = cx.tcx.parent(did); + res_parent == local_parent() || Some(res_parent) == extra_local_parent(local_parent()) + } + } else { + true + } } diff --git a/tests/ui/lint/non_local_definitions.stderr b/tests/ui/lint/non_local_definitions.stderr index f9f29ec63a805..f140b0133adf1 100644 --- a/tests/ui/lint/non_local_definitions.stderr +++ b/tests/ui/lint/non_local_definitions.stderr @@ -7,7 +7,7 @@ LL | const Z: () = { LL | impl Uto for &Test {} | ^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current constant `Z` + = help: move this `impl` block outside the of the current ? `Z` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -19,7 +19,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto for *mut Test {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current constant expression `` + = help: move this `impl` block outside the of the current ? `A` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -30,7 +30,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto for Test {} | ^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current constant expression `` + = help: move this `impl` block outside the of the current ? `Enum` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -41,7 +41,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto2 for Test {} | ^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current static `A` + = help: move this `impl` block outside the of the current ? `A` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -52,7 +52,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto3 for Test {} | ^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current constant `B` + = help: move this `impl` block outside the of the current ? `B` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -63,7 +63,7 @@ warning: non-local `macro_rules!` definition, they should be avoided as they go LL | macro_rules! m0 { () => { } }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current constant `B` + = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current ? `B` = note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -74,7 +74,7 @@ warning: non-local `macro_rules!` definition, they should be avoided as they go LL | macro_rules! m { () => { } }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current function `main` + = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current ? `main` = note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -88,7 +88,7 @@ LL | | fn foo() {} LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -102,7 +102,7 @@ LL | | fn bar() {} LL | | } | |_________^ | - = help: move this `impl` block outside the of the current constant expression `` and up 2 bodies + = help: move this `impl` block outside the of the current ? `main` and up 2 bodies = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -116,7 +116,7 @@ LL | | fn hoo() {} LL | | } | |_________^ | - = help: move this `impl` block outside the of the current inline constant `` and up 2 bodies + = help: move this `impl` block outside the of the current ? `main` and up 2 bodies = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -130,7 +130,7 @@ LL | | fn foo2() {} LL | | } | |_________^ | - = help: move this `impl` block outside the of the current constant `_` and up 2 bodies + = help: move this `impl` block outside the of the current ? `_` and up 2 bodies = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -146,7 +146,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -157,7 +157,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl dyn Uto5 {} | ^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -168,7 +168,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for Vec { } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -179,7 +179,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for &dyn Uto5 {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -190,7 +190,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for *mut Test {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -201,7 +201,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for *mut [Test] {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -212,7 +212,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for [Test; 8] {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -223,7 +223,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for (Test,) {} | ^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -234,7 +234,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for fn(Test) -> () {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -245,7 +245,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for fn() -> Test {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -256,7 +256,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for Test {} | ^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current closure `` and up 2 bodies + = help: move this `impl` block outside the of the current ? `main` and up 2 bodies = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -267,7 +267,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for &Test {} | ^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current constant expression `` and up 2 bodies + = help: move this `impl` block outside the of the current ? `A` and up 2 bodies = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -278,7 +278,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for &(Test,) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current constant expression `` and up 2 bodies + = help: move this `impl` block outside the of the current ? `a` and up 2 bodies = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -289,7 +289,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for &(Test,Test) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current constant expression `` and up 2 bodies + = help: move this `impl` block outside the of the current ? `b` and up 2 bodies = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -300,7 +300,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for *mut InsideMain {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -311,7 +311,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for *mut [InsideMain] {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -322,7 +322,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for [InsideMain; 8] {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -333,7 +333,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for (InsideMain,) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -344,7 +344,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for fn(InsideMain) -> () {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -355,7 +355,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto5 for fn() -> InsideMain {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -371,7 +371,7 @@ LL | | } LL | | } | |_________^ | - = help: move this `impl` block outside the of the current function `inside_inside` and up 2 bodies + = help: move this `impl` block outside the of the current ? `inside_inside` and up 2 bodies = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -388,7 +388,7 @@ LL | | } LL | | } | |_________^ | - = help: move this `impl` block outside the of the current function `inside_inside` and up 2 bodies + = help: move this `impl` block outside the of the current ? `inside_inside` and up 2 bodies = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -399,7 +399,7 @@ warning: non-local `macro_rules!` definition, they should be avoided as they go LL | macro_rules! m2 { () => { } }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current associated function `bar` and up 3 bodies + = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current ? `bar` and up 3 bodies = note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -410,7 +410,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto3 for Vec { } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `main` + = help: move this `impl` block outside the of the current ? `main` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -421,7 +421,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto7 for Test where Local: std::any::Any {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `bad` + = help: move this `impl` block outside the of the current ? `bad` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -432,7 +432,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | impl Uto8 for T {} | ^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current function `bad` + = help: move this `impl` block outside the of the current ? `bad` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -448,7 +448,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `fun` + = help: move this `impl` block outside the of the current ? `fun` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -464,7 +464,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `meow` + = help: move this `impl` block outside the of the current ? `meow` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -478,7 +478,7 @@ LL | | fn as_ref(&self) -> &Cat { &Cat } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `meow` + = help: move this `impl` block outside the of the current ? `meow` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -494,7 +494,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `fun2` + = help: move this `impl` block outside the of the current ? `fun2` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -510,7 +510,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `woof` + = help: move this `impl` block outside the of the current ? `woof` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -526,7 +526,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `woof` + = help: move this `impl` block outside the of the current ? `woof` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -542,7 +542,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `woof` + = help: move this `impl` block outside the of the current ? `woof` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -558,7 +558,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `woof` + = help: move this `impl` block outside the of the current ? `woof` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -574,7 +574,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `rawr` + = help: move this `impl` block outside the of the current ? `rawr` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -590,7 +590,7 @@ LL | | } LL | | } | |_____^ | - = help: move this `impl` block outside the of the current function `rawr` + = help: move this `impl` block outside the of the current ? `rawr` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -604,7 +604,7 @@ LL | impl MacroTrait for OutsideStruct {} LL | m!(); | ---- in this macro invocation | - = help: move this `impl` block outside the of the current function `my_func` + = help: move this `impl` block outside the of the current ? `my_func` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -616,7 +616,7 @@ warning: non-local `impl` definition, they should be avoided as they go against LL | non_local_macro::non_local_impl!(CargoUpdate); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: move this `impl` block outside the of the current constant `_IMPL_DEBUG` + = help: move this `impl` block outside the of the current ? `_IMPL_DEBUG` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -629,7 +629,7 @@ warning: non-local `macro_rules!` definition, they should be avoided as they go LL | non_local_macro::non_local_macro_rules!(my_macro); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current constant `_MACRO_EXPORT` + = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current ? `_MACRO_EXPORT` = note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue diff --git a/tests/ui/proc-macro/nested-macro-rules.stderr b/tests/ui/proc-macro/nested-macro-rules.stderr index 111be8827714f..bc80e3f995f78 100644 --- a/tests/ui/proc-macro/nested-macro-rules.stderr +++ b/tests/ui/proc-macro/nested-macro-rules.stderr @@ -17,7 +17,7 @@ LL | | } LL | nested_macro_rules::outer_macro!(SecondStruct, SecondAttrStruct); | ---------------------------------------------------------------- in this macro invocation | - = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current function `main` + = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current ? `main` = note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue From 0903c02c19b67f7d267fd79ea334b18f7862d1da Mon Sep 17 00:00:00 2001 From: Urgau Date: Mon, 26 Feb 2024 21:11:58 +0100 Subject: [PATCH 3/4] Even more laziness and caching --- compiler/rustc_lint/src/non_local_def.rs | 60 +++++++++++++----------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_lint/src/non_local_def.rs b/compiler/rustc_lint/src/non_local_def.rs index 974a9bd8ca8f6..0c15c78c95cdf 100644 --- a/compiler/rustc_lint/src/non_local_def.rs +++ b/compiler/rustc_lint/src/non_local_def.rs @@ -65,26 +65,14 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { return; } - if !matches!(item.kind, ItemKind::Impl(_) | ItemKind::Macro(_, _)) { - return; - } - - let Some((_, parent_node)) = cx.tcx.hir().parent_owner_iter(item.hir_id()).next() else { - return; + let mut parent_node = { + let mut parent_node_cache = None; + move || { + *parent_node_cache.get_or_insert_with(|| { + cx.tcx.hir().parent_owner_iter(item.hir_id()).next().unwrap().1 + }) + } }; - let parent_is_anon_const = matches!( - parent_node, - OwnerNode::Item(Item { - ident: Ident { name: kw::Underscore, .. }, - kind: ItemKind::Const(..), - .. - }) - ); - - // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module. - if self.body_depth == 1 && parent_is_anon_const { - return; - } let cargo_update = || { let oexpn = item.span.ctxt().outer_expn_data(); @@ -119,9 +107,21 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { // If that's the case this means that this impl block declaration // is using local items and so we don't lint on it. - // We also ignore anon-const in item by including the anon-const - // parent as well; and since it's quite uncommon, we use smallvec - // to avoid unnecessary heap allocations. + let mut parent_node_is_anon_const = { + let mut parent_node_is_anon_const = None; + move || { + *parent_node_is_anon_const.get_or_insert_with(|| { + matches!( + parent_node(), + OwnerNode::Item(Item { + ident: Ident { name: kw::Underscore, .. }, + kind: ItemKind::Const(..), + .. + }) + ) + }) + } + }; let mut local_parent = { let mut local_parent_cache = None; move || { @@ -132,8 +132,9 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { let mut extra_local_parent = { let mut extra_parent_cache = None; move |did| { - *extra_parent_cache - .get_or_insert_with(|| parent_is_anon_const.then(|| cx.tcx.parent(did))) + *extra_parent_cache.get_or_insert_with(|| { + parent_node_is_anon_const().then(|| cx.tcx.parent(did)) + }) } }; @@ -184,8 +185,13 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { // If none of them have a local parent (LOGICAL NOR) this means that // this impl definition is a non-local definition and so we lint on it. if !(self_ty_has_local_parent || of_trait_has_local_parent) { + // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module. + if parent_node_is_anon_const() && self.body_depth == 1 { + return; + } + let const_anon = if self.body_depth == 1 - && let OwnerNode::Item(item) = parent_node + && let OwnerNode::Item(item) = parent_node() && let ItemKind::Const(ty, _, _) = item.kind && let TyKind::Tup(&[]) = ty.kind { @@ -200,7 +206,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { NonLocalDefinitionsDiag::Impl { depth: self.body_depth, body_kind_descr: "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */, - body_name: parent_node + body_name: parent_node() .ident() .map(|s| s.name.to_ident_string()) .unwrap_or_else(|| "".to_string()), @@ -219,7 +225,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { NonLocalDefinitionsDiag::MacroRules { depth: self.body_depth, body_kind_descr: "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */, - body_name: parent_node + body_name: parent_node() .ident() .map(|s| s.name.to_ident_string()) .unwrap_or_else(|| "".to_string()), From c84776f56636f6da83618dd63c8ad3ce3900bca8 Mon Sep 17 00:00:00 2001 From: Urgau Date: Tue, 27 Feb 2024 18:42:47 +0100 Subject: [PATCH 4/4] Remove the impl logic, only keep the macro_rules one --- compiler/rustc_lint/src/lints.rs | 1 + compiler/rustc_lint/src/non_local_def.rs | 250 ++++---- tests/ui/lint/non_local_definitions.rs | 385 ------------- tests/ui/lint/non_local_definitions.stderr | 640 --------------------- 4 files changed, 127 insertions(+), 1149 deletions(-) delete mode 100644 tests/ui/lint/non_local_definitions.rs delete mode 100644 tests/ui/lint/non_local_definitions.stderr diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index d8a0b75f8bd18..b2536f973cdce 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1,5 +1,6 @@ #![allow(rustc::diagnostic_outside_of_impl)] #![allow(rustc::untranslatable_diagnostic)] +#![allow(dead_code)] use std::num::NonZero; use crate::errors::RequestedLevel; diff --git a/compiler/rustc_lint/src/non_local_def.rs b/compiler/rustc_lint/src/non_local_def.rs index 0c15c78c95cdf..9280630997ba6 100644 --- a/compiler/rustc_lint/src/non_local_def.rs +++ b/compiler/rustc_lint/src/non_local_def.rs @@ -1,3 +1,5 @@ +#![allow(warnings)] + use rustc_hir::{Body, Item, ItemKind, OwnerNode, Path, QPath, TyKind}; use rustc_span::def_id::{DefId, LOCAL_CRATE}; use rustc_span::{sym, symbol::kw, symbol::Ident, ExpnKind, MacroKind}; @@ -92,130 +94,130 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { }; match item.kind { - ItemKind::Impl(impl_) => { - // The RFC states: - // - // > An item nested inside an expression-containing item (through any - // > level of nesting) may not define an impl Trait for Type unless - // > either the **Trait** or the **Type** is also nested inside the - // > same expression-containing item. - // - // To achieve this we get try to get the paths of the _Trait_ and - // _Type_, and we look inside thoses paths to try a find in one - // of them a type whose parent is the same as the impl definition. - // - // If that's the case this means that this impl block declaration - // is using local items and so we don't lint on it. - - let mut parent_node_is_anon_const = { - let mut parent_node_is_anon_const = None; - move || { - *parent_node_is_anon_const.get_or_insert_with(|| { - matches!( - parent_node(), - OwnerNode::Item(Item { - ident: Ident { name: kw::Underscore, .. }, - kind: ItemKind::Const(..), - .. - }) - ) - }) - } - }; - let mut local_parent = { - let mut local_parent_cache = None; - move || { - *local_parent_cache - .get_or_insert_with(|| cx.tcx.parent(item.owner_id.to_def_id())) - } - }; - let mut extra_local_parent = { - let mut extra_parent_cache = None; - move |did| { - *extra_parent_cache.get_or_insert_with(|| { - parent_node_is_anon_const().then(|| cx.tcx.parent(did)) - }) - } - }; - - let self_ty_has_local_parent = match impl_.self_ty.kind { - TyKind::Path(QPath::Resolved(_, ty_path)) => path_has_local_parent( - ty_path, - cx, - &mut local_parent, - &mut extra_local_parent, - ), - TyKind::TraitObject([principle_poly_trait_ref, ..], _, _) => { - path_has_local_parent( - principle_poly_trait_ref.trait_ref.path, - cx, - &mut local_parent, - &mut extra_local_parent, - ) - } - TyKind::TraitObject([], _, _) - | TyKind::InferDelegation(_, _) - | TyKind::Slice(_) - | TyKind::Array(_, _) - | TyKind::Ptr(_) - | TyKind::Ref(_, _) - | TyKind::BareFn(_) - | TyKind::Never - | TyKind::Tup(_) - | TyKind::Path(_) - | TyKind::AnonAdt(_) - | TyKind::OpaqueDef(_, _, _) - | TyKind::Typeof(_) - | TyKind::Infer - | TyKind::Err(_) => false, - }; - - let of_trait_has_local_parent = impl_ - .of_trait - .map(|of_trait| { - path_has_local_parent( - of_trait.path, - cx, - &mut local_parent, - &mut extra_local_parent, - ) - }) - .unwrap_or(false); - - // If none of them have a local parent (LOGICAL NOR) this means that - // this impl definition is a non-local definition and so we lint on it. - if !(self_ty_has_local_parent || of_trait_has_local_parent) { - // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module. - if parent_node_is_anon_const() && self.body_depth == 1 { - return; - } - - let const_anon = if self.body_depth == 1 - && let OwnerNode::Item(item) = parent_node() - && let ItemKind::Const(ty, _, _) = item.kind - && let TyKind::Tup(&[]) = ty.kind - { - Some(item.ident.span) - } else { - None - }; - - cx.emit_span_lint( - NON_LOCAL_DEFINITIONS, - item.span, - NonLocalDefinitionsDiag::Impl { - depth: self.body_depth, - body_kind_descr: "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */, - body_name: parent_node() - .ident() - .map(|s| s.name.to_ident_string()) - .unwrap_or_else(|| "".to_string()), - cargo_update: cargo_update(), - const_anon, - }, - ) - } - } + // ItemKind::Impl(impl_) => { + // // The RFC states: + // // + // // > An item nested inside an expression-containing item (through any + // // > level of nesting) may not define an impl Trait for Type unless + // // > either the **Trait** or the **Type** is also nested inside the + // // > same expression-containing item. + // // + // // To achieve this we get try to get the paths of the _Trait_ and + // // _Type_, and we look inside thoses paths to try a find in one + // // of them a type whose parent is the same as the impl definition. + // // + // // If that's the case this means that this impl block declaration + // // is using local items and so we don't lint on it. + // + // let mut parent_node_is_anon_const = { + // let mut parent_node_is_anon_const = None; + // move || { + // *parent_node_is_anon_const.get_or_insert_with(|| { + // matches!( + // parent_node(), + // OwnerNode::Item(Item { + // ident: Ident { name: kw::Underscore, .. }, + // kind: ItemKind::Const(..), + // .. + // }) + // ) + // }) + // } + // }; + // let mut local_parent = { + // let mut local_parent_cache = None; + // move || { + // *local_parent_cache + // .get_or_insert_with(|| cx.tcx.parent(item.owner_id.to_def_id())) + // } + // }; + // let mut extra_local_parent = { + // let mut extra_parent_cache = None; + // move |did| { + // *extra_parent_cache.get_or_insert_with(|| { + // parent_node_is_anon_const().then(|| cx.tcx.parent(did)) + // }) + // } + // }; + // + // let self_ty_has_local_parent = match impl_.self_ty.kind { + // TyKind::Path(QPath::Resolved(_, ty_path)) => path_has_local_parent( + // ty_path, + // cx, + // &mut local_parent, + // &mut extra_local_parent, + // ), + // TyKind::TraitObject([principle_poly_trait_ref, ..], _, _) => { + // path_has_local_parent( + // principle_poly_trait_ref.trait_ref.path, + // cx, + // &mut local_parent, + // &mut extra_local_parent, + // ) + // } + // TyKind::TraitObject([], _, _) + // | TyKind::InferDelegation(_, _) + // | TyKind::Slice(_) + // | TyKind::Array(_, _) + // | TyKind::Ptr(_) + // | TyKind::Ref(_, _) + // | TyKind::BareFn(_) + // | TyKind::Never + // | TyKind::Tup(_) + // | TyKind::Path(_) + // | TyKind::AnonAdt(_) + // | TyKind::OpaqueDef(_, _, _) + // | TyKind::Typeof(_) + // | TyKind::Infer + // | TyKind::Err(_) => false, + // }; + // + // let of_trait_has_local_parent = impl_ + // .of_trait + // .map(|of_trait| { + // path_has_local_parent( + // of_trait.path, + // cx, + // &mut local_parent, + // &mut extra_local_parent, + // ) + // }) + // .unwrap_or(false); + // + // // If none of them have a local parent (LOGICAL NOR) this means that + // // this impl definition is a non-local definition and so we lint on it. + // if !(self_ty_has_local_parent || of_trait_has_local_parent) { + // // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module. + // if parent_node_is_anon_const() && self.body_depth == 1 { + // return; + // } + // + // let const_anon = if self.body_depth == 1 + // && let OwnerNode::Item(item) = parent_node() + // && let ItemKind::Const(ty, _, _) = item.kind + // && let TyKind::Tup(&[]) = ty.kind + // { + // Some(item.ident.span) + // } else { + // None + // }; + // + // cx.emit_span_lint( + // NON_LOCAL_DEFINITIONS, + // item.span, + // NonLocalDefinitionsDiag::Impl { + // depth: self.body_depth, + // body_kind_descr: "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */, + // body_name: parent_node() + // .ident() + // .map(|s| s.name.to_ident_string()) + // .unwrap_or_else(|| "".to_string()), + // cargo_update: cargo_update(), + // const_anon, + // }, + // ) + // } + // } ItemKind::Macro(_macro, MacroKind::Bang) if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) => { diff --git a/tests/ui/lint/non_local_definitions.rs b/tests/ui/lint/non_local_definitions.rs deleted file mode 100644 index c9aaa04934631..0000000000000 --- a/tests/ui/lint/non_local_definitions.rs +++ /dev/null @@ -1,385 +0,0 @@ -//@ check-pass -//@ edition:2021 -//@ aux-build:non_local_macro.rs -//@ rustc-env:CARGO=/usr/bin/cargo - -#![feature(inline_const)] - -extern crate non_local_macro; - -use std::fmt::{Debug, Display}; - -struct Test; - -impl Debug for Test { - fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - todo!() - } -} - -mod do_not_lint_mod { - pub trait Tait {} - - impl super::Test { - fn hugo() {} - } - - impl Tait for super::Test {} -} - -trait Uto {} -const Z: () = { - trait Uto1 {} - - impl Uto1 for Test {} // the trait is local, don't lint - - impl Uto for &Test {} - //~^ WARN non-local `impl` definition -}; - -trait Ano {} -const _: () = { - impl Ano for &Test {} // ignored since the parent is an anon-const -}; - -type A = [u32; { - impl Uto for *mut Test {} - //~^ WARN non-local `impl` definition - - 1 -}]; - -enum Enum { - Discr = { - impl Uto for Test {} - //~^ WARN non-local `impl` definition - - 1 - } -} - -trait Uto2 {} -static A: u32 = { - impl Uto2 for Test {} - //~^ WARN non-local `impl` definition - - 1 -}; - -trait Uto3 {} -const B: u32 = { - impl Uto3 for Test {} - //~^ WARN non-local `impl` definition - - #[macro_export] - macro_rules! m0 { () => { } }; - //~^ WARN non-local `macro_rules!` definition - - trait Uto4 {} - impl Uto4 for Test {} - - 1 -}; - -trait Uto5 {} -fn main() { - #[macro_export] - macro_rules! m { () => { } }; - //~^ WARN non-local `macro_rules!` definition - - impl Test { - //~^ WARN non-local `impl` definition - fn foo() {} - } - - let _array = [0i32; { - impl Test { - //~^ WARN non-local `impl` definition - fn bar() {} - } - - 1 - }]; - - const { - impl Test { - //~^ WARN non-local `impl` definition - fn hoo() {} - } - - 1 - }; - - const _: u32 = { - impl Test { - //~^ WARN non-local `impl` definition - fn foo2() {} - } - - 1 - }; - - impl Display for Test { - //~^ WARN non-local `impl` definition - fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - todo!() - } - } - - impl dyn Uto5 {} - //~^ WARN non-local `impl` definition - - impl Uto5 for Vec { } - //~^ WARN non-local `impl` definition - - impl Uto5 for &dyn Uto5 {} - //~^ WARN non-local `impl` definition - - impl Uto5 for *mut Test {} - //~^ WARN non-local `impl` definition - - impl Uto5 for *mut [Test] {} - //~^ WARN non-local `impl` definition - - impl Uto5 for [Test; 8] {} - //~^ WARN non-local `impl` definition - - impl Uto5 for (Test,) {} - //~^ WARN non-local `impl` definition - - impl Uto5 for fn(Test) -> () {} - //~^ WARN non-local `impl` definition - - impl Uto5 for fn() -> Test {} - //~^ WARN non-local `impl` definition - - let _a = || { - impl Uto5 for Test {} - //~^ WARN non-local `impl` definition - - 1 - }; - - type A = [u32; { - impl Uto5 for &Test {} - //~^ WARN non-local `impl` definition - - 1 - }]; - - fn a(_: [u32; { - impl Uto5 for &(Test,) {} - //~^ WARN non-local `impl` definition - - 1 - }]) {} - - fn b() -> [u32; { - impl Uto5 for &(Test,Test) {} - //~^ WARN non-local `impl` definition - - 1 - }] { todo!() } - - struct InsideMain; - - impl Uto5 for *mut InsideMain {} - //~^ WARN non-local `impl` definition - impl Uto5 for *mut [InsideMain] {} - //~^ WARN non-local `impl` definition - impl Uto5 for [InsideMain; 8] {} - //~^ WARN non-local `impl` definition - impl Uto5 for (InsideMain,) {} - //~^ WARN non-local `impl` definition - impl Uto5 for fn(InsideMain) -> () {} - //~^ WARN non-local `impl` definition - impl Uto5 for fn() -> InsideMain {} - //~^ WARN non-local `impl` definition - - impl Debug for InsideMain { - fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - todo!() - } - } - - impl InsideMain { - fn foo() {} - } - - fn inside_inside() { - impl Display for InsideMain { - //~^ WARN non-local `impl` definition - fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - todo!() - } - } - - impl InsideMain { - //~^ WARN non-local `impl` definition - fn bar() { - #[macro_export] - macro_rules! m2 { () => { } }; - //~^ WARN non-local `macro_rules!` definition - } - } - } - - trait Uto6 {} - impl dyn Uto6 {} - impl Uto5 for dyn Uto6 {} - - impl Uto3 for Vec { } - //~^ WARN non-local `impl` definition -} - -trait Uto7 {} -trait Uto8 {} - -fn bad() { - struct Local; - impl Uto7 for Test where Local: std::any::Any {} - //~^ WARN non-local `impl` definition - - impl Uto8 for T {} - //~^ WARN non-local `impl` definition -} - -struct UwU(T); - -fn fun() { - #[derive(Debug)] - struct OwO; - impl Default for UwU { - //~^ WARN non-local `impl` definition - fn default() -> Self { - UwU(OwO) - } - } -} - -struct Cat; - -fn meow() { - impl From for () { - //~^ WARN non-local `impl` definition - fn from(_: Cat) -> () { - todo!() - } - } - - #[derive(Debug)] - struct Cat; - impl AsRef for () { - //~^ WARN non-local `impl` definition - fn as_ref(&self) -> &Cat { &Cat } - } -} - -struct G; - -fn fun2() { - #[derive(Debug, Default)] - struct B; - impl PartialEq for G { - //~^ WARN non-local `impl` definition - fn eq(&self, _: &B) -> bool { - true - } - } -} - -fn side_effects() { - dbg!(().as_ref()); // prints `Cat` - dbg!(UwU::default().0); - let _ = G::eq(&G, dbg!(&<_>::default())); -} - -struct Dog; - -fn woof() { - impl PartialEq for &Dog { - //~^ WARN non-local `impl` definition - fn eq(&self, _: &Dog) -> bool { - todo!() - } - } - - impl PartialEq<()> for Dog { - //~^ WARN non-local `impl` definition - fn eq(&self, _: &()) -> bool { - todo!() - } - } - - impl PartialEq<()> for &Dog { - //~^ WARN non-local `impl` definition - fn eq(&self, _: &()) -> bool { - todo!() - } - } - - impl PartialEq for () { - //~^ WARN non-local `impl` definition - fn eq(&self, _: &Dog) -> bool { - todo!() - } - } - - struct Test; - impl PartialEq for Test { - fn eq(&self, _: &Dog) -> bool { - todo!() - } - } -} - -struct Wrap(T); - -impl Wrap>> {} - -fn rawr() { - struct Lion; - - impl From>> for () { - //~^ WARN non-local `impl` definition - fn from(_: Wrap>) -> Self { - todo!() - } - } - - impl From<()> for Wrap { - //~^ WARN non-local `impl` definition - fn from(_: ()) -> Self { - todo!() - } - } -} - -macro_rules! m { - () => { - trait MacroTrait {} - struct OutsideStruct; - fn my_func() { - impl MacroTrait for OutsideStruct {} - //~^ WARN non-local `impl` definition - } - } -} - -m!(); - -struct CargoUpdate; - -non_local_macro::non_local_impl!(CargoUpdate); -//~^ WARN non-local `impl` definition - -non_local_macro::non_local_macro_rules!(my_macro); -//~^ WARN non-local `macro_rules!` definition - -fn bitflags() { - struct Flags; - - const _: () = { - impl Flags {} - }; -} diff --git a/tests/ui/lint/non_local_definitions.stderr b/tests/ui/lint/non_local_definitions.stderr deleted file mode 100644 index f140b0133adf1..0000000000000 --- a/tests/ui/lint/non_local_definitions.stderr +++ /dev/null @@ -1,640 +0,0 @@ -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:36:5 - | -LL | const Z: () = { - | - help: use a const-anon item to suppress this lint: `_` -... -LL | impl Uto for &Test {} - | ^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `Z` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - = note: `#[warn(non_local_definitions)]` on by default - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:46:5 - | -LL | impl Uto for *mut Test {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `A` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:54:9 - | -LL | impl Uto for Test {} - | ^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `Enum` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:63:5 - | -LL | impl Uto2 for Test {} - | ^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `A` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:71:5 - | -LL | impl Uto3 for Test {} - | ^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `B` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `macro_rules!` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:75:5 - | -LL | macro_rules! m0 { () => { } }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current ? `B` - = note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `macro_rules!` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:87:5 - | -LL | macro_rules! m { () => { } }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current ? `main` - = note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:90:5 - | -LL | / impl Test { -LL | | -LL | | fn foo() {} -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:96:9 - | -LL | / impl Test { -LL | | -LL | | fn bar() {} -LL | | } - | |_________^ - | - = help: move this `impl` block outside the of the current ? `main` and up 2 bodies - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:105:9 - | -LL | / impl Test { -LL | | -LL | | fn hoo() {} -LL | | } - | |_________^ - | - = help: move this `impl` block outside the of the current ? `main` and up 2 bodies - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:114:9 - | -LL | / impl Test { -LL | | -LL | | fn foo2() {} -LL | | } - | |_________^ - | - = help: move this `impl` block outside the of the current ? `_` and up 2 bodies - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:122:5 - | -LL | / impl Display for Test { -LL | | -LL | | fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -LL | | todo!() -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:129:5 - | -LL | impl dyn Uto5 {} - | ^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:132:5 - | -LL | impl Uto5 for Vec { } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:135:5 - | -LL | impl Uto5 for &dyn Uto5 {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:138:5 - | -LL | impl Uto5 for *mut Test {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:141:5 - | -LL | impl Uto5 for *mut [Test] {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:144:5 - | -LL | impl Uto5 for [Test; 8] {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:147:5 - | -LL | impl Uto5 for (Test,) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:150:5 - | -LL | impl Uto5 for fn(Test) -> () {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:153:5 - | -LL | impl Uto5 for fn() -> Test {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:157:9 - | -LL | impl Uto5 for Test {} - | ^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` and up 2 bodies - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:164:9 - | -LL | impl Uto5 for &Test {} - | ^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `A` and up 2 bodies - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:171:9 - | -LL | impl Uto5 for &(Test,) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `a` and up 2 bodies - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:178:9 - | -LL | impl Uto5 for &(Test,Test) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `b` and up 2 bodies - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:186:5 - | -LL | impl Uto5 for *mut InsideMain {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:188:5 - | -LL | impl Uto5 for *mut [InsideMain] {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:190:5 - | -LL | impl Uto5 for [InsideMain; 8] {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:192:5 - | -LL | impl Uto5 for (InsideMain,) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:194:5 - | -LL | impl Uto5 for fn(InsideMain) -> () {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:196:5 - | -LL | impl Uto5 for fn() -> InsideMain {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:210:9 - | -LL | / impl Display for InsideMain { -LL | | -LL | | fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -LL | | todo!() -LL | | } -LL | | } - | |_________^ - | - = help: move this `impl` block outside the of the current ? `inside_inside` and up 2 bodies - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:217:9 - | -LL | / impl InsideMain { -LL | | -LL | | fn bar() { -LL | | #[macro_export] -... | -LL | | } -LL | | } - | |_________^ - | - = help: move this `impl` block outside the of the current ? `inside_inside` and up 2 bodies - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `macro_rules!` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:221:17 - | -LL | macro_rules! m2 { () => { } }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current ? `bar` and up 3 bodies - = note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:231:5 - | -LL | impl Uto3 for Vec { } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `main` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:240:5 - | -LL | impl Uto7 for Test where Local: std::any::Any {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `bad` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:243:5 - | -LL | impl Uto8 for T {} - | ^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `bad` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:252:5 - | -LL | / impl Default for UwU { -LL | | -LL | | fn default() -> Self { -LL | | UwU(OwO) -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `fun` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:263:5 - | -LL | / impl From for () { -LL | | -LL | | fn from(_: Cat) -> () { -LL | | todo!() -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `meow` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:272:5 - | -LL | / impl AsRef for () { -LL | | -LL | | fn as_ref(&self) -> &Cat { &Cat } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `meow` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:283:5 - | -LL | / impl PartialEq for G { -LL | | -LL | | fn eq(&self, _: &B) -> bool { -LL | | true -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `fun2` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:300:5 - | -LL | / impl PartialEq for &Dog { -LL | | -LL | | fn eq(&self, _: &Dog) -> bool { -LL | | todo!() -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `woof` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:307:5 - | -LL | / impl PartialEq<()> for Dog { -LL | | -LL | | fn eq(&self, _: &()) -> bool { -LL | | todo!() -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `woof` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:314:5 - | -LL | / impl PartialEq<()> for &Dog { -LL | | -LL | | fn eq(&self, _: &()) -> bool { -LL | | todo!() -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `woof` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:321:5 - | -LL | / impl PartialEq for () { -LL | | -LL | | fn eq(&self, _: &Dog) -> bool { -LL | | todo!() -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `woof` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:343:5 - | -LL | / impl From>> for () { -LL | | -LL | | fn from(_: Wrap>) -> Self { -LL | | todo!() -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `rawr` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:350:5 - | -LL | / impl From<()> for Wrap { -LL | | -LL | | fn from(_: ()) -> Self { -LL | | todo!() -LL | | } -LL | | } - | |_____^ - | - = help: move this `impl` block outside the of the current ? `rawr` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:363:13 - | -LL | impl MacroTrait for OutsideStruct {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -... -LL | m!(); - | ---- in this macro invocation - | - = help: move this `impl` block outside the of the current ? `my_func` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - = note: this warning originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) - -warning: non-local `impl` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:373:1 - | -LL | non_local_macro::non_local_impl!(CargoUpdate); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: move this `impl` block outside the of the current ? `_IMPL_DEBUG` - = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - = note: the macro `non_local_macro::non_local_impl` may come from an old version of the `non_local_macro` crate, try updating your dependency with `cargo update -p non_local_macro` - = note: this warning originates in the macro `non_local_macro::non_local_impl` (in Nightly builds, run with -Z macro-backtrace for more info) - -warning: non-local `macro_rules!` definition, they should be avoided as they go against expectation - --> $DIR/non_local_definitions.rs:376:1 - | -LL | non_local_macro::non_local_macro_rules!(my_macro); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: remove the `#[macro_export]` or move this `macro_rules!` outside the of the current ? `_MACRO_EXPORT` - = note: a `macro_rules!` definition is non-local if it is nested inside an item and has a `#[macro_export]` attribute - = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module - = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue - = note: the macro `non_local_macro::non_local_macro_rules` may come from an old version of the `non_local_macro` crate, try updating your dependency with `cargo update -p non_local_macro` - = note: this warning originates in the macro `non_local_macro::non_local_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info) - -warning: 50 warnings emitted -