Skip to content

Commit b7cbac0

Browse files
committed
fix bug where suggestion to run coercion was shown even when coercion wasn't possible
1 parent f34ade1 commit b7cbac0

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

compiler/ml/error_message_utils.ml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ let extract_string_constant text =
233233
| _ -> None
234234
235235
let print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
236-
(bottom_aliases : (Types.type_expr * Types.type_expr) option)
236+
(bottom_aliases : (Types.type_expr * Types.type_expr) option) trace
237237
type_clash_context =
238238
match (type_clash_context, bottom_aliases) with
239239
| Some (MathOperator {for_float; operator; is_constant}), _ -> (
@@ -608,10 +608,16 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
608608
| None -> ())
609609
| None -> ())
610610
| _ -> ())
611-
| _, Some (t1, t2) ->
611+
| _, Some (_supplied_type, target_type) ->
612+
(* Coercion should always target the top level types. *)
613+
let top_level_types =
614+
match trace with
615+
| (_, t1_top) :: (_, t2_top) :: _ -> Some (t1_top, t2_top)
616+
| _ -> None
617+
in
612618
let can_show_coercion_message =
613-
match (t1.desc, t2.desc) with
614-
| Tvariant _, Tvariant _ ->
619+
match top_level_types with
620+
| Some ({Types.desc = Tvariant _}, {Types.desc = Tvariant _}) ->
615621
(* Subtyping polymorphic variants give some weird messages sometimes,
616622
so let's turn it off for now. For an example, turn them on again and try:
617623
```
@@ -620,13 +626,14 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
620626
```
621627
*)
622628
false
623-
| _ -> (
629+
| Some (t1, t2) -> (
624630
try
625631
Ctype.subtype env t1 t2 ();
626632
true
627633
with _ -> false)
634+
| None -> false
628635
in
629-
let target_type_string = Format.asprintf "%a" type_expr t2 in
636+
let target_type_string = Format.asprintf "%a" type_expr target_type in
630637
let target_expr_text = Parser.extract_text_at_loc loc in
631638
let suggested_rewrite =
632639
match

compiler/ml/typecore.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ let print_expr_type_clash ~context env loc trace ppf =
800800
| ppf -> error_type_text ppf context)
801801
(function ppf -> error_expected_type_text ppf context);
802802
print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
803-
bottom_aliases_result context;
803+
bottom_aliases_result trace context;
804804
show_extra_help ppf env trace
805805

806806
let report_arity_mismatch ~arity_a ~arity_b ppf =
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/dict_show_no_coercion.res:2:23-35
4+
5+
1 │ // This should not show coercion suggestion since just the inner types a
6+
│ re coercable, not the full type + expression (dict<float> -> dict<JSON.t
7+
│ >)
8+
2 │ let x: dict<JSON.t> = dict{"1": 1.}
9+
3 │
10+
11+
This has type: dict<float>
12+
But it's expected to have type: dict<JSON.t>
13+
14+
The incompatible parts:
15+
float vs JSON.t (defined as JSON.t)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This should not show coercion suggestion since just the inner types are coercable, not the full type + expression (dict<float> -> dict<JSON.t>)
2+
let x: dict<JSON.t> = dict{"1": 1.}

0 commit comments

Comments
 (0)