Skip to content

Commit 82c288e

Browse files
committed
fix false positive in variant to primitive branch, and add tests
1 parent cedf6ac commit 82c288e

10 files changed

+70
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_to_variant_coercion.res:6:10-15
4+
5+
4 │ let x: x = One(true)
6+
5 │
7+
6 │ let y = (x :> y)
8+
7 │
9+
10+
Type x is not a subtype of y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_to_variant_coercion_as.res:6:10-15
4+
5+
4 │ let x: x = One(true)
6+
5 │
7+
6 │ let y = (x :> y)
8+
7 │
9+
10+
Type x is not a subtype of y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_to_variant_coercion_tag.res:6:10-15
4+
5+
4 │ let x: x = One(true)
6+
5 │
7+
6 │ let y = (x :> y)
8+
7 │
9+
10+
Type x is not a subtype of y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_to_variant_coercion_unboxed.res:6:10-15
4+
5+
4 │ let x: x = One(true)
6+
5 │
7+
6 │ let y = (x :> y)
8+
7 │
9+
10+
Type x is not a subtype of y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type x = One(bool) | Two
2+
type y = One(string) | Two
3+
4+
let x: x = One(true)
5+
6+
let y = (x :> y)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type x = | @as("one") One(bool) | Two(string)
2+
type y = One(bool) | Two(string)
3+
4+
let x: x = One(true)
5+
6+
let y = (x :> y)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@tag("kind") type x = One(bool) | Two(string)
2+
type y = One(bool) | Two(string)
3+
4+
let x: x = One(true)
5+
6+
let y = (x :> y)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@unboxed type x = One(bool) | Two
2+
type y = One(bool) | Two
3+
4+
let x: x = One(true)
5+
6+
let y = (x :> y)

jscomp/ml/ctype.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -3952,10 +3952,10 @@ let rec subtype_rec env trace t1 t2 cstrs =
39523952
| (Tconstr(p1, _, _), _) when generic_private_abbrev env p1 ->
39533953
subtype_rec env trace (expand_abbrev_opt env t1) t2 cstrs
39543954
| (Tconstr(_, [], _), Tconstr(path, [], _)) when Variant_coercion.can_coerce_path path &&
3955-
extract_concrete_typedecl env t1 |> Variant_coercion.is_variant_typedecl |> Option.is_some
3956-
->
3955+
extract_concrete_typedecl env t1 |> Variant_coercion.can_try_coerce_variant_to_primitive |> Option.is_some
3956+
->
39573957
(* type coercion for variants to primitives *)
3958-
(match Variant_coercion.is_variant_typedecl (extract_concrete_typedecl env t1) with
3958+
(match Variant_coercion.can_try_coerce_variant_to_primitive (extract_concrete_typedecl env t1) with
39593959
| Some constructors ->
39603960
if constructors |> Variant_coercion.can_coerce_variant ~path then
39613961
cstrs

jscomp/ml/variant_coercion.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ let can_coerce_variant ~(path : Path.t)
4040
then true
4141
else false
4242

43-
let is_variant_typedecl
44-
((_, _, typedecl) : Path.t * Path.t * Types.type_declaration) =
43+
let can_try_coerce_variant_to_primitive
44+
((_, p, typedecl) : Path.t * Path.t * Types.type_declaration) =
4545
match typedecl with
46-
| {type_kind = Type_variant constructors} -> Some constructors
46+
| {type_kind = Type_variant constructors; type_params=[]} when Path.name p <> "bool"-> Some constructors
4747
| _ -> None
4848

4949
let variant_representation_matches (c1_attrs : Parsetree.attributes)

0 commit comments

Comments
 (0)