False error: a switch case on a library enum (e.g. DayOfWeek) does not know its label (Found shows #MONDAY_value)

Offen
#500 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
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
48/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
java
Bereich
compilers

Rechercherichtung

Start by comparing how the analyzer translates enum constants in if conditions and switch case labels, especially for enums declared in JDK or library code; the issue gives a reproducing example using DayOfWeek. Make both translations represent the same constant consistently, or assign enum constants stable distinct values. Done means the example no longer reports a refinement error and the diagnostic does not expose #MONDAY_value.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug

Follow-up to #494 / #497. A case label of an enum declared outside the project (a JDK or library enum such as java.time.DayOfWeek) is not linked to the same constant used elsewhere, so the case's condition adds no information. Correct code is rejected, and the message shows an internal name.

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

class LibEnum {
    // Correct: the door is opened exactly on Mondays and closed exactly on Mondays
    static void monday(DayOfWeek day) {
        Door d = new Door();
        if (day == DayOfWeek.MONDAY)
            d.open();
        switch (day) {
            case MONDAY:
                d.close();
                break;
            default:
                break;
        }
    }
}

On the #497 branch (432a508c):

State Refinement Error: found day == #MONDAY_value ? open(d) : closed(d) but expected open(d)
25 |                 d.close();
Hints:
  - d can be in closed or open here, depending on the path taken, and no sequence of calls moves all of them to open. Make d reach this line in the same state on every path

Expected: no error, since inside case MONDAY the condition day == DayOfWeek.MONDAY holds. #MONDAY_value is an internal name users are not told about.

Enums declared in the project already work. Fix direction: translate library enum constants the same way in both the if condition and the case label (or give every enum constant a stable, distinct value), so case MONDAY adds day == DayOfWeek.MONDAY.

Not needed for the error-message study (no study exercise switches on a library enum).

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.