You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Thodoris Sotiropoulos (Jira)" <ji...@apache.org> on 2022/02/15 01:56:00 UTC
[jira] [Closed] (GROOVY-10480) Closure that assigns null to object changes the type of variable
[ https://issues.apache.org/jira/browse/GROOVY-10480?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Thodoris Sotiropoulos closed GROOVY-10480.
------------------------------------------
Resolution: Duplicate
Duplicate of https://issues.apache.org/jira/browse/GROOVY-10356.
> Closure that assigns null to object changes the type of variable
> ----------------------------------------------------------------
>
> Key: GROOVY-10480
> URL: https://issues.apache.org/jira/browse/GROOVY-10480
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Reporter: Thodoris Sotiropoulos
> Priority: Major
>
> I have the following Groovy program
> {code:java}
> class Foo {
> void foo() {}
> }
> class Main {
> static void bar() {
> def x = new Foo();
> if (false) {
> x = null;
> }
> x.foo(); // works
> }
> static void baz() {
> def x = new Foo();
> def clos = { -> {
> x = null;
>
> }}
> x.foo(); // fails
> }
> }
> {code}
> h3. Actual behaviour
> The assignment that takes inside closure changes the type of variable `x`, even though closure is never being called.
> The compilerthen rejects the program by raising the following error message.
> {code}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> test.groovy: 22: [Static type checking] - A closure shared variable [x] has been assigned with various types and the method [foo()] does not exist in the lowest upper bound of those types: [java.lang.Object]. In general, this is a bad practice (variable reuse) because the compiler cannot determine safely what is the type of the variable at the moment of the call in a multithreaded context.
> @ line 22, column 5.
> x.foo();
> ^
> 1 error
> {code}
> h3. Expected behaviour
> Compile successfully
> Tested against master (https://github.com/apache/groovy/commit/5601ea14304b67e71586b0196e38f90fa1a46f5b).
--
This message was sent by Atlassian Jira
(v8.20.1#820001)