[guards] Inconsistency when compiling guard with mutable local

Remi Forax forax at univ-mlv.fr
Tue Oct 11 16:14:39 UTC 2022


----- Original Message -----
> From: "Tagir Valeev" <amaembo at gmail.com>
> To: "amber-dev" <amber-dev at openjdk.org>
> Sent: Tuesday, October 11, 2022 4:01:30 PM
> Subject: [guards] Inconsistency when compiling guard with mutable local

> Hello!
> 
> Consider the following code
> 
> class Test {
>    void test(Object o) {
>        int s = 0;
>        switch (new Object()) {
>            case Integer i when (s = 1) > 0 -> {}
>            default -> {}
>        }
>    }
> }
> 
> With --enable-preview it's compiled successfully (tried 20-ea+18-1281)
> 
> If we change (s = 1) to (s++), it cannot be compiled anymore with
> quite expected error message:
> 
> Test.java:5: error: local variables referenced from a guard must be
> final or effectively final
>            case Integer i when (s++) > 0 -> {}
>                                 ^
> 
> Likely, the first sample should be rejected as well.

I agree.

> 
> With best regards,
> Tagir Valeev.

Rémi


More information about the amber-dev mailing list