Skip to content

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

Open

Description

@CatarinaGamboa

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 (432a508):

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions