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

Show helpful error message when calling a function in a constant value #3130

Conversation

nino
Copy link
Contributor

@nino nino commented May 11, 2024

This should resolve #2983.

Attempting to call a function in a constant definition will now show a more helpful error message:

error: Syntax error
  ┌─ /src/parse/error.gleam:3:18
  │
3 │ const wib: Int = wibble(1, "wobble")
  │                  ^^^^^^^ Functions can only be called within other functions
error: Syntax error
  ┌─ /src/one/two.gleam:1:15
  │
1 │ const first = list.at([1], 0)
  │               ^^^^^^^^ Functions can only be called within other functions

(the second test is at compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__functions_called_outside_module.snap, which already existed)

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Could you add tests for calling functions from other modules please? For example list.map(items, to_string).

@nino
Copy link
Contributor Author

nino commented May 13, 2024

@lpil Turns out, there already was such a test case. I've updated the code to make the error message look the same whether or not the function is from another module. (i.e., the error highlights the whole function call from beginning of name to end of arguments)

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit unsure about using the expression function args parsing method here as it emits errors relating to expressions, not constants. This will be inaccurate at times.

Previously we stopped when we saw a ( as this is enough to know that it's a function call. How come you decided to move away from that here?

@nino
Copy link
Contributor Author

nino commented May 14, 2024

@lpil

as it emits errors relating to expressions, not constants

That's a good point. I'd decided to parse the function params because that would make the error message nicer, by highlighting the whole function call, rather than just the name and opening paren. (Also hadn't realized that the parser already checks for ( after imported identifiers (const a = b.c() already showed a reasonable error).)

I guess, to get the error message to highlight the whole function call, we'd have to wrap parse_fn_args in some custom error handling logic or something. Which is probably overkill.

I've now changed the code for const a = b() to match the existing code for const a = b.c(). Now the error message only highlights up to the opening paren, but it won't show wrong error messages inside the parens.

@nino nino force-pushed the Improve-error-message-for-function-call-inside-constant-2983 branch from 79d5a50 to 01fbdd2 Compare May 14, 2024 12:26
@nino nino force-pushed the Improve-error-message-for-function-call-inside-constant-2983 branch from 01fbdd2 to be6afe9 Compare May 14, 2024 12:30
Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! OK cool that sounds great.

Thank you once again! This should help people a lot 💜

@lpil lpil merged commit 94ecc2e into gleam-lang:main May 15, 2024
12 checks passed
@nino nino deleted the Improve-error-message-for-function-call-inside-constant-2983 branch May 15, 2024 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve error message for function call inside constant
2 participants