Skip to content

Commit 6f972d7

Browse files
authored
Error messages: Improve "Somewhere wanted" messages (#6410)
* first batch of changes trying to improve the Somewhere wanted error messages * tweaks * add more fixtures * remove redundant text * add specialized errors for array items and setting record fields * tweak set record field message * hint about tuple in array message * move message handling to own file * cleanup * cleanup * cleanup * cleanup * changelog * add example of tuple to error message
1 parent a8c7f15 commit 6f972d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+558
-74
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
- A little performance improvement for JSX V4 runtime helper by removing one object allocation for components with key prop. https://github.com/rescript-lang/rescript-compiler/pull/6376
2929
- The error message for "toplevel expressions should evaluate to unit" has been revamped and improved. https://github.com/rescript-lang/rescript-compiler/pull/6407
30+
- Improve "Somewhere wanted" error messages by changing wording and adding more context + suggested solutions to the error messages where appropriate. https://github.com/rescript-lang/rescript-compiler/pull/6410
3031

3132
# 11.0.0-rc.3
3233

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/array_item_type_mismatch.res:1:16-22
4+
5+
1 │ let x = [1, 2, "hello"]
6+
2 │
7+
8+
This array item has type: string
9+
But this array is expected to have items of type: int
10+
11+
Arrays can only contain items of the same type.
12+
13+
Possible solutions:
14+
- Convert all values in the array to the same type.
15+
- Use a tuple, if your array is of fixed length. Tuples can mix types freely, and compiles to a JavaScript array. Example of a tuple: `let myTuple = (10, "hello", 15.5, true)
16+
17+
You can convert string to int with Belt.Int.fromString.

jscomp/build_tests/super_errors/expected/collections.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
3 │
88

99
This has type: string
10-
Somewhere wanted: int
10+
But it's expected to have type: int
1111

1212
You can convert string to int with Belt.Int.fromString.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/comparison_operator.res:3:17
4+
5+
1 │ let f = Some(0)
6+
2 │
7+
3 │ let x = 100 === f
8+
4 │
9+
10+
This has type: option<int>
11+
But it's being compared to something of type: int
12+
13+
You can only compare things of the same type.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/function_argument_mismatch.res:3:28-30
4+
5+
1 │ let makeName = (s, i) => s ++ i
6+
2 │
7+
3 │ let name = makeName("123", 123)
8+
4 │
9+
10+
This has type: int
11+
But this function argument is expecting: string
12+
13+
You can convert int to string with Belt.Int.toString.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/function_call_mismatch.res:6:3-10
4+
5+
4 │
6+
5 │ let cloneInTemp = (temp: string): string => {
7+
6 │ cd(temp)
8+
7 │ exec("git clone [email protected]:myorg/myrepo.git")
9+
8 │ }
10+
11+
This function call returns: string
12+
But it's expected to return: unit
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/function_return_mismatch.res:9:3-5
4+
5+
7 │
6+
8 │ let x = fnExpectingCleanup(() => {
7+
9 │ 123
8+
10 │ })
9+
11 │
10+
11+
This has type: int
12+
But it's expected to have type: cleanup (defined as unit => unit)

jscomp/build_tests/super_errors/expected/highlighting1.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
4 │
99

1010
This has type: string
11-
Somewhere wanted: int
11+
But it's expected to have type: int
1212

1313
You can convert string to int with Belt.Int.fromString.

jscomp/build_tests/super_errors/expected/highlighting2.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
5 ┆
1010

1111
This has type: string
12-
Somewhere wanted: int
12+
But it's expected to have type: int
1313

1414
You can convert string to int with Belt.Int.fromString.

jscomp/build_tests/super_errors/expected/highlighting3.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
5 │
1010

1111
This has type: string
12-
Somewhere wanted: int
12+
But it's expected to have type: int
1313

1414
You can convert string to int with Belt.Int.fromString.

0 commit comments

Comments
 (0)