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

Misleading error message when variable is not re-initialized in loop #83760

Closed
Aaron1011 opened this issue Apr 1, 2021 · 1 comment · Fixed by #85686
Closed

Misleading error message when variable is not re-initialized in loop #83760

Aaron1011 opened this issue Apr 1, 2021 · 1 comment · Fixed by #85686
Assignees
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

Aaron1011 commented Apr 1, 2021

The following code compiles:

struct Foo;

fn main() {
    let mut val = Some(Foo);
    while let Some(foo) = val {
        if true {
            val = None;
        } else {
            val = None;
        }
    }
}

If we don't ensure that val is re-initialized at the end of the loop:

struct Foo;

fn main() {
    let mut val = Some(Foo);
    while let Some(foo) = val {
        if true {
            val = None;
        } else {
            
        }
    }
}

we get the following error:

error[E0382]: use of moved value
 --> src/main.rs:5:20
  |
5 |     while let Some(foo) = val {
  |                    ^^^ value moved here, in previous iteration of loop
  |
  = note: move occurs because value has type `Foo`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving `val.0`
  |
5 |     while let Some(ref foo) = val {
  |                    ^^^

error: aborting due to previous error; 1 warning emitted

This error message seems to imply that the while let pat = variable { ... } pattern is invalid. However, the fact that val gets re-initialized in one branch of the loop means that the user likely intended to initialize it in all branches.

We should detect when this kind of case occurs, and explain that val might not be initialized at the end of the loop. We could even try to detect where the missing initializations need to be inserted, but that might be very difficult.

@Aaron1011 Aaron1011 added A-diagnostics Area: Messages for errors, warnings, and lints A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Apr 1, 2021
@JohnTitor JohnTitor added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Apr 2, 2021
@ptrojahn
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants