Repository navigation
Support .length on array expressions and spread varargs calls - #400
CatarinaGamboa wants to merge 4 commits into
Conversation
A `.length` read was translated to the refinement `_ == length(<target>)` by printing the target expression into the refinement string, so any target other than a plain name (`grid[i]`, `o.arr()`, `this.arr`, `r.arr`) produced a string the refinement parser rejects, reported as "Syntax Error: Invalid refinement expression" on code with no refinements at all. Only a local variable or parameter can be named in a refinement, so only those keep the `length(x)` refinement; the length of any other array expression is now an unconstrained int, which is sound (it can only lose information). Fixes #348 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Arguments were matched one-to-one with the method parameters, so a call to a varargs method with more arguments than parameters indexed past the parameter list and crashed (`Index 1 out of bounds for length 1`), and a call with zero or one spread argument was checked against the varargs parameter as if that argument were the array itself. When a call passes its trailing arguments individually (the argument count differs from the parameter count, or the last argument is not a subtype of the array type and not null), the varargs parameter is now bound to a fresh array instance whose only refinement is its length (the number of spread arguments). Refinements on the varargs parameter are checked against that array; the individual elements are not checked against anything. Passing an explicit array keeps the one-to-one mapping. Parameter checking now iterates over the mapped parameters instead of the arguments. Fixes #349 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `PartialSourcePosition only contains a CompilationUnit` crash came from Spoon 10.4.2 at the old compliance level; it no longer reproduces since Spoon 11.5.0 and compliance level detection (#352, #368). Pattern switches need Java 21, above the level of liquidjava-example (20, where they fail to compile and produce a position-less warning), so the tests verify a temporary project that declares release 21: a pattern switch expression and statement (with a guard and the pattern variable used in the case body) verify cleanly, and a violation inside a pattern case body is still reported. Fixes #345 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Codex Review: Spread varargs calls lose their return refinement inside arithmetic expressions. For example: @Refinement("_ == length(xs)")
static int count(int... xs) { return xs.length; }
@Refinement("_ == 3") int n = count(1, 2) + 1;I ran this against this PR; it reports a Refinement Error because the call becomes an unconstrained |
Arithmetic operands and loop havoc looked methods up by the call's argument count, so a call that spreads its varargs missed the declaration: its return refinement was lost inside an expression, and a typestate-changing varargs call in a loop did not havoc its receiver, letting a post-loop state violation pass. Both now use the number of parameters the method declares. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Addressed in 7f18649. Your exact example (
🤖 Generated with Claude Code |
Fixes #448 ## Root cause `RefinementTypeChecker.visitCtNewArray` builds the `length(_) == n` refinement of a new array only from its size expressions (`new byte[n]`). An array initializer (`new byte[] {1, 2}`, or `{...}` in a declaration) has no size expression, so the new array got no refinement at all and its length was unknown. When the issue was filed, the argument with no refinement was wrapped as `arg == true`, which Z3 rejected (`Sorts |byte[]| and Bool are incompatible`). Since #496 (an argument with no known refinement is unconstrained), the crash is gone, but the correct call is still rejected because nothing is known about `length(b)`. ## Fix When a `CtNewArray` has no size expressions, its refinement is `length(_) == <number of elements>`. This covers every element type, nested initializers (the outer length is the number of rows), and empty initializers (`new int[] {}`, `int[] e = {};`). ## Before / after ```java static void put(byte[] b, @refinement("_ >= 0 && _ <= length(b)") int n) { } put(new byte[] {1, 2}, 2); put(new byte[] {1, 2}, 3); ``` | call | main (e3b11c8) | this PR | |---|---|---| | `put(new byte[] {1, 2}, 2)` | `Refinement Error: found n == 2 but expected n >= 0 && n <= length(b)`, counterexample `length(b) == 0` | `Correct! Passed Verification.` | | `put(new byte[] {1, 2}, 3)` | same error, counterexample `length(b) == 0` | `Refinement Error: found n == 3 && length(b) == 2 but expected n >= 0 && n <= length(b)` | I also checked these by hand: `int[]`, `String[]`, `int[][] {{1}, {2, 3}}` (length 2), `new int[] {}`, `int[] e = {}`, and `@Refinement("length(_) == 2") int[] f = {4, 5}` all pass. The same calls with a wrong length give a Refinement Error. ## Test I extended `CorrectStringArrayLength` with one call that passes an initializer and one that passes an empty declaration initializer, both against an exact `_ == length(names)` refinement. The fixture fails on main. I added no Error fixture, because main already reports an error for a wrong length (only for the wrong reason), so such a fixture would pass without the fix. The exact `==` refinement already catches a wrong element count. The full `mvn test` passes locally. ## Relation to #400 #400 (`.length` on array expressions, spread varargs) gives a spread varargs call a fresh array with `length == number of arguments`. This PR handles explicit initializers, the case #400's `count(new String[] { "x" })` passes through the normal argument path. The two PRs touch different methods and do not conflict. ## Not changed here `new int[2][3]` gets `length == 3`, because the loop over size expressions keeps the last one (the existing `TODO only working for 1 dimension`). So `grid(new int[2][3], 3)` against `_ == length(b)` is wrongly accepted. This bug is older than this PR and is not part of it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Fixes #348, fixes #349, closes #345.
#348:
.lengthon array expressions..lengthwas translated to_ == length(<target>)by printing the target into the refinement string, so any target other than a plain name (grid[i],o.arr(), and also field targets such asthis.arrandr.arr) failed to parse and gave a spurious syntax error. Only a local variable or parameter now keeps thelength(x)refinement; other array expressions get an unconstrainedint, which only loses information.Fixtures:
CorrectLengthOfArrayExpression,ErrorLengthOfArrayExpression(grid[i].length - 10passed to a_ >= 0parameter is still rejected).#349: varargs calls.
MethodsFunctionsChecker.mapInvocationandcheckParametersmatched arguments one-to-one with parameters. The newspreadsVarargscheck detects a call that passes its trailing arguments individually (argument count differs, or the last argument is neither null nor an array subtype). The varargs parameter is then bound to a fresh array whose only refinement is its length, so refinements such aslength(xs) > 0are checked; an explicit array keeps the one-to-one mapping.checkParametersnow iterates over the mapped parameters. Covered: zero, one and several spread arguments, an explicit array, constructors, a call inside an arithmetic expression.Fixtures:
CorrectVarargsCall,ErrorVarargsCall(bad value for a refined non-varargs parameter),ErrorEmptyVarargsCall(first()againstlength(xs) > 0).#345: type-pattern switch. No longer reproduces on main (Spoon 11.5.0 and compliance detection, #368/#352).
liquidjava-examplecompiles at Java 20, where pattern switches do not compile, so the regression test isTestPatternSwitch, which verifies a temporary release-21 project: a guarded pattern switch using the pattern variable verifies, and a violation inside a case body is reported on the right line. The VS Code language server still pins Spoon 10.4.2 and will keep crashing until it is bumped.Operand and loop lookups.
OperationsCheckerand loop havoc (RefinementTypeChecker.havocChangedIn) now find methods by their declared parameter count (Utils.declaredArity), so a spread varargs call keeps its return refinement inside an expression and a typestate-changing varargs call in a loop havocs its receiver (ErrorVarargsLoopState). A return refinement that mentions parameters is still not linked to the arguments when the call is an operand, for any method (#405).Follow-ups, not changed here:
length(x)is not known to be non-negative, so a return refinement_ >= 0onnames.lengthfails (pre-existing, False error: array length is not known to be non-negative #404).Validation:
mvn testpasses all 419 tests (255 TestExamples cases).🤖 Generated with Claude Code