Skip to content

Commit

Permalink
broken poc of how one could leverage the runtime representation for e…
Browse files Browse the repository at this point in the history
…rror messages
  • Loading branch information
zth committed Sep 15, 2024
1 parent 1f1acaa commit 44c3fef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
16 changes: 16 additions & 0 deletions jscomp/ml/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,22 @@ let report_subtyping_error ppf env tr1 txt1 tr2 =
let tr1 = List.map prepare_expansion tr1
and tr2 = List.map prepare_expansion tr2 in
fprintf ppf "@[<v>%a" (trace true (tr2 = []) txt1) tr1;
(match tr1 with
| [(t1, _); (_, t2)] ->
let a_runtime_representation = Runtime_representation.to_runtime_representation t2 env in
let b_runtime_representation = Runtime_representation.to_runtime_representation t1 env in
a_runtime_representation |> List.iter(
fun a_value ->
b_runtime_representation |> List.iter(
fun b_value ->
if Runtime_representation.runtime_values_match a_value b_value then (
()
)
else Runtime_representation.explain_why_not_matching a_value b_value
|> List.iter(fun s -> fprintf ppf "@ %s" s)
))
| _ -> ()
);
if tr2 = [] then fprintf ppf "@]" else
let mis = mismatch tr2 in
fprintf ppf "%a%t@]"
Expand Down
8 changes: 8 additions & 0 deletions jscomp/ml/runtime_representation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ let rec to_runtime_representation (type_expr : Types.type_expr) (env : Env.t)
|> List.concat
| _ -> []

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

let runtime_values_match (a : runtime_js_value) (b : runtime_js_value) =
match (a, b) with
| StringLiteral {value = a_value}, StringLiteral {value = b_value} ->
Expand Down
14 changes: 4 additions & 10 deletions tst.res
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
type x = [#One | #Two]
type one = OK
type two = NOPE

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

let x: x = #One

let xx = #One({"hello": "hi"})

let y: y = One({hello: #hello})

let z = (x :> y)
let two = (one :> two)

0 comments on commit 44c3fef

Please sign in to comment.