Skip to content

Improve various error messages #7500

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

Merged
merged 9 commits into from
May 22, 2025
Merged

Improve various error messages #7500

merged 9 commits into from
May 22, 2025

Conversation

zth
Copy link
Collaborator

@zth zth commented May 22, 2025

This explores how we can re-parse the relevant parts of the source where the error is, to give the viewer more concrete error messages (and suggestions for fixes) that we can't derive just from the type errors we see. It also improves a bunch of specific error messages:

  • Better JSX element mismatch messages (suggest how to convert int/float/string/array into Jsx.element)
  • Full rewrite suggestion for when passing an object to where a record is expected
  • Full rewrite suggestion when passing an array literal where a tuple is expected

Comment on lines +2 to +14
We've found a bug for you!
/.../fixtures/array_literal_passed_to_tuple.res:5:17-34

3 │ }
4 │
5 │ let x = doStuff(["hello", "world"])
6 │

This has type: array<'a>
But it's expected to have type: (string, string)

- Fix this by passing a tuple instead of an array, like: ("hello", "world")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When the user wrote an actual array literal, we can suggest the exact code for turning it into a tuple.

Copy link
Member

Choose a reason for hiding this comment

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

awesome!

Comment on lines +2 to +13
We've found a bug for you!
/.../fixtures/array_var_passed_to_tuple.res:7:17-18

5 │ let xx = ["hello", "world"]
6 │
7 │ let x = doStuff(xx)
8 │

This has type: array<string>
But this function argument is expecting: (string, string)

- Fix this by passing a tuple instead of an array
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When it's not an actual array literal, we just say that you need to pass a tuple instead.

@zth zth force-pushed the tuple-error-message-improvement branch from 5163b5f to 131c0ed Compare May 22, 2025 10:37
Comment on lines +12 to +17
You're passing a ReScript object where a record is expected. Objects are written with quoted keys, and records with unquoted keys.

Possible solutions:
- Rewrite the object to a record, like: {one: true}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Another example of how we can suggest an actual and correct rewrite here, since we now have the relevant expression to look at.

Copy link
Member

Choose a reason for hiding this comment

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

this is great too! We could also do the same thing for mismatch of objects vs dicts now that we have dict literals, they could become quite common for configs.

@zth
Copy link
Collaborator Author

zth commented May 22, 2025

@mediremi @nojaf FYI, the work in this PR can probably serve as a good foundation for "compiler error message driven code actions" that we've been exploring before. Just need to figure out how to integrate it in a good way.

Copy link

pkg-pr-new bot commented May 22, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7500

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7500

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7500

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7500

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7500

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7500

commit: 2f2dcb7

@zth zth changed the title [WIP] Improve array <-> tuple error message Improve various error messages May 22, 2025
@@ -41,6 +69,7 @@ let process_file sourcefile ?kind ppf =
properly
*)
setup_outcome_printer ();
Error_message_utils_support.setup ();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think process_file is the appropriate place to run this, but maybe there's a better place, idk.

@zth zth requested review from cristianoc, cknitt and tsnobip May 22, 2025 12:46
@zth zth marked this pull request as ready for review May 22, 2025 12:47
@zth
Copy link
Collaborator Author

zth commented May 22, 2025

@cknitt @tsnobip another round of error message feedback please! 😄

Copy link
Member

@tsnobip tsnobip left a comment

Choose a reason for hiding this comment

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

great work really @zth. We could also do the same thing for dicts now that we have dict literals that are kinda close to objects syntactically.

Comment on lines +2 to +14
We've found a bug for you!
/.../fixtures/array_literal_passed_to_tuple.res:5:17-34

3 │ }
4 │
5 │ let x = doStuff(["hello", "world"])
6 │

This has type: array<'a>
But it's expected to have type: (string, string)

- Fix this by passing a tuple instead of an array, like: ("hello", "world")

Copy link
Member

Choose a reason for hiding this comment

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

awesome!

Comment on lines +13 to +14
You need to unwrap this option to its underlying value first, then turn that value into a JSX element.
For None, you can use React.null to output nothing into JSX.
Copy link
Member

Choose a reason for hiding this comment

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

this is nice because not obvious for newcomers!

Comment on lines +12 to +17
You're passing a ReScript object where a record is expected. Objects are written with quoted keys, and records with unquoted keys.

Possible solutions:
- Rewrite the object to a record, like: {one: true}

Copy link
Member

Choose a reason for hiding this comment

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

this is great too! We could also do the same thing for mismatch of objects vs dicts now that we have dict literals, they could become quite common for configs.

@zth
Copy link
Collaborator Author

zth commented May 22, 2025

great work really @zth. We could also do the same thing for dicts now that we have dict literals that are kinda close to objects syntactically.

Oh, great idea.

Copy link
Member

@cknitt cknitt left a comment

Choose a reason for hiding this comment

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

Awesome stuff!

@zth
Copy link
Collaborator Author

zth commented May 22, 2025

@tsnobip what error messages would you add for dicts?

@tsnobip
Copy link
Member

tsnobip commented May 22, 2025

@tsnobip what error messages would you add for dicts?

@zth Something like this when an object literal is provided but it should actually be a dict:

 You're passing a �[1;31mReScript object�[0m where a �[1;dict�[0m is expected. Dict literals start with dict{.

  Possible solutions: 
  - Rewrite the object to a dict, like: �[1;33mdict{"one": true}

@zth
Copy link
Collaborator Author

zth commented May 22, 2025

@zth Something like this when an object literal is provided but it should actually be a dict:

 You're passing a �[1;31mReScript object�[0m where a �[1;dict�[0m is expected. Dict literals start with dict{.

  Possible solutions: 
  - Rewrite the object to a dict, like: �[1;33mdict{"one": true}

The rewrite part is harder with dict since we need to make sure that they'd be compatible. But I guess some variant of it could be done. Might do it in a follow up though.

@zth
Copy link
Collaborator Author

zth commented May 22, 2025

@tsnobip did a basic one in: d2cac67

We can refine it later.

@zth zth enabled auto-merge (squash) May 22, 2025 16:15
@zth zth force-pushed the tuple-error-message-improvement branch from 5c85e04 to 2f2dcb7 Compare May 22, 2025 16:17
@zth zth merged commit 2193869 into master May 22, 2025
16 checks passed
@zth zth deleted the tuple-error-message-improvement branch May 22, 2025 16:37
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.

3 participants