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

Aperta
#500 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
compilers

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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).

Lingua principale
Java
Stelle
73
Fork
38
Merge medio
12h 32m
PR unite (30g)
45

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di liquid-java/liquidjava

Tutte le issue di liquid-java/liquidjava

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.