Skip to content

Commit 7fe8df8

Browse files
committed
do not suggest coercion for constant expressions
1 parent 8b11764 commit 7fe8df8

File tree

4 files changed

+10
-19
lines changed

4 files changed

+10
-19
lines changed

compiler/ml/error_message_utils.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,16 @@ let print_extra_type_clash_help ~extract_concrete_typedecl ~env loc ppf
373373
| structure, comments -> Some (!Parser.reprint_source structure comments)
374374
in
375375
376-
if is_subtype then (
376+
(* Suggesting coercion only makes sense for non-constant values. *)
377+
let is_constant =
378+
match !Parser.parse_source target_expr_text with
379+
| ( [{Parsetree.pstr_desc = Pstr_eval ({pexp_desc = Pexp_constant _}, _)}],
380+
_ ) ->
381+
true
382+
| _ -> false
383+
in
384+
385+
if is_subtype && not is_constant then (
377386
fprintf ppf
378387
"@,\
379388
@,\

tests/build_tests/super_errors/expected/primitives1.res.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,5 @@
99
This has type: int
1010
But it's expected to have type: float
1111

12-
Possible solutions:
13-
- These types are compatible at runtime. You can use the coercion operator :> to convert to the expected type float.
14-
If you want to use coercion, rewrite the highlighted code to: (2 :> float)
15-

16-
17-
1812
You can convert int to float with Int.toFloat.
1913
If this is a literal, try a number with a trailing dot (e.g. 20.).

tests/build_tests/super_errors/expected/primitives6.res.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,5 @@
1010
This has type: int
1111
But it's expected to have type: float
1212

13-
Possible solutions:
14-
- These types are compatible at runtime. You can use the coercion operator :> to convert to the expected type float.
15-
If you want to use coercion, rewrite the highlighted code to: (10 :> float)
16-

17-
18-
1913
You can convert int to float with Int.toFloat.
2014
If this is a literal, try a number with a trailing dot (e.g. 20.).

tests/build_tests/super_errors/expected/type1.res.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,5 @@
88
This has type: int
99
But it's expected to have type: float
1010

11-
Possible solutions:
12-
- These types are compatible at runtime. You can use the coercion operator :> to convert to the expected type float.
13-
If you want to use coercion, rewrite the highlighted code to: (2 :> float)
14-

15-
16-
1711
You can convert int to float with Int.toFloat.
1812
If this is a literal, try a number with a trailing dot (e.g. 20.).

0 commit comments

Comments
 (0)