shithub: mc

Download patch

ref: 290709fdfc3fa92e84f9f2bf214c6a144f24c5ee
parent: 987d3f2938a42db4285417109fed8d4a82655676
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Sep 2 12:11:54 EDT 2017

Unjank trait resolution.

--- a/parse/gram.y
+++ b/parse/gram.y
@@ -31,7 +31,6 @@
 static Op binop(int toktype);
 static Node *mkpseudodecl(Srcloc l, Type *t);
 static void installucons(Stab *st, Type *t);
-static void addtrait(Type *t, char *str);
 static void setattrs(Node *dcl, char **attrs, size_t nattrs);
 static void setupinit(Node *n);
 
@@ -499,13 +498,13 @@
 	: Ttyparam {$$ = mktyparam($1->loc, $1->id);}
 	| Ttyparam Twith name {
 		$$ = mktyparam($1->loc, $1->id);
-		addtrait($$, $3->name.name);
+		lappend(&$$->traits, &$$->ntraits, $3);
 	}
 	| Ttyparam Twith Toparen typaramlist Tcparen {
 		size_t i;
 		$$ = mktyparam($1->loc, $1->id);
 		for (i = 0; i < $4.nn; i++)
-			addtrait($$, $4.nl[i]->name.name);
+			lappend(&$$->traits, &$$->ntraits, $4.nl[i]);
 	}
 	;
 
@@ -1075,21 +1074,6 @@
 	n->decl.isinit = 1;
 	n->decl.vis = Vishidden;
 	n->decl.name->name.name = strdup(s);
-}
-
-static void addtrait(Type *t, char *str)
-{
-	size_t i;
-
-	for (i = 0; i < ntraittab; i++) {
-		if (!strcmp(namestr(traittab[i]->name), str)) {
-			if (!t->trneed)
-				t->trneed = mkbs();
-			bsput(t->trneed, i);
-			return;
-		}
-	}
-	lfatal(t->loc, "Constraint %s does not exist", str);
 }
 
 static Node *mkpseudodecl(Srcloc l, Type *t)
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -480,6 +480,7 @@
 tyresolve(Type *t)
 {
 	size_t i;
+	Trait *tr;
 
 	if (t->resolved)
 		return;
@@ -521,6 +522,15 @@
 		break;
 	default:
 		break;
+	}
+
+	for (i = 0; i < t->ntraits; i++) {
+		tr = gettrait(curstab(), t->traits[i]);
+		if (!tr)
+			lfatal(t->loc, "trait %s does not exist", ctxstr(t->traits[i]));
+		if (!t->trneed)
+			t->trneed = mkbs();
+		bsput(t->trneed, tr->uid);
 	}
 
 	for (i = 0; i < t->nsub; i++) {
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -125,6 +125,9 @@
 	Vis vis;
 
 
+	Node **traits;		/* trait list */
+	size_t ntraits;		/* trait list size */
+
 	Type **gparam;		/* Tygeneric: type parameters that match the type args */
 	size_t ngparam;		/* Tygeneric: count of type parameters */
 	Type **arg;		/* Tyname: type arguments instantiated */