Skip to content

Commit

Permalink
Avoid spurious errors in anon class constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
mcimadamore committed Sep 25, 2024
1 parent d20e3cc commit 65c809a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -3064,16 +3064,17 @@ public void report(DiagnosticPosition _unused, JCDiagnostic details) {
};
}

void checkNewInnerClass(DiagnosticPosition pos, Env<AttrContext> env, Type type) {
void checkNewInnerClass(DiagnosticPosition pos, Env<AttrContext> 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 {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3788,9 +3788,10 @@ Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol locatio
Symbol findSelfContaining(DiagnosticPosition pos,
Env<AttrContext> env,
TypeSymbol c,
Name name) {
Name name,
boolean isSuper) {
Assert.check(name == names._this);
Env<AttrContext> env1 = env;
Env<AttrContext> env1 = isSuper ? env.outer : env;
boolean staticOnly = false;
while (env1.outer != null) {
if (isStatic(env1)) staticOnly = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 65c809a

Please sign in to comment.