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

Incorrect warning about value assigned to variable not being used #3796

Closed
robertknight opened this issue Oct 17, 2012 · 10 comments
Closed

Incorrect warning about value assigned to variable not being used #3796

robertknight opened this issue Oct 17, 2012 · 10 comments
Labels
A-typesystem Area: The type system P-low Low priority

Comments

@robertknight
Copy link
Contributor

Compiling this code results in a warning: "test.rs:8:1: 8:2 warning: value assigned to x is never read", though since the variable has been captured by the lambda f() by reference, the assignment does have an effect when f() is invoked subsequently.

Tested with git commit 07edf90

fn main() {
    let mut x = 1;
    let f = fn() -> int {
        return x+20;
    };
    io::println(f().to_str());
    x += 1;
    io::println(f().to_str());
}
@sanxiyn
Copy link
Member

sanxiyn commented Mar 26, 2013

How would this code be written in the latest syntax? I get mutable variables cannot be implicitly captured.

@catamorphism
Copy link
Contributor

Not critical for 0.6; de-milestoning

@catamorphism
Copy link
Contributor

Reproduced with 64963d6 -- updated test case:

#[deny(dead_assignment)];
fn main() {
    let mut x = 1;
    let f: &fn() -> int = || { x + 20 };
    assert_eq!(f(), 21);
    x += 1;
    assert_eq!(f(), 22);
}

This is borderline but I'll nominate it for milestone 5, production-ready. Warnings should be accurate.

@catamorphism
Copy link
Contributor

(bug triage) Revisited; reproduced with 0c6fc46. Nomination still stands.

@graydon
Copy link
Contributor

graydon commented Jul 11, 2013

accepted for production-ready milestone

@nikomatsakis
Copy link
Contributor

Liveness is clearly not considered reads from closures correctly.

@prsteele
Copy link

I think I'm running into the same bug, but my test case is a bit simpler.

When compiled, the code

fn main() {
    let mut a = 1;
    let mut b = 1;

    while b < 10 {
        a = b;
        b = a + b;
    }

    println!("Result: {}", b);
}

produces the error message "bug.rs:2:12: 2:13 warning: value assigned to a is never read, #[warn(dead_assignment)] on by default", whereas

fn main() {
    let mut b = 1;

    while b < 10 {
        let a = b;
        b = a + b;
    }

    println!("Result: {}", b);
}

compiles cleanly. Both programs produce binaries that run as expected. I am compiling with

$ rustc --version
rustc 0.9-pre (009c3d8 2013-10-13 11:31:26 -0700)
host: x86_64-unknown-linux-gnu

@sanxiyn
Copy link
Member

sanxiyn commented Oct 29, 2013

@prsteele The warning is correct. You assigned 1 to a, which is never read.

@pnkfelix
Copy link
Member

Assigning P-low.

@alexcrichton
Copy link
Member

Closing, f now borrows x so this code is rejected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system P-low Low priority
Projects
None yet
Development

No branches or pull requests

8 participants