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

Only warn about unused mut in user-written code #54787

Merged
merged 2 commits into from
Oct 6, 2018

Conversation

varkor
Copy link
Member

@varkor varkor commented Oct 3, 2018

Fixes #54586.

r? @pnkfelix
cc @blitzerr

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 3, 2018
@rust-highfive

This comment has been minimized.

@nikomatsakis
Copy link
Contributor

[00:51:50] 
[00:51:50] 8    = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
[00:51:50] 9    = note: required by `std::iter::IntoIterator::into_iter`
[00:51:50] 10 
[00:51:50] - error: aborting due to previous error
[00:51:50] + error[E0277]: the size for values of type `dyn std::iter::Iterator<Item=&mut u8>` cannot be known at compilation time
[00:51:50] +   --> $DIR/issue-20605.rs:12:17
[00:51:50] +    |
[00:51:50] + LL |     for item in *things { *item = 0 }
[00:51:50] +    |
[00:51:50] +    |
[00:51:50] +    = help: the trait `std::marker::Sized` is not implemented for `dyn std::iter::Iterator<Item=&mut u8>`
[00:51:50] +    = note: all local variables must have a statically known size
[00:51:50] +    = help: unsized locals are gated as an unstable feature
[00:51:50] + 
[00:51:50] + error: aborting due to 2 previous errors
[00:51:50] + error: aborting due to 2 previous errors
[00:51:50] 12 
[00:51:50] 13 For more information about this error, try `rustc --explain E0277`.
[00:51:50] 14 
[00:51:50] 

@nikomatsakis
Copy link
Contributor

Curious.

@nikomatsakis
Copy link
Contributor

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,
Copy link
Contributor

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.

Copy link
Contributor

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 ?

Copy link
Member Author

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.

Copy link
Contributor

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.

@@ -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,
Copy link
Contributor

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 ?

Copy link
Member Author

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,
Copy link
Contributor

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(
Copy link
Contributor

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 ?

Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

makes sense

@blitzerr
Copy link
Contributor

blitzerr commented Oct 4, 2018

@varkor For my own understanding, how can we generate the intermediate/generated code, to see the unused 'mut' the linter is complaining about ?

@varkor
Copy link
Member Author

varkor commented Oct 4, 2018

@blitzerr: I was simply using the desugar reference in lowering.rs:

// From: `[opt_ident]: for <pat> in <head> <body>`
ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
// to:
//
// {
// let result = match ::std::iter::IntoIterator::into_iter(<head>) {
// mut iter => {
// [opt_ident]: loop {
// let mut __next;
// match ::std::iter::Iterator::next(&mut iter) {
// ::std::option::Option::Some(val) => __next = val,
// ::std::option::Option::None => break
// };
// let <pat> = __next;
// StmtKind::Expr(<body>);
// }
// }
// };
// result
// }

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
              }
          }

@varkor
Copy link
Member Author

varkor commented Oct 4, 2018

Okay, that seems to have fixed it!

@nikomatsakis
Copy link
Contributor

@bors r+

Great!

@bors
Copy link
Contributor

bors commented Oct 4, 2018

📌 Commit ea3d8f5 has been approved by nikomatsakis

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 4, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Oct 5, 2018
…nikomatsakis

Only warn about unused `mut` in user-written code

Fixes rust-lang#54586.

r? @pnkfelix
cc @blitzerr
bors added a commit that referenced this pull request Oct 6, 2018
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
@bors
Copy link
Contributor

bors commented Oct 6, 2018

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

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 6, 2018
@bors bors merged commit ea3d8f5 into rust-lang:master Oct 6, 2018
@varkor varkor deleted the unused-mut-in-desugaring branch October 6, 2018 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants