Skip to content

Commit 78415d5

Browse files
authored
Brush up a few object related error messages (#7580)
* brush up a few object related error messages * changelog
1 parent 3079bdb commit 78415d5

File tree

6 files changed

+80
-6
lines changed

6 files changed

+80
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#### :nail_care: Polish
2424

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

2728
#### :house: Internal
2829

compiler/ml/printtyp.ml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,13 +1352,29 @@ let explanation unif t3 t4 ppf =
13521352
fprintf ppf "@,Self type cannot be unified with a closed object type"
13531353
| _, Tfield (lab, _, _, _) when lab = dummy_method ->
13541354
fprintf ppf "@,Self type cannot be unified with a closed object type"
1355-
| Tfield (l, _, _, {desc = Tnil}), Tfield (l', _, _, {desc = Tnil})
1355+
| Tfield (l, _, f1, {desc = Tnil}), Tfield (l', _, f2, {desc = Tnil})
13561356
when l = l' ->
1357-
fprintf ppf "@,Types for method %s are incompatible" l
1358-
| (Tnil | Tconstr _), Tfield (l, _, _, _) ->
1359-
fprintf ppf "@,@[The first object type has no field %s@]" l
1360-
| Tfield (l, _, _, _), (Tnil | Tconstr _) ->
1361-
fprintf ppf "@,@[The second object type has no field %s@]" l
1357+
fprintf ppf
1358+
"@,\
1359+
@,\
1360+
Types for field @{<info>\"%s\"@} are incompatible:@,\
1361+
Field @{<info>\"%s\"@} in the passed object has type @{<error>%a@}, but \
1362+
is expected to have type @{<info>%a@}."
1363+
l l type_expr f1 type_expr f2
1364+
| (Tnil | Tconstr _), Tfield (l, _, f1, _) ->
1365+
fprintf ppf
1366+
"@,\
1367+
@,\
1368+
@[The first object is expected to have a field @{<info>\"%s\"@} of type \
1369+
@{<info>%a@}, but it does not.@]"
1370+
l type_expr f1
1371+
| Tfield (l, _, f1, _), (Tnil | Tconstr _) ->
1372+
fprintf ppf
1373+
"@,\
1374+
@,\
1375+
@[The second object is expected to have a field @{<info>\"%s\"@} of \
1376+
type @{<info>%a@}, but it does not.@]"
1377+
l type_expr f1
13621378
| Tnil, Tconstr _ | Tconstr _, Tnil ->
13631379
fprintf ppf
13641380
"@,@[The %s object type has an abstract row, it cannot be closed@]"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/object_field_mismatch.res:11:20-22
4+
5+
9 │ }
6+
10 │
7+
11 │ let _ = doStuff(~ctx)
8+
12 │ }
9+
13 │
10+
11+
This has type: {"multiply": (string, string) => string}
12+
But this function argument ~ctx is expecting:
13+
{.."multiply": (int, int) => int}
14+
15+
Types for field "multiply" are incompatible:
16+
Field "multiply" in the passed object has type (string, string) => string, but is expected to have type (int, int) => int.
17+
18+
You can convert string to int with Int.fromString.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/object_field_missing.res:11:20-22
4+
5+
9 │ }
6+
10 │
7+
11 │ let _ = doStuff(~ctx)
8+
12 │ }
9+
13 │
10+
11+
This has type: {"log": (string, string) => string}
12+
But this function argument ~ctx is expecting:
13+
{.."multiply": (int, int) => int}
14+
15+
The first object is expected to have a field "multiply" of type (int, int) => int, but it does not.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let doStuff = (~ctx) => {
2+
let multiply: (int, int) => int = ctx["multiply"]
3+
multiply(1, 2)
4+
}
5+
6+
let main = () => {
7+
let ctx = {
8+
"multiply": (a, b) => a ++ b,
9+
}
10+
11+
let _ = doStuff(~ctx)
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let doStuff = (~ctx) => {
2+
let multiply: (int, int) => int = ctx["multiply"]
3+
multiply(1, 2)
4+
}
5+
6+
let main = () => {
7+
let ctx = {
8+
"log": (a, b) => a ++ b,
9+
}
10+
11+
let _ = doStuff(~ctx)
12+
}

0 commit comments

Comments
 (0)