Skip to content

Reject non-exhaustive nested sealed record patterns in switch #5182#5192

Open
kamilkrzywanski wants to merge 1 commit into
eclipse-jdt:masterfrom
kamilkrzywanski:issue_5182
Open

Reject non-exhaustive nested sealed record patterns in switch #5182#5192
kamilkrzywanski wants to merge 1 commit into
eclipse-jdt:masterfrom
kamilkrzywanski:issue_5182

Conversation

@kamilkrzywanski

Copy link
Copy Markdown
Contributor

Summary

  • Fixes incorrect exhaustiveness for switches on sealed types when a case uses a record pattern that only covers some nested sealed subtypes of a component (e.g. case Absolute(GlobalPosition p) without covering Absolute(StartPosition)).
  • ECJ previously treated the permitted record type as covered by type name alone, accepted the switch, and deferred failure to a runtime MatchException. javac correctly rejects the same code.
  • For permitted record types, reuse existing record-pattern coverage (RNode / CoverageCheckerVisitor) so nested components are checked (JLS 14.11.1.1). Type patterns such as case Absolute a still fully cover the record.
  • Adds regression tests in SwitchPatternTest.

Fixes #5182

Test plan

  • Reproduced with ECJ (accepted) vs javac (rejected) using the issue sample
  • After fix, ECJ reports non-exhaustive switch expression for the sample
  • Exhaustive nested cases and type-pattern coverage still compile
  • SwitchPatternTest — 334 tests, 0 failures

…-jdt#5182

When exhaustiveness checks a sealed switch selector, a case whose type
is a permitted record was treated as covering that record even if nested
record patterns only matched some of the component's sealed subtypes
(e.g. Absolute(GlobalPosition) without Absolute(StartPosition)). For
permitted records, reuse existing record-pattern coverage so ECJ matches
javac and JLS 14.11.1.1. Add regression tests.

Fixes eclipse-jdt#5182

Signed-off-by: Kamil Krzywannski <kamilkrzywanski01@gmail.com>
@srikanth-sankaran srikanth-sankaran added this to the 4.41 M2 milestone Jul 13, 2026
@srikanth-sankaran srikanth-sankaran self-requested a review July 13, 2026 07:18
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

Successfully merging this pull request may close these issues.

ECJ accepts a non-exhaustive switch over nested sealed record patterns that javac rejects

2 participants