[
https://issues.apache.org/jira/browse/GROOVY-12428?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Jochen Kemnade updated GROOVY-12428:
------------------------------------
Labels: patch patch-available (was: patch-available)
> 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
(v8.20.10#820010)