-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Misplaced parenthesis in if let produces unhelpful error message #82827
Comments
Oddly enough, simply switching to a nightly compiler changes the second, "expected expression, found statement" error (not because the code has been changed, but because the compiler actually checks if it's a nightly build):
Which is maybe clearer (if the user realizes "directly" means "without parentheses"; the second note does hint that parentheses are significant). I think the intent of changing the message like this is to avoid referring to "let expressions" on the stable compiler - they don't exist in stable syntax, I think it would be possible to avoid emitting the "experimental" error in any situation that causes the second error to be emitted - whether the second error is emitted isn't changed by the presence of nightly features (only its text), so any code that causes the second error is invalid regardless of whether the feature is enabled. The "unnecessary parentheses" warning really is a style lint and it's sort of a coincidence that it's a helpful hint here. One of the previous two errors should be made to recognize that this is valid syntax without the parentheses and suggest removing them itself. @rustbot label A-parser A-diagnostics T-compiler |
The PR accounts for any number of unnecessary parentheses. I think there would still be value in eliminating the first error entirely, but that will require a larger refactoring than I wish to embark on right now 😅 |
Partially address rust-lang#82827.
IMHO, I think this issue should stay open until this error message is eliminated:
I just ran into this and got really confused before I ran into this issue. |
@atsuzaki in nightly, the output is
We close tickets when the problem has been fixed in master, we can't keep tickets open beyond that because the logistics to close them would become too onerous (but maybe we could fix that in the future with tooling). The lint now mentions that you might have wanted If you don't mind, could you open a new ticket dedicated only to the silencing of the lint in this case (linking to this ticket)? That makes it easier to track what still needs to be done, instead of changing the labels after the fact to change its categorization. Doing it that way makes traceability of the work done on the project possible. |
Thanks a bunch, I've opened #83274! |
I am learning Rust. I haven't read the full manual yet, I am just bumbling through. Tonight I hit an error message that confused me. I am using Rust 1.50.0 on OS X 10.13.6, but I can reproduce the problem on Godbolt. Here's my Godbolt repro:
This is wrong because the parenthesis between
if
andlet
confuses Rust completely. If you remove the parenthesis (Godbolt link) it works fine. But I didn't know that.The wrong version produces the following error messages:
In my opinion, this is not as helpful as it could be.
The "experimental" error mislead me badly. First off, saying something is "experimental" does not mean it is wrong, it could mean "this will be legal in the next released version". Second off, the "experimental" error does not say what is experimental. Is it destructuring which is not allowed? Is it destructuring *in this position* that is not allowed? (In context-- IE not this minimal example-- I had the if embedded as an expression inside a match.) The linked github issue is over two years old and is not enlightening, in fact, it's just frankly kinda scary.
The "expected expression, found statement" error is potentially helpful, but confused me a little, because I'd used "if let" before— and I wasn't paying as much attention to it anyway, because (C habit that I shouldn't be bringing with me into Rust?) if I get two errors on the same line, I assume the first one is the "real" error and the second one is just the compiler being thrown off by the first error.
The warning is a good hint (I had a conversation on twitter about this) but first off it looks like a style recommendation, not a functional one; and also I did not see it.
My expected behavior:
if (let x = ...)
has been a syntax since C++03), it would be great if the compiler would detect this would have been correct syntax without the parenthesis and print a "hint: there are no parenthesis in if let, recommend remove parenthesis" there.Postscript: I had some thoughts about that parenthesis warning. I thought they would be distracting in this issue, and those thoughts were more like a feature request or a question than a bug report, so as your issue form suggests I made a forum thread about that instead.
The text was updated successfully, but these errors were encountered: