diff options
| author | 2026-10-15 09:56:19 +0330 | |
|---|---|---|
| committer | 2026-10-15 09:56:19 +0330 | |
| commit | 6bbac2edf7c50dac4d755a364b6dfd16471284d7 (patch) | |
| tree | e83b493f55ed07287cd2ab4e3a940c04871d9a14 /tests | |
| parent | analyzer: use-after-end messages name the borrower, the owner and how the own... (diff) | |
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
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/errors/borrows_body.zc | 13 | ||||
| -rw-r--r-- | tests/errors/borrows_decl.zc | 14 | ||||
| -rw-r--r-- | tests/errors/borrows_extern.zc | 11 | ||||
| -rw-r--r-- | tests/errors/borrows_fntype.zc | 12 | ||||
| -rw-r--r-- | tests/errors/forward_borrow_return.zc | 11 | ||||
| -rw-r--r-- | tests/programs/borrows_clause.zc | 11 | ||||
| -rw-r--r-- | tests/programs/forward_borrow.zc | 16 | ||||
| -rwxr-xr-x | tests/test.sh | 2 |
8 files changed, 90 insertions, 0 deletions
diff --git a/tests/errors/borrows_body.zc b/tests/errors/borrows_body.zc new file mode 100644 index 0000000..e6c92c6 --- /dev/null +++ b/tests/errors/borrows_body.zc @@ -0,0 +1,13 @@ +namespace; +include rt; + +// expect: first: returns a borrow of b, but its borrows clause doesn't name it +// the body is checked against the prototype's borrows clause. +char* first(char* a, char* b) borrows a; +fn main() { + char* t = rt.new(4); t[0] = 'x'; t[1] = 0; + char* u = rt.new(4); u[0] = 'y'; u[1] = 0; + let r = first(t, u); rt.put_c(r[0]); rt.free(u); rt.free(t); + return 0; +} +char* first(char* a, char* b) { return b; } diff --git a/tests/errors/borrows_decl.zc b/tests/errors/borrows_decl.zc new file mode 100644 index 0000000..5229979 --- /dev/null +++ b/tests/errors/borrows_decl.zc @@ -0,0 +1,14 @@ +namespace; +include rt; + +// expect: r: used after it was given away; r borrows t, and t was freed on line 11 (t declared on line 8) +// a prototype's borrows clause ties the result to the first argument's owner only. +char* first(char* a, char* b) borrows a; +fn main() { + char* t = rt.new(4); t[0] = 'x'; t[1] = 0; + char* u = rt.new(4); u[0] = 'y'; u[1] = 0; + let r = first(t, u); rt.free(u); rt.put_c(r[0]); + rt.free(t); rt.put_c(r[0]); + return 0; +} +char* first(char* a, char* b) { return a; } diff --git a/tests/errors/borrows_extern.zc b/tests/errors/borrows_extern.zc new file mode 100644 index 0000000..e9285d4 --- /dev/null +++ b/tests/errors/borrows_extern.zc @@ -0,0 +1,11 @@ +namespace; +include rt; + +// expect: r: used after it was given away; r borrows t, and t was freed on line 9 (t declared on line 8) +// an extern declares its borrow return by parameter position. +extern char* strchr(char*, int) borrows 0; +fn main() { + char* t = rt.new(4); t[0] = 'x'; t[1] = 0; + let r = strchr(t, 'x'); rt.free(t); rt.put_c(r[0]); + return 0; +} diff --git a/tests/errors/borrows_fntype.zc b/tests/errors/borrows_fntype.zc new file mode 100644 index 0000000..a73b0d8 --- /dev/null +++ b/tests/errors/borrows_fntype.zc @@ -0,0 +1,12 @@ +namespace; +include rt; + +// expect: r: used after it was given away; r borrows t, and t was freed on line 10 (t declared on line 9) +// a function type carries a borrows clause, by parameter position. +char* skip(char* s) { return s + 1; } +fn main() { + char*(char*) borrows 0 f = skip; + char* t = rt.new(4); t[0] = 'x'; t[1] = 'y'; t[2] = 0; + let r = f(t); rt.free(t); rt.put_c(r[0]); + return 0; +} diff --git a/tests/errors/forward_borrow_return.zc b/tests/errors/forward_borrow_return.zc new file mode 100644 index 0000000..6c3ae93 --- /dev/null +++ b/tests/errors/forward_borrow_return.zc @@ -0,0 +1,11 @@ +namespace; +include rt; + +// expect: r: used after it was given away; r borrows t, and t was freed on line 8 (t declared on line 7); skip, defined after this call, returns a borrow of its argument +// skip is called before its body: its borrow return is checked when the body is read. +fn main() { + char* t = rt.new(8); t[0] = ' '; t[1] = 'a'; t[2] = 0; + let r = skip(t); rt.free(t); rt.put_c(r[0]); + return 0; +} +char* skip(char* s) { int i = 0; while (s[i] == ' ') i++; return s + i; } diff --git a/tests/programs/borrows_clause.zc b/tests/programs/borrows_clause.zc new file mode 100644 index 0000000..6f43581 --- /dev/null +++ b/tests/programs/borrows_clause.zc @@ -0,0 +1,11 @@ +namespace; +include rt; +// a borrows clause on a prototype and its definition: freeing the other argument keeps the result. +char* first(char* a, char* b) borrows a; +fn main() { + char* t = rt.new(4); t[0] = 'x'; t[1] = 0; + char* u = rt.new(4); u[0] = 'y'; u[1] = 0; + let r = first(t, u); rt.free(u); rt.put_c(r[0]); rt.free(t); + return 0; +} +char* first(char* a, char* b) borrows a { return a; } diff --git a/tests/programs/forward_borrow.zc b/tests/programs/forward_borrow.zc new file mode 100644 index 0000000..7cc44b1 --- /dev/null +++ b/tests/programs/forward_borrow.zc @@ -0,0 +1,16 @@ +namespace; +include rt; +// forward calls whose results do not outlive the owner, or do not borrow it, are accepted. +fn main() { + char* t = rt.new(8); t[0] = ' '; t[1] = 'a'; t[2] = 0; + let n = count(t); rt.free(t); rt.put_n(n); + let u = rt.new(8); u[0] = 'b'; u[1] = 0; + let r = skip(u); rt.put_c(r[0]); rt.free(u); + let v = rt.new(8); v[0] = 0; + let q = skip(v); rt.free(v); q = 0; rt.put_n(q); + let w = rt.new(8); w[0] = 0; + let k = skip(w)[0]; rt.free(w); rt.put_n(k); + return 0; +} +int count(char* s) { int i = 0; while (s[i]) i++; return i; } +char* skip(char* s) { int i = 0; while (s[i] == ' ') i++; return s + i; } diff --git a/tests/test.sh b/tests/test.sh index 7745a66..d8ad0fb 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -216,6 +216,8 @@ ABC" "$l o3 lifts byte loads and stores" chk "$($vm < tests/programs/o3.zc)" "3142 42 43 9 57 98781 520625 10192 -18782 1152" "$l o3 regions: loops, calls, recursion, switch, arrays, more vars than registers" chk "$($vm < tests/programs/o3scopes.zc)" "116" "$l o3 registers shared by scopes, cached fixed arrays" chk "$($vm < tests/programs/borrow_rules.zc)" "$(cat tests/programs/borrow_rules.expected)" "$l inferred transfers, projections and shared closure cells" + chk "$($vm < tests/programs/forward_borrow.zc)" "2b00" "$l forward calls: results that do not borrow, or are not used after the owner ends, are accepted" + chk "$($vm < tests/programs/borrows_clause.zc)" "x" "$l a borrows clause ties the result to the named argument only" chk "$($vm < tests/programs/replace_owner.zc)" "$(cat tests/programs/replace_owner.expected)" "$l replacing a released owner moves ownership and cleans up once" chk "$($vm < tests/programs/temporary_borrows.zc)" "$(cat tests/programs/temporary_borrows.expected)" "$l temporary borrows preserve aliases and release allocations" chk "$($vm < tests/programs/array_values.zc)" "$(cat tests/programs/array_values.expected)" "$l array initializers, designators, zero filling, nested and compound literals" |
