ref: 1441fb3b590d17f08cd3d4bc19abfd6b92e85b98
parent: b7ad8cdab0be770c844a9e1054c4d7f63773abdf
author: Ori Bernstein <[email protected]>
date: Sun Jun 17 19:08:42 EDT 2012
Add test of string indexing, and make it work We didn't properly constrain stuff.
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -153,6 +153,18 @@
return s;
}
+
+static void constrainby(Node *ctx, Type *a, Cstr *c)
+{
+ if (a->type == Tyvar) {
+ if (!a->cstrs)
+ a->cstrs = mkbs();
+ constrain(a, c);
+ } else if (!bshas(a->cstrs, c->cid)) {
+ fatal(ctx->line, "%s needs %s near %s", tystr(a), c->name, ctxstr(ctx));
+ }
+}
+
static void mergecstrs(Node *ctx, Type *a, Type *b)
{
if (b->type == Tyvar) {
@@ -349,9 +361,10 @@
settype(n, t);
break;
case Oidx: /* @a[@b::tcint] -> @a */
- t = mktyidxhack(n->line, type(args[1]));
+ t = mktyidxhack(n->line, mktyvar(n->line));
unify(n, type(args[0]), t);
- settype(n, type(args[1]));
+ constrainby(n, type(args[1]), cstrtab[Tcint]);
+ settype(n, tf(t->sub[0]));
break;
case Oslice: /* @a[@b::tcint,@b::tcint] -> @a[,] */
t = mktyidxhack(n->line, type(args[1]));
--- a/test/tests
+++ b/test/tests
@@ -31,6 +31,7 @@
B structasn E 42
B log-and E 0
B log-or E 1
+B str E 102
F declmismatch
F infermismatch
F flow