commit f9465c3
shrub
·
2026-11-18 20:34:17 +0000 UTC
parent 62956d5
fix treating tuples in case as linear temportary + tilde fix
1 files changed,
+55,
-6
M
linear.c
M
linear.c
+55,
-6
1@@ -175,6 +175,56 @@ look(struct expr *e) {
2 }
3 }
4
5+static int
6+hastilde(struct pat *p) {
7+ struct pat *a;
8+
9+ if(p->tilde)
10+ return 1;
11+ if(p->kind == PTUPLE)
12+ for(a = p->args; a; a = a->next)
13+ if(hastilde(a))
14+ return 1;
15+ return 0;
16+}
17+
18+static struct lvar *
19+findvar(struct expr *e) {
20+ struct lvar *v;
21+
22+ if(e->kind != EIDENT)
23+ return nil;
24+ for(v = vars; v; v = v->next)
25+ if(v->binding == e->binding)
26+ return v;
27+ return nil;
28+}
29+
30+/* f or a case (a, b) of, borrow each tracked identifier in the tuple
31+ * instead of treating tuple as a linear temporary */
32+static void
33+borrowtuple(struct expr *e) {
34+ struct expr *a;
35+ struct lvar *v;
36+ struct lvar *saved[16];
37+ int old[16];
38+ int n = 0, i;
39+
40+ for(a = e->scrut->args; a; a = a->next) {
41+ look(a);
42+ v = findvar(a);
43+ if(v && n < 16) {
44+ saved[n] = v;
45+ old[n] = v->borrowed;
46+ n++;
47+ v->borrowed = 1;
48+ }
49+ }
50+ arms(e);
51+ for(i = n - 1; i >= 0; i--)
52+ saved[i]->borrowed = old[i];
53+}
54+
55 static void
56 use(struct expr *e) {
57 struct lvar *v;
58@@ -235,7 +285,7 @@ use(struct expr *e) {
59 break;
60 case ECASE:
61 for(r = e->arm; r; r = r->next) {
62- if(r->pat->tilde) {
63+ if(hastilde(r->pat)) {
64 tilde = 1;
65 break;
66 }
67@@ -245,12 +295,11 @@ use(struct expr *e) {
68 arms(e);
69 break;
70 }
71- v = nil;
72- if(e->scrut->kind == EIDENT) {
73- for(v = vars; v; v = v->next)
74- if(v->binding == e->scrut->binding)
75- break;
76+ if(e->scrut->kind == ETUPLE) {
77+ borrowtuple(e);
78+ break;
79 }
80+ v = findvar(e->scrut);
81 if(!v) {
82 /*look at it for a linear temporary like case f(l) of */
83 look(e->scrut);