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

NLL erroneous Warning: variable does not need to be mutable #50461

Closed
rukai opened this issue May 5, 2018 · 4 comments
Closed

NLL erroneous Warning: variable does not need to be mutable #50461

rukai opened this issue May 5, 2018 · 4 comments
Labels
A-NLL Area: Non-lexical lifetimes (NLL) NLL-diagnostics Working towards the "diagnostic parity" goal

Comments

@rukai
Copy link
Contributor

rukai commented May 5, 2018

#![feature(nll)]
struct Foo {
    pub value: i32
}

// triggers bug
fn use_foo_mut(mut foo: Foo) {
    foo = foo;
    println!("{}", foo.value);
}

// does not trigger bug
fn use_i32_mut(mut i: i32) {
    i = i;
    println!("{}", i);
}

fn main() {
    use_foo_mut(Foo { value: 413 });
    use_i32_mut(413);
}

gives:

warning: variable does not need to be mutable
 --> src/main.rs:8:16
  |
8 | fn use_foo_mut(mut foo: Foo) {
  |                ----^^^
  |                |
  |                help: remove this `mut`
  |
  = note: #[warn(unused_mut)] on by default

If I remove the mut then I get a compile error.

Compiled with nightly-2018-05-05

This is not related to #50343 as that has been fixed in the nightly I am using.

@sanxiyn sanxiyn added the A-NLL Area: Non-lexical lifetimes (NLL) label May 6, 2018
@matthewjasper matthewjasper added the NLL-diagnostics Working towards the "diagnostic parity" goal label May 7, 2018
@pnkfelix
Copy link
Member

pnkfelix commented May 8, 2018

this is probably related to the recent changes to do the use-of-mut checks without requiring AST-borrowck.

@pnkfelix
Copy link
Member

pnkfelix commented May 8, 2018

i.e. this is probably related to PR #48605

@KiChjang
Copy link
Member

KiChjang commented May 8, 2018

cc me

@KiChjang
Copy link
Member

KiChjang commented May 9, 2018

Hmm... I think this bug is highlighted by @pnkfelix a couple of days ago, and it's regarding this branch:

Reservation(WriteKind::MutableBorrow(BorrowKind::Unique))
| Write(WriteKind::MutableBorrow(BorrowKind::Unique)) => {
if let Err(_place_err) = self.is_mutable(place, LocalMutationIsAllowed::Yes) {
span_bug!(span, "&unique borrow for {:?} should not fail", place);
}
}

Or perhaps in the branch further down as well:

Reservation(WriteKind::Move)
| Reservation(WriteKind::StorageDeadOrDrop)
| Reservation(WriteKind::MutableBorrow(BorrowKind::Shared))
| Write(WriteKind::Move)
| Write(WriteKind::StorageDeadOrDrop)
| Write(WriteKind::MutableBorrow(BorrowKind::Shared)) => {

I suspect it's the latter, because the MIR generated by the code above contains two move assignment statements. Either way, these two branches should really contain the used mut variables logic.

bors added a commit that referenced this issue May 18, 2018
Use EverInit instead of MaybeInit to determine initialization

Fixes #50461.
Fixes #50463.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) NLL-diagnostics Working towards the "diagnostic parity" goal
Projects
None yet
Development

No branches or pull requests

5 participants