diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index 613cdc77f3f..d28f07ff9ad 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -1356,6 +1356,20 @@ public void visitIdent(JCIdent tree) { analyzeSymbol(tree); } + boolean isIndexed = false; + + @Override + public void visitIndexed(JCArrayAccess tree) { + boolean previewIsIndexed = isIndexed; + try { + isIndexed = true; + scan(tree.indexed); + } finally { + isIndexed = previewIsIndexed; + } + scan(tree.index); + } + @Override public void visitSelect(JCFieldAccess tree) { SelectScanner ss = new SelectScanner(); @@ -1434,7 +1448,9 @@ void analyzeSymbol(JCTree tree) { } // Field may not have an initializer if ((sym.flags() & HASINIT) != 0) { - reportPrologueError(tree, sym, true); + if (!sym.type.hasTag(ARRAY) || !isIndexed) { + reportPrologueError(tree, sym, true); + } return; } // cant reference an instance field before a this constructor diff --git a/test/langtools/tools/javac/SuperInit/ValueClassSuperInitGood.java b/test/langtools/tools/javac/SuperInit/ValueClassSuperInitGood.java index 0d37baca8be..d25a0b50a98 100644 --- a/test/langtools/tools/javac/SuperInit/ValueClassSuperInitGood.java +++ b/test/langtools/tools/javac/SuperInit/ValueClassSuperInitGood.java @@ -26,9 +26,11 @@ * @bug 8324873 * @summary Test valid placements of super()/this() in constructors * @enablePreview - * @ignore fails at execution time because of Optional */ +import java.util.ArrayList; +import java.util.List; + public value class ValueClassSuperInitGood { ValueClassSuperInitGood(Object obj) { @@ -78,10 +80,10 @@ static value class Test3 { Test3() { new Object().hashCode(); new Object().hashCode(); - super(); this.x = new Object().hashCode(); this.y = new Object().hashCode() % 17; this.z = this.x + this.y; + super(); } } @@ -101,9 +103,9 @@ abstract value class Test5 extends Test5Abstract { // Initialization blocks value class Test6 { final long startTime; - final int x; + List l = new ArrayList<>(); { - this.x = 12; + l.add(""); } Test6() { long now = System.nanoTime(); @@ -116,8 +118,8 @@ value class Test6 { break; } } - super(); this.startTime = now; + super(); } } @@ -234,26 +236,6 @@ class Bar { } } - // Initializer in initializer block - public static value class Test14 { - final int x; // initialized in constructor - final int y; // initialized in initialization block - final int z = 13; // initialized with intializer value - public Test14() { - this(0); - } - public Test14(boolean z) { - this.x = z ? 1 : 0; - } - public Test14(int x) { - super(); - this.x = x; - } - { - this.y = -1; - } - } - // Qualified super() invocation with superclass instance public static abstract value class Test15 { @@ -382,32 +364,6 @@ public final V get() { } } - // Exceptions thrown by initializer block - public static value class Test18 extends AR { - - { - if ((this.get().hashCode() % 3) == 0) - throw new MyException(); - } - - public Test18(Object obj) throws MyException { - super(obj); - } - - public Test18(boolean fail) throws MyException { - Object obj; - for (obj = new Object(); true; obj = new Object()) { - if (((obj.hashCode() % 3) == 0) != fail) - continue; - break; - } - this(obj); - } - - public static class MyException extends Exception { - } - } - // super()/this() within outer try block but inside inner class public static value class Test19 { public Test19(int x) { @@ -424,29 +380,12 @@ public int hashCode() { } } - // local class declared before super(), but not used until after super() public static value class Test20 { - public Test20() { - class Foo { - Foo() { - Test20.this.hashCode(); - } + private final int[] data = new int[10]; + Test20() { + for (int i = 0; i < data.length; i++) { + data[i] = i; // OK we are assigning to an array component } - super(); - new Foo(); - } - } - - // local class inside super() parameter list - public static value class Test21 extends AR { - private int x = 1; - public Test21() { - super(switch ("foo".hashCode()) { - default -> { - class Nested {{ System.out.println(x); }} // class is NOT instantiated - OK - yield "bar"; - } - }); } } @@ -466,10 +405,6 @@ public static void main(String[] args) { new Test11(9); new Test12(); new Test13(); - Test14 t14 = new Test14(); - assert t14.x == 0 && t14.y == -1 && t14.z == 13; - t14 = new Test14(7); - assert t14.x == 7 && t14.y == -1 && t14.z == 13; new Test15c(new Test15("foo"){}, "bar"); new Test16().run(); new Test17.StringHolder("foo"); @@ -479,19 +414,7 @@ public static void main(String[] args) { } catch (NullPointerException e) { // expected } - try { - new Test18(true); - assert false : "expected exception"; - } catch (Test18.MyException e) { - // expected - } - try { - new Test18(false); - } catch (Test18.MyException e) { - assert false : "unexpected exception: " + e; - } new Test19(123); new Test20(); - new Test21(); } }