[patterns] Redefinition of pattern variable is allowed by javac in some cases

Jan Lahoda jan.lahoda at oracle.com
Mon Jan 6 11:59:41 UTC 2020


Thanks Tagir, I've filled:
https://bugs.openjdk.java.net/browse/JDK-8236670

Jan

On 26. 12. 19 11:10, Tagir Valeev wrote:
> Hello!
> 
> Playing with javac 14-ea+28-1366. This code can be successfully compiled:
> 
> class Test {
>    static void test(Object o1, Object o2) {
>        boolean b = o1 instanceof String s && (!(o2 instanceof String s)
> || consume(s));
>    }
> 
>    static boolean consume(String s) {
>       System.out.println(s);
>       return true;
>    }
> 
>    public static void main(String[] args) {
>      test("x", "y"); // prints y
>    }
> }
> 
> Spec draft says for `a || b`:
> It is a compile-time error if any pattern variable introduced by a
> when false is already in scope at b.
> 
> If I understand correctly, assuming a = !(o2 instanceof String s) and
> b = consume(s), we already have `s` (from `o1 instanceof String s`) in
> the scope at b, and a introduces new `s` when false, so this code
> should be rejected. Please correct me if I am wrong.
> 
> With best regards,
> Tagir Valeev.
> 


More information about the amber-dev mailing list