daniellansun commented on code in PR #3381:
URL: https://github.com/apache/groovy/pull/3381#discussion_r4764700576


##########
subprojects/groovy-contracts/src/test/groovy/org/apache/groovy/contracts/tests/other/GenericTypeTests.groovy:
##########
@@ -126,7 +126,7 @@ class GenericTypeTests extends BaseTestClass {
             @groovy.transform.CompileStatic
             @Invariant({ property.size() >= 0 })
             class A<T extends java.util.List> {
-                T property = []
+                T property = (T) []

Review Comment:
   Good catch — that line in `GenericTypeTests.groovy` was indeed a smell, and 
your point uncovered a real inconsistency between assignment checking and 
explicit casts to type variables at both compile time and runtime.
   
   Two separate things were going on:
   
   1. **Test fixture (`GenericTypeTests.groovy`):** `class A<T extends List>` 
initializing `T property = []` (or `(T) []`) is unsound because `T` could be 
instantiated as `LinkedList`, leaving an `ArrayList` in a `LinkedList` field. I 
updated the test to pass `property` through `A(T property)` instead of using an 
unchecked cast.
   
   2. **Explicit casts `(T) expr` at compile time and runtime:** Previously, 
STC checked `(T) expr` against `T`'s erasure using Groovy's coercion rules, and 
`@CompileStatic` lowered it through `ScriptBytecodeAdapter.castToType`. That 
meant an explicit cast could trigger Groovy type transformations (`(T) []` for 
`T extends Set`, `(T) 42` for `T extends BigDecimal`, `(T) "$x"` for `T extends 
String`, `(T) { }` for `T extends Runnable`) that have no meaningful target 
runtime type when `T` is erased.
   
      This is now tightened at both stages:
      - **Compile time (`CastCompatibilityChecker` + 
`StaticTypeCheckingVisitor.checkCast`):** Explicit casts targeting or sourced 
from a type variable now follow JLS §5.5.1 reference-cast rules without Groovy 
type transformations. Literal coercions are checked against the literal's 
actual runtime class (`ArrayList` for `[]`, `LinkedHashMap` for `[:]`, 
`Closure` for closures/method pointers, `GString` for interpolated strings), so 
`(T) []` is rejected for `T extends Set` or `T extends LinkedList`, `(T) 42` is 
rejected for `T extends BigDecimal`, `(T) "$x"` is rejected for `T extends 
String`, and `(T) { }` is rejected for `T extends Runnable`. All upper bounds 
of multi-bound type variables and provably distinct parameterized bounds are 
checked. For `T extends List`, `(T) []` remains an unchecked reference downcast 
only because `ArrayList` implements `List` (same as `(T) new ArrayList<>()` in 
Java), whereas implicit `T property = []` stays rejected because `ArrayList` is 
not guarantee
 d to be a subtype of `T`.
      - **Runtime (`CastExpressionOptimizer` + `AsmClassGenerator`):** Under 
`@CompileStatic`, non-`as` casts to a type variable now set `strict = true` so 
bytecode emits a direct JVM `CHECKCAST` against the erasure (after boxing 
primitives) instead of calling `castToType`.
   
   I also documented this in `COMPATIBILITY.md` and added compile-time and 
`@CompileStatic` runtime tests covering these cases in `GenericsSTCTest`.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to