Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for chance of class init deadlock with non-nested subclass #4429

Closed
wants to merge 2 commits into from
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 @@ -144,7 +144,7 @@ private void handle(ExpressionTree tree) {
if (!use.isSubClass(classSymbol, state.getTypes())) {
return;
}
if (!isStatic(use)) {
if (use.isEnclosedBy(classSymbol) && !isStatic(use)) {
// Nested inner classes implicitly take the enclosing instance as a constructor parameter,
// and can't be initialized without first initializing their containing class.
return;
Comment on lines -147 to 150
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the comment, it's clear the intention was to only match situations where the subclass is a non-static nested class of the superclass. But it's overly broad; any class that isn't a static inner class (since only inner classes can be marked static) will be matched by the current conditional. First checking if the class is enclosed by the defining class makes the conditional true to the comment.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,17 @@ public void nestedInterface() {
"}")
.doTest();
}

@Test
public void nonNestedSubclass() {
testHelper
.addSourceLines(
"Foo.java",
"class A {",
" // BUG: Diagnostic contains:",
" private static Object cycle = new B();",
"}",
"class B extends A {}")
.doTest();
}
}