Skip to content

More error messages #7522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 3, 2025
Prev Previous commit
Next Next commit
track optional function arguments
  • Loading branch information
zth committed Jun 3, 2025
commit 9411f977b6c4c62d38c469da78336af11e558b39
1 change: 1 addition & 0 deletions compiler/ml/error_message_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ let type_clash_context_for_function_argument type_clash_context sarg0 =
Some txt
| _ -> None);
})
| None -> Some (FunctionArgument {optional = false})
| type_clash_context -> type_clash_context

let type_clash_context_maybe_option ty_expected ty_res =
Expand Down
4 changes: 3 additions & 1 deletion compiler/ml/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3657,7 +3657,9 @@ and type_application ~context total_app env funct (sargs : sargs) :
env sarg0 ty ty0
else fun () ->
option_some
(type_argument ~context env sarg0
(type_argument
~context:(Some (FunctionArgument {optional = true}))
env sarg0
(extract_option_type env ty)
(extract_option_type env ty0))) )
in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

We've found a bug for you!
/.../fixtures/optional_fn_argument_pass_option.res:5:18

3 │ let t = Some(1)
4 │
5 │ let f = optFn(~x=t)
6 │

This has type: option<int>
But this optional function argument is expecting: int

You're passing an optional value into an optional function argument.
Optional function arguments expect you to pass the concrete value, not an option, when passed directly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this wording is easy to understand. Maybe we can omit the "when passed directly"?


Possible solutions:
- Unwrap the option and pass a concrete value directly
- If you really do want to pass the optional value, prepend the value with ? to show you want to pass the option, like: ?t
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let optFn = (~x: option<int>=?) => x

let t = Some(1)

let f = optFn(~x=t)