Skip to content

GROOVY-11983: STC: unsound smart-cast in else branch of if (cond && !…#2509

Merged
paulk-asert merged 1 commit into
apache:masterfrom
paulk-asert:groovy11983
May 3, 2026
Merged

GROOVY-11983: STC: unsound smart-cast in else branch of if (cond && !…#2509
paulk-asert merged 1 commit into
apache:masterfrom
paulk-asert:groovy11983

Conversation

@paulk-asert
Copy link
Copy Markdown
Contributor

…(x instanceof Y))

Copy link
Copy Markdown

Copilot AI left a comment

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-11983 by preventing Static Type Checking (STC) from performing an unsound smart-cast in the else/false branch when the condition uses && together with !instanceof, which could otherwise lead to an erroneous checkcast and runtime ClassCastException.

Changes:

  • Added regression tests covering if/else, ternary, and early-return forms for cond && !instanceof.
  • Updated STC’s else/false-branch narrowing inversion to be conditional (guarded by a new predicate).
  • Introduced canInvertNarrowingForElseBranch to decide when it is safe to invert temporary type info.

Reviewed changes

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

File Description
src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy Adds GROOVY-11983 regression coverage for && + !instanceof not producing a positive smart-cast in else/false paths.
src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java Makes else/false-branch inversion of temporary instanceof tracking conditional via canInvertNarrowingForElseBranch.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 3, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.3303%. Comparing base (88ca738) to head (7a6c967).

Files with missing lines Patch % Lines
...roovy/transform/stc/StaticTypeCheckingVisitor.java 75.0000% 3 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #2509        +/-   ##
==================================================
+ Coverage     67.3235%   67.3303%   +0.0068%     
- Complexity      32253      32272        +19     
==================================================
  Files            1490       1490                
  Lines          125044     125061        +17     
  Branches        22505      22518        +13     
==================================================
+ Hits            84184      84204        +20     
+ Misses          33535      33532         -3     
  Partials         7325       7325                
Files with missing lines Coverage Δ
...roovy/transform/stc/StaticTypeCheckingVisitor.java 87.4253% <75.0000%> (-0.0747%) ⬇️

... and 5 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.

@paulk-asert paulk-asert merged commit af95d66 into apache:master May 3, 2026
25 checks passed
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