diff options
author | S. Gilles <sgilles@math.umd.edu> | 2017-07-09 01:47:07 -0400 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2017-07-08 23:44:21 -0700 |
commit | 32397d07defce76d42a038e8b6b339b8a0c14699 (patch) | |
tree | 13d72e46758f8c13f90a5082926b000d788ff086 | |
parent | 5317028311f7eda55ab588bfca93f27bbb5cd839 (diff) | |
download | mc-32397d07defce76d42a038e8b6b339b8a0c14699.tar.gz |
Allow matching of empty structs and arrays
-rw-r--r-- | mi/match.c | 8 | ||||
-rw-r--r-- | test/empty-struct.myr | 26 | ||||
-rw-r--r-- | test/tests | 1 |
3 files changed, 32 insertions, 3 deletions
@@ -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) +} @@ -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 |