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 bb56fd705b210..3d2ba4e9bef6a 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 @@ -2590,7 +2590,7 @@ public void visitApply(JCMethodInvocation tree) { } else if (methName == names._super) { // qualifier omitted; check for existence // of an appropriate implicit qualifier. - checkNewInnerClass(tree.meth.pos(), localEnv.outer, site); + checkNewInnerClass(tree.meth.pos(), localEnv, site, true); } } else if (tree.meth.hasTag(SELECT)) { log.error(tree.meth.pos(), @@ -2799,7 +2799,7 @@ public void visitNewClass(final JCNewClass tree) { } } else { // Check for the existence of an apropos outer instance - checkNewInnerClass(tree.pos(), env, clazztype); + checkNewInnerClass(tree.pos(), env, clazztype, false); } // Attribute constructor arguments. @@ -3064,16 +3064,17 @@ public void report(DiagnosticPosition _unused, JCDiagnostic details) { }; } - void checkNewInnerClass(DiagnosticPosition pos, Env env, Type type) { + void checkNewInnerClass(DiagnosticPosition pos, Env env, Type type, boolean isSuper) { boolean isLocal = type.tsym.owner.kind == MTH; if ((type.tsym.flags() & (INTERFACE | ENUM | RECORD)) != 0 || - (!isLocal && !type.tsym.isInner())) { + (!isLocal && !type.tsym.isInner()) || + (isSuper && env.enclClass.sym.isAnonymous())) { // nothing to check return; } Symbol res = isLocal ? rs.findLocalClassOwner(env, type.tsym) : - rs.findSelfContaining(pos, env, type.getEnclosingType().tsym, names._this); + rs.findSelfContaining(pos, env, type.getEnclosingType().tsym, names._this, isSuper); if (res.exists()) { rs.accessBase(res, pos, env.enclClass.sym.type, names._this, true); } else { @@ -3739,7 +3740,7 @@ public void visitReference(final JCMemberReference that) { } if (!env.info.attributionMode.isSpeculative && that.getMode() == JCMemberReference.ReferenceMode.NEW) { - checkNewInnerClass(that.pos(), env, exprType); + checkNewInnerClass(that.pos(), env, exprType, false); } if (resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java index 99043a8d3ad60..2be306f843933 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -3788,9 +3788,10 @@ Symbol lookupMethod(Env env, DiagnosticPosition pos, Symbol locatio Symbol findSelfContaining(DiagnosticPosition pos, Env env, TypeSymbol c, - Name name) { + Name name, + boolean isSuper) { Assert.check(name == names._this); - Env env1 = env; + Env env1 = isSuper ? env.outer : env; boolean staticOnly = false; while (env1.outer != null) { if (isStatic(env1)) staticOnly = true; diff --git a/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.out b/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.out index 3042b057a7c3e..7b96671a2bd1c 100644 --- a/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.out +++ b/test/langtools/tools/javac/SuperInit/EarlyIndirectOuterCapture.out @@ -1,3 +1,2 @@ EarlyIndirectOuterCapture.java:21:60: compiler.err.cant.ref.before.ctor.called: this -EarlyIndirectOuterCapture.java:21:82: compiler.err.cant.ref.before.ctor.called: this -2 errors +1 error