ref: 32397d07defce76d42a038e8b6b339b8a0c14699
parent: 5317028311f7eda55ab588bfca93f27bbb5cd839
author: S. Gilles <sgilles@math.umd.edu>
date: Sat Jul 8 21:47:07 EDT 2017
Allow matching of empty structs and arrays
--- a/mi/match.c
+++ b/mi/match.c
@@ -246,9 +246,11 @@
return ret;
}
-static int isbasictype(Dtree *dt, Type *ty)
+static int isnonrecursive(Dtree *dt, Type *ty)
{
- return istyprimitive(ty) || ty->type == Tyvoid || ty->type == Tyfunc || ty->type == Typtr;
+ return istyprimitive(ty) || ty->type == Tyvoid || ty->type == Tyfunc ||
+ ty->type == Typtr || (ty->type == Tystruct && ty->nmemb == 0) ||
+ (ty->type == Tyarray && fold(ty->asize, 1)->expr.args[0]->lit.intval == 0);
}
static int ismatchable(Type *ty)
@@ -269,7 +271,7 @@
ty = tybase(ty);
if (ty->type == Typtr && start->any && start->any->ptrwalk) {
return addwildrec(loc, ty->sub[0], start->any, accept, end, nend);
- } else if (isbasictype(start, ty)) {
+ } else if (isnonrecursive(start, ty)) {
if (start->accept || start == accept)
return 0;
for (i = 0; i < start->nnext; i++)
--- /dev/null
+++ b/test/empty-struct.myr
@@ -1,0 +1,26 @@
+use std
+
+type foo = struct
+;;
+
+type bar = struct
+ baz : foo[:]
+ quux : foo[0][:]
+;;
+
+
+const main = {
+ var a : foo
+ var z : foo[0]
+ var b : bar = [.baz = [a, a][:], .quux = [z, z, z][:]]
+ var c : int = 0
+ for f in b.baz
+ c += 3
+ ;;
+
+ for f in b.quux
+ c += 5
+ ;;
+
+ std.exit(c)
+}
--- a/test/tests
+++ b/test/tests
@@ -144,6 +144,7 @@
B strjoin C
B exporttrait E 0
B local-labels E 10
+B empty-struct E 21
F declmismatch
F infermismatch
F usedef