Skip to content

Commit 65cc763

Browse files
Give new T[a][b] the length of its first dimension
A multi-dimensional array creation took the length of its last dimension, so length(g) of new int[2][3] was 3 and wrong programs were accepted. Only the first dimension now sets the length; inner arrays stay unmodelled. Fixes #557 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6a349c1 commit 65cc763

3 files changed

Lines changed: 20 additions & 15 deletions

File tree

‎liquidjava-example/src/main/java/testSuite/CorrectStringArrayLength.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ public static int count(@Refinement("length(names) > 0") String[] names) {
2020

2121
static void sized(String[] names, @Refinement("_ == length(names)") int n) {}
2222

23+
static void rows(String[][] grid, @Refinement("_ == length(grid)") int n) {}
24+
2325
public static void initializers() {
2426
sized(new String[] { "a", "b" }, 2);
2527
String[] none = {};
2628
sized(none, 0);
29+
rows(new String[2][3], 2);
2730
}
2831
}

‎liquidjava-example/src/main/java/testSuite/ErrorStringArrayLength.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ public static void first(@Refinement("length(names) >= 0") String[] names) {
88
@Refinement("_ > 0")
99
int size = names.length; // Expect: Refinement Error
1010
}
11+
12+
static void rows(String[][] grid, @Refinement("_ == length(grid)") int n) {}
13+
14+
public static void grid() {
15+
rows(new String[2][3], 3); // Expect: Refinement Error
16+
}
1117
}

‎liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/RefinementTypeChecker.java‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,18 @@ public <T> void visitCtNewArray(CtNewArray<T> newArray) {
261261
BuiltinFunctionPredicate.length(Keys.WILDCARD, newArray), Predicate.createLit(length, Types.INT)));
262262
return;
263263
}
264-
// TODO only working for 1 dimension
265-
for (CtExpression<?> exp : l) {
266-
Predicate c = getExpressionRefinements(exp);
267-
String name = GeneratedNames.of(GeneratedNames.ARRAY_LENGTH, context.getCounter());
268-
if (c.getVariableNames().contains(Keys.WILDCARD)) {
269-
c = c.substituteVariable(Keys.WILDCARD, name);
270-
} else {
271-
c = Predicate.createEquals(Predicate.createVar(name), c);
272-
}
273-
context.addVarToContext(name, factory.Type().INTEGER_PRIMITIVE, c, exp);
274-
Predicate ep;
275-
ep = Predicate.createEquals(BuiltinFunctionPredicate.length(Keys.WILDCARD, newArray),
276-
Predicate.createVar(name));
277-
278-
newArray.putMetadata(Keys.REFINEMENT, ep);
264+
// the length is the first dimension: inner arrays are not modelled, nothing is known about their lengths
265+
CtExpression<Integer> exp = l.get(0);
266+
Predicate c = getExpressionRefinements(exp);
267+
String name = GeneratedNames.of(GeneratedNames.ARRAY_LENGTH, context.getCounter());
268+
if (c.getVariableNames().contains(Keys.WILDCARD)) {
269+
c = c.substituteVariable(Keys.WILDCARD, name);
270+
} else {
271+
c = Predicate.createEquals(Predicate.createVar(name), c);
279272
}
273+
context.addVarToContext(name, factory.Type().INTEGER_PRIMITIVE, c, exp);
274+
newArray.putMetadata(Keys.REFINEMENT, Predicate
275+
.createEquals(BuiltinFunctionPredicate.length(Keys.WILDCARD, newArray), Predicate.createVar(name)));
280276
}
281277

282278
@Override

0 commit comments

Comments
 (0)