[ 
https://issues.apache.org/jira/browse/GROOVY-12469?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18129478#comment-18129478
 ] 

ASF GitHub Bot commented on GROOVY-12469:
-----------------------------------------

testlens-app[bot] commented on PR #2999:
URL: https://github.com/apache/groovy/pull/2999#issuecomment-6310849849

   ## 🚨 All tests passed but jobs failed 🚨
   
   ### Failed Jobs without Test Failures
   
   ❌ [Build and test / lts \(17, 
macos-latest\)](https://github.com/apache/groovy/actions/runs/39557248182/job/119079786297?pr=2999)
   
   🏷️ Commit: 7347739ce485fcfa062ba094002aca248522fe66
   ▶️ Tests:  37479 executed
   🟡 Checks: 3/30 completed
   
   ### Rerun Controls
   
   Click the checkbox to trigger a rerun:
   
   🔲 **Rerun jobs**
   
   ---
   _Learn more about TestLens at 
[testlens.app/docs](https://testlens.app/docs/features/pr-comment/)._
   




> SC: static compilation inserts wrong cast, derived from generic return type 
> when assigned to a variable
> -------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12469
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12469
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.5.23, 3.0.25, 5.0.3, 4.0.33, 5.1.3, 6.0.0
>            Reporter: Björn Kautler
>            Priority: Major
>
> Given the following snippet:
> {code:groovy}
> import java.lang.annotation.Annotation
> import java.lang.annotation.Retention
> import java.lang.annotation.Target
> import groovy.transform.CompileStatic
> @CompileStatic
> <T extends Annotation> T getRetention(Class<T> clazz) {
>   Target.getAnnotation(Target)
> }
> @CompileStatic
> def foo() {
>   Annotation x = getRetention(Retention) /*as Annotation*/
> }
> foo().toString()
> {code}
> This compiles fine under all versions, but fails with a 
> {{GroovyCastException}} in all versions.
>  
> The generated byte-code under SC has a cast on {{Retention}} in this, even 
> though the variable is of type {{Annotation}} explicitly.
> Commenting in the {{as Annotation}} cast makes it work.
> Removing the {{Annotation x =}} also makes it work.
> Removing the {{@CompileStatic}} or replacing it by {{@TypeChecked}} of course 
> also makes it work.
>  
> I'd say static-compilation should here not insert an unnecessary cast to 
> {{{}Retention{}}}, but at most a cast to {{Annotation}} as that is, what the 
> variable needs and which should be consistent with JVM type-erasure, and also 
> with the variant without the variable. Even with the method returning 
> {{Annotation}} instead of {{def}} it works just fine. Just if the method 
> declares to return {{Retention}} explicitly, the cast exception comes, but 
> then rightfully.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to