Skip to content

Commit

Permalink
Point the parent item and items that needs to be moved when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed May 22, 2024
1 parent 10fcf87 commit 7236f18
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 462 deletions.
11 changes: 6 additions & 5 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -543,18 +543,19 @@ lint_non_local_definitions_cargo_update = the {$macro_kind} `{$macro_name}` may
lint_non_local_definitions_deprecation = this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
lint_non_local_definitions_impl = non-local `impl` definition, `impl` blocks should be written at the same level as their item
.move_help =
move this `impl` block outside of the current {$body_kind_descr} {$depth ->
[one] `{$body_name}`
*[other] `{$body_name}` and up {$depth} bodies
}
.remove_help = remove `{$may_remove_part}` to make the `impl` local
.without_trait = methods and associated constants are still usable outside the current expression, only `impl Local` and `impl dyn Local` can ever be private, and only if the type is nested in the same item as the `impl`
.with_trait = 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`
.bounds = `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
.exception = items in an anonymous const item (`const _: () = {"{"} ... {"}"}`) are treated as in the same scope as the anonymous const's declaration
.const_anon = use a const-anon item to suppress this lint
lint_non_local_definitions_impl_move_help =
move this `impl` block outside of the current {$body_kind_descr} {$depth ->
[one] `{$body_name}`
*[other] `{$body_name}` and up {$depth} bodies
}
lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, `#[macro_export]` macro should be written at top level module
.help =
remove the `#[macro_export]` or move this `macro_rules!` outside the of the current {$body_kind_descr} {$depth ->
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,7 @@ pub enum NonLocalDefinitionsDiag {
body_name: String,
cargo_update: Option<NonLocalDefinitionsCargoUpdateNote>,
const_anon: Option<Option<Span>>,
move_help: Span,
may_move: Vec<Span>,
move_to: Option<(Span, Vec<Span>)>,
may_remove: Option<(Span, String)>,
has_trait: bool,
self_ty_str: String,
Expand All @@ -1373,8 +1372,7 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
body_name,
cargo_update,
const_anon,
move_help,
may_move,
move_to,
may_remove,
has_trait,
self_ty_str,
Expand All @@ -1395,11 +1393,13 @@ impl<'a> LintDiagnostic<'a, ()> for NonLocalDefinitionsDiag {
diag.note(fluent::lint_without_trait);
}

let mut ms = MultiSpan::from_span(move_help);
for sp in may_move {
ms.push_span_label(sp, fluent::lint_non_local_definitions_may_move);
if let Some((move_help, may_move)) = move_to {
let mut ms = MultiSpan::from_span(move_help);
for sp in may_move {
ms.push_span_label(sp, fluent::lint_non_local_definitions_may_move);
}
diag.span_help(ms, fluent::lint_non_local_definitions_impl_move_help);
}
diag.span_help(ms, fluent::lint_move_help);

if let Some((span, part)) = may_remove {
diag.arg("may_remove_part", part);
Expand Down
22 changes: 17 additions & 5 deletions compiler/rustc_lint/src/non_local_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,21 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
}
collector.visit_generics(&impl_.generics);

let may_move: Vec<Span> = collector
let mut may_move: Vec<Span> = collector
.paths
.into_iter()
.filter_map(|path| {
if path_has_local_parent(&path, cx, parent, parent_parent) {
Some(path_span_without_args(&path))
if let Some(did) = path.res.opt_def_id()
&& did_has_local_parent(did, cx.tcx, parent, parent_parent)
{
Some(cx.tcx.def_span(did))
} else {
None
}
})
.collect();
may_move.sort();
may_move.dedup();

let const_anon = matches!(parent_def_kind, DefKind::Const | DefKind::Static { .. })
.then_some(span_for_const_anon_suggestion);
Expand Down Expand Up @@ -244,13 +248,21 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
} else {
None
};
let move_to = if may_move.is_empty() {
ms.push_span_label(
cx.tcx.def_span(parent),
fluent::lint_non_local_definitions_impl_move_help,
);
None
} else {
Some((cx.tcx.def_span(parent), may_move))
};

cx.emit_span_lint(
NON_LOCAL_DEFINITIONS,
ms,
NonLocalDefinitionsDiag::Impl {
depth: self.body_depth,
move_help: item.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())
Expand All @@ -259,7 +271,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
const_anon,
self_ty_str,
of_trait_str,
may_move,
move_to,
may_remove,
has_trait: impl_.of_trait.is_some(),
},
Expand Down
6 changes: 1 addition & 5 deletions tests/ui/lint/non-local-defs/cargo-update.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ LL | non_local_macro::non_local_impl!(LocalStruct);
| |
| `LocalStruct` is not local
| `Debug` is not local
| move this `impl` block outside of the current constant `_IMPL_DEBUG`
|
= 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 constant `_IMPL_DEBUG`
--> $DIR/cargo-update.rs:17:1
|
LL | non_local_macro::non_local_impl!(LocalStruct);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= 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 <https://github.com/rust-lang/rust/issues/120363>
Expand Down
96 changes: 37 additions & 59 deletions tests/ui/lint/non-local-defs/consts.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ warning: non-local `impl` definition, `impl` blocks should be written at the sam
--> $DIR/consts.rs:13:5
|
LL | const Z: () = {
| - help: use a const-anon item to suppress this lint: `_`
| -----------
| | |
| | help: use a const-anon item to suppress this lint: `_`
| move this `impl` block outside of the current constant `Z`
...
LL | impl Uto for &Test {}
| ^^^^^---^^^^^-----
Expand All @@ -12,18 +15,15 @@ LL | impl Uto for &Test {}
|
= 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 constant `Z`
--> $DIR/consts.rs:13:5
|
LL | impl Uto for &Test {}
| ^^^^^^^^^^^^^^^^^^^^^
= 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 <https://github.com/rust-lang/rust/issues/120363>
= note: `#[warn(non_local_definitions)]` on by default

warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
--> $DIR/consts.rs:24:5
|
LL | static A: u32 = {
| ------------- move this `impl` block outside of the current static `A`
LL | impl Uto2 for Test {}
| ^^^^^----^^^^^----
| | |
Expand All @@ -32,17 +32,14 @@ LL | impl Uto2 for Test {}
|
= 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 static `A`
--> $DIR/consts.rs:24:5
|
LL | impl Uto2 for Test {}
| ^^^^^^^^^^^^^^^^^^^^^
= 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 <https://github.com/rust-lang/rust/issues/120363>

warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
--> $DIR/consts.rs:32:5
|
LL | const B: u32 = {
| ------------ move this `impl` block outside of the current constant `B`
LL | impl Uto3 for Test {}
| ^^^^^----^^^^^----
| | |
Expand All @@ -51,75 +48,60 @@ LL | impl Uto3 for Test {}
|
= 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 constant `B`
--> $DIR/consts.rs:32:5
|
LL | impl Uto3 for Test {}
| ^^^^^^^^^^^^^^^^^^^^^
= 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 <https://github.com/rust-lang/rust/issues/120363>

warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
--> $DIR/consts.rs:43:5
|
LL | fn main() {
| --------- move this `impl` block outside of the current function `main`
LL | impl Test {
| ^^^^^----
| |
| `Test` is not local
|
= note: methods and associated constants are still usable outside the current expression, only `impl Local` and `impl dyn Local` can ever be private, and only if the type is nested in the same item as the `impl`
help: move this `impl` block outside of the current function `main`
--> $DIR/consts.rs:43:5
|
LL | / impl Test {
LL | |
LL | | fn foo() {}
LL | | }
| |_____^
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>

warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
--> $DIR/consts.rs:50:9
|
LL | impl Test {
| ^^^^^----
| |
| `Test` is not local
|
= note: methods and associated constants are still usable outside the current expression, only `impl Local` and `impl dyn Local` can ever be private, and only if the type is nested in the same item as the `impl`
help: move this `impl` block outside of the current inline constant `<unnameable>` and up 2 bodies
--> $DIR/consts.rs:50:9
|
LL | / impl Test {
LL | const {
| ___________-
LL | | impl Test {
| | ^^^^^----
| | |
| | `Test` is not local
LL | |
LL | | fn hoo() {}
LL | | }
| |_________^
... |
LL | | 1
LL | | };
| |_____- move this `impl` block outside of the current inline constant `<unnameable>` and up 2 bodies
|
= note: methods and associated constants are still usable outside the current expression, only `impl Local` and `impl dyn Local` can ever be private, and only if the type is nested in the same item as the `impl`
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>

warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
--> $DIR/consts.rs:59:9
|
LL | const _: u32 = {
| ------------ move this `impl` block outside of the current constant `_` and up 2 bodies
LL | impl Test {
| ^^^^^----
| |
| `Test` is not local
|
= note: methods and associated constants are still usable outside the current expression, only `impl Local` and `impl dyn Local` can ever be private, and only if the type is nested in the same item as the `impl`
help: move this `impl` block outside of the current constant `_` and up 2 bodies
--> $DIR/consts.rs:59:9
|
LL | / impl Test {
LL | |
LL | | fn foo2() {}
LL | | }
| |_________^
= 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 <https://github.com/rust-lang/rust/issues/120363>

warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
--> $DIR/consts.rs:72:9
|
LL | let _a = || {
| -- move this `impl` block outside of the current closure `<unnameable>` and up 2 bodies
LL | impl Uto9 for Test {}
| ^^^^^----^^^^^----
| | |
Expand All @@ -128,29 +110,25 @@ LL | impl Uto9 for Test {}
|
= 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 closure `<unnameable>` and up 2 bodies
--> $DIR/consts.rs:72:9
|
LL | impl Uto9 for Test {}
| ^^^^^^^^^^^^^^^^^^^^^
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>

warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
--> $DIR/consts.rs:79:9
|
LL | impl Uto10 for Test {}
| ^^^^^-----^^^^^----
| | |
| | `Test` is not local
| `Uto10` is not local
LL | type A = [u32; {
| ____________________-
LL | | impl Uto10 for Test {}
| | ^^^^^-----^^^^^----
| | | |
| | | `Test` is not local
| | `Uto10` is not local
LL | |
... |
LL | | }];
| |_____- move this `impl` block outside of the current constant expression `<unnameable>` and up 2 bodies
|
= 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 constant expression `<unnameable>` and up 2 bodies
--> $DIR/consts.rs:79:9
|
LL | impl Uto10 for Test {}
| ^^^^^^^^^^^^^^^^^^^^^^
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>

warning: 8 warnings emitted
Expand Down
Loading

0 comments on commit 7236f18

Please sign in to comment.