Skip to content

Commit

Permalink
Jib: Avoid ctyp_suprema on monomorphic parts of polymorphic constructors
Browse files Browse the repository at this point in the history
Fixes #401
  • Loading branch information
Alasdair committed Dec 15, 2023
1 parent dffe98c commit 134b2d8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/lib/jib_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -669,17 +669,17 @@ module Make (C : CONFIG) = struct
(string_of_ctyp (cval_ctyp cval))
(string_of_ctyp (ctyp_of_typ ctx variant_typ))
)
)
else ();
);
let unifiers, ctor_ctyp =
let generic_ctors = Bindings.find var_id ctx.variants |> snd |> Bindings.bindings in
let unifiers =
ctyp_unify l (CT_variant (var_id, generic_ctors)) (cval_ctyp cval) |> KBindings.bindings |> List.map snd
in
let is_poly_ctor =
List.exists (fun (id, ctyp) -> Id.compare id ctor = 0 && is_polymorphic ctyp) generic_ctors
in
(unifiers, if is_poly_ctor then ctyp_suprema pat_ctyp else pat_ctyp)
match List.find_opt (fun (id, ctyp) -> Id.compare id ctor = 0 && is_polymorphic ctyp) generic_ctors with
| Some (_, poly_ctor_ctyp) ->
let instantiated_parts = KBindings.map ctyp_suprema (ctyp_unify l poly_ctor_ctyp pat_ctyp) in
(unifiers, subst_poly instantiated_parts poly_ctor_ctyp)
| None -> (unifiers, pat_ctyp)
in
let pre, instrs, cleanup, ctx =
compile_match ctx apat (V_ctor_unwrap (cval, (ctor, unifiers), ctor_ctyp)) case_label
Expand Down
1 change: 1 addition & 0 deletions test/c/issue401.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ok
20 changes: 20 additions & 0 deletions test/c/issue401.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
default Order dec
$include <prelude.sail>

union U('t : Type) = {
A : ('t, bits(1))
}

val f : U(unit) -> bits(1)
function f(u) = {
match u {
A(_, bs) => bs
}
}

val main : unit -> unit
function main () = {
let u : U(unit) = A((), 0b1) in
let res = f(u) in
print_endline("ok");
}

0 comments on commit 134b2d8

Please sign in to comment.