Skip to content

Conversation

zth
Copy link
Member

@zth zth commented Aug 25, 2025

The general if error was used before (because that's what the ternary desugars to). This gives ternaries a dedicated error.

@zth zth requested review from Copilot, mediremi, nojaf and tsnobip August 25, 2025 13:43
Comment on lines +2829 to +2840
(* 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
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

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

Would be great to track this in an issue instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done #7805

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a dedicated error message for ternary operators, distinguishing them from general if statement errors. Previously, ternary expressions used the same error messages as if statements since they desugar to if-then-else constructs.

  • Adds a new TernaryReturn context type to differentiate ternary expressions from if statements
  • Updates error message formatting to specifically mention ternaries with ? and : operators
  • Adds test case demonstrating the new error message for type mismatches in ternary branches

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
compiler/ml/typecore.ml Detects ternary expressions via attributes and uses TernaryReturn context instead of IfReturn
compiler/ml/error_message_utils.ml Adds TernaryReturn context type and corresponding error message formatting
tests/build_tests/super_errors/fixtures/ternary_branch_mismatch.res Test fixture with ternary type mismatch
tests/build_tests/super_errors/expected/ternary_branch_mismatch.res.expected Expected error output showing the new ternary-specific error message

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +2830 to +2833
let is_ternary =
let rec has_ternary = function
| [] -> false
| ({Location.txt = "res.ternary"}, _) :: _ -> true
Copy link

Copilot AI Aug 25, 2025

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.

Suggested change
let is_ternary =
let rec has_ternary = function
| [] -> false
| ({Location.txt = "res.ternary"}, _) :: _ -> true
let res_ternary_attribute = "res.ternary" in
let is_ternary =
let rec has_ternary = function
| [] -> false
| ({Location.txt = res_ternary_attribute}, _) :: _ -> true

Copilot uses AI. Check for mistakes.

Copy link
Member

@mediremi mediremi left a comment

Choose a reason for hiding this comment

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

Nice improvement 💪

@zth zth enabled auto-merge (squash) August 25, 2025 14:25
Copy link

pkg-pr-new bot commented Aug 25, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7804

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7804

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7804

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7804

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7804

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7804

commit: a080c4a

@zth zth merged commit 3931a96 into master Aug 25, 2025
36 of 37 checks passed
Copy link
Member

@tsnobip tsnobip left a comment

Choose a reason for hiding this comment

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

Nice improvement! Thanks @zth !

@zth zth deleted the error-message-ternary branch August 25, 2025 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants