[
https://issues.apache.org/jira/browse/GROOVY-12562?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18135263#comment-18135263
]
ASF GitHub Bot commented on GROOVY-12562:
-----------------------------------------
codeconsole commented on PR #3091:
URL: https://github.com/apache/groovy/pull/3091#issuecomment-6526846654
@daniellansun thanks.
1. The set lived as long as the visitor, and `StaticTypesTransformation` /
`StaticCompileTransformation` create one visitor per annotated class or method,
so it was bounded by that, not by the compilation unit. It is now tighter
(2b334f3834): `visitClass`, `visitConstructorOrMethod` and
`visitObjectInitializerStatements` each start a fresh set and restore the
enclosing one when they finish, so a member's guards are dropped once the
member is checked. It is a swap rather than a `clear()` because
`inferReturnTypeOnDemand` checks another method from inside an expression, and
anonymous inner classes can be visited inline; a clear there would drop the
guards of the condition being visited. `testInstanceOf33` covers that case (`if
(check1(x) && x instanceof String) x.length()` with `check1` inferred on
demand); its `@CompileStatic` variant fails with a naive per-member clear.
Closures are visited inline and share their member's set. 76e9cc928b also
corrects the issue number in the test comments (GROOVY-12492 was the wrong one).
2. `visitExpressionStatement` behaves as on master: master already pushed a
frame per expression statement and popped it, keeping only the assignment
marker; I moved that body into `popTemporaryTypeInfoKeepingAssignments` so the
loops can reuse it. `x instanceof String; x.length()` and `boolean b = x
instanceof String; x.length()` fail to compile on both. The behaviour change is
elsewhere: narrowing within an expression outside a guard position (`'' + (x
instanceof String) + x.length()`, `foo(x instanceof String, x.length())`, list
elements, `==` against another value, the right operand of `&`), the `== false`
/ `!= true` branch, and a use after a loop whose condition checked the
variable. Those compiled before and are now compile errors; under
`@CompileStatic` they threw `ClassCastException` whenever the check was false.
Added a Groovy 7 entry to COMPATIBILITY.md (1b856c3b9b), a paragraph in the
description, and the note on GROOVY-12562.
3. Out of scope and unchanged. `case String:` narrows through
`visitCaseStatement`, which calls `pushInstanceOfTypeInfo` directly and never
goes through the guard set, and a `catch` parameter has its declared type.
`@CompileStatic` scripts compiled against master's visitor and the branch's
give the same results for `case String:`, `case String ->`, use after a switch,
single and multi-catch, and `instanceof` inside `try`/`catch`/`finally`.
Also merged master. The `groovy.transform.stc` and
`org.codehaus.groovy.classgen.asm.sc` packages pass (4,555 tests; one `@Grab`
download failure that passes on rerun), as do the Checkstyle and CodeNarc gates.
> STC: instanceof narrows a variable where the check does not guard the code
> --------------------------------------------------------------------------
>
> Key: GROOVY-12562
> URL: https://issues.apache.org/jira/browse/GROOVY-12562
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> The type checker narrows a variable after any {{instanceof}} check on it, for
> the rest of the expression or statement, wherever the check is. Under
> {{@CompileStatic}} the program then fails with a ClassCastException:
> {code:groovy}
> @groovy.transform.CompileStatic
> String f(CharSequence cs) { 'x' + (cs instanceof String) + cs.length() }
> f(new StringBuilder('ab')) // ClassCastException: StringBuilder cannot be
> cast to String (5.0.8, 6.0.0)
> {code}
> The same holds for {{foo(cs instanceof String, cs.length())}}, {{[cs
> instanceof String, cs]}} and {{(cs instanceof String) == cs.isEmpty()}}. A
> comparison with {{false}} narrows the wrong branch, and the condition of a
> loop narrows after the loop:
> {code:groovy}
> @groovy.transform.CompileStatic
> int h(Object x) { if ((x instanceof String) == false) { return x.length() };
> 0 }
> h(42) // ClassCastException: Integer cannot be cast to String
> @groovy.transform.CompileStatic
> int g(Object x) { while (x instanceof String) { break }; x.length() }
> g(42) // ClassCastException
> {code}
> The last two fail in 4.0.33, 5.0.8 and 6.0.0, the first in 5.0.8 and 6.0.0.
> Expected: a check narrows only where its outcome guards what follows (the
> condition of an if, while, for, assert or conditional expression, and
> operands of &&, || and ! there); {{(x instanceof T) == true}} narrows like
> the check and {{== false}} like its negation; a loop condition narrows inside
> the loop only.
> PR: https://github.com/apache/groovy/pull/3091
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)