Skip to content

fix: preserve guarded multi-pattern hierarchy#6809

Open
martinfrancois wants to merge 1 commit into
INRIA:masterfrom
martinfrancois:agent/issue-6808-unnamed-pattern-hierarchy
Open

fix: preserve guarded multi-pattern hierarchy#6809
martinfrancois wants to merge 1 commit into
INRIA:masterfrom
martinfrancois:agent/issue-6808-unnamed-pattern-hierarchy

Conversation

@martinfrancois

@martinfrancois martinfrancois commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Preserve both patterns and the separate boolean guard in a switch label such as case First _, Second _ when enabled.

Fixes #6808.

Problem

First _ and Second _ are two alternative patterns: the first matches a First value and the second matches a Second value. enabled is a separate boolean guard. Spoon must build one CtCasePattern for each alternative and attach enabled as the CtCase guard.

ECJ stores the two patterns inside one EitherOrMultiPattern, then wraps that node and the guard condition in a GuardedPattern. For this label, the correct alternative count is two. Spoon uses that count while it receives ECJ's child nodes: the first two pattern children belong to the comma-separated label, and the following expression is the guard.

Spoon counted alternatives only when EitherOrMultiPattern was the direct case expression. The wrapper therefore made Spoon expect one pattern instead of two. Spoon attached First _ correctly, then treated the CtTypePattern for Second _ as the guard. Model construction failed because a CtTypePattern belongs under a CtCasePattern, not directly under a CtCase guard.

Change

  • Unwrap the ECJ GuardedPattern only while counting its alternatives.
  • Create one CtCasePattern for each alternative.
  • Keep each CtTypePattern below its CtCasePattern.
  • Attach the real guard to the CtCase.

Tests

  • Added a regression that checks both alternatives, their node types, all parent links, and the guard parent.
  • mvn -q -Dtest=spoon.test.pattern.SwitchPatternTest#testMultipleTypePatternsWithGuardInSwitch test passes.
  • The complete switch and record-pattern suites pass: 17 tests.
  • mvn -q -DskipTests install and mvn -q spotless:check pass.
  • StoneDetector processes OpenJDK Unnamed.java: AST 1/1, CFG 4/4, dominators 4/4, paths 4/4, and no reported errors.
  • Direct model inspection confirms that both guarded alternatives are retained.

Full test suite

The complete Java 25 suite passes 4,461 tests with 0 failures, 0 errors, and 16 skipped.

@martinfrancois
martinfrancois force-pushed the agent/issue-6808-unnamed-pattern-hierarchy branch from a06058c to 6de9d82 Compare July 17, 2026 17:17
@martinfrancois
martinfrancois marked this pull request as ready for review July 17, 2026 18:41
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.

[Bug]: Guarded multi-pattern cases lose their pattern hierarchy

1 participant