Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve diagnostic output the non_local_definitions lint #125089

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

Urgau
Copy link
Contributor

@Urgau Urgau commented May 13, 2024

This PR improves (or at least tries to improve) the diagnostic output the non_local_definitions lint, by simplifying the wording, by adding a "sort of" explanation of bounds interaction that leak the impl...

This PR is best reviewed commit by commit and is voluntarily made a bit vague as to have a starting point to improve on.

Related to https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/non_local_defs.20wording.20improvements

Fixes #125068
Fixes #124396
cc @workingjubilee
r? @estebank

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels May 13, 2024
@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau force-pushed the non_local_def-suggestions branch from 0c69265 to 2819950 Compare May 13, 2024 20:20
Copy link
Contributor

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to "nah actually" I am just trying to throw a couple wording ideas at the wall.

compiler/rustc_lint/messages.ftl Show resolved Hide resolved
compiler/rustc_lint/messages.ftl Outdated Show resolved Hide resolved
compiler/rustc_lint/messages.ftl Outdated Show resolved Hide resolved
compiler/rustc_lint/src/non_local_def.rs Show resolved Hide resolved
@Urgau
Copy link
Contributor Author

Urgau commented May 14, 2024

I pushed several more commits that tries to improve the clarity the message were are trying to pass, mainly by moving the "move help message" to a separate span with some labels.

warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
  --> $DIR/consts.rs:43:5
   |
LL |     impl Test {
   |     ^^^^^^^^^
   |
   = note: methods and assoc const are still usable outside the current expression, only `impl Local` and `impl dyn Local` are local and only if the `Local` type is at the same nesting as the `impl` block
help: move this `impl` block and outside the of the current function `main`
  --> $DIR/consts.rs:43:5
   |
LL |       impl Test {
   |       ^    ---- may need to be moved as well
   |  _____|
   | |
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>

compiler/rustc_lint/messages.ftl Outdated Show resolved Hide resolved
compiler/rustc_lint/messages.ftl Outdated Show resolved Hide resolved
compiler/rustc_lint/messages.ftl Outdated Show resolved Hide resolved
compiler/rustc_lint/messages.ftl Show resolved Hide resolved
@Urgau Urgau force-pushed the non_local_def-suggestions branch from 9a5bb1f to a39cf37 Compare May 14, 2024 21:41
@rustbot
Copy link
Collaborator

rustbot commented May 15, 2024

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@Urgau
Copy link
Contributor Author

Urgau commented May 15, 2024

I continued to improved the diagnostic output by adding labels that points to Self type and Trait to better put emphasis on the fact that they are non-local for the lint.

With the example in #125068 (comment), it now gives:

warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
 --> a.rs:6:5
  |
6 |     impl Trait for Option<Local> {}
  |     ^^^^^-----^^^^^------^^^^^^^
  |          |         |
  |          |         `Option` is not local
  |          `Trait` is not local
  |
  = 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`
 --> a.rs:6:5
  |
6 |     impl Trait for Option<Local> {}
  |     ^^^^^^^^^^^^^^^^^^^^^^-----^^^^
  |                           |
  |                           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 <https://github.com/rust-lang/rust/issues/120363>
  = note: `#[warn(non_local_definitions)]` on by default
with colors

image

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's quite a bit of code that I need to continue reviewing, but I have some nitpicks based on the output.

tests/ui/lint/non-local-defs/weird-exprs.stderr Outdated Show resolved Hide resolved
tests/ui/lint/non-local-defs/weird-exprs.stderr Outdated Show resolved Hide resolved
Comment on lines 15 to 16
LL | impl<T> Trait<InsideMain> for &Vec<below::Type<(InsideMain, T)>>
| ^ ---------- ----------- ---------- may need to be moved as well
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you're hedging your bets that the type itself might also be in a weird scope that would otherwise not be accessible to the impl in the new location. Ideally we would check that that is the case before saying anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand your comment, it's not betting, we are checking in the code if the type is at the same nesting, and if it is we emit the note saying that it might (as in may not be something you might want to do) need to be moved, there is no guess here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I see. In that case I would make it a message explaining alternative ways of sealing traits and types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make sense to recommend sealing the trait since if we warn that means it already not local so moving the impl shouldn't change it's availability/visibility.

Regarding sealing the type, idk if that's a thing, but it also wouldn't change anything, since if it is not local it is already leaked.

In general it is very difficult to say how users should change their code to make a impl local (which from the type-system point-of-view doesn't exist anyway), so in general the lint "fallback" to proposing to make the impl def true to what it is, a global impl, and as such should be in a "global context" (module) and not inside a function, consts, array count, ...

compiler/rustc_lint/src/non_local_def.rs Show resolved Hide resolved
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 17, 2024
@Urgau Urgau force-pushed the non_local_def-suggestions branch from abefe35 to 5044fb6 Compare May 18, 2024 13:10
@Urgau
Copy link
Contributor Author

Urgau commented May 20, 2024

All the review comments have been addressed or have been replied to.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 20, 2024
@bors
Copy link
Contributor

bors commented May 22, 2024

☔ The latest upstream changes (presumably #124417) made this pull request unmergeable. Please resolve the merge conflicts.

@Urgau Urgau force-pushed the non_local_def-suggestions branch from 5044fb6 to 7236f18 Compare May 22, 2024 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
6 participants