summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorS. Gilles <sgilles@math.umd.edu>2017-07-09 01:47:07 -0400
committerOri Bernstein <ori@eigenstate.org>2017-07-08 23:44:21 -0700
commit32397d07defce76d42a038e8b6b339b8a0c14699 (patch)
tree13d72e46758f8c13f90a5082926b000d788ff086
parent5317028311f7eda55ab588bfca93f27bbb5cd839 (diff)
downloadmc-32397d07defce76d42a038e8b6b339b8a0c14699.tar.gz
Allow matching of empty structs and arrays
-rw-r--r--mi/match.c8
-rw-r--r--test/empty-struct.myr26
-rw-r--r--test/tests1
3 files changed, 32 insertions, 3 deletions
diff --git a/mi/match.c b/mi/match.c
index 29f8cc4..3d15962 100644
--- a/mi/match.c
+++ b/mi/match.c
@@ -246,9 +246,11 @@ static int acceptall(Dtree *t, Dtree *accept)
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 @@ static int addwildrec(Srcloc loc, Type *ty, Dtree *start, Dtree *accept, Dtree *
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++)
diff --git a/test/empty-struct.myr b/test/empty-struct.myr
new file mode 100644
index 0000000..a3ec75a
--- /dev/null
+++ b/test/empty-struct.myr
@@ -0,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)
+}
diff --git a/test/tests b/test/tests
index b7ab8e1..fcd058c 100644
--- a/test/tests
+++ b/test/tests
@@ -144,6 +144,7 @@ B strfind C
B strjoin C
B exporttrait E 0
B local-labels E 10
+B empty-struct E 21
F declmismatch
F infermismatch
F usedef