Internal error "Expr cannot be cast to BoolExpr" on a local assigned from an array element (`Res r = rs[0]`)

未關閉
#437 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
72/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
java

研究方向

Start in RefinementTypeChecker.visitCtArrayRead at RefinementTypeChecker.java:273, where the issue says the array element refinement is set. Reproduce with the smaller int[] example and check the SMT failure; compare the generated refinement with the expected equality form. Done when checks on locals assigned from array elements report the expected refinement or state error instead of a ClassCastException.

由索引模型根據 Issue 內容生成。

描述

bug

Impact: internal error on any check that involves a local holding an array element. A check that should pass fails (false error), and a real violation there is shown as an internal error instead of a refinement or state error.

Description

RefinementTypeChecker.visitCtArrayRead (RefinementTypeChecker.java:273) sets the element's refinement to the bare variable arrayAccess_N instead of _ == arrayAccess_N. Passing xs[0] directly to a call works, but after int c = xs[0] (or Res r = rs[0], var c = objs[0]), c's refinement is that non-boolean term, and the first SMT check that includes it fails with a Z3 ClassCastException: IntExpr for an int[], Expr for an object array. Setting the metadata to _ == arrayAccess_N would probably fix it.

Minimal reproducer (typestate)

import liquidjava.specification.StateRefinement;
import liquidjava.specification.StateSet;

@StateSet({"open", "closed"})
class Res {
    @StateRefinement(to = "open(this)")
    public Res() {}

    @StateRefinement(from = "open(this)")
    public void read() {}

    @StateRefinement(from = "open(this)", to = "closed(this)")
    public void close() {}
}

public class Repro {
    void f(Res[] rs) {
        Res r = rs[0];
        r.close();
        r.read();
    }
}

Expected

A State Refinement Error at r.close(), as when r is a parameter (control below).

Actual

Error: class com.microsoft.z3.Expr cannot be cast to class com.microsoft.z3.BoolExpr (com.microsoft.z3.Expr and com.microsoft.z3.BoolExpr are in unnamed module of loader 'app')
17 |     void f(Res[] rs) {
18 |         Res r = rs[0];
19 |         r.close();
   |         ^^^^^^^^^^
20 |         r.read();
21 |     }

Control: the same calls on a parameter Res r

State Refinement Error: found true but expected open(r⁹)

Smaller form, without typestate

import liquidjava.specification.Refinement;

public class Repro {
    static void positive(@Refinement("_ > 0") int x) {}

    void f(int[] xs) {
        int c = xs[0];
        positive(c);
    }
}
Error: class com.microsoft.z3.IntExpr cannot be cast to class com.microsoft.z3.BoolExpr (com.microsoft.z3.IntExpr and com.microsoft.z3.BoolExpr are in unnamed module of loader 'app')
 6 |     void f(int[] xs) {
 7 |         int c = xs[0];
 8 |         positive(c);
   |         ^^^^^^^^^^^^

The reviewers' forms Res r = s.get()[0] (Supplier<Res[]>), Res r = rs[s.getAsInt()], and var c = objs[0]; any(c) (Object[]) give the same error.

Reproduced on main at 1a506fea (liquidjava-verifier 0.0.36).

Context

Found while reviewing #432. Related: #425 (crash on an array read whose element type cannot be resolved).

🤖 Generated with Claude Code

主要語言
Java
星號
73
分支
38
平均合併
12 小時 32 分鐘
30 天內合併 PR
45

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

liquid-java/liquidjava 的其他 Issue

查看 liquid-java/liquidjava 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。