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 function call inside constant #2983

Closed
giacomocavalieri opened this issue Apr 13, 2024 · 7 comments · Fixed by #3130
Closed

Improve error message for function call inside constant #2983

giacomocavalieri opened this issue Apr 13, 2024 · 7 comments · Fixed by #3130
Labels
bug Something isn't working good first issue Good for newcomers help wanted Contributions encouraged priority:medium

Comments

@giacomocavalieri
Copy link
Member

giacomocavalieri commented Apr 13, 2024

pub fn thing() {
  "a"
}

const wibble = thing()

This snippet of code results in this error message:

error: Syntax error
  ┌─ /src/main.gleam:7:21
  │
7 │ const wibble = thing()
  │                     ^ I was not expecting this

Expected one of: 
An import, const, type, if block, or function.

I think that's because the compiler sees it as:

const wibble = thing
()

And not as a function call

Another example is when you wrap the function call in a constructor/list/etc:

const thing = [wibble()]
//                   ^ I was not expecting this
pub fn wibble() { todo }
@giacomocavalieri giacomocavalieri added the bug Something isn't working label Apr 13, 2024
@MystPi
Copy link
Contributor

MystPi commented Apr 13, 2024

A bit unrelated, but should we update the Expected one of: message to remove the if block expectation? I noticed it in your example error message.

@giacomocavalieri
Copy link
Member Author

Nice catch! I'll open a separate issue for that

@lpil lpil added help wanted Contributions encouraged good first issue Good for newcomers priority:medium labels Apr 15, 2024
@lpil
Copy link
Member

lpil commented Apr 15, 2024

Thanks folks

@nino
Copy link
Contributor

nino commented May 6, 2024

I'd like to make an attempt at solving this! 🤞

@lpil
Copy link
Member

lpil commented May 8, 2024

Thank you!

@nino
Copy link
Contributor

nino commented May 9, 2024

I've gotten to a point where I'm getting a nice error message for a valid-looking function call:

error: Syntax error
  ┌─ /src/parse/error.gleam:3:18
  │
3 │ const wib: Int = wibble()
  │                  ^^^^^^^^ Functions can only be called within other functions

Now the question I'm trying to figure out is what to show when there's an invalid-looking function call. Right now I have

error: Syntax error
  ┌─ /src/parse/error.gleam:3:25
  │
3 │ const wib: Int = wibble(&)
  │                         ^ I can't figure out what to do with this character

Hint: Is it a typo?

This matches the error message we get with incorrect function calls generally (eg let a = foo(&bar)), but I don't know if it should. We'd first be telling the user that their function-call syntax is wrong, and after they fix it, we'll tell them they can't call functions here anyway.

An alternative might be to assume that everything from ( until a syntax error is an attempt at a function call, and we'd show something like

error: Syntax error
  ┌─ /src/parse/error.gleam:3:18
  │
3 │ const wib: Int = wibble(foo, &bar)
  │                  ^^^^^^^^^^^^^ Functions can only be called within other functions

Any preferences?

@lpil
Copy link
Member

lpil commented May 10, 2024

Let's leave it as erroring on the first syntax error (in this case the &) as that's what we do everywhere else, and it's not a mistake we've seen people make.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Contributions encouraged priority:medium
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants