Skip to content

Commit 44c3fef

Browse files
committed
broken poc of how one could leverage the runtime representation for error messages
1 parent 1f1acaa commit 44c3fef

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

jscomp/ml/printtyp.ml

+16
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,22 @@ let report_subtyping_error ppf env tr1 txt1 tr2 =
15231523
let tr1 = List.map prepare_expansion tr1
15241524
and tr2 = List.map prepare_expansion tr2 in
15251525
fprintf ppf "@[<v>%a" (trace true (tr2 = []) txt1) tr1;
1526+
(match tr1 with
1527+
| [(t1, _); (_, t2)] ->
1528+
let a_runtime_representation = Runtime_representation.to_runtime_representation t2 env in
1529+
let b_runtime_representation = Runtime_representation.to_runtime_representation t1 env in
1530+
a_runtime_representation |> List.iter(
1531+
fun a_value ->
1532+
b_runtime_representation |> List.iter(
1533+
fun b_value ->
1534+
if Runtime_representation.runtime_values_match a_value b_value then (
1535+
()
1536+
)
1537+
else Runtime_representation.explain_why_not_matching a_value b_value
1538+
|> List.iter(fun s -> fprintf ppf "@ %s" s)
1539+
))
1540+
| _ -> ()
1541+
);
15261542
if tr2 = [] then fprintf ppf "@]" else
15271543
let mis = mismatch tr2 in
15281544
fprintf ppf "%a%t@]"

jscomp/ml/runtime_representation.ml

+8
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ let rec to_runtime_representation (type_expr : Types.type_expr) (env : Env.t)
213213
|> List.concat
214214
| _ -> []
215215

216+
let explain_why_not_matching (a : runtime_js_value) (b : runtime_js_value) =
217+
match (a, b) with
218+
| StringLiteral {value = a_value}, StringLiteral {value = b_value} when a_value != b_value ->
219+
[Printf.sprintf "The left hand is will be the string '%s' in runtime, and the right hand will be '%s'." b_value a_value]
220+
| Any, _ -> ["We don't know what value left hand side would have at runtime."]
221+
| _, Any -> ["We don't know what value right hand side would have at runtime."]
222+
| _ -> []
223+
216224
let runtime_values_match (a : runtime_js_value) (b : runtime_js_value) =
217225
match (a, b) with
218226
| StringLiteral {value = a_value}, StringLiteral {value = b_value} ->

tst.res

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
type x = [#One | #Two]
1+
type one = OK
2+
type two = NOPE
23

3-
@tag("kind")
4-
type y = | @as("one") One({hello: [#hello]}) | @as(null) Two
4+
let one: one = OK
55

6-
let x: x = #One
7-
8-
let xx = #One({"hello": "hi"})
9-
10-
let y: y = One({hello: #hello})
11-
12-
let z = (x :> y)
6+
let two = (one :> two)

0 commit comments

Comments
 (0)