shithub: mc

Download patch

ref: 0855d34c6585b6479f74f059a91c483c58016ea8
parent: e1dd87b2721b7b42e252d83330798e73daf48a27
author: Ori Bernstein <[email protected]>
date: Thu Jan 16 06:08:35 EST 2014

Install ucons for nested unions.

    This allows unions that are defined inline (eg, within a
    struct) to be created:

        type t = struct
            foo : union
                `Bar
                `Baz
            ;;
        ;;

    Now has properly installed ucons. Before this change, the
    union constructors would all have an id of 0, which would
    lead to them all being collapsed to the first entry.

--- a/parse/gram.y
+++ b/parse/gram.y
@@ -831,12 +831,20 @@
     if (!t)
         return;
     b = tybase(t);
-    if (b->type != Tyunion)
-        return;
-    for (i = 0; i < b->nmemb; i++) {
-        b->udecls[i]->utype = t;
-        b->udecls[i]->id = i;
-        putucon(st, b->udecls[i]);
+    switch (b->type) {
+        case Tystruct:
+            for (i = 0; i < b->nmemb; i++)
+                installucons(st, b->sdecls[i]->decl.type);
+            break;
+        case Tyunion:
+            for (i = 0; i < b->nmemb; i++) {
+                b->udecls[i]->utype = t;
+                b->udecls[i]->id = i;
+                putucon(st, b->udecls[i]);
+            }
+            break;
+        default:
+            break;
     }
 }
 
--- /dev/null
+++ b/test/nestucon.myr
@@ -1,0 +1,18 @@
+use std
+
+type t = struct
+	x : union
+		`Int int
+		`Str byte[:]
+	;;
+;;
+
+const main = {
+	var a : t
+
+	a = [.x = `Str "asdf"]
+	match a
+	| [.x=`Str s]:	std.put("%s\n", s)
+	;;
+}
+
--- a/test/tests
+++ b/test/tests
@@ -72,6 +72,7 @@
 B generic	E	42
 B genericval	E	42
 B trait-builtin	E	42
+B nestucon	P	asdf
 B mkunion	E	0
 B genericcall	E	42
 B generictype	E	0