Skip to content

Brush up a few object related error messages #7580

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 2 commits into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#### :nail_care: Polish

- Better error message for when trying to await something that is not a promise. https://github.com/rescript-lang/rescript/pull/7561
- Better error messages for object field missing and object field type mismatches. https://github.com/rescript-lang/rescript/pull/7580

#### :house: Internal

Expand Down
28 changes: 22 additions & 6 deletions compiler/ml/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1352,13 +1352,29 @@ let explanation unif t3 t4 ppf =
fprintf ppf "@,Self type cannot be unified with a closed object type"
| _, Tfield (lab, _, _, _) when lab = dummy_method ->
fprintf ppf "@,Self type cannot be unified with a closed object type"
| Tfield (l, _, _, {desc = Tnil}), Tfield (l', _, _, {desc = Tnil})
| Tfield (l, _, f1, {desc = Tnil}), Tfield (l', _, f2, {desc = Tnil})
when l = l' ->
fprintf ppf "@,Types for method %s are incompatible" l
| (Tnil | Tconstr _), Tfield (l, _, _, _) ->
fprintf ppf "@,@[The first object type has no field %s@]" l
| Tfield (l, _, _, _), (Tnil | Tconstr _) ->
fprintf ppf "@,@[The second object type has no field %s@]" l
fprintf ppf
"@,\
@,\
Types for field @{<info>\"%s\"@} are incompatible:@,\
Field @{<info>\"%s\"@} in the passed object has type @{<error>%a@}, but \
is expected to have type @{<info>%a@}."
l l type_expr f1 type_expr f2
| (Tnil | Tconstr _), Tfield (l, _, f1, _) ->
fprintf ppf
"@,\
@,\
@[The first object is expected to have a field @{<info>\"%s\"@} of type \
@{<info>%a@}, but it does not.@]"
l type_expr f1
| Tfield (l, _, f1, _), (Tnil | Tconstr _) ->
fprintf ppf
"@,\
@,\
@[The second object is expected to have a field @{<info>\"%s\"@} of \
type @{<info>%a@}, but it does not.@]"
l type_expr f1
| Tnil, Tconstr _ | Tconstr _, Tnil ->
fprintf ppf
"@,@[The %s object type has an abstract row, it cannot be closed@]"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

We've found a bug for you!
/.../fixtures/object_field_mismatch.res:11:20-22

9 │ }
10 │
11 │ let _ = doStuff(~ctx)
12 │ }
13 │

This has type: {"multiply": (string, string) => string}
But this function argument ~ctx is expecting:
{.."multiply": (int, int) => int}

Types for field "multiply" are incompatible:
Field "multiply" in the passed object has type (string, string) => string, but is expected to have type (int, int) => int.

You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

We've found a bug for you!
/.../fixtures/object_field_missing.res:11:20-22

9 │ }
10 │
11 │ let _ = doStuff(~ctx)
12 │ }
13 │

This has type: {"log": (string, string) => string}
But this function argument ~ctx is expecting:
{.."multiply": (int, int) => int}

The first object is expected to have a field "multiply" of type (int, int) => int, but it does not.
12 changes: 12 additions & 0 deletions tests/build_tests/super_errors/fixtures/object_field_mismatch.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let doStuff = (~ctx) => {
let multiply: (int, int) => int = ctx["multiply"]
multiply(1, 2)
}

let main = () => {
let ctx = {
"multiply": (a, b) => a ++ b,
}

let _ = doStuff(~ctx)
}
12 changes: 12 additions & 0 deletions tests/build_tests/super_errors/fixtures/object_field_missing.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let doStuff = (~ctx) => {
let multiply: (int, int) => int = ctx["multiply"]
multiply(1, 2)
}

let main = () => {
let ctx = {
"log": (a, b) => a ++ b,
}

let _ = doStuff(~ctx)
}