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

Better error message for triple equals #3150

Merged
merged 1 commit into from
May 20, 2024

Conversation

rabingaire
Copy link
Contributor

@rabingaire rabingaire commented May 17, 2024

fix: #3125

This is the new error message after the fix:

error: Syntax error
  ┌─ /Users/rabingaire/Desktop/test_app/src/test_app.gleam:4:37
  │
4 │   [1,2,3] |> list.filter(fn (a) { a === 3 }) 
  │                                     ^^^ Did you mean `==`?

Gleam uses `==` to check for equality between two values.
See: https://tour.gleam.run/basics/equality

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 a test to ensure that this code does not result in that helpful error:

==     =

I think currently it will show the error in this case, which isn't what we want.

It may be better to implement this in the tokeniser rather than in the parser.

@rabingaire
Copy link
Contributor Author

Thank you. Could you add a test to ensure that this code does not result in that helpful error:

==     =

I think currently it will show the error in this case, which isn't what we want.

It may be better to implement this in the tokeniser rather than in the parser.

Thank you for the recommendation, I made the changes and now the logic is handled in tokenizer, let me know if the error message is good enough, if it is not we can add a special case like these

LexicalErrorType::UnrecognizedToken { tok } if *tok == '\'' => (
    "Unexpected single quote",
    vec!["Hint: Strings are written with double quotes.".into()],
),

@rabingaire rabingaire requested a review from lpil May 17, 2024 23:33
@lpil
Copy link
Member

lpil commented May 18, 2024

Hello! I gave this a try and I got this error:

error: Syntax error
  ┌─ /Users/louis/Desktop/thing/src/thing.gleam:4:5
  │
4 │   ===
  │     ^ I can't figure out what to do with this character

Hint: Is it a typo?

This doesn't seem any clearer to me than the previous error. Let's have one like Giacomo suggested in the issue, that'll help the programmer understand what the problem is.

I also tested with == = and I got the same error, but it should have the original error and the new error should only be for ===

@rabingaire
Copy link
Contributor Author

Hello! I gave this a try and I got this error:

error: Syntax error
  ┌─ /Users/louis/Desktop/thing/src/thing.gleam:4:5
  │
4 │   ===
  │     ^ I can't figure out what to do with this character

Hint: Is it a typo?

This doesn't seem any clearer to me than the previous error. Let's have one like Giacomo suggested in the issue, that'll help the programmer understand what the problem is.

I also tested with == = and I got the same error, but it should have the original error and the new error should only be for ===

Thank you for the suggestions, made changes as requested please check 👍🏽

Comment on lines 376 to 389
LexicalErrorType::InvalidOperator { expected } if *expected == "==" => (
"Did you mean `==`?",
vec![
"Gleam uses `==` to check for equality between two values.".into(),
"See: https://tour.gleam.run/basics/equality".into(),
],
),
LexicalErrorType::InvalidOperator { expected } => (
"This is not a valid operator at this context",
vec![
"Hint: Is it a typo?".into(),
format!("Try: `{}` operator instead.", expected),
],
),
Copy link
Member

Choose a reason for hiding this comment

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

I'd personally define a specific InvalidTripleEqual error variant instead of a general InvalidOperator one since we don't currently have any uses for it besides the triple equal case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review updated the commit 👍🏽

Copy link
Member

Choose a reason for hiding this comment

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

No, thank you!

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! Very nice. Could you update the changelog file too?

@rabingaire rabingaire force-pushed the fix-triple-equals branch 3 times, most recently from cb9311e to 40620a5 Compare May 20, 2024 17:55
@rabingaire
Copy link
Contributor Author

Thank you! Very nice. Could you update the changelog file too?

Thank you, added the changelog 👍🏽

@rabingaire rabingaire requested a review from lpil May 20, 2024 18:01
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.

Wonderful! Thank you

@lpil lpil enabled auto-merge (rebase) May 20, 2024 18:20
@lpil lpil merged commit 4316a5c into gleam-lang:main May 20, 2024
12 checks passed
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.

Error message more helpful when having triple equals instead of double inside lambda fn
3 participants