-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Only warn about unused mut
in user-written code
#54787
Conversation
This comment has been minimized.
This comment has been minimized.
|
Curious. |
Probably some span-based suppression now failing? |
let iter_pat = | ||
self.pat_ident_binding_mode(head_sp, iter, hir::BindingAnnotation::Mutable); | ||
let iter_pat = self.pat_ident_binding_mode( | ||
desugared_span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must be because this is now using desugared_span
and not head.span
— we could perhaps "normalize" the spans we use to suppress duplicate trait errors by skipping over "compiler desugarings" into the underlying span.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nikomatsakis For my understanding could you clarify what does normalizing spans mean ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blitzerr: notice that the problem in this failing test is that an error about a trait bound has been duplicated. The issue here is probably that to check whether these sorts of errors should be emitted (when they might be generated multiple times), the compiler checks against the spans of previously-emitted errors. In this case, one of the errors has span head_sp
and the other has desugared_span
, even though they really refer to the same issue. If we instead "normalised" the spans, so that head_sp
and desugared_span
were considered "the same", then we could suppress the other error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@varkor Thanks a lot for the explanation. Makes sense.
src/librustc/hir/lowering.rs
Outdated
@@ -4058,16 +4058,16 @@ impl<'a> LoweringContext<'a> { | |||
// expand <head> | |||
let head = self.lower_expr(head); | |||
let head_sp = head.span; | |||
let desugared_span = self.allow_internal_unstable( | |||
CompilerDesugaringKind::ForLoop, | |||
head.span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be better if we use the local variable "head_sp" here instead of de-referencing head.span again ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't really make a difference, but I suppose that would make more sense!
let iter_pat = | ||
self.pat_ident_binding_mode(head_sp, iter, hir::BindingAnnotation::Mutable); | ||
let iter_pat = self.pat_ident_binding_mode( | ||
desugared_span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nikomatsakis For my understanding could you clarify what does normalizing spans mean ?
@@ -4058,16 +4058,16 @@ impl<'a> LoweringContext<'a> { | |||
// expand <head> | |||
let head = self.lower_expr(head); | |||
let head_sp = head.span; | |||
let desugared_span = self.allow_internal_unstable( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I right that desugared_span is just the renaming of next_sp ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct. I just moved it so that both the spans that were being referenced multiple times were in the same place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense
@varkor For my own understanding, how can we generate the intermediate/generated code, to see the unused 'mut' the linter is complaining about ? |
@blitzerr: I was simply using the desugar reference in rust/src/librustc/hir/lowering.rs Lines 4037 to 4056 in 113141b
to identify where the problem was located, but you can also pretty-print the HIR to see the desugared code, e.g. rustc unreachable_mut.rs -Zunpretty=hir , which produces (with somewhat wacky indentation):
#![feature(nll)]
#![allow(unreachable_code)]
#[prelude_import]
use ::std::prelude::v1::*;
#[macro_use]
extern crate std;
fn main() {
{
let _result =
match ::std::iter::IntoIterator::into_iter({
return ();
::std::ops::Range{start:
0,
end:
3,}
}) {
mut iter =>
loop {
let mut __next;
match ::std::iter::Iterator::next(&mut iter) {
::std::option::Option::Some(val) =>
__next = val,
::std::option::Option::None => break ,
}
let _ = __next;
{ }
},
};
_result
}
} |
Okay, that seems to have fixed it! |
@bors r+ Great! |
📌 Commit ea3d8f5 has been approved by |
…nikomatsakis Only warn about unused `mut` in user-written code Fixes rust-lang#54586. r? @pnkfelix cc @blitzerr
Rollup of 11 pull requests Successful merges: - #54078 (Expand the documentation for the `std::sync` module) - #54717 (Cleanup rustc/ty part 1) - #54781 (Add examples to `TyKind::FnDef` and `TyKind::FnPtr` docs) - #54787 (Only warn about unused `mut` in user-written code) - #54804 (add suggestion for inverted function parameters) - #54812 (Regression test for #32382.) - #54833 (make `Parser::parse_foreign_item()` return a foreign item or error) - #54834 (rustdoc: overflow:auto doesn't work nicely on small screens) - #54838 (Fix typo in src/libsyntax/parse/parser.rs) - #54851 (Fix a regression in 1.30 by reverting #53564) - #54853 (Remove unneccessary error from test, revealing NLL error.) Failed merges: r? @ghost
☔ The latest upstream changes (presumably #54859) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixes #54586.
r? @pnkfelix
cc @blitzerr