Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sealed types][Enhanced Switch] Exhaustiveness in pattern matching switch is wrong #3166

Closed
jbyunes opened this issue Oct 26, 2024 · 4 comments
Assignees
Milestone

Comments

@jbyunes
Copy link

jbyunes commented Oct 26, 2024

As designed in JEP 441: Pattern Matching for switch, Switches and enum constants the following code is correct as it is exhaustive:

sealed interface CardClassification permits Suit, Tarot {}
public enum Suit implements CardClassification { CLUBS, DIAMONDS, HEARTS, SPADES }
final class Tarot implements CardClassification {}
static void exhaustiveSwitchWithBetterEnumSupport(CardClassification c) {
    switch (c) {
        case Suit.CLUBS -> {
            System.out.println("It's clubs");
        }
        case Suit.DIAMONDS -> {
            System.out.println("It's diamonds");
        }
        case Suit.HEARTS -> {
            System.out.println("It's hearts");
        }
        case Suit.SPADES -> {
            System.out.println("It's spades");
        }
        case Tarot t -> {
            System.out.println("It's a tarot");
        }
    }
}

Eclipse Version: 2024-09 (4.33.0) Build id: 20240905-0614, compiler compliance 22 is activated, reports it as an error and necessitate a default case which is obviously wrong:
Capture d’écran 2024-10-26 à 09 29 57

The code can be successfully compiled with any external Java compiler ≥21.

@srikanth-sankaran srikanth-sankaran self-assigned this Oct 26, 2024
@srikanth-sankaran
Copy link
Contributor

There have been numerous bug fixes in this area since 4.33. Are you able to test a recent I build to see if your problem is still there ??

@srikanth-sankaran
Copy link
Contributor

This is most likely the same issue as #2720

@srikanth-sankaran srikanth-sankaran changed the title exhaustiveness in pattern matching switch is wrong [Sealed types][Enhanced Switch] Exhaustiveness in pattern matching switch is wrong Oct 26, 2024
@srikanth-sankaran
Copy link
Contributor

This modified fuller program compiles and runs on master and prints:

sealed interface CardClassification permits Suit, Tarot {}
enum Suit implements CardClassification { CLUBS, DIAMONDS, HEARTS, SPADES }
final class Tarot implements CardClassification {}
public class X {
static void exhaustiveSwitchWithBetterEnumSupport(CardClassification c) {
    switch (c) {
        case Suit.CLUBS -> {
            System.out.println("It's clubs");
        }
        case Suit.DIAMONDS -> {
            System.out.println("It's diamonds");
        }
        case Suit.HEARTS -> {
            System.out.println("It's hearts");
        }
        case Suit.SPADES -> {
            System.out.println("It's spades");
        }
        case Tarot t -> {
            System.out.println("It's a tarot");
        }
    }
}
	public static void main(String[] args) {
		exhaustiveSwitchWithBetterEnumSupport(Suit.CLUBS);
		exhaustiveSwitchWithBetterEnumSupport(Suit.DIAMONDS);
		exhaustiveSwitchWithBetterEnumSupport(Suit.HEARTS);
		exhaustiveSwitchWithBetterEnumSupport(Suit.SPADES);
		exhaustiveSwitchWithBetterEnumSupport(new Tarot());
	}
}

compiles fine on master/HEAD and when run prints as expected:

It's clubs
It's diamonds
It's hearts
It's spades
It's a tarot

@srikanth-sankaran
Copy link
Contributor

Yes, I am able to confirm that the problem shows up before commit 4494017 and goes away with it. Closing as a duplicate of #2720

@srikanth-sankaran srikanth-sankaran closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2024
@srikanth-sankaran srikanth-sankaran added this to the 4.34 M2 milestone Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants