-
Notifications
You must be signed in to change notification settings - Fork 471
Dedicated error message for ternaries #7804
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2826,13 +2826,25 @@ and type_expect_ ~context ?in_function ?(recarg = Rejected) env sexp ty_expected | |
exp_env = env; | ||
} | ||
| Pexp_ifthenelse (scond, sifso, sifnot) -> ( | ||
(* TODO(attributes) Unify the attribute handling in the parser and rest of the compiler. *) | ||
let is_ternary = | ||
let rec has_ternary = function | ||
| [] -> false | ||
| ({Location.txt = "res.ternary"}, _) :: _ -> true | ||
| _ :: rest -> has_ternary rest | ||
in | ||
has_ternary sexp.pexp_attributes | ||
in | ||
let return_context = | ||
if is_ternary then Some TernaryReturn else Some IfReturn | ||
in | ||
Comment on lines
+2829
to
+2840
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting in my TODO to unify all of these attribute names across the syntax and compiler. A lot of them should probably be proper AST nodes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be great to track this in an issue instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done #7805 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
let cond = | ||
type_expect ~context:(Some IfCondition) env scond Predef.type_bool | ||
in | ||
match sifnot with | ||
| None -> | ||
let ifso = | ||
type_expect ~context:(Some IfReturn) env sifso Predef.type_unit | ||
type_expect ~context:return_context env sifso Predef.type_unit | ||
in | ||
rue | ||
{ | ||
|
@@ -2844,10 +2856,10 @@ and type_expect_ ~context ?in_function ?(recarg = Rejected) env sexp ty_expected | |
exp_env = env; | ||
} | ||
| Some sifnot -> | ||
let ifso = type_expect ~context:(Some IfReturn) env sifso ty_expected in | ||
let ifnot = type_expect ~context:(Some IfReturn) env sifnot ty_expected in | ||
let ifso = type_expect ~context:return_context env sifso ty_expected in | ||
let ifnot = type_expect ~context:return_context env sifnot ty_expected in | ||
(* Keep sharing *) | ||
unify_exp ~context:(Some IfReturn) env ifnot ifso.exp_type; | ||
unify_exp ~context:return_context env ifnot ifso.exp_type; | ||
re | ||
{ | ||
exp_desc = Texp_ifthenelse (cond, ifso, Some ifnot); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/ternary_branch_mismatch.res[0m:[2m1:24-26[0m | ||
|
||
[1;31m1[0m [2m│[0m let x = true ? "123" : [1;31m123[0m | ||
2 [2m│[0m | ||
|
||
This has type: [1;31mint[0m | ||
But this ternary is expected to return: [1;33mstring[0m | ||
|
||
Ternaries ([1;33m?[0m and [1;33m:[0m) must return the same type in both branches. | ||
|
||
You can convert [1;33mint[0m to [1;33mstring[0m with [1;33mInt.toString[0m. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let x = true ? "123" : 123 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic string "res.ternary" should be defined as a constant to avoid duplication and make it easier to maintain if the attribute name changes.
Copilot uses AI. Check for mistakes.