Skip to content

Commit 0dead21

Browse files
Resource closure analysis triggers NPE with compact constructors (eclipse-jdt#4123)
* Fixes eclipse-jdt#4070 * Fixes eclipse-jdt#4118
1 parent 164802b commit 0dead21

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,13 @@ else if (expression instanceof ConditionalExpression) {
265265
return null;
266266
// tracking var doesn't yet exist. This happens in finally block
267267
// which is analyzed before the corresponding try block
268-
Statement location = local.declaration;
268+
ASTNode location = local.declaration;
269+
if (location == null && local.isParameter() && local.declaringScope != null) {
270+
if (local.declaringScope.referenceContext() instanceof ConstructorDeclaration cd && cd.isCompactConstructor()) {
271+
TypeDeclaration referenceType = local.declaringScope.referenceType();
272+
location = referenceType.binding.getRecordComponent(local.name);
273+
}
274+
}
269275
local.closeTracker = new FakedTrackingVariable(local, location, flowInfo, flowContext, FlowInfo.UNKNOWN, useAnnotations);
270276
if (local.isParameter()) {
271277
local.closeTracker.globalClosingState |= OWNED_BY_OUTSIDE;

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10601,4 +10601,58 @@ public String withOverride() {
1060110601
"----------\n",
1060210602
JavacTestOptions.SKIP);
1060310603
}
10604+
10605+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4118
10606+
// Record with compact ctor - Internal compiler error: java.lang.RuntimeException: Internal Error compiling
10607+
public void testIssue4118() {
10608+
this.runConformTest(
10609+
new String[] {
10610+
"RecordCompactWithReader.java",
10611+
"""
10612+
import java.io.Reader;
10613+
import java.time.Instant;
10614+
import java.util.Objects;
10615+
10616+
public record RecordCompactWithReader(Instant modified, Reader reader) {
10617+
public RecordCompactWithReader {
10618+
Objects.requireNonNull(modified);
10619+
Objects.requireNonNull(reader);
10620+
}
10621+
public static void main(String [] args) {
10622+
System.out.println("OK!");
10623+
}
10624+
}
10625+
""",
10626+
},
10627+
"OK!"
10628+
);
10629+
}
10630+
10631+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4070
10632+
// Resource closure analysis triggers NPE with compact constructors
10633+
public void testIssue4070() {
10634+
this.runConformTest(
10635+
new String[] {
10636+
"Config.java",
10637+
"""
10638+
import java.net.URI;
10639+
import java.net.http.HttpClient;
10640+
import java.util.Objects;
10641+
10642+
public record Config(HttpClient httpClient, URI base, String defaultContentType) {
10643+
10644+
@SuppressWarnings("resource")
10645+
public Config {
10646+
Objects.requireNonNull(httpClient, "httpClient");
10647+
}
10648+
10649+
public static void main(String [] args) {
10650+
System.out.println("OK!");
10651+
}
10652+
}
10653+
""",
10654+
},
10655+
"OK!"
10656+
);
10657+
}
1060410658
}

0 commit comments

Comments
 (0)