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

+1
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

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

+1-1
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.
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.
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.
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 git@github.com:myorg/myrepo.git")
9+
8 │ }
10+
11+
This function call returns: string
12+
But it's expected to return: unit
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

+1-1
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

+1-1
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

+1-1
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/highlighting5.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
3 │
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/if_branch_mismatch.res:4:3-5
4+
5+
2 │ "123"
6+
3 │ } else {
7+
4 │ 123
8+
5 │ }
9+
6 │
10+
11+
This has type: int
12+
But this if statement is expected to return: string
13+
14+
if expressions must return the same type in all branches (if, else if, else).
15+
16+
You can convert int to string with Belt.Int.toString.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/if_condition_mismatch.res:1:12-18
4+
5+
1 │ let x = if "horse" {
6+
2 │ ()
7+
3 │ }
8+
9+
This has type: string
10+
But if conditions must always be of type: bool
11+
12+
To fix this, change the highlighted code so it evaluates to a bool.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/math_operator_constant.res:3:15-17
4+
5+
1 │ let num = 0
6+
2 │
7+
3 │ let x = num + 12.
8+
4 │
9+
10+
This value has type: float
11+
But it's being used with the + operator, which works on: int
12+
13+
Floats and ints have their own mathematical operators. This means you cannot add a float and an int without converting between the two.
14+
15+
Possible solutions:
16+
- Ensure all values in this calculation has the type int. You can convert between floats and ints via Belt.Float.toInt and Belt.Int.fromFloat.
17+
- Make 12. an int by removing the dot or explicitly converting to int
18+
19+
You can convert float to int with Belt.Float.toInt.
20+
If this is a literal, try a number without a trailing dot (e.g. 20).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/math_operator_float.res:3:9-11
4+
5+
1 │ let num = 0
6+
2 │
7+
3 │ let x = num +. 12.
8+
4 │
9+
10+
This has type: int
11+
But it's being used with the +. operator, which works on: float
12+
13+
Floats and ints have their own mathematical operators. This means you cannot add a float and an int without converting between the two.
14+
15+
Possible solutions:
16+
- Ensure all values in this calculation has the type float. You can convert between floats and ints via Belt.Float.toInt and Belt.Int.fromFloat.
17+
- Change the operator to +, which works on int
18+
19+
You can convert int to float with Belt.Int.toFloat.
20+
If this is a literal, try a number with a trailing dot (e.g. 20.).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/math_operator_int.res:3:9-11
4+
5+
1 │ let num = 0.
6+
2 │
7+
3 │ let x = num + 12.
8+
4 │
9+
10+
This has type: float
11+
But it's being used with the + operator, which works on: int
12+
13+
Floats and ints have their own mathematical operators. This means you cannot add a float and an int without converting between the two.
14+
15+
Possible solutions:
16+
- Ensure all values in this calculation has the type int. You can convert between floats and ints via Belt.Float.toInt and Belt.Int.fromFloat.
17+
- Change the operator to +., which works on float
18+
19+
You can convert float to int with Belt.Float.toInt.
20+
If this is a literal, try a number without a trailing dot (e.g. 20).

jscomp/build_tests/super_errors/expected/primitives1.res.expected

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
2 │ 2. + 2
77
3 │
88

9-
This has type: float
10-
Somewhere wanted: int
9+
This value has type: float
10+
But it's being used with the + operator, which works on: int
11+
12+
Floats and ints have their own mathematical operators. This means you cannot add a float and an int without converting between the two.
13+
14+
Possible solutions:
15+
- Ensure all values in this calculation has the type int. You can convert between floats and ints via Belt.Float.toInt and Belt.Int.fromFloat.
16+
- Make 2. an int by removing the dot or explicitly converting to int
1117

1218
You can convert float to int with Belt.Float.toInt.
1319
If this is a literal, try a number without a trailing dot (e.g. 20).

jscomp/build_tests/super_errors/expected/primitives11.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
6 │
99

1010
This has type: b (defined as option<bb>)
11-
Somewhere wanted: a (defined as option<aa>)
11+
But it's expected to have type: a (defined as option<aa>)
1212

1313
The incompatible parts:
1414
bb (defined as option<int>) vs aa (defined as option<string>)

jscomp/build_tests/super_errors/expected/primitives2.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
3 │
88

99
This has type: int
10-
Somewhere wanted: string
10+
But this function argument is expecting: string
1111

1212
You can convert int to string with Belt.Int.toString.

jscomp/build_tests/super_errors/expected/primitives6.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
4 │
99

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

1313
You can convert int to float with Belt.Int.toFloat.
1414
If this is a literal, try a number with a trailing dot (e.g. 20.).

jscomp/build_tests/super_errors/expected/primitives7.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
4 │
99

1010
This has type: list<int>
11-
Somewhere wanted: list<float>
11+
But this function argument is expecting: list<float>
1212

1313
The incompatible parts:
1414
int vs float

jscomp/build_tests/super_errors/expected/primitives9.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
2 │
77

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

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

jscomp/build_tests/super_errors/expected/record_type_spreads_deep_sub.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
10 │
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/set_record_field_type_match.res:11:13-14
4+
5+
9 │ }
6+
10 │
7+
11 │ user.name = 12
8+
12 │
9+
10+
You're assigning something to this field that has type: int
11+
But this record field is of type: string
12+
13+
You can convert int to string with Belt.Int.toString.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/switch_different_types.res:7:10-23
4+
5+
5 │ switch foo {
6+
6 │ | "world" => ()
7+
7 │ | _ => someFunction()
8+
8 │ }
9+
9 │ }
10+
11+
This has type: string
12+
But this switch is expected to return: unit
13+
14+
All branches in a switch must return the same type. To fix this, change your branch to return the expected type.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/switch_guard.res:6:16-22
4+
5+
4 │ let bar = () => {
6+
5 │ switch foo {
7+
6 │ | "world" if "horse" => ()
8+
7 │ | _ => someFunction()
9+
8 │ }
10+
11+
This has type: string
12+
But if conditions must always be of type: bool
13+
14+
To fix this, change the highlighted code so it evaluates to a bool.

jscomp/build_tests/super_errors/expected/syntaxErrors4.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
23 │ /* */
1414

1515
This has type: string
16-
Somewhere wanted: int
16+
But it's expected to have type: int
1717

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

jscomp/build_tests/super_errors/expected/type1.res.expected

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
1 │ let x = 2. + 2
66
2 │
77

8-
This has type: float
9-
Somewhere wanted: int
8+
This value has type: float
9+
But it's being used with the + operator, which works on: int
10+
11+
Floats and ints have their own mathematical operators. This means you cannot add a float and an int without converting between the two.
12+
13+
Possible solutions:
14+
- Ensure all values in this calculation has the type int. You can convert between floats and ints via Belt.Float.toInt and Belt.Int.fromFloat.
15+
- Make 2. an int by removing the dot or explicitly converting to int
1016

1117
You can convert float to int with Belt.Float.toInt.
1218
If this is a literal, try a number without a trailing dot (e.g. 20).

jscomp/build_tests/super_errors/expected/type2.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
8 │
1010

1111
This has type: string
12-
Somewhere wanted: int
12+
But this function argument is expecting: int
1313

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

jscomp/build_tests/super_errors/expected/unicode_location.res.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
│ (unicode symbols of length 2)
88

99
This has type: int
10-
Somewhere wanted: string
10+
But this function argument is expecting: string
1111

1212
You can convert int to string with Belt.Int.toString.

0 commit comments

Comments
 (0)