Skip to content

Commit 5b68db0

Browse files
committed
fix subtype error messages and add fixtures
1 parent a29b675 commit 5b68db0

6 files changed

+55
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_pattern_type_spreads_not_subtype.res:7:5-13
4+
5+
5 │ let lookup = (b: b) =>
6+
6 │ switch b {
7+
7 │ | ...c as c => Js.log(c)
8+
8 │ | Four => Js.log("four")
9+
9 │ | Five => Js.log("five")
10+
11+
Type c is not a subtype of b
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_pattern_type_spreads_not_variant.res:7:8
4+
5+
5 │ let lookup = (b: b) =>
6+
6 │ switch b {
7+
7 │ | ...c as c => Js.log(c)
8+
8 │ | Four => Js.log("four")
9+
9 │ | Five => Js.log("five")
10+
11+
The type c
12+
is not a variant type
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type a = One | Two | Three
2+
type b = | ...a | Four | Five
3+
type c = Six | Seven
4+
5+
let lookup = (b: b) =>
6+
switch b {
7+
| ...c as c => Js.log(c)
8+
| Four => Js.log("four")
9+
| Five => Js.log("five")
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type a = One | Two | Three
2+
type b = | ...a | Four | Five
3+
type c = {name: string}
4+
5+
let lookup = (b: b) =>
6+
switch b {
7+
| ...c as c => Js.log(c)
8+
| Four => Js.log("four")
9+
| Five => Js.log("five")
10+
}

jscomp/ml/typecore.ml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,12 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
11791179
end
11801180
| Ppat_alias({ppat_desc=Ppat_type lid; ppat_attributes}, name) when Variant_coercion.has_res_pat_variant_spread_attribute ppat_attributes ->
11811181
let (_, p, ty) = build_or_pat_for_variant_spread !env loc lid expected_ty in
1182-
Ctype.subtype !env ty expected_ty ();
1182+
(try
1183+
Ctype.subtype !env ty expected_ty ()
1184+
with
1185+
Ctype.Subtype (tr1, tr2) ->
1186+
raise(Error(loc, !env, Not_subtype(tr1, tr2)))
1187+
);
11831188
assert (constrs = None);
11841189

11851190
let id = enter_variable ~is_as_variable:true loc name ty in
@@ -1515,7 +1520,12 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
15151520
in k p)
15161521
| Ppat_type lid when Variant_coercion.has_res_pat_variant_spread_attribute sp.ppat_attributes ->
15171522
let (path, p, ty) = build_or_pat_for_variant_spread !env loc lid expected_ty in
1518-
Ctype.subtype !env ty expected_ty ();
1523+
(try
1524+
Ctype.subtype !env ty expected_ty ()
1525+
with
1526+
Ctype.Subtype (tr1, tr2) ->
1527+
raise(Error(loc, !env, Not_subtype(tr1, tr2)))
1528+
);
15191529
k { p with pat_extra =
15201530
(Tpat_type (path, lid), loc, sp.ppat_attributes) :: p.pat_extra }
15211531
| Ppat_type lid ->

jscomp/test/VariantPatternMatchingSpreads.res

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
type a = One | Two | Three
2-
type a1 = One
32
type b = | ...a | Four | Five
43

54
let doWithA = (a: a) => {

0 commit comments

Comments
 (0)