shithub: mc

Download patch

ref: 0c0c4f2f6b3606ab6387b92cef21e166bc9c075b
parent: e943a39350eb2431da359e58ba563d74db89f06c
author: Ori Bernstein <ori@markovcorp.com>
date: Fri Oct 6 09:37:31 EDT 2017

Don't spuriously constrain types.

--- a/parse/infer.c
+++ b/parse/infer.c
@@ -33,7 +33,7 @@
 static void infernode(Node **np, Type *ret, int *sawret);
 static void inferexpr(Node **np, Type *ret, int *sawret);
 static void inferdecl(Node *n);
-static int tryconstrain(Type *ty, Trait *tr);
+static int tryconstrain(Type *ty, Trait *tr, int update);
 
 static Type *tf(Type *t);
 
@@ -782,7 +782,7 @@
 		if (!pat->trneed)
 			return 0;
 		for (i = 0; bsiter(pat->trneed, &i); i++)
-			if (!tryconstrain(to, traittab[i]))
+			if (!tryconstrain(to, traittab[i], 0))
 				return -1;
 		return 0;
 	} else if (pat->type == Tyvar) {
@@ -862,7 +862,7 @@
 }
 
 static int
-tryconstrain(Type *base, Trait *tr)
+tryconstrain(Type *base, Trait *tr, int update)
 {
 	Traitmap *tm;
 	Bitset *bs;
@@ -879,7 +879,8 @@
 			if (ty->type == Tyvar) {
 				if (!ty->trneed)
 					ty->trneed = mkbs();
-				bsput(ty->trneed, tr->uid);
+				if (update)
+					bsput(ty->trneed, tr->uid);
 				return 1;
 			} 
 			if (bshas(tm->traits, tr->uid))
@@ -916,7 +917,7 @@
 static void
 constrain(Node *ctx, Type *base, Trait *tr)
 {
-	if (!tryconstrain(base, tr))
+	if (!tryconstrain(base, tr, 1))
 		fatal(ctx, "%s needs trait %s near %s", tystr(base), namestr(tr->name), ctxstr(ctx));
 }