From 2b9e79db9e1f4d1f74c6ca99c794ba4886e9f003 Mon Sep 17 00:00:00 2001 From: zinc Date: Thu, 15 Oct 2026 14:58:36 +0330 Subject: analyzer: infer .u from returns of fresh objects in the body (prescan guess, then checked as .u) --- src/compiler/decl.zc | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/compiler/decl.zc') diff --git a/src/compiler/decl.zc b/src/compiler/decl.zc index 52d08c7..6bc8245 100644 --- a/src/compiler/decl.zc +++ b/src/compiler/decl.zc @@ -81,8 +81,44 @@ int prescan_fn(int ret, int n, int o3) { sflag[s] = sflag[s] | F_SIG | F_PRE; } if (o3) sflag[s] = sflag[s] | F_O3; + if (!ret && is_op(s_lb) && prescan_owned(n)) { sflag[s] = sflag[s] | F_GUESS; if (sflag[s] & F_PRE) ssig[s] = ssig[s] | 1; } return 0; } +// true for a token that makes a fresh owned object when called: new or heap. +// nothing else: a call to the function itself is no evidence. +int prescan_maker() { + return tk == T_ID && (rt.eq_w(tv, "rt.new") || rt.eq_w(tv, "new") || rt.eq_w(tv, "heap")); +} +// prescan: at the { of an untagged function's body, guess whether it returns an +// owned object: a return of a fresh object, or of a local last set from one. +// the guess only makes the function compile as if declared .u; its body is then +// checked against that, so a wrong guess is an error, never a silent leak. +int prescan_owned(int self) { + int depth = 0, n, k, found = 0, nown = 0; let owned[16]; + while (tk != T_EOF) { + if (is_op(s_lb)) depth++; + else if (is_op(s_rb)) { depth--; if (!depth) { next(); return found; } } + else if (tk == T_ID && rt.eq_w(tv, "return")) { + next(); + if (tk == T_ID) { + n = tv; peek(); + if (pk_tk == T_OP && pk_tv == s_lp && prescan_maker()) found = 1; + else if (pk_tk == T_OP && pk_tv == s_semi) { k = 0; while (k < nown) { if (owned[k] == n) found = 1; k++; } } + } + continue; + } + else if (tk == T_ID) { + n = tv; peek(); + if (pk_tk == T_OP && pk_tv == s_eq) { + next(); next(); + if (prescan_maker()) { peek(); if (pk_tk == T_OP && pk_tv == s_lp && nown < 16) { owned[nown] = n; nown++; } } + continue; + } + } + next(); + } + return found; +} // scan ahead, without emitting code, to learn what a block of tokens contains. int prescan(int kind, int obj) { int depth = 0, sp = panic, sn = nerr, pend = 0, po3 = 0, n, dep; char* oldmodule = module_name; word oldimports = module_imports; @@ -359,6 +395,7 @@ int fndef(int chk, int rty, int ret, int n) { if (amb & 1) { t = lookup(pn[i]); if (t >= 0 && skind[t] == K_TYPE) { pty[i] = sval[t]; typed = 1; } } amb = amb >> 1; i++; } + if (!ret && (sflag[s] & F_GUESS)) ret = 1; // prescan saw it return an owned object: compile it as .u sig = ret + np + np + tags * 64; lo = borrows_clause(pn, np, 1); // a definition after a prototype keeps the prototype's borrows clause -- cgit v1.2.3 From 5aaf70abdda0026a7b4d647746a109d94b25f7a0 Mon Sep 17 00:00:00 2001 From: zinc Date: Thu, 15 Oct 2026 15:01:29 +0330 Subject: analyzer: a .u (declared or inferred) function can't return a borrowed parameter or a value derived from one; fixtures --- src/compiler/analyzer.zc | 8 ++++++++ src/compiler/decl.zc | 2 +- .../6-ownership/infer_owned_return_borrow_first.zc | 10 ++++++++++ tests/conformance/6-ownership/infer_owned_return_mixed.zc | 11 +++++++++++ tests/conformance/6-ownership/u_return_param_field.zc | 7 +++++++ tests/conformance/6-ownership/u_return_plain_param.zc | 8 ++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/conformance/6-ownership/infer_owned_return_borrow_first.zc create mode 100644 tests/conformance/6-ownership/infer_owned_return_mixed.zc create mode 100644 tests/conformance/6-ownership/u_return_param_field.zc create mode 100644 tests/conformance/6-ownership/u_return_plain_param.zc (limited to 'src/compiler/decl.zc') diff --git a/src/compiler/analyzer.zc b/src/compiler/analyzer.zc index 839c1ef..55a96bb 100644 --- a/src/compiler/analyzer.zc +++ b/src/compiler/analyzer.zc @@ -1644,6 +1644,7 @@ int assigncheck(int k, int i, int rc) { // the function has no .u (its callers compiled after this point see it). an m // object is lent, not given, so returning it is an error, and so is a plain word // from an owned function that was called before its body. +int ret_borrowed; int retcheck(int c) { int i; if (isptr(xty) || xty == tword) { @@ -1670,9 +1671,16 @@ int retcheck(int c) { if ((isptr(xty) || isagg(xty)) && ((xaf && xaf[2] == 4) || (xbor >= 0 && lafact[xbor] && lafact[xbor][2] == 4))) { cerr("an array literal cannot escape its function through return"); return 0; } i = -1; if (c >= C_VAR && tracked(c - C_VAR)) i = c - C_VAR; + // a value that comes from a parameter the caller still owns (not a .d one, + // which is tracked) is a borrow: it can't become the caller's owned result. + if (c != C_FRESH && i < 0 && isptr(xty) && effect_mask(c)) { + if (fret) { cerr("a borrowed parameter cannot be returned as an owned result"); return 0; } + ret_borrowed = 1; + } if (i >= 0 && lstate[lroot(i)] == S_DEAD) { cerrn(i, "used after it was given away"); return 0; } if (i >= 0 && (lstate[i] == S_D || (lstate[i] == S_M && lown[i])) && (fret || finfer)) lstate[i] = S_U; // returning an owned object (a d object, or a local that owns one) gives it up, as u*x does if (!fret && finfer && (c == C_FRESH || (i >= 0 && lstate[i] == S_U))) { + if (ret_borrowed) cerr("this function also returns a borrowed parameter, so its result can't be owned"); if (sflag[finsym] & F_EARLY) serrn(sname[finsym], "is called before its body, so its owned return can't be known there; declare the return type with .u"); ssig[finsym] = ssig[finsym] | 1; // the return is owned: callers read it from the signature sflag[finsym] = sflag[finsym] | F_RETOWN; diff --git a/src/compiler/decl.zc b/src/compiler/decl.zc index 6bc8245..e2c3613 100644 --- a/src/compiler/decl.zc +++ b/src/compiler/decl.zc @@ -435,7 +435,7 @@ int fndef(int chk, int rty, int ret, int n) { fixall(sfix[s], here); sfix[s] = 0; fn_begin(chk, ret); lt = lt_begin(sval[s], sname[s], lfl); xc_fn = sname[s]; - finsym = s; finfer = !ret; + finsym = s; finfer = !ret; ret_borrowed = 0; effect_current = effect_record(s); rt.fill_w(effect_current, 0, 50); effect_current[0] = 1; if (!chk || stubgen) effect_current[0] = 2; effect_current[1] = np; i = 0; while (i < np) { if (pt[i] == 2) { effect_current[2 + i] = EF_CONSUME; effect_current[26 + i] = EF_CONSUME; } i++; } if (typed && explicit_rty) fty = rty; diff --git a/tests/conformance/6-ownership/infer_owned_return_borrow_first.zc b/tests/conformance/6-ownership/infer_owned_return_borrow_first.zc new file mode 100644 index 0000000..e75fb10 --- /dev/null +++ b/tests/conformance/6-ownership/infer_owned_return_borrow_first.zc @@ -0,0 +1,10 @@ +// expect error: a borrowed parameter cannot be returned as an owned result +// spec 6.8 +include rt; +struct node { int v; node* next; }; +node* pick(node* other, int which) { + if (which) return other; + return rt.new(node); +} +fn main() { } +// zc0: skip diff --git a/tests/conformance/6-ownership/infer_owned_return_mixed.zc b/tests/conformance/6-ownership/infer_owned_return_mixed.zc new file mode 100644 index 0000000..5216003 --- /dev/null +++ b/tests/conformance/6-ownership/infer_owned_return_mixed.zc @@ -0,0 +1,11 @@ +// expect error: a borrowed parameter cannot be returned as an owned result +// spec 6.8 +include rt; +struct node { int value; node* next; }; +node* pick(node* other, int which) { + node* fresh = rt.new(node); + if (which) return fresh; + return other; +} +fn main() { node* a = rt.new(node); node* b = pick(a, 1); rt.put_n(b.value); } +// zc0: skip diff --git a/tests/conformance/6-ownership/u_return_param_field.zc b/tests/conformance/6-ownership/u_return_param_field.zc new file mode 100644 index 0000000..af8284d --- /dev/null +++ b/tests/conformance/6-ownership/u_return_param_field.zc @@ -0,0 +1,7 @@ +// expect error: a borrowed parameter cannot be returned as an owned result +// spec 6.8 +include rt; +struct node { int v; node* next; }; +node*.u nxt(node* l) { node* c = l.next; return c; } +fn main() { } +// zc0: skip diff --git a/tests/conformance/6-ownership/u_return_plain_param.zc b/tests/conformance/6-ownership/u_return_plain_param.zc new file mode 100644 index 0000000..5aca40b --- /dev/null +++ b/tests/conformance/6-ownership/u_return_plain_param.zc @@ -0,0 +1,8 @@ +// expect error: a borrowed parameter cannot be returned as an owned result +// spec 6.8 +include rt; +memdebug on; +struct node { int value; node* next; }; +node*.u pick(node* other) { return other; } +fn main() { node* a = rt.new(node); { node* b = pick(a); rt.put_n(b.value); } rt.put_n(a.value); } +// zc0: skip -- cgit v1.2.3 From d36190a5f3b8fa33fe3381771cd1efb9a1335e50 Mon Sep 17 00:00:00 2001 From: zinc Date: Thu, 15 Oct 2026 15:19:17 +0330 Subject: prescan: guess an owned return only from a local declared from new/heap, not a global assigned one (fixes lsp self-check); xref baseline for the new writers --- src/compiler/decl.zc | 8 ++++-- .../infer_owned_return_global_not_owned.zc | 7 ++++++ tests/xref.baseline | 29 +++++++++++----------- 3 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 tests/conformance/6-ownership/infer_owned_return_global_not_owned.zc (limited to 'src/compiler/decl.zc') diff --git a/src/compiler/decl.zc b/src/compiler/decl.zc index e2c3613..bbde5ba 100644 --- a/src/compiler/decl.zc +++ b/src/compiler/decl.zc @@ -94,8 +94,12 @@ int prescan_maker() { // the guess only makes the function compile as if declared .u; its body is then // checked against that, so a wrong guess is an error, never a silent leak. int prescan_owned(int self) { - int depth = 0, n, k, found = 0, nown = 0; let owned[16]; + int depth = 0, n, k, found = 0, nown = 0, decl = 0; let owned[16]; while (tk != T_EOF) { + // a declaration's name follows a type name, a *, let or a comma; a plain + // assignment (to a global, say) follows ; { or } + int was = decl; + decl = tk == T_ID || (tk == T_OP && (rt.eq_w(tv, "*") || rt.eq_w(tv, ","))); if (is_op(s_lb)) depth++; else if (is_op(s_rb)) { depth--; if (!depth) { next(); return found; } } else if (tk == T_ID && rt.eq_w(tv, "return")) { @@ -107,7 +111,7 @@ int prescan_owned(int self) { } continue; } - else if (tk == T_ID) { + else if (tk == T_ID && was) { n = tv; peek(); if (pk_tk == T_OP && pk_tv == s_eq) { next(); next(); diff --git a/tests/conformance/6-ownership/infer_owned_return_global_not_owned.zc b/tests/conformance/6-ownership/infer_owned_return_global_not_owned.zc new file mode 100644 index 0000000..5d7f793 --- /dev/null +++ b/tests/conformance/6-ownership/infer_owned_return_global_not_owned.zc @@ -0,0 +1,7 @@ +// expect: 3/ +// spec 6.8 +include rt; +int* cache; +int* refill() { cache = rt.new(int); *cache = 3; return cache; } +fn main() { refill(); rt.put_n(*cache); rt.put_c(10); } +// zc0: skip diff --git a/tests/xref.baseline b/tests/xref.baseline index 26b6931..8b7c725 100644 --- a/tests/xref.baseline +++ b/tests/xref.baseline @@ -310,8 +310,8 @@ lnsz_initial local_captured clocap_at newloc reserve_locals local_cell capture_cell newloc reserve_locals local_dropset drop_attach expr localdecl newloc reassign_free reserve_locals -local_end_info assigncheck end_owner newloc reserve_locals -local_ended assigncheck end_owner newloc reserve_locals +local_end_info assigncheck block_end_owners end_owner newloc reserve_locals +local_ended assigncheck block_end_owners end_owner newloc reserve_locals local_origin assigncheck newloc reserve_locals local_origin_mask assigncheck newloc reserve_locals local_shadow newloc reserve_locals shadow_take shadow_use @@ -340,7 +340,7 @@ lrsoff_initial lsets chk_unit lslot newloc reserve_locals lsp ctx_pop loop_end loop_push zc_init -lstate alias_end assigncheck backedge block_close closure end_owner fndef handup join newloc recast reserve_locals retcheck snap_cmp snap_restore touch +lstate alias_end assigncheck backedge block_close block_end_owners closure end_owner fndef handup join newloc recast reserve_locals retcheck snap_cmp snap_restore touch lt_at lt_note lt_base lt_begin lt_end lt_buf lt_begin lt_room lt_stmt @@ -368,10 +368,10 @@ ltxt lt_note ltxtcap lt_note ltype array_literal_tokens closure embed_bind fndef kw_switch localdecl newloc reserve_locals lused assigncheck luse newloc reserve_locals -lv array_dynamic_literal array_literal_tokens bin blockexpr call chk_reset closure ctx_pop drop_attach enhcall expr field fn_begin heap_box incdec kwcall methcall newtyped postfix_tail primary rval ternary unary +lv array_dynamic_literal array_literal_tokens bin blockexpr call chk_reset closure ctx_pop drop_attach enhcall expr field fn_begin heap_box incdec kwcall methcall newtyped postfix_tail primary printcall rval ternary unary lvescaped closure newloc reserve_locals unary lvi chk_reset ctx_pop field postfix_tail primary -lvk array_dynamic_literal array_literal_tokens bin blockexpr call chk_reset closure ctx_pop drop_attach enhcall expr field fn_begin heap_box incdec kwcall methcall newtyped postfix_tail primary ternary unary +lvk array_dynamic_literal array_literal_tokens bin blockexpr call chk_reset closure ctx_pop drop_attach enhcall expr field fn_begin heap_box incdec kwcall methcall newtyped postfix_tail primary printcall ternary unary lvkind closure expr incdec newloc reserve_locals unary vf_effect vf_store lvvalue newloc reserve_locals vf_store mdbg chk_reset kw_memdebug @@ -497,6 +497,7 @@ qp readsrc src_push srcend qst readsrc src_push srcend readonly_types tconst types_init receiver_methods add_method zc_init +ret_borrowed fndef retcheck rsbase chk_unit rscap chk_unit rs_put rsn chk_unit rs_put @@ -705,27 +706,27 @@ uname add_unop reserve_unary uncap reserve_unary useq add_unop reserve_unary vislo build_image_grow chk_unit start syntax_resident -xaf af_forget args array_cap array_dynamic_literal array_len array_literal_tokens array_pop bin call chk_reset drop_attach drop_member expr field heap_box incdec kw_share_value kwcall methcall newtyped postfix_tail primary reassign_free ternary unary -xbor array_cap array_dynamic_literal array_len array_literal_tokens array_pop assigncheck bin blockexpr call chk_reset closure discard drop_attach drop_member enhcall field fn_begin heap_box kw_share_value kwcall methcall postfix_tail primary reassign_free ternary typearg unary +xaf af_forget args array_cap array_dynamic_literal array_len array_literal_tokens array_pop bin call chk_reset drop_attach drop_member expr field heap_box incdec kw_share_value kwcall methcall newtyped postfix_tail primary printcall reassign_free ternary unary +xbor array_cap array_dynamic_literal array_len array_literal_tokens array_pop assigncheck bin blockexpr call chk_reset closure discard drop_attach drop_member enhcall field fn_begin heap_box kw_share_value kwcall methcall postfix_tail primary printcall reassign_free ternary typearg unary xc_file xc_init xc_fn closure fndef xc_hook xc_init xc_on xc_init xc_src unit_stdin xcls array_cap array_len array_pop array_push array_resize chk_reset ctx_pop kw_asm kwcall quote_tokens -xconst array_dynamic_literal array_literal_tokens bin call chk_reset closure drop_attach heap_box kw_share_value methcall postfix_tail primary unary +xconst array_dynamic_literal array_literal_tokens bin call chk_reset closure drop_attach heap_box kw_share_value methcall postfix_tail primary printcall unary xcval array_literal_tokens primary unary -xfloat array_dynamic_literal array_len array_literal_tokens array_pop array_push array_resize bin blockexpr call closure drop_attach expr fcoerce femit ftest heap_box incdec kw_cfunc kw_for methcall postfix_tail primary reassign_free ternary unary +xfloat array_dynamic_literal array_len array_literal_tokens array_pop array_push array_resize bin blockexpr call closure drop_attach expr fcoerce femit ftest heap_box incdec kw_cfunc kw_for methcall postfix_tail primary printcall reassign_free ternary unary xgive argcheck args chk_reset xlend assigncheck expr unary -xlenloc array_len bin call closure enhcall methcall postfix_tail primary unary +xlenloc array_len bin call closure enhcall methcall postfix_tail primary printcall unary xprojection ctx_pop field postfix_tail primary xshadow assigncheck call shadow_take -xstamp array_dynamic_literal array_len bin call closure enhcall methcall newtyped postfix_tail primary unary -xstr array_dynamic_literal array_len array_literal_tokens bin blockexpr call closure drop_attach heap_box incdec methcall postfix_tail primary reassign_free unary -xty args array_cap array_dynamic_literal array_len array_literal_tokens array_pop array_push array_resize bin blockexpr call closure ctx_pop drop_attach drop_member enhcall expr fcoerce field ftest heap_box kw_cfunc kw_share_value kw_sizeof kw_typeof kwcall methcall newtyped postfix_tail primary reassign_free ternary unary +xstamp array_dynamic_literal array_len bin call closure enhcall methcall newtyped postfix_tail primary printcall unary +xstr array_dynamic_literal array_len array_literal_tokens bin blockexpr call closure drop_attach heap_box incdec methcall postfix_tail primary printcall reassign_free unary +xty args array_cap array_dynamic_literal array_len array_literal_tokens array_pop array_push array_resize bin blockexpr call closure ctx_pop drop_attach drop_member enhcall expr fcoerce field ftest heap_box kw_cfunc kw_share_value kw_sizeof kw_typeof kwcall methcall newtyped postfix_tail primary printcall reassign_free ternary unary xub bin xubat bin xvk array_dynamic_literal array_literal_tokens bin blockexpr chk_reset ctx_pop expr field fn_begin incdec kw_share_value kwcall postfix_tail primary ternary unary vf_coerce vf_effect vf_forget xvv blockexpr primary unary vf_coerce -xzero array_dynamic_literal array_literal_tokens bin blockexpr call closure drop_attach enhcall expr field heap_box incdec kwcall methcall newtyped postfix_tail primary ternary unary +xzero array_dynamic_literal array_literal_tokens bin blockexpr call closure drop_attach enhcall expr field heap_box incdec kwcall methcall newtyped postfix_tail primary printcall ternary unary -- cgit v1.2.3