Navigation Menu

Skip to content

Commit

Permalink
8291154: Create a non static nested class without enclosing class thr…
Browse files Browse the repository at this point in the history
…ows VerifyError

Backport-of: f96aee74010476a850175f7012c196e40a31c188
  • Loading branch information
GoeLin committed Oct 23, 2023
1 parent 10b7536 commit 6414676
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Expand Up @@ -3007,7 +3007,8 @@ public void visitApply(JCMethodInvocation tree) {
// is qualified, pass qualifier as first argument in front of
// the explicit constructor arguments. If the call
// is not qualified, pass the correct outer instance as
// first argument.
// first argument. If we are a static class, there is no
// such outer instance, so generate an error.
if (c.hasOuterInstance()) {
JCExpression thisArg;
if (tree.meth.hasTag(SELECT)) {
Expand All @@ -3018,6 +3019,11 @@ public void visitApply(JCMethodInvocation tree) {
} else if (c.isDirectlyOrIndirectlyLocal() || methName == names._this){
// local class or this() call
thisArg = makeThis(tree.meth.pos(), c.type.getEnclosingType().tsym);
} else if (currentClass.isStatic()) {
// super() call from static nested class - invalid
log.error(tree.pos(),
Errors.NoEnclInstanceOfTypeInScope(c.type.getEnclosingType().tsym));
thisArg = make.Literal(BOT, null).setType(syms.botType);
} else {
// super() call of nested class - never pick 'this'
thisArg = makeOwnerThisN(tree.meth.pos(), c, false);
Expand Down
13 changes: 13 additions & 0 deletions test/langtools/tools/javac/nested/StaticNestedNonStaticSuper.java
@@ -0,0 +1,13 @@
/*
* @test /nodynamiccopyright/
* @bug 8291154
* @summary Disallow static nested subclasses of non-static nested classes
* @compile/fail/ref=StaticNestedNonStaticSuper.out -XDrawDiagnostics StaticNestedNonStaticSuper.java
*/

class StaticNestedNonStaticSuper{
public abstract class NonStaticNested {
public static class StaticNested extends NonStaticNested {
}
}
}
@@ -0,0 +1,2 @@
StaticNestedNonStaticSuper.java:10:23: compiler.err.no.encl.instance.of.type.in.scope: StaticNestedNonStaticSuper
1 error

1 comment on commit 6414676

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.