diff --git a/src/type.ml b/src/type.ml index 9604b955e46..395760d9459 100644 --- a/src/type.ml +++ b/src/type.ml @@ -628,6 +628,7 @@ let rec lub t1 t2 = | Prim Nat, Prim Int | Prim Int, Prim Nat -> Prim Int | Opt t1', Opt t2' -> Opt (lub t1' t2') + | Variant t1', Variant t2' -> Variant (lub_summands t1' t2') | Prim Null, Opt t' | Opt t', Prim Null -> Opt t' | Array t1', (Obj _ as t2) -> lub (array_obj t1') t2 @@ -635,6 +636,15 @@ let rec lub t1 t2 = | t1', t2' when eq t1' t2' -> t1 | _ -> Any +and lub_summands fs1 fs2 = match fs1, fs2 with + | fs1, [] -> fs1 + | [], fs2 -> fs2 + | f1::fs1', f2::fs2' -> + begin match compare_summand f1 f2 with + | 0 -> (fst f1, lub (snd f1) (snd f2))::lub_summands fs1' fs2' + | 1 -> f2::lub_summands fs1 fs2' + | _ -> f1::lub_summands fs1' fs2 + end let rec glb t1 t2 = if t1 == t2 then t1 else @@ -649,11 +659,21 @@ let rec glb t1 t2 = | Prim Nat, Prim Int | Prim Int, Prim Nat -> Prim Nat | Opt t1', Opt t2' -> Opt (glb t1' t2') + | Variant t1', Variant t2' -> Variant (glb_summands t1' t2') | Prim Null, Opt _ | Opt _, Prim Null -> Prim Null | t1', t2' when eq t1' t2' -> t1 | _ -> Non +and glb_summands fs1 fs2 = match fs1, fs2 with + | fs1, [] -> [] + | [], fs2 -> [] + | f1::fs1', f2::fs2' -> + begin match compare_summand f1 f2 with + | 0 -> (fst f1, glb (snd f1) (snd f2))::glb_summands fs1' fs2' + | 1 -> glb_summands fs1 fs2' + | _ -> glb_summands fs1' fs2 + end (* Pretty printing *) diff --git a/test/repl/ok/variant-shorthand.stderr.ok b/test/repl/ok/variant-shorthand.stderr.ok deleted file mode 100644 index 72004c6c942..00000000000 --- a/test/repl/ok/variant-shorthand.stderr.ok +++ /dev/null @@ -1 +0,0 @@ -stdin:3.1-3.72: warning, this array has type [Any] because elements have inconsistent types diff --git a/test/repl/ok/variant-shorthand.stdout.ok b/test/repl/ok/variant-shorthand.stdout.ok index 063251705ab..05fc885c24c 100644 --- a/test/repl/ok/variant-shorthand.stdout.ok +++ b/test/repl/ok/variant-shorthand.stdout.ok @@ -1,5 +1,5 @@ ActorScript 0.1 interpreter > #bar : {#bar} > #foo (#bar) : {#foo : {#bar}} -> [#Monday, #Tuesday, #Wednesday, #Thursday, #Friday, #Saturday, #Sunday] : [Any] +> [#Monday, #Tuesday, #Wednesday, #Thursday, #Friday, #Saturday, #Sunday] : [{#Friday; #Monday; #Saturday; #Sunday; #Thursday; #Tuesday; #Wednesday}] > diff --git a/test/run/variants.as b/test/run/variants.as index 61382baefe6..ecb986a7cea 100644 --- a/test/run/variants.as +++ b/test/run/variants.as @@ -51,4 +51,5 @@ assert (sayIcelandic (#Wednesday) == "Miưvikudagur"); assert (debug_show (#foo (#bar)) == "(#foo (#bar))"); -assert (([#Monday, #Tuesday, #Wednesday, #Thursday, #Friday, #Saturday, #Sunday] : [Weekday]).len() == 7); +assert ([#Monday, #Tuesday, #Wednesday, #Thursday, #Friday, #Saturday, #Sunday].len() == 7); +