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

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

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

   ## ✅ All tests passed ✅
   
   🏷️ Commit: 6611aceab19955d91336a05f477e899c3296bf33
   ▶️ Tests:  118681 executed
   ⚪️ Checks: 32/32 completed
   
   ---
   _Learn more about TestLens at 
[testlens.app/docs](https://testlens.app/docs/features/pr-comment/)._
   




> STC: instanceof narrowing leaks from one switch case into subsequent cases
> --------------------------------------------------------------------------
>
>                 Key: GROOVY-12428
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12428
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 5.0.3, 5.1.3, 6.0.0
>            Reporter: Jochen Kemnade
>            Priority: Major
>              Labels: patch, patch-available
>         Attachments: 
> 0001-GROOVY-12428-STC-flow-typing-narrowing-leaks-from-on.patch
>
>
> h3. Reproducer 1
> {code:groovy}
> import groovy.transform.CompileStatic
> @CompileStatic
> void check(String type, Object value) {
>     switch (type) {
>         case 'text':
>             assert value instanceof String
>             break
>         case 'integer':
>             assert value instanceof Integer
>             break
>         case 'boolean':
>             assert value instanceof Boolean
>             break
>     }
> }
> check('integer', 42)
> {code}
> With Groovy 6.0.0 this fails to compile:
> {noformat}
> Incompatible instanceof types: java.lang.String and java.lang.Integer
>  @ line 10, column 20.
> Incompatible instanceof types: java.lang.String and java.lang.Boolean
>  @ line 13, column 20.
> {noformat}
> h3. Reproducer 2
> {code:groovy}
> import groovy.transform.CompileStatic
> @CompileStatic
> void check(String type, Object value) {
>     switch (type) {
>         case 'text':
>             assert value instanceof String
>             break
>         case 'integer':
>             println value.toUpperCase()
>             break
>     }
> }
> check('integer', 42)
> {code}
> With Groovy 5.0.3, 5.1.3, and 6.0.0 this compiles and fails at runtime:
> {noformat}
> java.lang.ClassCastException: class java.lang.Integer cannot be cast to class 
> java.lang.String
> {noformat}
> h3. Expected result
> Each case ends with {{{}break{}}}, so the narrowing of {{value}} to 
> {{String}} in the first case should not apply to the other cases. Reproducer 
> 1 should compile. Reproducer 2 should be rejected with "Cannot find matching 
> method java.lang.Object#toUpperCase()".



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to