Skip to content

GROOVY-12289: Switch expressions with duplicate case labels compile under @TypeChecked but fail under @CompileStatic - #2826

Merged
paulk-asert merged 1 commit into
apache:masterfrom
paulk-asert:groovy12289
Aug 23, 2026
Merged

GROOVY-12289: Switch expressions with duplicate case labels compile under @TypeChecked but fail under @CompileStatic#2826
paulk-asert merged 1 commit into
apache:masterfrom
paulk-asert:groovy12289

Conversation

@paulk-asert

Copy link
Copy Markdown
Contributor

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.2651%. Comparing base (01f91d4) to head (7dedae9).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...sgen/asm/sc/StaticTypesSwitchExpressionWriter.java 75.0000% 0 Missing and 1 partial ⚠️
...roovy/transform/stc/StaticTypeCheckingVisitor.java 91.6667% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #2826        +/-   ##
==================================================
+ Coverage     70.2594%   70.2651%   +0.0057%     
- Complexity      36274      36283         +9     
==================================================
  Files            1569       1569                
  Lines          133723     133725         +2     
  Branches        24637      24638         +1     
==================================================
+ Hits            93953      93962         +9     
+ Misses          31257      31249         -8     
- Partials         8513       8514         +1     
Files with missing lines Coverage Δ
...sgen/asm/sc/StaticTypesSwitchExpressionWriter.java 93.9394% <75.0000%> (+0.1894%) ⬆️
...roovy/transform/stc/StaticTypeCheckingVisitor.java 87.1972% <91.6667%> (+0.0133%) ⬆️

... and 10 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses GROOVY-12289 by making duplicate constant case labels in switch expressions consistently rejected by the static type checker (so @TypeChecked and @CompileStatic fail the same way), while preserving dynamic “first match wins” behavior when type checking is explicitly bypassed (e.g., TypeCheckingMode.SKIP or a type-checking extension).

Changes:

  • Add a static type checking pass that detects duplicate constant case labels (int-family, String, enum constants) in switch expressions and reports a consistent STC error.
  • Update the static switch-expression bytecode writer to treat duplicates (when type checking is bypassed) as a reason to skip intrinsic switch optimizations and fall back to sequential dispatch.
  • Add regression tests covering dynamic semantics, STC/SC error reporting, skip-mode fallback behavior, and an extension-based “escape hatch”.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/test/groovy/org/codehaus/groovy/classgen/Jep361SwitchExpressionTest.groovy Adds coverage for duplicate-label behavior across dynamic, @TypeChecked, and @CompileStatic.
src/test/groovy/org/codehaus/groovy/classgen/asm/sc/SwitchExpressionStaticCompileTest.groovy Updates expected error messaging and adds a skip-mode fallback test for sequential dispatch semantics.
src/test/groovy/groovy/transform/stc/TypeCheckingExtensionsTest.groovy Adds a test demonstrating a type-checking extension can opt a DSL method out of the duplicate-label error.
src/test-resources/groovy/transform/stc/Groovy12289Extension.groovy Introduces the STC extension used by the new escape-hatch test.
src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java Implements duplicate constant label detection during STC for switch expressions.
src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesSwitchExpressionWriter.java Removes late duplicate-label hard error and instead skips optimizer to allow sequential dispatch when checking is bypassed.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java Outdated
@testlens-app

testlens-app Bot commented Aug 23, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: ec38a5d
▶️ Tests: 111722 executed
⚪️ Checks: 31/31 completed


Learn more about TestLens at testlens.app/docs.

@paulk-asert
paulk-asert merged commit 7f877b3 into apache:master Aug 23, 2026
31 of 32 checks passed
@paulk-asert
paulk-asert deleted the groovy12289 branch August 23, 2026 21:28
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.

3 participants