Skip to content

Commit b167877

Browse files
committed
track optional function arguments
1 parent 59b0b9c commit b167877

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

compiler/ml/error_message_utils.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ let type_clash_context_for_function_argument type_clash_context sarg0 =
527527
Some txt
528528
| _ -> None);
529529
})
530+
| None -> Some (FunctionArgument {optional = false})
530531
| type_clash_context -> type_clash_context
531532
532533
let type_clash_context_maybe_option ty_expected ty_res =

compiler/ml/typecore.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3654,7 +3654,9 @@ and type_application ~context total_app env funct (sargs : sargs) :
36543654
env sarg0 ty ty0
36553655
else fun () ->
36563656
option_some
3657-
(type_argument ~context env sarg0
3657+
(type_argument
3658+
~context:(Some (FunctionArgument {optional = true}))
3659+
env sarg0
36583660
(extract_option_type env ty)
36593661
(extract_option_type env ty0))) )
36603662
in
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/optional_fn_argument_pass_option.res:5:18
4+
5+
3 │ let t = Some(1)
6+
4 │
7+
5 │ let f = optFn(~x=t)
8+
6 │
9+
10+
This has type: option<int>
11+
But this optional function argument is expecting: int
12+
13+
You're passing an optional value into an optional function argument.
14+
Optional function arguments expect you to pass the concrete value, not an option, when passed directly.
15+
16+
Possible solutions:
17+
- Unwrap the option and pass a concrete value directly
18+
- If you really do want to pass the optional value, prepend the value with ? to show you want to pass the option, like: ?t
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let optFn = (~x: option<int>=?) => x
2+
3+
let t = Some(1)
4+
5+
let f = optFn(~x=t)

0 commit comments

Comments
 (0)