diff --git a/docs/osc/OSC-1-DESIGN.md b/docs/osc/OSC-1-DESIGN.md index 9be6cf84d..e93b500d4 100644 --- a/docs/osc/OSC-1-DESIGN.md +++ b/docs/osc/OSC-1-DESIGN.md @@ -127,7 +127,7 @@ argument for the duration of a call) holds a borrow of one or more owners. a move, any use of `a` (read, index, borrow, move) is refused: **use after move**. A move inside a loop body of an owner declared outside that loop is refused (use after move on the next iteration). A move on only some paths of - an `if` is refused (`conditional move`): OSC-1 has no drop flags. + an `if` is refused (`conditional move`): OSC-1 has no drop flags (superseded by OSC-3 item 3, drop flags). - Shared borrow `&a`: allowed while `a` is owned and not mutably borrowed. - Mutable borrow `&mut a`: `a` must be owned and not borrowed at all; through a borrow binding `r: &mut`, `&mut r` is a reborrow of the same target. diff --git a/docs/osc/OSC-3-DESIGN.md b/docs/osc/OSC-3-DESIGN.md index 7b3f23d08..abafc0c9f 100644 --- a/docs/osc/OSC-3-DESIGN.md +++ b/docs/osc/OSC-3-DESIGN.md @@ -185,6 +185,106 @@ runtime model replay: ... rejected=0 checked by the item 2 receipt against the OSC-2 baseline). - The exact receipt is under `evidence/OSC-3/receipts/osc3-handles-*.json`. +## Item 3: drop flags (conditional moves) + +OSC-1 and OSC-2 refused any move of an owner inside an `if` +(`CONDITIONAL_MOVE`, OSC-1-DESIGN "no drop flags"). Item 3 accepts it: an +owner that is moved on some paths and not on others gets a hidden drop flag, +and its scope-end release runs only when the flag says live. This supersedes +the OSC-1 rule; `CONDITIONAL_MOVE` keeps its number but is no longer emitted. + +### Subset + +- Owners are unique array owners (`own [T; N]`) and unique struct owners + (`own S`), declared by `let` (alloc, struct literal or let-move) or as an + `own` parameter. Arena owners (`in r`) are unchanged: they cannot be moved + (`ARENA_MOVE`), so they never need a flag. Handles (item 2) own nothing + and are out of scope. +- A move is a let-move (`let b: own T = a;`) or passing the owner as an + `own` argument. Either may now sit inside one branch of `if` / `else` / + `else if`, at any nesting depth. +- After an `if`, each owner declared before it is in one of three states: + live (moved on no path that falls through), moved (moved on every path + that falls through) or maybe-moved (moved on some). A branch that ends in + `return` does not count: its state never reaches the join. +- Only maybe-moved owners get a flag. Programs without a conditional move + compile to byte-identical IR and code (no `OSC3_CODE_CHANGE_OK`). + +### Grammar + +No new syntax. The grammar of OSC-2 is unchanged; programs that OSC-2 +refused with `CONDITIONAL_MOVE` are now accepted when the rules below hold. + +### Semantics + +- Merge rule at the end of an `if` (both arms falling through): equal + states stay; any difference (live vs moved, live vs maybe, moved vs maybe) + becomes maybe-moved. If one arm returns, the other arm's state is taken. +- Flag: one `bool` virtual register per flagged declaration, allocated by + the lowerer when the checker marks the declaration (`OscNode.dflag`). It + is set to 1 (`CONST 1`) right after the declaration (for an `own` + parameter: first thing in the entry block), and set to 0 (`CONST 0`) at + every move of that owner (before the `CALL` for an own argument, before + the new binding for a let-move), on every path. +- Release: every scope-end release of a flagged owner (block end and every + `return` it is live or maybe-moved at) becomes + `CBR flag -> release, join; release: RELEASE owner; BR join`. Owners that + are definitely moved are not released (as in OSC-2); live owners without + a flag are released unconditionally (as in OSC-2). Release order stays + reverse declaration order; a skipped release simply drops out. +- Loop-local owners: an owner declared inside a loop body may be moved on + one path of that body; its declaration sets the flag again each + iteration, so each iteration's object is released at most once. +- Frame layout: the flag is an ordinary vreg, so it has the ordinary stack + slot of the native frame, `[sp, #8*(v+1)]` (`osc_cg.c slot()`), holding + 0 or 1 in a 64-bit cell; non-parameter slots are zeroed by the prologue + and the declaration's `CONST 1` sets it. No new IR op, no interpreter or + backend change: `CONST`, `CBR`, `BR` and `RELEASE` already exist, so both + engines run the same IR and agree by construction. +- Static model trace: around the `if` the checker emits `SAVE`/`RESTORE` + as before. A definitely moved owner gets its `MOVE obj -> 0` re-emitted + after the `if` (unchanged). A maybe-moved owner emits nothing after the + `if`: on the straight-line trace it stays live and its guarded scope-end + `RELEASE` replays as a plain release (the trace follows the path on which + the flag is set). Each run's runtime log replays exactly (a release that + did not run is simply absent). +- Soundness argument: the flag equals "this owner's object has not been + moved" on every executed path, because it is set at the (re)declaration + and cleared at each move, and every release of a flagged owner tests it. + So no path releases twice (no `RUNTIME` double-release trap) and no path + leaks (live count 0 after a normal return); both are checked on every + fuzz run in both engines. + +### Static refusals (named) + +| Diagnostic | Transition | When | +|---|---|---| +| `USE_AFTER_MOVE` | use after maybe-move | read, write, field access or call use of a maybe-moved owner ("used after it may have been moved at line N (moved on some path of an 'if')") | +| `USE_AFTER_MOVE` | use after maybe-move | borrow (`&` / `&mut`) of a maybe-moved owner | +| `USE_AFTER_MOVE` | use after maybe-move | moving a maybe-moved owner again (double move across branches / two ifs) | +| `USE_AFTER_MOVE` | use after move (move inside loop) | an owner declared outside a loop moved inside it, also inside an `if` in the loop (unchanged from OSC-1) | +| `IMMUTABLE_ASSIGN` | assign to immutable binding | re-assigning an owner, maybe-moved or not ("owners are never reassigned"); item 3 does not add a set-live rule | + +For the model the maybe-move refusals are use-after-move: the checker emits +an accepted `MOVE obj -> 0` (the path that moved it) right before the refused +event, so the negative harness replays them as `use-after-move`. + +### Limits + +- Flags exist only for owners moved inside `if` arms; moves of an outer + owner inside a loop stay refused (no re-initialisation rule). +- No reassignment of owners (refused), so a flag only goes live -> moved + within one declaration's lifetime. +- One flag per declaration (up to the checker's symbol limit); flags are not + packed into bits. +- The static trace checks the path on which each maybe-moved owner is still + live; the moved path is checked by every executed run's runtime replay + (fuzz and goldens), not statically. + +### Evidence + +EVIDENCE_PLACEHOLDER + ## Generation width: remaining 32-bit sites and fix plan ADR OMEGA-SYSTEMS-CORE-0000 decision 1 fixes capability and object generations diff --git a/src/compiler/osc_check.c b/src/compiler/osc_check.c index ae92a6c54..1a9b7dd27 100644 --- a/src/compiler/osc_check.c +++ b/src/compiler/osc_check.c @@ -40,6 +40,7 @@ typedef struct { uint16_t dlive; uint8_t cap_off; int pool, prov; + int decl; /* OSC-3 item 3: declaring AST node (owners: carries the drop-flag mark) */ } Sym; /* OSC-3 item 2: handle state saved around branches and loop bodies */ @@ -49,8 +50,13 @@ typedef struct { int nprov, nsym; } HSave; +/* Obj.moved. OSC-3 item 3 (drop flags) adds OM_MAYBE: moved on some but not + * all paths that reach the current point. Such an owner cannot be used, + * borrowed or moved; at scope end its release is guarded by a drop flag. */ +enum { OM_LIVE = 0, OM_MOVED = 1, OM_GONE = 2 /* released (scope gone) */, OM_MAYBE = 3 }; + typedef struct { - uint8_t moved; + uint8_t moved; /* OM_* */ uint16_t n_shared, n_mut; uint32_t move_line; int sym; /* owning symbol or -1 (stand-in / dummy) */ @@ -248,6 +254,17 @@ static int diag_own(C *c, OscDiagKind k, uint32_t line, uint32_t col, int s, con return -1; } +/* OSC-3 item 3: a maybe-moved owner (OM_MAYBE) is refused exactly like a + * moved one. Its trace records the path on which it was moved (MOVE obj -> 0, + * accepted) before the refused event, so the refusal replays through the + * model as use-after-move. Returns 1 for a maybe-moved owner. */ +static int maybe_path(C *c, const Sym *y) +{ + if (c->obj[y->obj].moved != OM_MAYBE) return 0; + tr_ev(c, OSC_EV_MOVE, y->obj, -1, -1, -1, c->obj[y->obj].move_line, 0); + return 1; +} + /* direct or via-borrow access of symbol s (owner or borrow binding). wr: write. */ static int access(C *c, int s, int wr, uint32_t line, uint32_t col) { @@ -257,9 +274,10 @@ static int access(C *c, int s, int wr, uint32_t line, uint32_t col) Obj *o = &c->obj[y->obj]; uint32_t k = wr ? OSC_EV_USE_WRITE : OSC_EV_USE_READ; if (o->moved) { + int mb = maybe_path(c, y); tr_ev(c, k, y->obj, -1, -1, -1, line, 1); - snprintf(msg, sizeof msg, "'%s' used after it was moved at line %u", y->name, o->move_line); - return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, "use after move", msg); + snprintf(msg, sizeof msg, mb ? "'%s' used after it may have been moved at line %u (moved on some path of an 'if')" : "'%s' used after it was moved at line %u", y->name, o->move_line); + return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, mb ? "use after maybe-move" : "use after move", msg); } if (o->n_mut || (wr && o->n_shared)) { conflict_name(c, y->obj, -1, !wr, other, sizeof other); @@ -300,9 +318,10 @@ static int take_borrow(C *c, int s, int mut, uint32_t line, uint32_t col) if (y->kind == SK_OWNER) { Obj *o = &c->obj[y->obj]; if (o->moved) { + int mb = maybe_path(c, y); tr_ev(c, k, y->obj, -1, c->nbor, -1, line, 1); - snprintf(msg, sizeof msg, "borrow of '%s' after it was moved at line %u", y->name, o->move_line); - return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, "use after move", msg); + snprintf(msg, sizeof msg, mb ? "borrow of '%s' after it may have been moved at line %u (moved on some path of an 'if')" : "borrow of '%s' after it was moved at line %u", y->name, o->move_line); + return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, mb ? "use after maybe-move" : "use after move", msg); } if (o->n_mut || (mut && o->n_shared)) { conflict_name(c, y->obj, -1, !mut, other, sizeof other); @@ -344,9 +363,10 @@ static int do_move(C *c, int s, int dest, uint32_t line, uint32_t col) Obj *o = &c->obj[y->obj]; char other[64], msg[160]; if (o->moved) { + int mb = maybe_path(c, y); tr_ev(c, OSC_EV_MOVE, y->obj, dest, -1, -1, line, 1); - snprintf(msg, sizeof msg, "'%s' moved again after it was moved at line %u", y->name, o->move_line); - return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, "use after move", msg); + snprintf(msg, sizeof msg, mb ? "'%s' moved again after it may have been moved at line %u (moved on some path of an 'if')" : "'%s' moved again after it was moved at line %u", y->name, o->move_line); + return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, mb ? "use after maybe-move" : "use after move", msg); } if (o->n_shared || o->n_mut) { conflict_name(c, y->obj, -1, 0, other, sizeof other); @@ -400,6 +420,7 @@ static int declare(C *c, const OscNode *n, uint8_t kind, const OscType *ty, int y->ty = *ty; y->loop_depth = c->loop_depth; y->obj = y->bor = -1; + y->decl = (int)(n - c->ast->nodes); c->vis[c->nvis++] = s; return s; } @@ -447,7 +468,9 @@ static int scope_exit_events(C *c, int levels, uint32_t line, OscNode *rec) if (y->bor >= 0 && c->bor[y->bor].live) end_bor(c, y->bor, line); } else if (y->kind == SK_OWNER) { Obj *o = &c->obj[y->obj]; - if (!o->moved && !o->arena1) { + /* OSC-3 item 3: a maybe-moved owner is listed too; the lowerer + * guards its release with the owner's drop flag */ + if ((o->moved == OM_LIVE || o->moved == OM_MAYBE) && !o->arena1) { tr_ev(c, OSC_EV_RELEASE, y->obj, -1, -1, -1, line, 0); o->moved = 2; /* released (scope gone) */ if (a->nrel < OSC_AST_MAX_REL) { a->rel[a->nrel++] = (int16_t)s; rec->rel_count++; } @@ -1690,18 +1713,19 @@ static int chk_if(C *c, int i) } else if (!t1) { /* OSC-3 item 2: freed only if freed on both paths */ for (int p = 0; p < h0->nprov; p++) c->pfreed[p] = (uint8_t)(c->pfreed[p] && h1->pfreed[p]); + /* OSC-3 item 3 (drop flags): an owner moved on one path and not (or + * only maybe) on the other is maybe-moved after the `if`. Its + * declaration gets a drop flag: set at the declaration, cleared at + * every move, tested by its scope-end release. Both paths moved: + * definitely moved (no flag). Neither: unchanged. Pre-existing + * objects are never OM_GONE on a path that falls through. */ for (int o = 0; o < nobj0; o++) { - int m1 = s1->obj[o].moved == 1, m2 = c->obj[o].moved == 1; - if (m1 != m2) { - int sym = c->obj[o].sym; - uint32_t ml = m1 ? s1->obj[o].move_line : c->obj[o].move_line; - char msg[320]; - snprintf(msg, sizeof msg, "'%s' is moved on only one path of the 'if' at line %u (no drop flags)", - sym >= 0 ? c->sym[sym].name : "?", n->line); - osc_diag_set(c->d, OSC_DIAG_CONDITIONAL_MOVE, ml, 0, sym >= 0 ? c->sym[sym].name : "?", - sym >= 0 ? c->sym[sym].line : 0, "if", "conditional move", "%s", msg); - goto out; - } + int m1 = s1->obj[o].moved, m2 = c->obj[o].moved; + if (m1 == m2) continue; + if (m2 == OM_LIVE || (m2 == OM_MOVED && m1 == OM_MAYBE)) c->obj[o].move_line = s1->obj[o].move_line; + c->obj[o].moved = OM_MAYBE; + int sym = c->obj[o].sym; + if (sym >= 0) NODE(c->sym[sym].decl)->dflag = 1; } } c->term = 0; diff --git a/src/compiler/osc_lower.c b/src/compiler/osc_lower.c index 3eede7cc2..c90b8d7d2 100644 --- a/src/compiler/osc_lower.c +++ b/src/compiler/osc_lower.c @@ -28,6 +28,11 @@ typedef struct { * vreg r is its slot and r + 1 its generation. */ uint8_t ispool[OSC_CHECK_MAX_SYMS]; uint8_t ishandle[OSC_CHECK_MAX_SYMS]; + /* OSC-3 item 3: drop-flag vreg (bool) of an owner symbol whose + * declaration the checker marked (dflag), -1 otherwise. 1 = live, + * 0 = moved. Set at the declaration, cleared at every move, tested by + * the owner's scope-end release (CBR around RELEASE). */ + int16_t dfv[OSC_CHECK_MAX_SYMS]; } L; #define NODE(i) (&l->ast->nodes[(i)]) @@ -101,14 +106,43 @@ static int kconst(L *l, OscScalar s, uint64_t v, uint32_t line) return r; } +/* OSC-3 item 3 drop flags: write imm (1 live / 0 moved) into owner s's flag */ +static void dflag_set(L *l, int s, uint64_t imm, uint32_t line) +{ + if (s < 0 || l->dfv[s] < 0) return; + OscInsn *x = emit(l, OSC_I_CONST, line); + x->dst = l->dfv[s]; + x->imm = imm; +} +/* owner s just declared by node n: give it a flag (set live) if marked */ +static void dflag_decl(L *l, int s, const OscNode *n) +{ + if (!n->dflag) return; + l->dfv[s] = (int16_t)newvs(l, OSC_T_BOOL, n->line); + dflag_set(l, s, 1, n->line); +} + static void releases(L *l, const OscNode *n) { + if (l->cur < 0) return; /* dead code: nothing to release (emit would drop it) */ for (uint32_t k = 0; k < n->rel_count; k++) { int s = l->ast->rel[n->rel_start + k]; /* OSC-2 arenas: an arena symbol's vreg is its u64 handle (owners are REFs) */ int arena = l->f->vtype[l->vreg[s]].s != OSC_T_REF; + int guard = !l->ispool[s] && !arena && l->dfv[s] >= 0 ? l->dfv[s] : -1; + int rb = -1, jb = -1; + if (guard >= 0) { /* OSC-3 item 3: release only if the drop flag says live */ + rb = newblk(l, n->line); + jb = newblk(l, n->line); + cbr(l, guard, rb, jb, n->line); + l->cur = rb; + } OscInsn *x = emit(l, l->ispool[s] ? OSC_I_PCLOSE : arena ? OSC_I_ADESTROY : OSC_I_RELEASE, n->line); x->a = l->vreg[s]; + if (guard >= 0) { + br(l, jb, n->line); + l->cur = jb; + } } } @@ -154,6 +188,12 @@ static int call(L *l, int i, int want_value) args[na++] = l->vreg[an->sym]; /* own argument: moves the owner */ else args[na++] = expr(l, a); } + /* OSC-3 item 3: an owner passed to an own parameter is moved here */ + for (int a = n->a; a >= 0; a = NODE(a)->next) { + const OscNode *an = NODE(a); + if (an->kind == ON_NAME && an->sym >= 0 && an->sym < OSC_CHECK_MAX_SYMS && l->dfv[an->sym] >= 0) + dflag_set(l, an->sym, 0, n->line); + } const OscNode *g = NODE(l->ast->fns[n->sym]); int dst = -1; if (g->ty.s != OSC_T_VOID && want_value) dst = newvs(l, g->ty.s, n->line); @@ -367,6 +407,7 @@ static void stmt(L *l, int i) x->imm = (uint64_t)fn->hi; } } + dflag_decl(l, n->sym, n); /* OSC-3 item 3 */ break; } int v = expr(l, n->a); @@ -376,6 +417,7 @@ static void stmt(L *l, int i) if (ah >= 0) x->b = (int16_t)ah; x->dst = (int16_t)r; x->a = (int16_t)v; + dflag_decl(l, n->sym, n); /* OSC-3 item 3 */ break; } case ON_LET_MOVE: @@ -385,6 +427,10 @@ static void stmt(L *l, int i) OscInsn *x = emit(l, OSC_I_MOV, n->line); x->dst = (int16_t)r; x->a = l->vreg[n->sym2]; + if (n->kind == ON_LET_MOVE) { /* OSC-3 item 3: the source is moved; the new owner starts live */ + dflag_set(l, n->sym2, 0, n->line); + dflag_decl(l, n->sym, n); + } break; } case ON_ASSIGN: { @@ -604,6 +650,7 @@ static int lower_fn(L *l, int fi) l->nblk = 0; memset(l->ispool, 0, sizeof l->ispool); memset(l->ishandle, 0, sizeof l->ishandle); + memset(l->dfv, 0xff, sizeof l->dfv); /* OSC-3 item 3: no drop flags */ for (int p = fn->a; p >= 0; p = NODE(p)->next) { const OscNode *pn = NODE(p); int r = newv(l, &pn->ty, pn->line); @@ -611,6 +658,9 @@ static int lower_fn(L *l, int fi) f->nparams++; } l->cur = newblk(l, fn->line); + /* OSC-3 item 3: own parameters that may be moved on only some paths get + * their drop flag (vregs after the parameters), set live at entry */ + for (int p = fn->a; p >= 0; p = NODE(p)->next) dflag_decl(l, NODE(p)->sym, NODE(p)); l->fi = fi; if (a->reqn[fi] >= 0 && !a->req_elide[fi]) contract_check(l, a->reqn[fi], OSC_TRAP_REQUIRES); /* body block inlined (same emission as block()) so a void function's diff --git a/src/compiler/osc_parse.h b/src/compiler/osc_parse.h index 7936f9d05..12ca3ff8c 100644 --- a/src/compiler/osc_parse.h +++ b/src/compiler/osc_parse.h @@ -92,6 +92,9 @@ typedef struct { OscType ty; /* declared type (decls, params, casts, fn ret) / result type (exprs, checker) */ uint8_t mut; uint8_t flag; + uint8_t dflag; /* OSC-3 item 3, checker: an owner declaration (ON_LET_ALLOC, + * ON_LET_MOVE, own parameter) whose owner may be moved on + * only some paths; the lowerer gives it a drop flag */ /* checker annotations */ uint8_t is_const; /* expression folded to cval (already canonical for ty) */ uint64_t cval; diff --git a/tests/compiler/neg/df_borrow_after_maybe_move.osc b/tests/compiler/neg/df_borrow_after_maybe_move.osc new file mode 100644 index 000000000..eca43bb34 --- /dev/null +++ b/tests/compiler/neg/df_borrow_after_maybe_move.osc @@ -0,0 +1,8 @@ +// expect: USE_AFTER_MOVE object=a line=7 +// transition: use after maybe-move +fn rd(x: &[u8; 2]) -> u8 { return x[0]; } +fn f(c: bool) -> u8 { + let a: own [u8; 2] = alloc(3); + if c { } else { let b: own [u8; 2] = a; } + return rd(&a); +} diff --git a/tests/compiler/neg/df_double_move_branches.osc b/tests/compiler/neg/df_double_move_branches.osc new file mode 100644 index 000000000..03a0a6e14 --- /dev/null +++ b/tests/compiler/neg/df_double_move_branches.osc @@ -0,0 +1,9 @@ +// expect: USE_AFTER_MOVE object=a line=7 +// transition: use after maybe-move +fn take(x: own [u8; 2]) -> u8 { return x[0]; } +fn f(k: u8) -> u8 { + let a: own [u8; 2] = alloc(3); + if k > 1 { let m: own [u8; 2] = a; } + if k > 5 { return take(a); } + return 0; +} diff --git a/tests/compiler/neg/df_move_in_loop_if.osc b/tests/compiler/neg/df_move_in_loop_if.osc new file mode 100644 index 000000000..422adf074 --- /dev/null +++ b/tests/compiler/neg/df_move_in_loop_if.osc @@ -0,0 +1,10 @@ +// expect: USE_AFTER_MOVE object=a line=7 +// transition: move inside loop +fn take(x: own [u8; 2]) -> u8 { return x[0]; } +fn f() -> u8 { + let a: own [u8; 2] = alloc(3); + for i in 0 .. 3 { + if i == 1 { let s: u8 = take(a); } + } + return 0; +} diff --git a/tests/compiler/neg/df_nested_maybe_move.osc b/tests/compiler/neg/df_nested_maybe_move.osc new file mode 100644 index 000000000..cc4c66e03 --- /dev/null +++ b/tests/compiler/neg/df_nested_maybe_move.osc @@ -0,0 +1,12 @@ +// expect: USE_AFTER_MOVE object=p line=11 +// transition: use after maybe-move +struct P { x: u32, y: u32 } +fn eat(p: own P) -> u32 { return p.x; } +fn f(k: u32) -> u32 { + let p: own P = P { x: 1, y: 2 }; + let mut r: u32 = 0; + if k > 1 { + if k > 4 { r = eat(p); } else { r = 2; } + } else { r = p.y; } + return r + p.x; +} diff --git a/tests/compiler/neg/df_reassign_maybe_moved.osc b/tests/compiler/neg/df_reassign_maybe_moved.osc new file mode 100644 index 000000000..4aaef56ac --- /dev/null +++ b/tests/compiler/neg/df_reassign_maybe_moved.osc @@ -0,0 +1,10 @@ +// expect: IMMUTABLE_ASSIGN object=a line=8 +// re-assigning a maybe-moved owner is refused (owners are never reassigned). +fn take(x: own [u8; 2]) -> u8 { return x[0]; } +fn f(c: bool) -> u8 { + let a: own [u8; 2] = alloc(3); + let b: own [u8; 2] = alloc(4); + if c { let s: u8 = take(a); } + a = b; + return 0; +} diff --git a/tests/compiler/neg/conditional_move.osc b/tests/compiler/neg/df_use_after_maybe_move.osc similarity index 59% rename from tests/compiler/neg/conditional_move.osc rename to tests/compiler/neg/df_use_after_maybe_move.osc index 70820b990..1d0f63232 100644 --- a/tests/compiler/neg/conditional_move.osc +++ b/tests/compiler/neg/df_use_after_maybe_move.osc @@ -1,9 +1,8 @@ -// expect: CONDITIONAL_MOVE object=a line=7 -// transition: conditional move +// expect: USE_AFTER_MOVE object=a line=7 +// transition: use after maybe-move fn take(x: own [u8; 2]) -> u8 { return x[0]; } - fn f(c: bool) -> u8 { let a: own [u8; 2] = alloc(3); if c { let s: u8 = take(a); } - return 1; + return a[1]; } diff --git a/tests/compiler/progs/df_both.osc b/tests/compiler/progs/df_both.osc new file mode 100644 index 000000000..d2a73b5e8 --- /dev/null +++ b/tests/compiler/progs/df_both.osc @@ -0,0 +1,15 @@ +// entry: f +// OSC-3 item 3 (drop flags): an owner moved on both paths is definitely +// moved after the `if`: no drop flag, no release (code as in OSC-2). +fn take(x: own [u16; 3]) -> u16 { return x[0] + x[2]; } + +fn f(k: u16) -> u16 { + let a: own [u16; 3] = alloc(k & 255); + let b: own [u16; 3] = alloc(2); + let mut r: u16 = 0; + if k > 100 { r = take(a); } else { let m: own [u16; 3] = a; r = m[1] + 1; } + return r + take(b); +} +// expect-run: f 200 -> 404 +// expect-run: f 7 -> 12 +// expect-run: f 300 -> 92 diff --git a/tests/compiler/progs/df_conditional_move.osc b/tests/compiler/progs/df_conditional_move.osc new file mode 100644 index 000000000..bcba38b57 --- /dev/null +++ b/tests/compiler/progs/df_conditional_move.osc @@ -0,0 +1,13 @@ +// entry: f +// Was neg/conditional_move.osc (CONDITIONAL_MOVE, OSC-1/OSC-2). OSC-3 item 3 +// accepts it: `a` gets a drop flag and is released at the return only when +// the branch did not move it. +fn take(x: own [u8; 2]) -> u8 { return x[0]; } + +fn f(c: bool) -> u8 { + let a: own [u8; 2] = alloc(3); + if c { let s: u8 = take(a); } + return 1; +} +// expect-run: f 1 -> 1 +// expect-run: f 0 -> 1 diff --git a/tests/compiler/progs/df_else.osc b/tests/compiler/progs/df_else.osc new file mode 100644 index 000000000..31b67d33e --- /dev/null +++ b/tests/compiler/progs/df_else.osc @@ -0,0 +1,18 @@ +// entry: f +// OSC-3 item 3 (drop flags): a struct owner moved in the else-branch only +// (by a let-move into a branch-local owner, released at the branch end). +struct P { x: u32, y: u32 } + +fn f(k: u32) -> u32 { + let p: own P = P { x: k & 255, y: 3 }; + let mut r: u32 = 1; + if k < 10 { + r = r + p.x; + } else { + let q: own P = p; + r = q.y * 2; + } + return r; +} +// expect-run: f 4 -> 5 +// expect-run: f 12 -> 6 diff --git a/tests/compiler/progs/df_nested.osc b/tests/compiler/progs/df_nested.osc new file mode 100644 index 000000000..f06e62df9 --- /dev/null +++ b/tests/compiler/progs/df_nested.osc @@ -0,0 +1,34 @@ +// entry: f g +// OSC-3 item 3 (drop flags): moves at different depths of nested ifs and an +// else-if chain; returns inside branches while another owner is maybe-moved +// (the return's release of that owner is guarded by its flag). +fn take(x: own [u32; 2]) -> u32 { return x[0] + x[1]; } + +fn f(k: u32) -> u32 { + let a: own [u32; 2] = alloc(1); + let b: own [u32; 2] = alloc(2); + let mut r: u32 = 0; + if k > 2 { + if k > 5 { r = take(a); } else { r = take(b); } + } else if k == 1 { + r = take(a) + take(b); + } + return r + 100; +} + +fn g(k: u32) -> u32 { + let a: own [u32; 2] = alloc(k & 7); + let b: own [u32; 2] = alloc(3); + if k > 4 { let m: own [u32; 2] = a; b[0] = m[0]; } + if k > 6 { return b[0] + 1; } + if k == 2 { let n: own [u32; 2] = b; return n[1]; } + return b[1] + 10; +} +// expect-run: f 7 -> 102 +// expect-run: f 4 -> 104 +// expect-run: f 1 -> 106 +// expect-run: f 0 -> 100 +// expect-run: g 7 -> 8 +// expect-run: g 5 -> 13 +// expect-run: g 2 -> 3 +// expect-run: g 0 -> 13 diff --git a/tests/compiler/progs/df_order.osc b/tests/compiler/progs/df_order.osc new file mode 100644 index 000000000..3b769724d --- /dev/null +++ b/tests/compiler/progs/df_order.osc @@ -0,0 +1,24 @@ +// entry: order +// OSC-3 item 3 (drop flags): release order with conditional moves, asserted +// by test_osc_compiler for k = 0, 1, 3 in the interpreter and natively: +// k=0: alloc 1 2 3, release 2 (q), alloc 4 5, release 5 4 3 1 (p skipped) +// k=1: alloc 1 2 3, release 1 (eata), alloc 4 5, release 5 4 3 2 (a skipped) +// k=3: alloc 1 2 3, release 1, alloc 4, release 4 (eata), alloc 5, release 5 3 2 +struct P { x: u32, y: u32 } + +fn eata(a: own [u32; 2]) -> u32 { return a[0] + a[1]; } + +fn order(k: u32) -> u32 { + let a: own [u32; 2] = alloc(k); + let p: own P = P { x: k, y: 2 }; + let b: own [u32; 2] = alloc(5); + let mut r: u32 = 0; + if k > 0 { r = eata(a); } else { let q: own P = p; r = q.x + q.y; } + let c: own [u32; 2] = alloc(7); + if k > 1 { if k > 2 { r = r + eata(c); } } + let d: own [u32; 2] = alloc(1); + return r + b[0] + d[0]; +} +// expect-run: order 0 -> 8 +// expect-run: order 1 -> 8 +// expect-run: order 3 -> 26 diff --git a/tests/compiler/progs/df_struct.osc b/tests/compiler/progs/df_struct.osc new file mode 100644 index 000000000..88cf513d4 --- /dev/null +++ b/tests/compiler/progs/df_struct.osc @@ -0,0 +1,40 @@ +// entry: f h +// OSC-3 item 3 (drop flags): struct owners, an own parameter moved on one +// path, and an owner declared inside a loop body and moved on one path (its +// flag is set again by each iteration's declaration). +struct P { x: u32, v: [u32; 2] } + +fn eat(p: own P) -> u32 { return p.x + p.v[1]; } + +fn eat2(a: own [u32; 2]) -> u32 { return a[0] + a[1]; } + +fn pass(p: own P, k: u32) -> u32 { + if k > 3 { return eat(p) + 1; } + let mut r: u32 = p.x; + if k == 1 { r = r + eat(p); } + return r; +} + +fn f(k: u32) -> u32 { + let p: own P = P { x: k & 7, v: [1; 2] }; + let q: own P = P { x: 2, v: [k & 3; 2] }; + let mut r: u32 = pass(q, k); + if k > 8 { r = r + eat(p); } + return r; +} + +fn h(n: u32) -> u32 { + let mut s: u32 = 0; + for i in 0 .. 6 { + let a: own [u32; 2] = alloc(i as u32); + if (i as u32) < n { s = s + eat2(a); } + } + return s; +} +// expect-run: f 0 -> 2 +// expect-run: f 1 -> 5 +// expect-run: f 5 -> 4 +// expect-run: f 9 -> 6 +// expect-run: h 3 -> 6 +// expect-run: h 0 -> 0 +// expect-run: h 10 -> 30 diff --git a/tests/compiler/progs/df_then.osc b/tests/compiler/progs/df_then.osc new file mode 100644 index 000000000..e547e87a5 --- /dev/null +++ b/tests/compiler/progs/df_then.osc @@ -0,0 +1,31 @@ +// entry: f g +// OSC-3 item 3 (drop flags): an owner moved in the then-branch only. After +// the `if` it is maybe-moved: its drop flag is set at the declaration and +// cleared by the move, and its scope-end release runs only when the flag +// says live. A then-branch that returns leaves the owner definitely live +// after the `if` (no flag). +fn take(x: own [u8; 2]) -> u8 { return x[0] + x[1]; } + +fn f(k: u8) -> u8 { + let a: own [u8; 2] = alloc(k & 15); + let b: own [u8; 2] = alloc(4); + let mut r: u8 = 0; + if k > 3 { r = take(a); } + return r + b[0]; +} + +fn g(k: u8) -> u8 { + let a: own [u8; 2] = alloc(1); + if k == 1 { + let m: own [u8; 2] = a; + m[0] = 9; + return m[0] + m[1]; + } + a[1] = k & 7; + return a[0] + a[1]; +} +// expect-run: f 5 -> 14 +// expect-run: f 2 -> 4 +// expect-run: f 20 -> 12 +// expect-run: g 1 -> 10 +// expect-run: g 5 -> 6 diff --git a/tests/compiler/test_osc_compiler.c b/tests/compiler/test_osc_compiler.c index d3037b560..308f4829a 100644 --- a/tests/compiler/test_osc_compiler.c +++ b/tests/compiler/test_osc_compiler.c @@ -918,10 +918,30 @@ static void struct_layout(void) /* Destruction order of progs/structs_dtor.osc: owners released at scope end in * reverse declaration order, the moved-from struct is not released, in the * interpreter and natively. */ -/* Run entry 0 of