Skip to content

Commit e1b0bef

Browse files
committed
Improve default argument type mismatch errors (#8389)
* Regenerate super errors test snapshots * Improve error for generated optional default match Signed-off-by: Christoph Knittel <ck@cca.io> * CHANGELOG --------- Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 52b690d commit e1b0bef

4 files changed

Lines changed: 20 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
- Improve error message for dependency without `rescript.json`. https://github.com/rescript-lang/rescript/pull/8292
4040
- Resolve workspace dependencies in editor analysis. https://github.com/rescript-lang/rescript/pull/8392
4141
- Improve deprecated attribute extraction and support record form. https://github.com/rescript-lang/rescript/pull/8396
42+
- Improve default argument type mismatch errors. https://github.com/rescript-lang/rescript/pull/8389
4243

4344
# 12.2.0
4445

compiler/ml/typecore.ml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,7 @@ and type_expect_ ?deprecated_context ~context ?in_function ?(recarg = Rejected)
24332433
in
24342434
let smatch =
24352435
Exp.match_ ~loc:sloc
2436+
~attrs:[(mknoloc "#optional_arg_default", PStr [])]
24362437
(Exp.ident ~loc (mknoloc (Longident.Lident "*opt*")))
24372438
scases
24382439
in
@@ -2521,13 +2522,15 @@ and type_expect_ ?deprecated_context ~context ?in_function ?(recarg = Rejected)
25212522
(* Note: val_caselist = [] and exn_caselist = [], i.e. a fully
25222523
empty pattern matching can be generated by Camlp4 with its
25232524
revised syntax. Let's accept it for backward compatibility. *)
2525+
let has_attr name =
2526+
Ext_list.exists sexp.pexp_attributes (fun ({txt}, _) -> txt = name)
2527+
in
25242528
let call_context =
2525-
if
2526-
Ext_list.exists sexp.pexp_attributes (fun ({txt}, _) ->
2527-
match txt with
2528-
| "let.unwrap" -> true
2529-
| _ -> false)
2530-
then `LetUnwrap
2529+
(* Optional-default lowering synthesizes a match expression, but from the
2530+
user's point of view this is still part of a function definition. Use
2531+
function-style diagnostics instead of reporting a phantom switch. *)
2532+
if has_attr "let.unwrap" then `LetUnwrap
2533+
else if has_attr "#optional_arg_default" then `Function
25312534
else `Switch
25322535
in
25332536
let val_cases, partial =
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/optional_default_arg_type_mismatch.res:1:29-30
4+
5+
1 │ let f = (~test: option<int>=42) => ()
6+
2 │
7+
8+
This has type: int
9+
But it's expected to have type: option<int>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let f = (~test: option<int>=42) => ()

0 commit comments

Comments
 (0)