Skip to content

Commit

Permalink
Additional test that uses a switch expression as return expression
Browse files Browse the repository at this point in the history
directly
  • Loading branch information
srikanth-sankaran committed Oct 24, 2024
1 parent 9029a38 commit a687b5f
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1198,4 +1198,50 @@ static Stuff match(PatternMatching pm, int v) {
""";
runner.runNegativeTest();
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2522
// Pattern matching on sealed classes cannot infer NonNull (JDK 21)
public void testIssue2522_2() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"PatternMatching.java",
"""
import org.eclipse.jdt.annotation.*;
public sealed interface PatternMatching {
record Stuff() implements PatternMatching {}
@NonNull
static Stuff match(PatternMatching pm, int v) {
if (v == 0) {
return switch (pm) {
case Stuff s -> s;
case null -> throw new NullPointerException();
}; // no error here - good
} else if (v == 2) {
return switch (pm) {
case Stuff s -> s;
}; // no error here -- good
} else if (v == 3) {
return switch (pm) {
case Stuff s -> null; // get error here - good
};
}
return new Stuff();
}
}
"""
};
runner.expectedCompilerLog =
"""
----------
1. ERROR in PatternMatching.java (at line 20)
case Stuff s -> null; // get error here - good
^^^^
Null type mismatch: required 'PatternMatching.@NonNull Stuff' but the provided value is null
----------
""";
runner.runNegativeTest();
}
}

0 comments on commit a687b5f

Please sign in to comment.