6541876: "Enclosing Instance" error new in 1.6
authormcimadamore
Wed, 02 Mar 2011 10:56:39 +0000
changeset 8622 4f032629a0fd
parent 8621 ce0bce60cffc
child 8623 cc57bd7697a2
6541876: "Enclosing Instance" error new in 1.6 Summary: unqualified 'this' should not be selected in a qualified super() call in a default constructor Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java
langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java
langtools/test/tools/javac/implicitThis/6541876/T6541876a.java
langtools/test/tools/javac/implicitThis/6541876/T6541876b.java
langtools/test/tools/javac/implicitThis/NewBeforeOuterConstructed3.java
langtools/test/tools/javac/nested/4903103/T4903103.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Mar 01 12:00:06 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Mar 02 10:56:39 2011 +0000
@@ -1425,7 +1425,7 @@
                             // qualifier omitted; check for existence
                             // of an appropriate implicit qualifier.
                             rs.resolveImplicitThis(tree.meth.pos(),
-                                                   localEnv, site);
+                                                   localEnv, site, true);
                         }
                     } else if (tree.meth.getTag() == JCTree.SELECT) {
                         log.error(tree.meth.pos(), "illegal.qual.not.icls",
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java	Tue Mar 01 12:00:06 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java	Wed Mar 02 10:56:39 2011 +0000
@@ -2817,8 +2817,8 @@
                     // local class or this() call
                     thisArg = makeThis(tree.meth.pos(), c.type.getEnclosingType().tsym);
                 } else {
-                    // super() call of nested class
-                    thisArg = makeOwnerThis(tree.meth.pos(), c, false);
+                    // super() call of nested class - never pick 'this'
+                    thisArg = makeOwnerThisN(tree.meth.pos(), c, false);
                 }
                 tree.args = tree.args.prepend(thisArg);
             }
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Tue Mar 01 12:00:06 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Mar 02 10:56:39 2011 +0000
@@ -1736,24 +1736,26 @@
      */
     Symbol resolveSelfContaining(DiagnosticPosition pos,
                                  Env<AttrContext> env,
-                                 Symbol member) {
+                                 Symbol member,
+                                 boolean isSuperCall) {
         Name name = names._this;
-        Env<AttrContext> env1 = env;
+        Env<AttrContext> env1 = isSuperCall ? env.outer : env;
         boolean staticOnly = false;
-        while (env1.outer != null) {
-            if (isStatic(env1)) staticOnly = true;
-            if (env1.enclClass.sym.isSubClass(member.owner, types) &&
-                isAccessible(env, env1.enclClass.sym.type, member)) {
-                Symbol sym = env1.info.scope.lookup(name).sym;
-                if (sym != null) {
-                    if (staticOnly) sym = new StaticError(sym);
-                    return access(sym, pos, env.enclClass.sym.type,
-                                  name, true);
+        if (env1 != null) {
+            while (env1 != null && env1.outer != null) {
+                if (isStatic(env1)) staticOnly = true;
+                if (env1.enclClass.sym.isSubClass(member.owner, types)) {
+                    Symbol sym = env1.info.scope.lookup(name).sym;
+                    if (sym != null) {
+                        if (staticOnly) sym = new StaticError(sym);
+                        return access(sym, pos, env.enclClass.sym.type,
+                                      name, true);
+                    }
                 }
+                if ((env1.enclClass.sym.flags() & STATIC) != 0)
+                    staticOnly = true;
+                env1 = env1.outer;
             }
-            if ((env1.enclClass.sym.flags() & STATIC) != 0)
-                staticOnly = true;
-            env1 = env1.outer;
         }
         log.error(pos, "encl.class.required", member);
         return syms.errSymbol;
@@ -1764,9 +1766,13 @@
      * JLS2 8.8.5.1 and 15.9.2
      */
     Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
+        return resolveImplicitThis(pos, env, t, false);
+    }
+
+    Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) {
         Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
                          ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
-                         : resolveSelfContaining(pos, env, t.tsym)).type;
+                         : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
         if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
             log.error(pos, "cant.ref.before.ctor.called", "this");
         return thisType;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/implicitThis/6541876/T6541876a.java	Wed Mar 02 10:56:39 2011 +0000
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 6541876
+ * @summary  "Enclosing Instance" error new in 1.6
+ *
+ */
+
+public class T6541876a {
+    class X {
+        class Y {}
+    }
+
+    class A extends X {
+        class B extends X.Y {}
+    }
+
+    public static void main(String[] args) {
+        new T6541876a().new A().new B();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/implicitThis/6541876/T6541876b.java	Wed Mar 02 10:56:39 2011 +0000
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 6541876 6569091
+ * @summary  "Enclosing Instance" error new in 1.6
+ *
+ */
+
+public class T6541876b {
+
+    enum ENUM {
+        ENUM_CONST {
+            public AbstractClass method() {
+                return new AbstractClass() {
+                    public boolean method() {
+                        return true;
+                    }
+                };
+            }
+        };
+
+        public abstract AbstractClass method();
+
+        private abstract class AbstractClass {
+            public abstract boolean method();
+        }
+    }
+
+    public static void main(String[] args) {
+        ENUM.ENUM_CONST.method();
+    }
+}
--- a/langtools/test/tools/javac/implicitThis/NewBeforeOuterConstructed3.java	Tue Mar 01 12:00:06 2011 -0800
+++ b/langtools/test/tools/javac/implicitThis/NewBeforeOuterConstructed3.java	Wed Mar 02 10:56:39 2011 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,6 @@
  * @test
  * @bug 4704371 6313120
  * @summary compiler generates unverifiable code for implicit reference to uninit'd this
- * @compile/fail NewBeforeOuterConstructed3.java
  */
 
 public class NewBeforeOuterConstructed3 {
--- a/langtools/test/tools/javac/nested/4903103/T4903103.java	Tue Mar 01 12:00:06 2011 -0800
+++ b/langtools/test/tools/javac/nested/4903103/T4903103.java	Wed Mar 02 10:56:39 2011 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,11 +25,14 @@
  * @test
  * @bug     4903103
  * @summary Can't compile subclasses of inner classes
- * @compile T4903103.java
  */
 
 public class T4903103 {
     private class InnerSuperclass extends T4903103 {}
 
     private class InnerSubclass extends InnerSuperclass {}
+
+    public static void main(String[] args) {
+        new T4903103().new InnerSubclass();
+    }
 }