mirror of
https://github.com/Nexoniarz/Finch.git
synced 2026-11-03 00:44:27 +00:00
Warnings for big copies: a struct with 256+ bytes of plain data as a parameter or in for x in list
- on stderr (finch ir stays the IR), as yellow warnings in editors (@W in the index), once per place - str, arrays and maps inside a struct don't count: passing it copies only their handles - none in the compiler, the examples or the tests other than the new one - docs: the warning instead of "not planned"; what was and wasn't checked for Android Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016GzNP6r8dg6WDxUVZy8wjL
This commit is contained in:
parent
961ba951ec
commit
d557b5505e
15 changed files with 210 additions and 18 deletions
|
|
@ -34,7 +34,8 @@ for f in run/*.fch new/*.fch; do
|
|||
failed=$((failed + 1)); echo "FAIL (build) $f"; head -5 "$tmp/err"; continue
|
||||
fi
|
||||
got=$(run_exe "$exe" "$input")
|
||||
if [ "$got" == "$(tr -d '\r' < "${f%.fch}.out")" ]; then
|
||||
# (warnings come from the build, which is not part of what the program prints)
|
||||
if [ "$got" == "$(tr -d '\r' < "${f%.fch}.out" | grep -v ': warning: ')" ]; then
|
||||
pass=$((pass + 1))
|
||||
else
|
||||
failed=$((failed + 1)); echo "FAIL $f"; diff <(echo "$got") "${f%.fch}.out" | head -10
|
||||
|
|
|
|||
|
|
@ -193,6 +193,15 @@ c.notify("textDocument/didChange", {"textDocument": {"uri": uri2, "version": 4},
|
|||
diags = c.diagnostics(uri2)
|
||||
check("error: unhandled failure", diags and "can fail" in diags[0]["message"], diags)
|
||||
|
||||
# a big struct copied on every call: a warning (yellow), not an error
|
||||
src4 = "struct V {\n" + "".join(" float f%d\n" % i for i in range(40)) + "}\n\nfn len2(V v) -> float {\n return v.f0\n}\n\nfn main() {\n print(len2(V()))\n}\n"
|
||||
path4 = os.path.join(d, "big.fch")
|
||||
open(path4, "w").write(src4)
|
||||
uri4 = pathlib.Path(path4).resolve().as_uri()
|
||||
c.notify("textDocument/didOpen", {"textDocument": {"uri": uri4, "languageId": "finch", "version": 1, "text": src4}})
|
||||
w4 = c.diagnostics(uri4)
|
||||
check("warning for a big copy", w4 and len(w4) == 1 and w4[0]["severity"] == 2 and "copied on every call" in w4[0]["message"] and w4[0]["range"]["start"]["line"] == 43, w4)
|
||||
|
||||
# a module imported under another name: completion after its name
|
||||
src3 = "import path as pt\n\nfn main() {\n print(pt.base(\"a/b\"))\n}\n"
|
||||
path3 = os.path.join(d, "alias.fch")
|
||||
|
|
|
|||
52
tests/new/big_copies.fch
Normal file
52
tests/new/big_copies.fch
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
// a struct of 256 bytes or more copied without the code saying so: a warning (the program still builds)
|
||||
struct M4 {
|
||||
float a0
|
||||
float a1
|
||||
float a2
|
||||
float a3
|
||||
float b0
|
||||
float b1
|
||||
float b2
|
||||
float b3
|
||||
float c0
|
||||
float c1
|
||||
float c2
|
||||
float c3
|
||||
float d0
|
||||
float d1
|
||||
float d2
|
||||
float d3
|
||||
}
|
||||
|
||||
struct Body {
|
||||
M4 world
|
||||
M4 local
|
||||
int id
|
||||
}
|
||||
|
||||
fn weight(Body b) -> float { // warned: 264 bytes copied on every call
|
||||
return b.world.a0 + float(b.id)
|
||||
}
|
||||
|
||||
fn nudge(ptr[Body] b) { // shared: no warning
|
||||
b.world.a0 += 1.0
|
||||
}
|
||||
|
||||
fn small(M4 m) -> float { // 128 bytes: no warning
|
||||
return m.a0
|
||||
}
|
||||
|
||||
fn main() {
|
||||
b := Body(id: 2)
|
||||
nudge(b)
|
||||
print(weight(b), small(b.world))
|
||||
bodies := [b, b]
|
||||
total := 0.0
|
||||
for x in bodies { // warned: each element copied into x
|
||||
total += x.world.a0
|
||||
}
|
||||
for i in 0..bodies.len { // in place: no warning
|
||||
total += bodies[i].world.a0
|
||||
}
|
||||
print(total)
|
||||
}
|
||||
4
tests/new/big_copies.out
Normal file
4
tests/new/big_copies.out
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
new/big_copies.fch:27:11: warning: 'b' is a Body (264 bytes), copied on every call to weight; take ptr[Body] to share it instead (callers can still pass the variable as it is)
|
||||
new/big_copies.fch:45:5: warning: each Body (264 bytes) is copied into 'x'; for i in 0..bodies.len with bodies[i] works on it in place
|
||||
3 1
|
||||
4
|
||||
Loading…
Reference in a new issue