-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Unexpected "expected function" error with unit type #106515
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
Comments
This comment was marked as resolved.
This comment was marked as resolved.
The compiler would consider the case
the same as
I think it's hard to determine if the user wants invoke a closure or not by writing the "()" to the next line.
|
We should definitely be able to look at the spans of the callee and the argument list, determine if they are on separate lines (via the |
The case should be handled in both the parser and the call error. In the parser we can't be certain that the call expression is incorrect, except for maybe literals, but if you had fn main() {
let x = 5;
let y = x
()
} we are back to ambiguity, so that should be handled in E0618 and suggest a semicolon there as well. You could also have fn main() {
let x = 5;
let y = x
();
} which would parse successfully but fail only in the call. |
@rustbot claim |
When adding a nice parser error you have to make sure that you never emit a parser error for valid rust syntax. fn foo() {
1
()
} doesn't give a parser error but only errors during type checking. This is because of macros, which may transform code that is valid syntax but invalid semantically into something entirely valid. |
At the very least, these cases should be handled fn a() {
let x = 5;
let y = x //~ ERROR expected function
()
} //~ ERROR expected `;`, found `}`
fn b() {
let x = 5;
let y = x //~ ERROR expected function
();
}
fn c() {
let x = 5;
x //~ ERROR expected function
()
}
fn d() { // ok
let x = || ();
x
()
}
fn e() { // ok
let x = || ();
x
();
}
fn f()
{
let y = 5 //~ ERROR expected function
()
} //~ ERROR expected `;`, found `}`
fn g() {
5 //~ ERROR expected function
();
} |
I guess I should also be clear that the suggestion currently provided by rustc could never be correct: function (and closure) names can't start with integer |
The diagnostics currently emitted by the compiler are absolutely correct: An integer cannot be called and the let-statement must be followed by a semicolon. Indeed, identifiers (“function (and closure) names”) cannot start with a digit. However, this is not relevant here: Syntactically speaking, the callee (the thing being called) in a call expression can be an arbitrary expression. Consider So This is the reason why you see the error messages you posted. Edit: I hope it's already clear from context but I'd like to emphasize that I am not saying that the diagnostics as emitted today should stay the way they are (we should definitely suggest to add a |
Ahh right, I forget briefly that function calls could be on values, I was thinking of the integer literal being seen as an identifier only (and of course integer identifiers don't exist). Silly me! |
@Ezrashaw the point you're making is why we can proactively catch the "literal followed by ()" case in the parser, and avoid the second, unnecessary "invalid call" error. But for the general case, where the parser doesn't have enough context (a, b and c in my examples), we would have to emit two errors, or delay the parse error until resolve. |
@lyming2007 the compiler errors are correct, but they are misleading, so we'll put some effort to improve the output to make it clear what is happening to everyone, regardless of expertise level, at a glance. |
ok. So we still let the compiler emit 2 diagnostics, one is from the parser, another one is error[E0618]. How about the output being like this?
Which won't be misleading the user to program code like |
@estebank @lyming2007 Hmm, I haven't really been keeping up to date with this so this might not be ideal/possible but:
|
Disclaimer: I might be biased by the phrasing of existing diagnostics. The error message expected I expect it to look similar to:
|
@fmease Sorry, I should have been more clear:
doesn't really make sense to me, it leaves out the As for your suggested diagnostic, the proposed suggestion doesn't seem quite right in terms of wording. Adding a semicolon does not fix the error; it does not provide a function, but rather makes the error go away by removing the expectation of a function in the first place. This has to be explained to the user, otherwise it just looks even more confusing. |
…errors Detect missing `;` that parses as function call Fix rust-lang#106515.
…rors Detect missing `;` that parses as function call Fix rust-lang#106515.
Uh oh!
There was an error while loading. Please reload this page.
I tried this code:
I expected to see this happen:
One error occurs from the missing semicolon.
Instead, this happened:
An additional unexpected error occurs when when I obviously do not want to invoke a closure. Also note that the semicolon error is after the supposed closure invocation, when I would expect it to be after the
let x = 5
.Meta
Occurs on latest stable and nightly.
The text was updated successfully, but these errors were encountered: