Improve diagnostic for when field is never read#83004
Improve diagnostic for when field is never read#83004sunjay wants to merge 7 commits intorust-lang:masterfrom
Conversation
|
(rust-highfive has picked a reviewer for you, use r? to override) |
|
r? @pnkfelix |
|
@pnkfelix I had to use My PR now adds the same Here's what that looks like for a field that is never read: I thought this consistency would work well for providing people with a quick fix in a format that they're probably already familiar with from the unused variable warning. I've gone through many iterations of the note at the bottom of the diagnostic. It definitely isn't perfect and it's still longer than I would probably like. I'd love some suggestions for how we can do better here. The diagnostic message matters a lot! :) The tricky thing is that we need something that also works for items (e.g. functions, structs, enums, associated constants, etc.). Most of the messages I can think of only work well for values or fields. Here's the same error but for an associated constant: This works okay, but we can probably improve on it. If you have some advice for how to get the word wrap to work in all cases, I'd appreciate that too. A previous iteration that I liked better but definitely doesn't work well for items: Here's what that looks like for the associated constant example: |
|
@rustbot label +S-waiting-on-author -S-waiting-on-review |
sunjay
left a comment
There was a problem hiding this comment.
@pnkfelix The diagnostic messages are much shorter now! I think the help combined with the note does a good job of conveying both the fix and the reason why it works. Let me know if you have any feedback. :)
Here's what it looks like for fields:
error: field is never read: `items`
--> $DIR/field-used-in-ffi-issue-81658.rs:13:5
|
LL | items: Option<Vec<T>>,
| ^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_items`
|
= note: the leading underscore signals that this field serves some other purpose
even if it isn't used in a way that we can detect. (e.g. for its effect
when dropped or in foreign code)
And for associated constants:
error: associated constant is never used: `BAR`
--> $DIR/associated-const-dead-code.rs:6:5
|
LL | const BAR: u32 = 1;
| ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_BAR`
|
= note: the leading underscore signals that this associated constant serves some other purpose
even if it isn't used in a way that we can detect.
Let me know if the wording needs to be adjusted. 😁
compiler/rustc_passes/src/dead.rs
Outdated
There was a problem hiding this comment.
It would be more future proof to use a match so Rust's exhaustiveness checks tell us what code to change when we add more variants to the enum. I opted to keep it simple though for the sake of this PR. Let me know if you have thoughts on that.
There was a problem hiding this comment.
The structured suggestions are slightly incorrect. We want to point only at Z, not the whole item. I believe this is a problem for enum variants with data and consts as well.
There was a problem hiding this comment.
I was able to get it to look like this, is this sufficient? I'm passing in the right span now but it's still printing the ^ underline.
warning: type alias is never used: `Z`
--> $DIR/issue-37515.rs:5:1
|
LL | type Z = dyn for<'x> Send;
| ^^^^^-^^^^^^^^^^^^^^^^^^^^
| |
| help: if this is intentional, prefix it with an underscore: `_Z`
|
= note: the leading underscore signals that this type alias serves some other purpose even if it isn't used in a way that we can detect.
38a6c6f to
3e34eb8
Compare
|
@pnkfelix sorry for the delay on getting back to this. I've updated the PR and addressed all the comments. Let me know what the next steps should be. :) |
|
@sunjay fyi you can change the labels with |
|
@bors r+ lets merge this; the only thing I'd consider changing is the enum variant that controls the emission of the extra parenthetical in the note, and I think such changes can wait until we see motivation from somewhere else. |
|
📌 Commit 3e34eb8 has been approved by |
|
@bors rollup |
…, r=pnkfelix Improve diagnostic for when field is never read Related to (but does not close) rust-lang#81658 This completes the first step of ````````@pnkfelix's```````` [mentoring instructions](rust-lang#81658 (comment)) but does not actually improve the diagnostics (yet!). The two tests are heavily reduced versions of code from the original bug report. I've confirmed that the reduced `field-used-in-ffi` test [fails on nightly](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f0862c89ddca028c55c20a5ed05e679a) but [passes on stable](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f0862c89ddca028c55c20a5ed05e679a). This confirms that the regression is reproduced correctly. The `drop-only-field` test is a case that ````````@pnkfelix```````` mentioned in his mentoring instructions. It is not a regression, but will come in handy when we make the diagnostic smarter by looking at whether the field type implements `Drop`. Per the [rustc-dev-guide](https://rustc-dev-guide.rust-lang.org/tests/adding.html), each test includes a comment summarizing what it is about.
|
likely failure of rollup for some diagnosis: |
|
@sunjay Ping from triage, it seems this PR might be cause of rustfix check failure. Could you test the related unit tests locally to verify? Thanks! |
|
#81658 (comment) reports that issue #81658 is being worked around on 1.53-beta (by reverting the associated PR), and that issue #81658 solely remains on 1.54-nightly at the moment -- so it would be good to resolve this before 1.54-beta branches. I'm not sure how much time I have to look into the issues here, but if we don't see word from @sunjay then I'll try to follow up myself. |
|
I still haven't had a chance to look into this. The schedule for when betas are cut is Friday before release (Note that the beta cut happens on Tuesday before release, but it will use the commit from Friday, so effectively Friday is the deadline from a compiler contribution point of view.) Either I'll get chance to look at this today, or I'll put up revert of PR #81473 on mainline today. |
|
Sorry for the delay. Had a bunch of life things come up that took my time and energy away from this. Working on reproducing/fixing the issue now during my lunch break. |
|
@pnkfelix Okay so here's what I think is happening: My change introduces a new suggestion which rustfix uses to remove dead code. The test that is breaking is #![deny(unused_mut)]
pub fn foo() -> u32 {
let mut x = 3;
x
}
fn bar() {}It then asserts that I tried a few different variations on the test but it seems that any dead code gets removed by rustfix with my changes from this PR. I can get the test running again by using an unused mut as shown below: #![deny(unused_mut)]
pub fn foo() -> u32 {
let mut x = 3;
x
}
pub fn bar() {
#[allow(unused_mut)]
let mut _y = 4;
}With this change all the cargo tests pass and it seems to work fine. If this is an acceptable fix, does anyone know how to get that landed? I'm in the process of creating a PR for cargo but don't know what the process is after that. |
|
@pnkfelix PR for The fix I've suggested doesn't break cargo or rely on any of the details of this PR, so we should be able to land that separately, then update the submodules in rust-lang/rust to get this PR through. I've never needed to do this while contributing to rustc before (this is just by best guess at the process) so any guidance would be appreciated. :) |
|
@sunjay you can update submodules like this (in a clone of rust-lang/rust): cd src/tools/cargo
git fetch origin
git checkout origin/master
cd ../../../
git add src/tools/cargo
git commit -m "update cargo submodule" |
|
@sunjay If you need to update cargo to get the fix in, I think it will be tricky because there are some integration issues with the current batch of cargo updates. I have a branch with the fixes, but it may be difficult to get everything in before the beta cutoff. I'll go ahead and prep a PR tonight, but I'm not sure everything will land in time. |
|
Thanks for chiming in everyone! I think we could probably get it in quickly by updating the submodules, but I spoke to Manish and Felix and I think we will probably hold off for now. @pnkfelix said this to me in our DMs:
I sat down with Manish and he showed me another potential way to fix this that is probably a better solution. He mentioned that I might be passing the wrong |
|
Sounds good. I went ahead and posted #86207 anyways, just in case you need it. It didn't contain the change I was thinking of, so it was a little simpler than expected. |
|
Triage: |
|
Since the revert has landed and the original issue has been closed, I'm guessing this PR is no longer relevant. Happy to re-open and keep working on it if it is still needed. |
Related to (but does not close) #81658
This completes the first step of @pnkfelix's mentoring instructions but does not actually improve the diagnostics (yet!). The two tests are heavily reduced versions of code from the original bug report.
I've confirmed that the reduced
field-used-in-ffitest fails on nightly but passes on stable. This confirms that the regression is reproduced correctly. Thedrop-only-fieldtest is a case that @pnkfelix mentioned in his mentoring instructions. It is not a regression, but will come in handy when we make the diagnostic smarter by looking at whether the field type implementsDrop.Per the rustc-dev-guide, each test includes a comment summarizing what it is about.