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

Reviewed-by: vromero
  • Loading branch information
archiecobbs authored and Vicente Romero committed Mar 24, 2023
1 parent 4ec720d commit f96aee7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3140,7 +3140,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 @@ -3151,6 +3152,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
Original file line number Diff line number Diff line change
@@ -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 {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
StaticNestedNonStaticSuper.java:10:23: compiler.err.no.encl.instance.of.type.in.scope: StaticNestedNonStaticSuper
1 error

3 comments on commit f96aee7

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on f96aee7 Oct 19, 2023

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on f96aee7 Oct 19, 2023

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-f96aee74 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit f96aee74 from the openjdk/jdk repository.

The commit being backported was authored by Archie L. Cobbs on 24 Mar 2023 and was reviewed by Vicente Romero.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git GoeLin-backport-f96aee74:GoeLin-backport-f96aee74
$ git checkout GoeLin-backport-f96aee74
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git GoeLin-backport-f96aee74

Please sign in to comment.