Skip to content

More error messages #7522

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 17 commits into from
Jun 3, 2025
Merged

More error messages #7522

merged 17 commits into from
Jun 3, 2025

Conversation

zth
Copy link
Collaborator

@zth zth commented May 26, 2025

A bunch of more error message improvements.

Copy link

pkg-pr-new bot commented May 26, 2025

Open in StackBlitz

rescript

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

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/win32-x64

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

commit: 1171e16

@zth zth force-pushed the refactor-error-msg-context branch from 9f79885 to b88d643 Compare May 27, 2025 07:43
Comment on lines +583 to +600
match p with
| Path.Pdot (Path.Pident {Ident.name = jsx_module_name}, "fragmentProps", _)
when Some jsx_module_name = !configured_jsx_module ->
Some {props_record_path = p; fields = []; jsx_type = `Fragment}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This logic isn't perfect, and at some point we probably need to figure out a better approach for matching on "what is a JSX component" in order to give better error messages. But for now with the state we're in, this is an improvement.

when Some jsx_module_name = !configured_jsx_module ->
Some {props_record_path = p; fields = []; jsx_type = `Fragment}
| _ -> (
(* TODO: handle lowercase components using JSXDOM.domProps *)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Leaving this for a follow up.

@zth zth changed the title [WIP] Refactor error msg context More error messages May 30, 2025
@zth zth marked this pull request as ready for review May 30, 2025 19:02
@zth
Copy link
Collaborator Author

zth commented May 30, 2025

@cknitt @tsnobip could you look over the messages/wording please? 🙏

@cristianoc could you have a general look at the code? I tried to make passing the context more explicit to make it simpler to follow the logic, and to add new cases as needed.

@zth zth requested review from cknitt, tsnobip and cristianoc May 30, 2025 19:03
@cristianoc
Copy link
Collaborator

@cknitt @tsnobip could you look over the messages/wording please? 🙏

@cristianoc could you have a general look at the code? I tried to make passing the context more explicit to make it simpler to follow the logic, and to add new cases as needed.

The code looks good. Augmenting the error info with more details seems pretty robust.

@@ -2637,7 +2651,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
}
| Pexp_record (lid_sexp_list, Some sexp) ->
assert (lid_sexp_list <> []);
let exp = type_exp ~recarg env sexp in
let exp = type_exp ~context:None ~recarg env sexp in
Copy link
Collaborator

Choose a reason for hiding this comment

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

@zth this is where I believe one can pass context to special case error messages for e.g. optional fields.

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.

Nice work!
Here are some suggestions for the messages.

But this optional function argument is expecting: int

You're passing an optional value into an optional function argument.
Optional function arguments expect you to pass the concrete value, not an option, when passed directly.
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if this wording is easy to understand. Maybe we can omit the "when passed directly"?

@zth zth force-pushed the refactor-error-msg-context branch from 2bcdff7 to 86f63f3 Compare June 2, 2025 15:48
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.

kudos @zth, great QoL improvements! Just a few comments, mostly nitpicking, so you can entirely decide to ignore them ^^

@@ -9,6 +9,6 @@
15 ┆ otherExtra: Some({test: true, anotherInlined: {record: true}}),

This has type: int
But it's expected to have type: string
But the record field age is expected to have type: string
Copy link
Member

Choose a reason for hiding this comment

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

hell yeah, there were way too many "this" and "it"s

25 │

This has type: float
But children passed to this component are expected to have type:
Copy link
Member

Choose a reason for hiding this comment

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

I understand why @cknitt corrected "is" to "are", but I still find it a bit confusing to use the plural for something that can be singular so we could maybe use a more neutral verb

Suggested change
But children passed to this component are expected to have type:
But children passed to this component must be of have type:

or we could even keep the default wording:

Suggested change
But children passed to this component are expected to have type:
But the component prop children is expected to have type:

But definitely nitpicking here!

Comment on lines 11 to 12
But children of JSX fragments are expected to have type:
React.element (defined as Jsx.element)
Copy link
Member

Choose a reason for hiding this comment

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

it's less true here since fragments only accept React.element children that can indeed be plural.

But this optional function argument ~x is expecting: int

You're passing an optional value into an optional function argument.
Optional function arguments expect you to pass the concrete value, not an option.
Copy link
Member

Choose a reason for hiding this comment

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

nitpicking again, but I'm not a big fan of concrete here, especially since sometimes, the concrete value that is expected can actually be an option, maybe use "unwrapped" instead? I won't start a war over this, it's really ok as is too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No that's great! I'm really not sure how to word this to make it clear, so all suggestions are great.

Comment on lines +10 to +16
This has type: string
But this try/catch is expected to return: int

The try body and the catch block must return the same type.
To fix this, change your try/catch blocks to return the expected type.

You can convert string to int with Int.fromString.
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 a great improvement!

@zth zth requested review from cknitt and tsnobip June 2, 2025 17:03
@zth
Copy link
Collaborator Author

zth commented Jun 2, 2025

@tsnobip @cknitt I've tweaked the wording some. Could you have another look?

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.

Looks good to me!

But this optional function argument ~x is expecting: int

You're passing an optional value into an optional function argument.
Optional function arguments expect you to pass a non-optional value.
Copy link
Member

Choose a reason for hiding this comment

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

As I said, an optional parameter can expect an optional type in some cases, but I can't find a better wording and in 99% of the cases it's indeed a non optional value, so let's keep this for now!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can add a condition to check that the field isn't expecting an option per se.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, it'd be better if we could just adjust the wording to be generic enough to cover both cases. I'll try and think of something. Maybe @cknitt or @cristianoc has ideas?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If the argument had option type we wouldn't have this error (except if we're passing nested when a single is expected).

Attempt: values passed to optional arguments don't need to be wrapped into an option.
You might need to adjust the type of the value supplied: avoid wrapping the value into an option, or unwrap the option from the value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's great wording @cristianoc , thanks!

@zth zth force-pushed the refactor-error-msg-context branch from 32ccee2 to f2d3278 Compare June 3, 2025 08:50
@zth zth enabled auto-merge (squash) June 3, 2025 08:50
@zth zth disabled auto-merge June 3, 2025 08:52
@zth zth enabled auto-merge (squash) June 3, 2025 09:01
@zth zth merged commit a5b8f7a into master Jun 3, 2025
16 checks passed
@zth zth deleted the refactor-error-msg-context branch June 3, 2025 09: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.

4 participants