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

improve error message for missing return type #1672

Closed
emekoi opened this issue Oct 20, 2018 · 9 comments
Closed

improve error message for missing return type #1672

emekoi opened this issue Oct 20, 2018 · 9 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. error message This issue points out an error message that is unhelpful and should be improved.
Milestone

Comments

@emekoi
Copy link
Contributor

emekoi commented Oct 20, 2018

when the return type is missing for a function, i get the error expected token '{', found 'fn'. could someone point me towards the code that handles parsing of functions, so i could fix this?

@thejoshwolfe
Copy link
Contributor

thejoshwolfe commented Oct 21, 2018

I actually think we should disallow return type expressions starting with {. That would solve this problem.

Here's the real question: why would you ever want a return expression to legitimately begin with {? I believe it's never going to work semantically, since the only value a {...} block could possibly evaluate to is the void value {}, and that's not a type. You would have to put a label on the block to get any other value, and in that case the expression doesn't start with { anymore.

@ghost
Copy link

ghost commented Oct 21, 2018

I would make this a priority... It's something that newcomers are going to run into again and again. (As well as people who frequently switch between Zig and another language like JS.) The error message still initially confuses me after the tenth time running into it.

And if there was a reason to allow return types to start with {, I think it would be worth adding a new symbol to the syntax introducing the return type.

For me, this issue ranks right up there with the 'copying fieldParentPtr thing out of a struct' footgun.

@winksaville
Copy link
Contributor

winksaville commented Oct 21, 2018 via email

@emekoi
Copy link
Contributor Author

emekoi commented Oct 21, 2018

@thejoshwolfe i think we're talking about two different things here. i'm talking about the error message you get from code like this:

fn foo() {

}

fn bar() void {

}

@thejoshwolfe
Copy link
Contributor

@emekoi that is what I'm talking about too. Once the parser sees:

fn foo() {

there's no possible way that the program could compile successfully. We can have the parser report an error pointing to that { saying that you need a return type.

@Hejsil
Copy link
Contributor

Hejsil commented Oct 21, 2018

This is related to #760, and I'm currently working on a solution.

@andrewrk andrewrk added this to the 0.4.0 milestone Oct 23, 2018
@Hejsil
Copy link
Contributor

Hejsil commented Nov 13, 2018

After #1685 we now get this error:

/home/jhc/repo/zig/test.zig:1:8: error: invalid token: '{'
fn a() {}
       ^

Not perfect, but we get the error earlier, which should help alot.

@andrewrk
Copy link
Member

I would consider this solved, with a low priority further improvement being to special case this error and add something like "expected return type"

@andrewrk andrewrk modified the milestones: 0.4.0, 1.1.0 Nov 13, 2018
@SpexGuy SpexGuy added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Mar 20, 2021
@SpexGuy SpexGuy added the error message This issue points out an error message that is unhelpful and should be improved. label Apr 5, 2021
@nektro
Copy link
Contributor

nektro commented Dec 14, 2021

with Zig master this now works as expected

pub fn main() {
}
test.zig:1:15: error: expected return type expression, found '{'
pub fn main() {
              ^

@ifreund ifreund closed this as completed Dec 14, 2021
@ifreund ifreund modified the milestones: 1.1.0, 0.9.0 Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. error message This issue points out an error message that is unhelpful and should be improved.
Projects
None yet
Development

No branches or pull requests

8 participants