Skip to content

Commit

Permalink
Check for chance of class init deadlock with non-nested subclass
Browse files Browse the repository at this point in the history
This PR fixes a bug with the `ClassInitializationDeadlock` check to find chances for a class initialization deadlock when the subclass isn't nested within the superclass. This situation [can still produce a deadlock](https://gist.github.com/tkindy/a83ec86c95bb7d7119f6b208d239ea94).

Fixes #4429

COPYBARA_INTEGRATE_REVIEW=#4429 from HubSpot:non-nested-class-init-deadlock c5cc883
PiperOrigin-RevId: 642281177
  • Loading branch information
tkindy authored and Error Prone Team committed Jun 11, 2024
1 parent 1bf08c2 commit f29d6e5
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit f29d6e5

Please sign in to comment.