File: DuplicateBindingTest.java

package info (click to toggle)
openjdk-25 25.0.1%2B8-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,408 kB
  • sloc: java: 5,585,680; cpp: 1,333,948; xml: 1,321,242; ansic: 488,034; asm: 404,003; objc: 21,088; sh: 15,106; javascript: 13,265; python: 8,319; makefile: 2,518; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (62 lines) | stat: -rw-r--r-- 3,736 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * @test /nodynamiccopyright/
 * @bug 8231827
 * @summary Basic pattern bindings scope test
 * @compile/fail/ref=DuplicateBindingTest.out -XDrawDiagnostics DuplicateBindingTest.java
 */

public class DuplicateBindingTest {

    int f;

    public static boolean main(String[] args) {
        Object o1 = "";
        Object o2 = "";

        if (args != null) {
            int s;
            if (o1 instanceof String s) { // NOT OK. Redef same scope.
            }
            if (o1 instanceof String f) { // OK to redef field.
            }
        }


        if (o1 instanceof String s && o2 instanceof String s) {} //error - already in scope on RHS (in scope due to LHS when true)
        if (o1 instanceof String s && !(o2 instanceof String s)) {} //error - already in scope on RHS (in scope due to LHS when true)
        if (!(o1 instanceof String s) && !(o2 instanceof String s)) {} //error - the same binding variable on LHS and RHS when false
        if (!(o1 instanceof String s) && (o2 instanceof String s)) {} //ok

        if (!(o1 instanceof String s) || o2 instanceof String s) {} //error - already in scope on RHS (in scope due to LHS when false)
        if (!(o1 instanceof String s) || !(o2 instanceof String s)) {} //error - already in scope on RHS (in scope due to LHS when false)
        if (o1 instanceof String s || o2 instanceof String s) {} //error - the same binding variable on LHS and RHS when true
        if (o1 instanceof String s || !(o2 instanceof String s)) {} //ok

        if (o1 instanceof String s ? o2 instanceof String s : true) {} //error - already in scope in the true section (due to condition when true)
        if (o1 instanceof String s ? true : o2 instanceof String s) {} //error - the same binding variable in condition when true and false section when true
        if (!(o1 instanceof String s) ? o2 instanceof String s : true) {} //error - the same binding variable in condition when false and true section when true
        if (args.length == 1 ? o2 instanceof String s : o2 instanceof String s) {} //error - the same binding variable in true section when true and false section when true
        if (o1 instanceof String s ? true : !(o2 instanceof String s)) {} //error - the same binding variable in condition when true and false section when false
        if (!(o1 instanceof String s) ? !(o2 instanceof String s) : true) {} //error - the same binding variable in condition when false and true section when false
        if (args.length == 1 ? !(o2 instanceof String s) : !(o2 instanceof String s)) {} //error - the same binding variable in true section when false and false section when false

        //verify that errors are reported to clashes in expression in non-conditional statements:
        boolean b = o1 instanceof String s && o2 instanceof String s;
        b = o1 instanceof String s && o2 instanceof String s;
        b &= o1 instanceof String s && o2 instanceof String s;
        assert o1 instanceof String s && o2 instanceof String s;
        testMethod(o1 instanceof String s && o2 instanceof String s, false);
        b = switch (args.length) { default -> o1 instanceof String s && o2 instanceof String s; };
        b = switch (args.length) { default -> { yield o1 instanceof String s && o2 instanceof String s; } };
        if (true) return o1 instanceof String s && o2 instanceof String s;

        //verify the bindings don't go through method invocation parameters or outside a lambda:
        b = ((VoidPredicate) () -> o1 instanceof String s).get() && s.isEmpty();
        testMethod(o1 instanceof String s, s.isEmpty());
    }

    static void testMethod(boolean b1, boolean b2) {}
    interface VoidPredicate {
        public boolean get();
    }
}