Skip to content

Commit faddac7

Browse files
cknittclaude
andauthored
Suggest a coercion to the whole expected type (#9053)
Why: The "compatible at runtime" hint checked the top-level types but printed the innermost mismatch, so its suggestion didn't compile. Diagnostic modified (coercion hint): Example: let consume = (_f: unit => Iterable.t<int>) => () let make: unit => Generator.t<int, unit, unit> = %raw(`function* () { yield 1 }`) let () = consume(make) Before (diagnostic): - These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (make :> iterable<int>) After (diagnostic): - These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (make :> unit => Iterable.t<int>) Fixes #9037 Claude-Session: https://claude.ai/code/session_01LZcKAmGvz6roKbTCq7or6f Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1178cb3 commit faddac7

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Suggest a coercion to the whole expected type, not the nested part that differs.

‎compiler/ml/error_message_utils.ml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,11 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
772772
with _ -> false)
773773
| None -> false
774774
in
775+
let target_type =
776+
match top_level_types with
777+
| Some (_, t2_top) -> t2_top
778+
| None -> target_type
779+
in
775780
let target_type_string = Format.asprintf "%a" type_expr target_type in
776781
let target_expr_text = Parser.extract_text_at_loc loc in
777782
let suggested_rewrite =
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/coercion_hint_nested_mismatch.res:3:18-21
4+
5+
1 │ let consume = (_f: unit => Iterable.t<int>) => ()
6+
2 │ let make: unit => Generator.t<int, unit, unit> = %raw(`function* () { yi
7+
│ eld 1 }`)
8+
3 │ let () = consume(make)
9+
4 │
10+
11+
This has type: unit => Generator.t<int, unit, unit>
12+
But this function argument is expecting: unit => Iterable.t<int>
13+
14+
The incompatible parts:
15+
Generator.t<int, unit, unit> (defined as Generator.t<int, unit, unit>) vs
16+
Iterable.t<int> (defined as iterable<int>)
17+
18+
Possible solutions:
19+
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (make :> unit => Iterable.t<int>)
20+

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let consume = (_f: unit => Iterable.t<int>) => ()
2+
let make: unit => Generator.t<int, unit, unit> = %raw(`function* () { yield 1 }`)
3+
let () = consume(make)

0 commit comments

Comments
 (0)