diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 800cd398adb..ad6e7b958c7 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1337,8 +1337,7 @@ pub enum NonLocalDefinitionsDiag { cargo_update: Option, const_anon: Option>, move_help: Span, - self_ty: Span, - of_trait: Option, + may_move: Vec, has_trait: bool, }, MacroRules { @@ -1361,8 +1360,7 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag { cargo_update, const_anon, move_help, - self_ty, - of_trait, + may_move, has_trait, } => { diag.primary_message(fluent::lint_non_local_definitions_impl); @@ -1376,10 +1374,10 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag { } else { diag.note(fluent::lint_without_trait); } + let mut ms = MultiSpan::from_span(move_help); - ms.push_span_label(self_ty, fluent::lint_non_local_definitions_may_move); - if let Some(of_trait) = of_trait { - ms.push_span_label(of_trait, fluent::lint_non_local_definitions_may_move); + for sp in may_move { + ms.push_span_label(sp, fluent::lint_non_local_definitions_may_move); } diag.span_help(ms, fluent::lint_help); diff --git a/compiler/rustc_lint/src/non_local_def.rs b/compiler/rustc_lint/src/non_local_def.rs index 6b75e546a66..0805c2e2766 100644 --- a/compiler/rustc_lint/src/non_local_def.rs +++ b/compiler/rustc_lint/src/non_local_def.rs @@ -1,3 +1,5 @@ +use rustc_hir::intravisit::{self, Visitor}; +use rustc_hir::HirId; use rustc_hir::{def::DefKind, Body, Item, ItemKind, Node, TyKind}; use rustc_hir::{Path, QPath}; use rustc_infer::infer::InferCtxt; @@ -214,6 +216,29 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { None }; + let mut collector = PathCollector { paths: Vec::new() }; + collector.visit_ty(&impl_.self_ty); + if let Some(of_trait) = &impl_.of_trait { + collector.visit_trait_ref(of_trait); + } + collector.visit_generics(&impl_.generics); + + let may_move: Vec = collector + .paths + .into_iter() + .filter_map(|path| { + if path_has_local_parent(&path, cx, parent, parent_parent) { + if let Some(args) = &path.segments.last().unwrap().args { + Some(path.span.until(args.span_ext)) + } else { + Some(path.span) + } + } else { + None + } + }) + .collect(); + let const_anon = matches!(parent_def_kind, DefKind::Const | DefKind::Static { .. }) .then_some(span_for_const_anon_suggestion); @@ -223,14 +248,13 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { NonLocalDefinitionsDiag::Impl { depth: self.body_depth, move_help: item.span, - self_ty: impl_.self_ty.span, - of_trait: impl_.of_trait.map(|t| t.path.span), body_kind_descr: cx.tcx.def_kind_descr(parent_def_kind, parent), body_name: parent_opt_item_name .map(|s| s.to_ident_string()) .unwrap_or_else(|| "".to_string()), cargo_update: cargo_update(), const_anon, + may_move, has_trait: impl_.of_trait.is_some(), }, ) @@ -348,6 +372,18 @@ impl<'a, 'tcx, F: FnMut(DefId) -> bool> TypeFolder> } } +/// Simple hir::Path collector +struct PathCollector<'tcx> { + paths: Vec>, +} + +impl<'tcx> Visitor<'tcx> for PathCollector<'tcx> { + fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) { + self.paths.push(path.clone()); // need to clone, bc of the restricted lifetime + intravisit::walk_path(self, path) + } +} + /// Given a path and a parent impl def id, this checks if the if parent resolution /// def id correspond to the def id of the parent impl definition. /// diff --git a/tests/ui/lint/non-local-defs/cargo-update.stderr b/tests/ui/lint/non-local-defs/cargo-update.stderr index 617a45aaec8..30696b0f8e5 100644 --- a/tests/ui/lint/non-local-defs/cargo-update.stderr +++ b/tests/ui/lint/non-local-defs/cargo-update.stderr @@ -11,9 +11,6 @@ help: move this `impl` block outside of the current constant `_IMPL_DEBUG` | LL | non_local_macro::non_local_impl!(LocalStruct); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | may need to be moved as well - | may need to be moved as well = 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: items in an anonymous const item (`const _: () = { ... }`) are treated as in the same scope as the anonymous const's declaration = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue diff --git a/tests/ui/lint/non-local-defs/consts.stderr b/tests/ui/lint/non-local-defs/consts.stderr index c05c68d0b55..820e3a2d6e0 100644 --- a/tests/ui/lint/non-local-defs/consts.stderr +++ b/tests/ui/lint/non-local-defs/consts.stderr @@ -13,10 +13,7 @@ help: move this `impl` block outside of the current constant `Z` --> $DIR/consts.rs:13:5 | LL | impl Uto for &Test {} - | ^^^^^---^^^^^-----^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^ = note: items in an anonymous const item (`const _: () = { ... }`) are treated as in the same scope as the anonymous const's declaration = 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 @@ -33,10 +30,7 @@ help: move this `impl` block outside of the current static `A` --> $DIR/consts.rs:24:5 | LL | impl Uto2 for Test {} - | ^^^^^----^^^^^----^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^ = note: items in an anonymous const item (`const _: () = { ... }`) are treated as in the same scope as the anonymous const's declaration = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -52,10 +46,7 @@ help: move this `impl` block outside of the current constant `B` --> $DIR/consts.rs:32:5 | LL | impl Uto3 for Test {} - | ^^^^^----^^^^^----^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^ = note: items in an anonymous const item (`const _: () = { ... }`) are treated as in the same scope as the anonymous const's declaration = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue @@ -69,10 +60,7 @@ LL | impl Test { help: move this `impl` block outside of the current function `main` --> $DIR/consts.rs:43:5 | -LL | impl Test { - | ^ ---- may need to be moved as well - | _____| - | | +LL | / impl Test { LL | | LL | | fn foo() {} LL | | } @@ -89,10 +77,7 @@ LL | impl Test { help: move this `impl` block outside of the current inline constant `` and up 2 bodies --> $DIR/consts.rs:50:9 | -LL | impl Test { - | ^ ---- may need to be moved as well - | _________| - | | +LL | / impl Test { LL | | LL | | fn hoo() {} LL | | } @@ -109,10 +94,7 @@ LL | impl Test { help: move this `impl` block outside of the current constant `_` and up 2 bodies --> $DIR/consts.rs:59:9 | -LL | impl Test { - | ^ ---- may need to be moved as well - | _________| - | | +LL | / impl Test { LL | | LL | | fn foo2() {} LL | | } @@ -132,10 +114,7 @@ help: move this `impl` block outside of the current closure `` and u --> $DIR/consts.rs:72:9 | LL | impl Uto9 for Test {} - | ^^^^^----^^^^^----^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -150,10 +129,7 @@ help: move this `impl` block outside of the current constant expression ` $DIR/consts.rs:79:9 | LL | impl Uto10 for Test {} - | ^^^^^-----^^^^^----^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: 8 warnings emitted diff --git a/tests/ui/lint/non-local-defs/exhaustive-trait.stderr b/tests/ui/lint/non-local-defs/exhaustive-trait.stderr index 031927ba669..a9e3624fde5 100644 --- a/tests/ui/lint/non-local-defs/exhaustive-trait.stderr +++ b/tests/ui/lint/non-local-defs/exhaustive-trait.stderr @@ -9,11 +9,7 @@ LL | impl PartialEq<()> for Dog { help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive-trait.rs:7:5 | -LL | impl PartialEq<()> for Dog { - | ^ ------------- --- may need to be moved as well - | | | - | _____| may need to be moved as well - | | +LL | / impl PartialEq<()> for Dog { LL | | LL | | fn eq(&self, _: &()) -> bool { LL | | todo!() @@ -34,11 +30,7 @@ LL | impl PartialEq<()> for &Dog { help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive-trait.rs:14:5 | -LL | impl PartialEq<()> for &Dog { - | ^ ------------- ---- may need to be moved as well - | | | - | _____| may need to be moved as well - | | +LL | / impl PartialEq<()> for &Dog { LL | | LL | | fn eq(&self, _: &()) -> bool { LL | | todo!() @@ -58,11 +50,7 @@ LL | impl PartialEq for () { help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive-trait.rs:21:5 | -LL | impl PartialEq for () { - | ^ -------------- -- may need to be moved as well - | | | - | _____| may need to be moved as well - | | +LL | / impl PartialEq for () { LL | | LL | | fn eq(&self, _: &Dog) -> bool { LL | | todo!() @@ -82,11 +70,7 @@ LL | impl PartialEq<&Dog> for () { help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive-trait.rs:28:5 | -LL | impl PartialEq<&Dog> for () { - | ^ --------------- -- may need to be moved as well - | | | - | _____| may need to be moved as well - | | +LL | / impl PartialEq<&Dog> for () { LL | | LL | | fn eq(&self, _: &&Dog) -> bool { LL | | todo!() @@ -106,11 +90,7 @@ LL | impl PartialEq for &Dog { help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive-trait.rs:35:5 | -LL | impl PartialEq for &Dog { - | ^ -------------- ---- may need to be moved as well - | | | - | _____| may need to be moved as well - | | +LL | / impl PartialEq for &Dog { LL | | LL | | fn eq(&self, _: &Dog) -> bool { LL | | todo!() @@ -130,11 +110,7 @@ LL | impl PartialEq<&Dog> for &Dog { help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive-trait.rs:42:5 | -LL | impl PartialEq<&Dog> for &Dog { - | ^ --------------- ---- may need to be moved as well - | | | - | _____| may need to be moved as well - | | +LL | / impl PartialEq<&Dog> for &Dog { LL | | LL | | fn eq(&self, _: &&Dog) -> bool { LL | | todo!() diff --git a/tests/ui/lint/non-local-defs/exhaustive.stderr b/tests/ui/lint/non-local-defs/exhaustive.stderr index 91d4b2d4f65..c6b8dc26e31 100644 --- a/tests/ui/lint/non-local-defs/exhaustive.stderr +++ b/tests/ui/lint/non-local-defs/exhaustive.stderr @@ -8,10 +8,7 @@ LL | impl Test { help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:10:5 | -LL | impl Test { - | ^ ---- may need to be moved as well - | _____| - | | +LL | / impl Test { LL | | LL | | fn foo() {} LL | | } @@ -30,11 +27,7 @@ LL | impl Display for Test { help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:15:5 | -LL | impl Display for Test { - | ^ ------- ---- may need to be moved as well - | | | - | _____| may need to be moved as well - | | +LL | / impl Display for Test { LL | | LL | | fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { LL | | todo!() @@ -54,9 +47,7 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:22:5 | LL | impl dyn Trait {} - | ^^^^^---------^^^ - | | - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -71,10 +62,7 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:25:5 | LL | impl Trait for Vec { } - | ^^^^^^^^^^^^^^^-----^^^^^------^^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -89,10 +77,7 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:28:5 | LL | impl Trait for &dyn Trait {} - | ^^^^^-----^^^^^----------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -107,10 +92,7 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:31:5 | LL | impl Trait for *mut Test {} - | ^^^^^-----^^^^^---------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -125,10 +107,7 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:34:5 | LL | impl Trait for *mut [Test] {} - | ^^^^^-----^^^^^-----------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -143,10 +122,7 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:37:5 | LL | impl Trait for [Test; 8] {} - | ^^^^^-----^^^^^---------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -161,10 +137,7 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:40:5 | LL | impl Trait for (Test,) {} - | ^^^^^-----^^^^^-------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -179,10 +152,7 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:43:5 | LL | impl Trait for fn(Test) -> () {} - | ^^^^^-----^^^^^--------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -197,10 +167,7 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:46:5 | LL | impl Trait for fn() -> Test {} - | ^^^^^-----^^^^^------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -215,10 +182,7 @@ help: move this `impl` block outside of the current closure `` and u --> $DIR/exhaustive.rs:50:9 | LL | impl Trait for Test {} - | ^^^^^-----^^^^^----^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -233,10 +197,9 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:58:5 | LL | impl Trait for *mut InsideMain {} - | ^^^^^-----^^^^^---------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^----------^^^ + | | + | may need to be moved as well = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -251,10 +214,9 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:60:5 | LL | impl Trait for *mut [InsideMain] {} - | ^^^^^-----^^^^^-----------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^----------^^^^ + | | + | may need to be moved as well = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -269,10 +231,9 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:62:5 | LL | impl Trait for [InsideMain; 8] {} - | ^^^^^-----^^^^^---------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^----------^^^^^^^ + | | + | may need to be moved as well = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -287,10 +248,9 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:64:5 | LL | impl Trait for (InsideMain,) {} - | ^^^^^-----^^^^^-------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^----------^^^^^ + | | + | may need to be moved as well = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -305,10 +265,9 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:66:5 | LL | impl Trait for fn(InsideMain) -> () {} - | ^^^^^-----^^^^^--------------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^----------^^^^^^^^^^ + | | + | may need to be moved as well = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -323,10 +282,9 @@ help: move this `impl` block outside of the current function `main` --> $DIR/exhaustive.rs:68:5 | LL | impl Trait for fn() -> InsideMain {} - | ^^^^^-----^^^^^------------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^----------^^^ + | | + | may need to be moved as well = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -340,11 +298,7 @@ LL | impl Display for InsideMain { help: move this `impl` block outside of the current function `inside_inside` and up 2 bodies --> $DIR/exhaustive.rs:72:9 | -LL | impl Display for InsideMain { - | ^ ------- ---------- may need to be moved as well - | | | - | _________| may need to be moved as well - | | +LL | / impl Display for InsideMain { LL | | LL | | fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { LL | | todo!() @@ -363,10 +317,7 @@ LL | impl InsideMain { help: move this `impl` block outside of the current function `inside_inside` and up 2 bodies --> $DIR/exhaustive.rs:79:9 | -LL | impl InsideMain { - | ^ ---------- may need to be moved as well - | _________| - | | +LL | / impl InsideMain { LL | | LL | | fn bar() {} LL | | } diff --git a/tests/ui/lint/non-local-defs/from-local-for-global.stderr b/tests/ui/lint/non-local-defs/from-local-for-global.stderr index de3fabc97f4..27ffe35532a 100644 --- a/tests/ui/lint/non-local-defs/from-local-for-global.stderr +++ b/tests/ui/lint/non-local-defs/from-local-for-global.stderr @@ -9,11 +9,7 @@ LL | impl From for () { help: move this `impl` block outside of the current function `main` --> $DIR/from-local-for-global.rs:8:5 | -LL | impl From for () { - | ^ --------- -- may need to be moved as well - | | | - | _____| may need to be moved as well - | | +LL | / impl From for () { LL | | LL | | fn from(_: Cat) -> () { LL | | todo!() @@ -35,9 +31,8 @@ help: move this `impl` block outside of the current function `main` --> $DIR/from-local-for-global.rs:18:5 | LL | impl From>> for () { - | ^ -------------------------- -- may need to be moved as well - | | | - | _____| may need to be moved as well + | ^ -------- may need to be moved as well + | _____| | | LL | | LL | | fn from(_: Wrap>) -> Self { @@ -59,10 +54,9 @@ help: move this `impl` block outside of the current function `only_global` --> $DIR/from-local-for-global.rs:32:5 | LL | impl StillNonLocal for &Foo {} - | ^^^^^-------------^^^^^----^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^---^^^ + | | + | may need to be moved as well = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -77,9 +71,8 @@ help: move this `impl` block outside of the current function `same_function` --> $DIR/from-local-for-global.rs:40:5 | LL | impl From for GlobalSameFunction { - | ^ ------------ ------------------ may need to be moved as well - | | | - | _____| may need to be moved as well + | ^ ------ may need to be moved as well + | _____| | | LL | | LL | | fn from(x: Local1) -> GlobalSameFunction { @@ -101,9 +94,8 @@ help: move this `impl` block outside of the current function `same_function` --> $DIR/from-local-for-global.rs:48:5 | LL | impl From for GlobalSameFunction { - | ^ ------------ ------------------ may need to be moved as well - | | | - | _____| may need to be moved as well + | ^ ------ may need to be moved as well + | _____| | | LL | | LL | | fn from(x: Local2) -> GlobalSameFunction { diff --git a/tests/ui/lint/non-local-defs/generics.stderr b/tests/ui/lint/non-local-defs/generics.stderr index 8ef6e3b71da..1adefd40ffb 100644 --- a/tests/ui/lint/non-local-defs/generics.stderr +++ b/tests/ui/lint/non-local-defs/generics.stderr @@ -10,10 +10,9 @@ help: move this `impl` block outside of the current function `main` --> $DIR/generics.rs:9:5 | LL | impl Global for Vec { } - | ^^^^^^^^^^^^^^^------^^^^^------^^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^ + | | + | may need to be moved as well = 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 @@ -29,10 +28,9 @@ help: move this `impl` block outside of the current function `bad` --> $DIR/generics.rs:20:5 | LL | impl Uto7 for Test where Local: std::any::Any {} - | ^^^^^----^^^^^----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^ + | | + | may need to be moved as well = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -47,10 +45,7 @@ help: move this `impl` block outside of the current function `bad` --> $DIR/generics.rs:23:5 | LL | impl Uto8 for T {} - | ^^^^^^^^----^^^^^-^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -65,9 +60,8 @@ help: move this `impl` block outside of the current function `fun` --> $DIR/generics.rs:32:5 | LL | impl Default for UwU { - | ^ ------- -------- may need to be moved as well - | | | - | _____| may need to be moved as well + | ^ --- may need to be moved as well + | _____| | | LL | | LL | | fn default() -> Self { @@ -89,9 +83,8 @@ help: move this `impl` block outside of the current function `meow` --> $DIR/generics.rs:43:5 | LL | impl AsRef for () { - | ^ ---------- -- may need to be moved as well - | | | - | _____| may need to be moved as well + | ^ --- may need to be moved as well + | _____| | | LL | | LL | | fn as_ref(&self) -> &Cat { &Cat } @@ -111,9 +104,8 @@ help: move this `impl` block outside of the current function `fun2` --> $DIR/generics.rs:54:5 | LL | impl PartialEq for G { - | ^ ------------ - may need to be moved as well - | | | - | _____| may need to be moved as well + | ^ - may need to be moved as well + | _____| | | LL | | LL | | fn eq(&self, _: &B) -> bool { @@ -135,9 +127,8 @@ help: move this `impl` block outside of the current function `rawr` --> $DIR/generics.rs:69:5 | LL | impl From>> for () { - | ^ ---------------------- -- may need to be moved as well - | | | - | _____| may need to be moved as well + | ^ ---- may need to be moved as well + | _____| | | LL | | LL | | fn from(_: Wrap>) -> Self { @@ -159,9 +150,8 @@ help: move this `impl` block outside of the current function `rawr` --> $DIR/generics.rs:76:5 | LL | impl From<()> for Wrap { - | ^ -------- ---------- may need to be moved as well - | | | - | _____| may need to be moved as well + | ^ ---- may need to be moved as well + | _____| | | LL | | LL | | fn from(_: ()) -> Self { diff --git a/tests/ui/lint/non-local-defs/inside-macro_rules.stderr b/tests/ui/lint/non-local-defs/inside-macro_rules.stderr index 0325fd2bdc7..fea211a4e50 100644 --- a/tests/ui/lint/non-local-defs/inside-macro_rules.stderr +++ b/tests/ui/lint/non-local-defs/inside-macro_rules.stderr @@ -13,10 +13,7 @@ help: move this `impl` block outside of the current function `my_func` --> $DIR/inside-macro_rules.rs:9:13 | LL | impl MacroTrait for OutsideStruct {} - | ^^^^^----------^^^^^-------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... LL | m!(); | ---- in this macro invocation diff --git a/tests/ui/lint/non-local-defs/suggest-moving-inner.rs b/tests/ui/lint/non-local-defs/suggest-moving-inner.rs new file mode 100644 index 00000000000..61b32e5bad9 --- /dev/null +++ b/tests/ui/lint/non-local-defs/suggest-moving-inner.rs @@ -0,0 +1,17 @@ +//@ check-pass + +trait Trait {} + +fn main() { + mod below { + pub struct Type(T); + } + struct InsideMain; + trait HasFoo {} + + impl Trait for &Vec> + //~^ WARN non-local `impl` definition + where + T: HasFoo + {} +} diff --git a/tests/ui/lint/non-local-defs/suggest-moving-inner.stderr b/tests/ui/lint/non-local-defs/suggest-moving-inner.stderr new file mode 100644 index 00000000000..83557a7b9a2 --- /dev/null +++ b/tests/ui/lint/non-local-defs/suggest-moving-inner.stderr @@ -0,0 +1,28 @@ +warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item + --> $DIR/suggest-moving-inner.rs:12:5 + | +LL | impl Trait for &Vec> + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `impl` may be usable in bounds, etc. from outside the expression, which might e.g. make something constructible that previously wasn't, because it's still on a publicly-visible type + = note: an `impl` is never scoped, even when it is nested inside an item, as it may impact type checking outside of that item, which can be the case if neither the trait or the self type are at the same nesting level as the `impl` +help: move this `impl` block outside of the current function `main` + --> $DIR/suggest-moving-inner.rs:12:5 + | +LL | impl Trait for &Vec> + | ^ ---------- ----------- ---------- may need to be moved as well + | | | | + | | | may need to be moved as well + | _____| may need to be moved as well + | | +LL | | +LL | | where +LL | | T: HasFoo + | | ------ may need to be moved as well +LL | | {} + | |______^ + = 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: 1 warning emitted + diff --git a/tests/ui/lint/non-local-defs/trait-solver-overflow-123573.stderr b/tests/ui/lint/non-local-defs/trait-solver-overflow-123573.stderr index b52ea62230a..252296099f6 100644 --- a/tests/ui/lint/non-local-defs/trait-solver-overflow-123573.stderr +++ b/tests/ui/lint/non-local-defs/trait-solver-overflow-123573.stderr @@ -10,10 +10,9 @@ help: move this `impl` block outside of the current function `main` --> $DIR/trait-solver-overflow-123573.rs:12:5 | LL | impl Test for &Local {} - | ^^^^^----^^^^^------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^-----^^^ + | | + | may need to be moved as well = 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 diff --git a/tests/ui/lint/non-local-defs/weird-exprs.stderr b/tests/ui/lint/non-local-defs/weird-exprs.stderr index d49e2b777dd..e8cd4ffef09 100644 --- a/tests/ui/lint/non-local-defs/weird-exprs.stderr +++ b/tests/ui/lint/non-local-defs/weird-exprs.stderr @@ -10,10 +10,7 @@ help: move this `impl` block outside of the current constant expression ` $DIR/weird-exprs.rs:8:5 | LL | impl Uto for *mut Test {} - | ^^^^^---^^^^^---------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^ = 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 @@ -29,10 +26,7 @@ help: move this `impl` block outside of the current constant expression ` $DIR/weird-exprs.rs:16:9 | LL | impl Uto for Test {} - | ^^^^^---^^^^^----^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -45,10 +39,7 @@ LL | impl Test { help: move this `impl` block outside of the current constant expression `` and up 2 bodies --> $DIR/weird-exprs.rs:25:9 | -LL | impl Test { - | ^ ---- may need to be moved as well - | _________| - | | +LL | / impl Test { LL | | LL | | fn bar() {} LL | | } @@ -67,10 +58,7 @@ help: move this `impl` block outside of the current constant expression ` $DIR/weird-exprs.rs:34:9 | LL | impl Uto for &Test {} - | ^^^^^---^^^^^-----^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -85,10 +73,7 @@ help: move this `impl` block outside of the current constant expression ` $DIR/weird-exprs.rs:41:9 | LL | impl Uto for &(Test,) {} - | ^^^^^---^^^^^--------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item @@ -103,10 +88,7 @@ help: move this `impl` block outside of the current constant expression ` $DIR/weird-exprs.rs:48:9 | LL | impl Uto for &(Test,Test) {} - | ^^^^^---^^^^^------------^^^ - | | | - | | may need to be moved as well - | may need to be moved as well + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue warning: 6 warnings emitted