-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Wrong compiler error for assignment in if
conditonal
#17283
Comments
Assignments in if i = (n {
...
}) instead of an assignment to a variable: if i = (n) {
...
} (Note that that latter example can be used to force the parser to parse it as expected.) I was under the impression that #14803 was supposed to fix this, making the parser resolve it as the latter instead of the former. |
One workaround to get the effect is this:
|
Taking a look at this one |
It looks like propagating the struct literal restriction through to the RHS expression of an assignment yields a much better error message: Codefn main() {
let x = 1u;
if x = x {
println!("{}", x);
}
} Before
After
I'll see about getting a PR out for this. |
cc @nick29581 |
This prevents confusing errors when accidentally using an assignment in an `if` expression. For example: ```rust fn main() { let x = 1u; if x = x { println!("{}", x); } } ``` Previously, this yielded: ``` test.rs:4:16: 4:17 error: expected `:`, found `!` test.rs:4 println!("{}", x); ^ ``` With this change, it now yields: ``` test.rs:3:8: 3:13 error: mismatched types: expected `bool`, found `()` (expected bool, found ()) test.rs:3 if x = x { ^~~~~ ``` Closes issue rust-lang#17283
This now gives
which is much, much better. |
I'm running a recent nightly (
rustc 0.12.0-pre-nightly (0d3bd7720 2014-08-27 21:31:13 +0000)
) on x86_64 GNU/Linux. I get this error:with the following program:
I don't know whether assignments within
if
conditions are supposed to be allowed, but the compiler should probably recognize that case and report something more useful. I also have no idea what it wants with the colon.The text was updated successfully, but these errors were encountered: