diff options
Diffstat (limited to 'src/compiler')
| -rw-r--r-- | src/compiler/analyzer.zc | 38 | ||||
| -rw-r--r-- | src/compiler/base.zc | 11 |
2 files changed, 45 insertions, 4 deletions
diff --git a/src/compiler/analyzer.zc b/src/compiler/analyzer.zc index ff6a6c1..a1588e8 100644 --- a/src/compiler/analyzer.zc +++ b/src/compiler/analyzer.zc @@ -122,7 +122,7 @@ int shadow_finish(int from) { while (k < 24) { if ((e[2] >> k) & 1) flags = flags | effect_get(e[1], k); k++; } if ((flags & (EF_ALIAS | EF_IDENTITY)) && !(flags & EF_UNKNOWN) && !dry) { tunpack(e[3]); curinc = e[4]; panic = 0; - errat(); ended_message(e[6], e[7], e[8], e[9]); rt.put_s("; "); rt.put_s(sname[e[1]]); rt.put_s(", defined after this call, returns a borrow of its argument"); errend(); + errat(); ended_message(e[6], e[7], e[8], e[9], 0); rt.put_s("; "); rt.put_s(sname[e[1]]); rt.put_s(", defined after this call, returns a borrow of its argument"); errend(); } e = next; } @@ -1147,6 +1147,7 @@ int block_close(int save) { unused_check(save); exit_vars(fdepth, 1); lend_drop(fdepth, save); + block_end_owners(save); alias_end(fdepth, save); while (i < save) { if (lhome[i] >= fdepth) { lstate[i] = S_DEAD; lhome[i] = fdepth - 1; } i++; } local_drop(save); @@ -1155,6 +1156,26 @@ int block_close(int save) { return 0; } +// owners freed at this block's end: an outer alias of one, or an outer owner +// stored into one, dies with it and remembers where (why 9). +int block_end_owners(int save) { + int i = save, j; word info = 0; + if (!fchk || !live) return 0; + while (i < nloc) { + if (lhome[i] >= fdepth && lstate[i] == S_M && lown[i]) { + info = 0; j = lbase; + while (j < save) { + if (lstate[j] == S_CHILD && lroot(j) == i) { + if (!info) { info = talloc(6); info[0] = lname[i]; info[1] = lpos[i]; info[2] = tpack(); info[3] = 9; info[4] = 0; info[5] = info[2]; } + local_ended[j] = info[2]; local_end_info[j] = info; lstate[j] = S_DEAD; + } + j++; + } + } + i++; + } + return 0; +} // an alias in an outer block whose owner is about to disappear dies with it int alias_end(int dep, int save) { int i, r; @@ -1532,6 +1553,19 @@ int assigncheck(int k, int i, int rc) { else lstate[i] = S_PLAIN; return C_PLAIN; } + // an outer owner moved into a deeper container takes the container back: + // head = item after item.next = head. the container leaves its block. + if (st == S_CHILD && lown[i] && j >= 0 && lroot(i) == j && lstate[j] == S_M && lown[j] + && lhome[j] >= lhome[i] && !local_captured[i] && !local_captured[j]) { + int k = lbase; + while (k < nloc) { if (k != i && lstate[k] == S_CHILD && lowner[k] == j) lowner[k] = i; k++; } + lstate[i] = S_M; lused[i] = 0; + lnsz[i] = lnsz[j]; lleaf[i] = lleaf[j]; + local_ended[i] = 0; local_end_info[i] = 0; + end_why = 3; end_whom = lname[i]; end_owner(j); luse(j); ev_put(j, EV_MOVE, i); + if (ltag[i] & 15) lstate[i] = ltag[i] & 15; + return C_PLAIN; + } if (!empty(st)) { cerrn(i, "still owns an object; overwriting it would leak it"); return C_PLAIN; @@ -1587,7 +1621,7 @@ int assigncheck(int k, int i, int rc) { } else { end_why = 4; end_whom = lname[i]; end_owner(j); luse(j); ev_put(j, EV_STORE, i); emitown(i); } // storing it gives it away } else if (js != st) cerrn(j, "its tag doesn't match its container's (both have to be m, u or d)"); - else if (lhome[j] < lhome[r]) cerrn(j, "lives in an outer block than the container it's stored into"); + else if (lhome[j] < lhome[r] && (r != i || !lown[j] || (local_captured && (local_captured[i] || local_captured[j])))) cerrn(j, "lives in an outer block than the container it's stored into"); else { lstate[j] = S_CHILD; lowner[j] = i; luse(j); ev_put(j, EV_STORE, i); emitown(i); } return C_PLAIN; } diff --git a/src/compiler/base.zc b/src/compiler/base.zc index 7e4b5bc..de699aa 100644 --- a/src/compiler/base.zc +++ b/src/compiler/base.zc @@ -980,9 +980,16 @@ int cerr(char* msg) { } // how an owner ended, from end_owner's record: the use, the owner, what ended it and where. // "r: used after it was given away; r borrows t, and t was freed on line 5 (t declared on line 4)" -int ended_message(char* use, word info, char* root, int cause) { +int ended_message(char* use, word info, char* root, int cause, int own) { char* owner = root; int why = 0; char* whom = 0; if (info) { owner = info[0]; why = info[3]; whom = info[4]; dg_related(info[5], info[1], why, owner); } + if (why == 9) { + rt.put_s(use); rt.put_s(": "); + if (rt.eq_w(use, owner)) rt.put_s("was freed"); + else { if (own) rt.put_s("belongs to "); else rt.put_s("borrows from "); rt.put_s(owner); rt.put_s(", which was freed"); } + rt.put_s(" at the end of its block (line "); rt.put_n(cause & 0xFFFFFF); rt.put_c(')'); + return 0; + } rt.put_s(use); rt.put_s(": used after it was given away; "); if (!rt.eq_w(use, owner)) { rt.put_s(use); rt.put_s(" borrows "); rt.put_s(owner); rt.put_s(", and "); } rt.put_s(owner); @@ -1008,7 +1015,7 @@ int cerrn(int i, char* msg) { if (!info && local_end_info) info = local_end_info[root]; if (info) cause = info[2]; else if (local_ended) cause = local_ended[root]; } - if (cause && !panic && !dry) { errat(); dg_place(lreadpos[i]); ended_message(lname[i], info, lname[root], cause); errend(); } + if (cause && !panic && !dry) { errat(); dg_place(lreadpos[i]); ended_message(lname[i], info, lname[root], cause, lown[i]); errend(); } else serrn(lname[i], msg); } return 0; |
