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

Bug: The compiler claims that a variable isn't read from when it is #56436

Closed
JohnGinger opened this issue Dec 2, 2018 · 2 comments
Closed
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@JohnGinger
Copy link
Contributor

JohnGinger commented Dec 2, 2018

The compiler claims that a variable isn't read from when it is

I tried this code:

fn main() {
    let mut read_compiler_error = 0;
    let mut i = 0;
    loop {
        i += 1;
        if i == 4 {
            read_compiler_error = i;
            break;
        }
    }
    let loops = 25 % read_compiler_error;

    println!("Loops are {}", loops)
}

I expected to see this happen:

The code to compile without warning

Instead, this happened:

warning: value assigned to `read_compiler_error` is never read
 --> src/main.rs:2:13
  |
2 |     let mut read_compiler_error = 0;
  |             ^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_assignments)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.50s
     Running `target/debug/bug`
Loops are 1

It has to have been read, otherwise it wouldn't give loops are 1.

Meta

rustc --version --verbose:

Johns-MacBook-Pro-2:bug johnginger$ rustc --version --verbose
rustc 1.30.1 (1433507eb 2018-11-07)
binary: rustc
commit-hash: 1433507eba7d1a114e4c6f27ae0e1a74f60f20de
commit-date: 2018-11-07
host: x86_64-apple-darwin
release: 1.30.1
LLVM version: 8.0

I might be doing something wrong, I'm fairly new to rust, but this seems like a bug to me.

@jonas-schievink
Copy link
Contributor

The warning is about the initial value 0, which is always overwritten inside the loop. Remove that initializer and you can even make read_compiler_error immutable (it is only ever assigned once, and the compiler is smart enough to see this).

fn main() {
    let read_compiler_error;
    let mut i = 0;
    loop {
        i += 1;
        if i == 4 {
            read_compiler_error = i;
            break;
        }
    }
    let loops = 25 % read_compiler_error;

    println!("Loops are {}", loops)
}

@Centril Centril added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Dec 2, 2018
@JohnGinger
Copy link
Contributor Author

JohnGinger commented Dec 2, 2018

Ah ok that makes sense . Thanks for explaining :)

I don't know how generic this lint catching is, but some text like
"warning: value assigned to read_compiler_error is always overwritten before it is read"
might have been clearer for me.

or maybe
"warning: value assigned to read_compiler_error is never read, or is always overwritten before it is read"

pietroalbini added a commit to pietroalbini/rust that referenced this issue Dec 11, 2018
Clearer error message for dead assign

I'm not that this is the right place for this (if it needs an RFC or not).

I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/.

I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons.

I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change.

If this is the wrong place for this sort of thing I'll happily delete/move it.
kennytm added a commit to kennytm/rust that referenced this issue Dec 12, 2018
Clearer error message for dead assign

I'm not that this is the right place for this (if it needs an RFC or not).

I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/.

I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons.

I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change.

If this is the wrong place for this sort of thing I'll happily delete/move it.
pietroalbini added a commit to pietroalbini/rust that referenced this issue Dec 12, 2018
Clearer error message for dead assign

I'm not that this is the right place for this (if it needs an RFC or not).

I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/.

I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons.

I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change.

If this is the wrong place for this sort of thing I'll happily delete/move it.
pietroalbini added a commit to pietroalbini/rust that referenced this issue Dec 13, 2018
Clearer error message for dead assign

I'm not that this is the right place for this (if it needs an RFC or not).

I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/.

I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons.

I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change.

If this is the wrong place for this sort of thing I'll happily delete/move it.
kennytm added a commit to kennytm/rust that referenced this issue Dec 13, 2018
Clearer error message for dead assign

I'm not that this is the right place for this (if it needs an RFC or not).

I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/.

I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons.

I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change.

If this is the wrong place for this sort of thing I'll happily delete/move it.
kennytm added a commit to kennytm/rust that referenced this issue Dec 14, 2018
Clearer error message for dead assign

I'm not that this is the right place for this (if it needs an RFC or not).

I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/.

I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons.

I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change.

If this is the wrong place for this sort of thing I'll happily delete/move it.
kennytm added a commit to kennytm/rust that referenced this issue Dec 14, 2018
Clearer error message for dead assign

I'm not that this is the right place for this (if it needs an RFC or not).

I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/.

I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons.

I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change.

If this is the wrong place for this sort of thing I'll happily delete/move it.
pietroalbini added a commit to pietroalbini/rust that referenced this issue Dec 14, 2018
Clearer error message for dead assign

I'm not that this is the right place for this (if it needs an RFC or not).

I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/.

I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons.

I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change.

If this is the wrong place for this sort of thing I'll happily delete/move it.
Centril added a commit to Centril/rust that referenced this issue Dec 16, 2018
Clearer error message for dead assign

I'm not that this is the right place for this (if it needs an RFC or not).

I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/.

I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons.

I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change.

If this is the wrong place for this sort of thing I'll happily delete/move it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

3 participants