Unannotated methods of the same class: a parameter's state is unknown inside, and the method's effect is lost at the call site

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

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
35/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
java

Rechercherichtung

Start with the two-file Java reproducer in the issue and trace how private same-class methods are checked and how their effects are propagated at call sites. Compare the reported error inside rewind with the missing error after rewind(br); also consider the described exception and initCause case. Done means the reproducer reports no error inside rewind and flags the second br.reset().

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

enhancement

Impact: a false error inside a helper, and a missed bug at its call site.

Description

A private (or otherwise unannotated) method of the same class that takes a typestate object as a parameter is checked as if the parameter could be in any state, and its effect on the argument is not applied at the call site. In the reproducer:

  1. inside rewind, r.reset() is reported ("found true but expected marked(r)") although its only caller marks the reader first;
  2. at the call site, br is still taken to be marked after rewind(br), so the second br.reset(), which throws at run time, is not reported.

The same applies to objects that reach a method by other ways the file shows, e.g. an exception thrown by a method of the same class and caught by its caller: the catch parameter's state is unknown, so every initCause on it is reported, whether or not the thrower had set a cause.

Minimal reproducer (two files)

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

@ExternalRefinementsFor("java.io.BufferedReader")
@StateSet({"open", "marked", "closed"})
interface BufferedReaderRefinements {
    @StateRefinement(to = "open(this)")
    void BufferedReader(java.io.Reader in);

    @StateRefinement(from = "open(this)")
    @StateRefinement(from = "marked(this)")
    String readLine();

    @StateRefinement(from = "open(this)", to = "marked(this)")
    @StateRefinement(from = "marked(this)")
    void mark(int readAheadLimit);

    @StateRefinement(from = "marked(this)", to = "open(this)")
    void reset();

    @StateRefinement(from = "!closed(this)", to = "closed(this)")
    void close();
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

public class Repro {
    static void read(Reader in) throws IOException {
        BufferedReader br = new BufferedReader(in);
        br.mark(100);
        rewind(br);
        br.reset();   // rewind() already reset br, so it is no longer marked: not reported
    }

    private static void rewind(BufferedReader r) throws IOException {
        r.reset();    // reported: "found true but expected marked(r)", though every caller here marks r first
    }
}

Expected: no error inside rewind (every caller passes a marked reader), and an error at the second br.reset() in read.

Actual:

State Refinement Error: found true but expected marked(r²⁴)
14 |         r.reset();    // reported: "found true but expected marked(r)", though every caller here marks r first

and nothing at line 10.

Possible directions: a way to write the state a parameter needs and leaves (e.g. @StateRefinement(from = "marked(r)", to = "open(r)") on the method), and/or inferring it for private methods of the same class from their bodies and call sites.

Vorherrschende Sprache
Java
Sterne
73
Forks
38
Ø Merge
12 Std. 32 Min.
Gemergte PRs (30 T.)
45

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.