8156180: javac accepts code that violates JLS chapter 16
Summary: DA/DU analysis should skip parenthesis around variable references
Reviewed-by: vromero
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java Mon May 16 13:05:42 2016 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java Mon May 16 13:07:57 2016 +0100
@@ -2345,10 +2345,11 @@
// assigned before reading their value
public void visitSelect(JCFieldAccess tree) {
super.visitSelect(tree);
+ JCTree sel = TreeInfo.skipParens(tree.selected);
if (enforceThisDotInit &&
- tree.selected.hasTag(IDENT) &&
- ((JCIdent)tree.selected).name == names._this &&
- tree.sym.kind == VAR) {
+ sel.hasTag(IDENT) &&
+ ((JCIdent)sel).name == names._this &&
+ tree.sym.kind == VAR) {
checkInit(tree.pos(), (VarSymbol)tree.sym);
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/DefiniteAssignment/8156180/T8156180.java Mon May 16 13:07:57 2016 +0100
@@ -0,0 +1,23 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8156180
+ * @summary javac accepts code that violates JLS chapter 16
+ *
+ * @compile/fail/ref=T8156180.out -XDrawDiagnostics T8156180.java
+ */
+
+class T8156180 {
+ public final int a1, b1, c1, d1;
+ public int a2, b2, c2, d2;
+
+ T8156180(int value) {
+ a2 = this.a1;
+ b2 = (this).b1;
+ c2 = ((this)).c1;
+ d2 = (((this))).d1;
+ a1 = value;
+ b1 = value;
+ c1 = value;
+ d1 = value;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/DefiniteAssignment/8156180/T8156180.out Mon May 16 13:07:57 2016 +0100
@@ -0,0 +1,5 @@
+T8156180.java:14:18: compiler.err.var.might.not.have.been.initialized: a1
+T8156180.java:15:20: compiler.err.var.might.not.have.been.initialized: b1
+T8156180.java:16:22: compiler.err.var.might.not.have.been.initialized: c1
+T8156180.java:17:24: compiler.err.var.might.not.have.been.initialized: d1
+4 errors