From 6bbac2edf7c50dac4d755a364b6dfd16471284d7 Mon Sep 17 00:00:00 2001 From: zinc Date: Thu, 15 Oct 2026 09:56:19 +0330 Subject: analyzer: check borrow returns of forward-called functions at the unit's end; borrows clause for prototypes, definitions, externs and function types, checked against the body --- src/compiler/builtins.zc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/compiler/builtins.zc') diff --git a/src/compiler/builtins.zc b/src/compiler/builtins.zc index d28d2fd..f40e223 100644 --- a/src/compiler/builtins.zc +++ b/src/compiler/builtins.zc @@ -1119,6 +1119,7 @@ int kw_extern() { } } expect(s_rp); + int borrowed = borrows_clause(0, np, 1); if (accept(s_eq)) { if (tk != T_STR) { die("expected the c name as a string"); return 0; } cname = tv; @@ -1194,7 +1195,10 @@ int kw_extern() { ebn10(i); i++; } - ebs(") { let b = "); ebn10(a); ebs(";"); + ebs(")"); + // the stub keeps the declaration's borrows clause, so calls see it in the signature + if (borrowed) { ebs(" borrows "); i = 0; g = 0; while (i < np) { if ((borrowed >> i) & 1) { if (g) ebs(", "); ebs("p"); ebn10(i); g = 1; } i++; } } + ebs(" { let b = "); ebn10(a); ebs(";"); // the native descriptor borrows an integer address, not ownership of r. // clear that scratch address after the call so it cannot keep the result rooted. if (sret) { ebs(" "); ebs(rn); ebs("* r = rt.new("); ebs(rn); ebs("); b[3] = core.addr_b(r) >> 3;"); } -- cgit v1.2.3