Skip to content

Fix coercion hint for parenthesized operands and aliased target types - #9118

Merged
cknitt merged 1 commit into
masterfrom
fix-9105-coercion-hint
Oct 29, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-9105-coercion-hint

Conversation

@cknitt

@cknitt cknitt commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

The "use the coercion operator" hint could suggest a rewrite that does not compile, and it named a different type than the error message right above it.

Suggestion after a parenthesized operand. The hint wraps the source text at the expression's location in (… :> T). The parser leaves the parentheses around an expression's first or last operand out of its location, so for v->(r => r) the text was v->(r => r and the suggestion v->(r => r :> b) does not type check. With parentheses on both ends, as in (c ? v : v)->(r => r), the cut-off text even parses again once wrapped. The hint now offers a concrete rewrite only when the text at the location parses by itself as one expression, and the hint parser hook rejects text that only parses with errors. Otherwise it falls back to the bare :> it already shows when no rewrite is available. Widening the location to the parentheses would give (v->(r => r) :> b), but the parser leaves them out on purpose (comment placement), and changing that would affect locations everywhere.

Expanded target type. The target type came from the expanded half of the trace pair, so Iterable.t<'a> was suggested as iterable<'a>. It now uses the unexpanded type, the one the message shows.

New super_errors fixtures:

  • coercion_hint_cut_off_operand.res: an operand cut off at its parentheses on both ends, (true ? v : v)->(r => r). Its wrapped text (true ? v : v)->(r => r :> b) parses, so it also rules out checking only the wrapped suggestion; the issue's v->(r => r) takes the same path.
  • coercion_hint_names_alias.res: the Generator.t to Iterable.t case, which should suggest Iterable.t<'a>.

Both fail without the fix.

For a cut-off operand, the hint falls back to the bare :> rather than rebuilding the full expression; widening the parser location over the adjacent parentheses would change locations everywhere, so this is not done.

Fixes #9105

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix the coercion hint after a parenthesized operand and name the expected type as the error message shows it.

Semantic Diff

1 commit · Full page

Suggest a coercion only for an operand that parses and name the shown type

dfe3118 · cknitt + Claude · Oct 28, 2026, 15:49 UTC

Why

The coercion hint wrapped the source text at the expression's location, which leaves out the parentheses around a first or last operand, so the suggestion did not compile (#9105). It also named the expanded target type instead of the one the message shows.

Diagnostic modified (coercion hint)

type a = {x: int, y: int}
type b = {x: int}
let v: a = {x: 1, y: 2}
let w: b = v->(r => r)

diagnostic

-- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: v->(r => r :> b)
+- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: :>

Diagnostic modified (coercion target)

let f = (x: Generator.t<'a, unit, unit>): Iterable.t<'a> => x

diagnostic

 But it's expected to have type: Iterable.t<'a> (defined as iterable<'a>)
 …
-You can use the coercion operator to convert to the expected type: (x :> iterable<'a>)
+You can use the coercion operator to convert to the expected type: (x :> Iterable.t<'a>)

Review

  • Why not widen the location to the parentheses? The parser leaves them out of locations on purpose, for comment placement, so a hint that cannot rebuild the operand falls back to the bare operator.

@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.47%. Comparing base (85f31fb) to head (dfe3118).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
compiler/ml/error_message_utils.ml 75.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9118   +/-   ##
=======================================
  Coverage   81.46%   81.47%           
=======================================
  Files         460      460           
  Lines       64675    64692   +17     
=======================================
+ Hits        52689    52707   +18     
+ Misses      11986    11985    -1     
Files with missing lines Coverage Δ
compiler/bsc/rescript_compiler_driver.ml 81.29% <100.00%> (+0.06%) ⬆️
compiler/ml/error_message_utils.ml 80.44% <75.00%> (+0.72%) ⬆️

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9118

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9118

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9118

@rescript/win32-x64

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

commit: dfe3118

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T16:42:18.550133Z 33823be Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 33823be87f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cknitt
cknitt force-pushed the fix-9105-coercion-hint branch from 33823be to 8c8747d Compare October 28, 2026 20:09
@cknitt
cknitt requested a review from cristianoc October 28, 2026 20:11
… type

Why:
    The coercion hint wrapped the source text at the expression's location,
    which leaves out the parentheses around a first or last operand, so the
    suggestion did not compile (#9105). It also named the expanded target
    type instead of the one the message shows.

Diagnostic modified (coercion hint):
Example:
    type a = {x: int, y: int}
    type b = {x: int}
    let v: a = {x: 1, y: 2}
    let w: b = v->(r => r)
Before (diagnostic):
    - These types are compatible at runtime. You can use the coercion operator to convert to the expected type: v->(r => r :> b)
After (diagnostic):
    - These types are compatible at runtime. You can use the coercion operator to convert to the expected type: :>

Diagnostic modified (coercion target):
Example:
    let f = (x: Generator.t<'a, unit, unit>): Iterable.t<'a> => x
Before (diagnostic):
    But it's expected to have type: Iterable.t<'a> (defined as iterable<'a>)
    …
    You can use the coercion operator to convert to the expected type: (x :> iterable<'a>)
After (diagnostic):
    But it's expected to have type: Iterable.t<'a> (defined as iterable<'a>)
    …
    You can use the coercion operator to convert to the expected type: (x :> Iterable.t<'a>)

Review: Why not widen the location to the parentheses? The parser leaves
    them out of locations on purpose, for comment placement, so a hint that
    cannot rebuild the operand falls back to the bare operator.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP
Signed-off-by: Christoph Knittel <[email protected]>
@cknitt
cknitt force-pushed the fix-9105-coercion-hint branch from 8c8747d to dfe3118 Compare October 28, 2026 20:13

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the clean-parse guard for extracted expressions, target-type alias selection and regression fixtures. No blocking issues found. CI, including super_errors and super_errors_multi, passes; validation was source review and CI, not a local rerun.

@cknitt
cknitt marked this pull request as ready for review October 29, 2026 05:48
@cknitt
cknitt merged commit 3562ce6 into master Oct 29, 2026
26 checks passed
@cknitt
cknitt deleted the fix-9105-coercion-hint branch October 29, 2026 05:48
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.

Coercion hint: suggestion that doesn't compile after a parenthesized operand, and the hint names the expanded type

2 participants