Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3843,12 +3843,15 @@ Symbol findLocalClassOwner(Env<AttrContext> env, TypeSymbol c) {
Env<AttrContext> env1 = env;
boolean staticOnly = false;
while (env1.outer != null) {
// If the local class is defined inside a static method, and the instance creation expression
// occurs in that same method, the creation occurs (technically) inside a static context, but that's ok.
if (env1.info.scope.owner == owner) {
return (staticOnly) ?
new BadLocalClassCreation(c) :
owner;
} else if (isStatic(env1) || env1.enclClass.sym.isStatic()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In other similar loops, we would check isStatic as the first statement in the loop. However, here we can't do that -- if the local class is static, it will be defined in a static method -- this means the search will always end with a static context. But this is ok, and we don't want to raise an error!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See tests staticLocal and staticLocalFromAnon (these were derived from some failing code in the Stream API).

staticOnly = true;
}
if (isStatic(env1)) staticOnly = true;
env1 = env1.outer;
}
return owner.kind == MTH ?
Expand Down
72 changes: 72 additions & 0 deletions test/langtools/tools/javac/SuperInit/NewLocalNotInInner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* @test /nodynamiccopyright/
* @bug 8373570
* @summary Javac stack overflow on method-local class with nested record referring to enclosing type
* @compile/fail/ref=NewLocalNotInInner.out -XDrawDiagnostics NewLocalNotInInner.java
*/
class NewLocalNotInInner {
void m() {
class Local {
static class Foo {
void m() {
new Local(); // error
}
}
}
}

void m_anon() {
class Local {
static class Foo {
void m() {
new Local() { }; // error
}
}
}
}

void m_record() {
class Local {
record Foo() {
void m() {
new Local(); // error
}
}
}
}

void m_intf() {
class Local {
interface Foo {
default void m() {
new Local(); // error
}
}
}
}

void sup() {
class Local {
static class Foo {
void m() {
class Sub extends Local { }; // error
new Sub();
}
}
}
}

static void staticLocal() {
class Local { }
new Local(); // ok
}

static void staticLocalFromAnon() {
class Local { }
new Object() {
Local local() {
return new Local(); // ok
}
};
}
}
6 changes: 6 additions & 0 deletions test/langtools/tools/javac/SuperInit/NewLocalNotInInner.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NewLocalNotInInner.java:12:21: compiler.err.local.cant.be.inst.static: kindname.class, Local
NewLocalNotInInner.java:22:21: compiler.err.local.cant.be.inst.static: kindname.class, Local
NewLocalNotInInner.java:32:21: compiler.err.local.cant.be.inst.static: kindname.class, Local
NewLocalNotInInner.java:42:21: compiler.err.local.cant.be.inst.static: kindname.class, Local
NewLocalNotInInner.java:52:21: compiler.err.local.cant.be.inst.static: kindname.class, Local
5 errors