GROOVY-11983: STC: unsound smart-cast in else branch of if (cond && !…#2509
Merged
Conversation
There was a problem hiding this comment.
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 forcond && !instanceof. - Updated STC’s else/false-branch narrowing inversion to be conditional (guarded by a new predicate).
- Introduced
canInvertNarrowingForElseBranchto 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 Report❌ Patch coverage is
Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
…(x instanceof Y))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…(x instanceof Y))