False error: an anonymous subclass (new Anon() {}) does not get the state of the constructor it calls

Offen
#470 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
69/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
java
Bereich
devtools

Rechercherichtung

Start by reading RefinementTypeChecker.visitCtNewClass and comparing it with visitCtConstructorCall, then inspect AuxStateHandler.onlySuperCall and delegatedConstructorState. Use the minimal reproducer in the issue to verify the behavior; it is done when the anonymous subclass with no instance or field initializer inherits the constructor's ready state and verification passes.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug

Impact: an object created as an anonymous subclass (new Anon() {}) has an unknown state, so the first state-refined call on it is a false error. The hint then suggests the very constructor that was called.

Minimal reproducer

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

@StateSet({"fresh", "ready"})
public abstract class Anon {
    @StateRefinement(to = "ready(this)")
    public Anon() {}

    @StateRefinement(from = "ready(this)", to = "fresh(this)")
    public void use() {}

    static void ok() {
        Anon a = new Anon() {};
        a.use();   // the constructor made a ready
    }
}

Expected: Correct! Passed Verification.; the anonymous class has no initializer, so a is in the state Anon() gives (ready).

Actual (main @ 0053074e):

State Refinement Error: found true but expected ready(a⁶)
12 |     static void ok() {
13 |         Anon a = new Anon() {};
14 |         a.use();   // the constructor made a ready
   |         ^^^^^^^^
15 |     }
16 | }

Hints:
  - The state of a is unknown here. To get it to ready, use: new Anon()

Control: with a non-abstract Anon and Anon a = new Anon();, it verifies.

Cause. RefinementTypeChecker.visitCtNewClass only calls super.visitCtNewClass. Unlike visitCtConstructorCall, it never calls mfc.getConstructorInvocationRefinements, so the super constructor's to state is not applied. AuxStateHandler.delegatedConstructorState also returns empty for every CtNewClass, because the anonymous body may change the state. Applying the super constructor's state when the anonymous body has no instance initializer or field initializer (the same condition AuxStateHandler.onlySuperCall uses) would fix it.

🤖 Generated with Claude Code

Vorherrschende Sprache
Java
Sterne
71
Forks
37
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
46

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus liquid-java/liquidjava

Alle Issues in liquid-java/liquidjava

Ähnliche Issues

Weitere Issues zu Java

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.