Skip to content

False error: an if condition that mentions a variable is dropped when that variable is assigned (if (n <= 10 - t) t = t + n) #538

Closed

Description

@CatarinaGamboa

Summary

When an if condition mentions a variable and that variable is then assigned on the guarded path, the condition is dropped from what is known at the assignment. A guard written exactly to keep the variable in range therefore does not prove the assignment.

Minimal reproducer

import liquidjava.specification.Refinement;

public class G {
    static int read() { return 1; }

    static void fill() {
        @Refinement("_ <= 10") int t = 0;
        int n = read();
        if (n <= 10 - t) {
            t = t + n;   // Refinement Error
        }
    }
}

Output on 0.0.38:

Refinement Error: found `t == #read_result` but expected `t <= 10`
 7 |         int n = read();
 8 |         if (n <= 10 - t) {
 9 |             t = t + n;
   |             ^^^^^^^^^^
10 |         }
11 |     }
 --> Counterexample: `t == 11 && #read_result == 11`

Expected: Correct! Passed Verification. The guard n <= 10 - t (with the old t == 0) excludes the counterexample n == 11, but it is not in Found.

The early-exit form fails in the same way:

if (n > 10 - t)
    return;
t = t + n;   // found `t == #read_result` but expected `t <= 10`

What narrows it down

The guard is kept when the variable it mentions is not the one assigned. This is accepted:

static void fill(int n) {
    int t = 0;
    if (n > 10 - t)
        return;
    @Refinement("_ <= 10") int u = t + n;   // passes
}

So it looks as if a path condition that mentions t is discarded when t gets a new version, where it should be kept about the previous version (n <= 10 - t@old).

Why it matters

Bounded accumulation is commonly guarded this way:

@Refinement("_ >= 0 && _ <= 1024") int total = 0;
while (total < 1024) {
    int n = in.read(buf, total, 1024 - total);
    if (n <= 0 || n > 1024 - total)
        break;
    total = total + n;   // rejected; counterexample total == 1025 with n == 2, which the guard excludes
}

Seen in agent-written code: after the verifier rejected a plain read loop (see the issue on loop counters), agents added the annotation and this guard, and were rejected again.

Activity

  1. CatarinaGamboa commented on Nov 23, 2026

    @CatarinaGamboa
    CollaboratorAuthor

    [Claude] Cause: when a variable is assigned, checkAssignment drops every path condition that mentions it. Each path condition is stored twice: on the plain name (n <= 10 - t, which is stale after the assignment) and on the instances at that point (n@1 <= 10 - t@0, which stays true). Fix: keep the instance copy and drop only the plain-name part. This is the prerequisite step of the loop invariant inference plan, #542.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions