[guards] Inconsistency when compiling guard with mutable local

Tagir Valeev amaembo at gmail.com
Tue Oct 11 14:01:30 UTC 2022


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.

With best regards,
Tagir Valeev.


More information about the amber-dev mailing list