From 58012320b0fac361f64fa44b27e6d76e15f49935 Mon Sep 17 00:00:00 2001 From: Stephan Herrmann Date: Wed, 11 Dec 2024 20:55:47 +0100 Subject: [PATCH] Bug 561334 - [resource] leak should be classified as potential without annotation support: + mark as shared, to change problem to MandatoryCloseNotShown + move this and sibling to irritant PotentiallyUnclosedCloseable with annotation support: + nothing changed, since we assume OWNED_BY_DEFAULT Fixes https://bugs.eclipse.org/561334 --- .../compiler/ast/FakedTrackingVariable.java | 1 + .../compiler/problem/ProblemReporter.java | 2 +- .../regression/CompilerInvocationTests.java | 4 +- .../ResourceLeakAnnotatedTests.java | 21 +++++++- .../regression/ResourceLeakTests.java | 48 +++++++++++++++++++ .../model/OwningAnnotationModelTests.java | 2 + 6 files changed, 74 insertions(+), 4 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java index f48813dd78f..7fb5fb3f863 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java @@ -546,6 +546,7 @@ public static FlowInfo analyseCloseableAcquisition(BlockScope scope, FlowInfo fl acquisition.closeTracker = tracker; } tracker.acquisition = acquisition; + tracker.globalClosingState |= SHARED_WITH_OUTSIDE; FlowInfo outsideInfo = flowInfo.copy(); outsideInfo.markAsDefinitelyNonNull(tracker.binding); tracker.markNullStatus(flowInfo, flowContext, FlowInfo.NULL); diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java index d44a5e6dcb5..50a50eb0cd9 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java @@ -589,9 +589,9 @@ public static int getIrritant(int problemID) { case IProblem.UnclosedCloseable: case IProblem.UnclosedCloseableAtExit: + return CompilerOptions.UnclosedCloseable; case IProblem.MandatoryCloseNotShown: case IProblem.MandatoryCloseNotShownAtExit: - return CompilerOptions.UnclosedCloseable; case IProblem.PotentiallyUnclosedCloseable: case IProblem.PotentiallyUnclosedCloseableAtExit: return CompilerOptions.PotentiallyUnclosedCloseable; diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java index 96c45a4075c..51a1ec1e43d 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java @@ -1949,8 +1949,8 @@ class ProblemAttributes { expectedProblemAttributes.put("LocalVariableIsNeverUsed", new ProblemAttributes(JavaCore.COMPILER_PB_UNUSED_LOCAL)); expectedProblemAttributes.put("LocalVariableMayBeNull", SKIP); expectedProblemAttributes.put("MaskedCatch", new ProblemAttributes(JavaCore.COMPILER_PB_HIDDEN_CATCH_BLOCK)); - expectedProblemAttributes.put("MandatoryCloseNotShown", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE)); - expectedProblemAttributes.put("MandatoryCloseNotShownAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE)); + expectedProblemAttributes.put("MandatoryCloseNotShown", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE)); + expectedProblemAttributes.put("MandatoryCloseNotShownAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE)); expectedProblemAttributes.put("MessageSendWithUnresolvedOwningAnnotation", SKIP); expectedProblemAttributes.put("MethodButWithConstructorName", new ProblemAttributes(JavaCore.COMPILER_PB_METHOD_WITH_CONSTRUCTOR_NAME)); expectedProblemAttributes.put("MethodCanBePotentiallyStatic", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD)); diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakAnnotatedTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakAnnotatedTests.java index 0507c41e8c6..86007908514 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakAnnotatedTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakAnnotatedTests.java @@ -138,7 +138,7 @@ protected String getTest056e_log() { protected String getTest056y_log() { return """ ---------- - 1. ERROR in X.java (at line 4) + 1. WARNING in X.java (at line 4) final FileReader reader31 = new FileReader("file"); ^^^^^^^^ Mandatory close of resource 'reader31' has not been shown @@ -199,6 +199,25 @@ protected String getTestBug440282_log() { "----------\n"; } +@Override +String getBug561334_log() { + // 1. info is only reported when annotations are enabled + // 2. warning is more severe since we set OWNED_BY_DEFAULT on the resource from getFoo() + return """ + ---------- + 1. INFO in Foo.java (at line 18) + foo = new Foo("Hello, world!"); + ^^^^^^^^^^^^^^^^^^^^^^^^ + Mandatory close of resource '' has not been shown + ---------- + 2. WARNING in Foo.java (at line 21) + System.out.println(getFoo().thing); + ^^^^^^^^ + Resource leak: '' is never closed + ---------- + """; +} + public void testBug411098_comment19_annotated() { runLeakTestWithAnnotations( new String[] { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java index 88fc9f6b3ee..981d6a977c4 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java @@ -7295,4 +7295,52 @@ public static void main(String[] args) throws Exception { """, options); } +public void testBug561334() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.INFO); + runLeakWarningTest( + new String[] { + "Foo.java", + """ + public class Foo implements AutoCloseable { + private final String thing; + public Foo(String thing) { + this.thing = thing; + } + + @Override + public void close() { + // + } + + private static Foo foo; + private static Foo getFoo() { + return foo; + } + + public static void main(String[] args) { + foo = new Foo("Hello, world!"); + try { + System.out.println(foo.thing); + System.out.println(getFoo().thing); + } finally { + getFoo().close(); + } + } + } + """ + }, + getBug561334_log(), + compilerOptions); +} +String getBug561334_log() { + return """ + ---------- + 1. INFO in Foo.java (at line 21) + System.out.println(getFoo().thing); + ^^^^^^^^ + Mandatory close of resource '' has not been shown + ---------- + """; +} } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/OwningAnnotationModelTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/OwningAnnotationModelTests.java index f47c9e319a2..763112ad2c2 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/OwningAnnotationModelTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/OwningAnnotationModelTests.java @@ -160,6 +160,7 @@ public void close() throws Exception { IJavaProject p2 = createJavaProject("P2", new String[] {""}, new String[] {"JCL_23_LIB"}, "bin", "23"); p2.setOption(JavaCore.COMPILER_ANNOTATION_RESOURCE_ANALYSIS, JavaCore.ENABLED); + p2.setOption(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, JavaCore.WARNING); addClasspathEntry(p2, JavaCore.newProjectEntry(p1.getPath())); // no access to the annotation lib, since not re-exported createFolder("/P2/client"); @@ -469,6 +470,7 @@ public void close() throws Exception { IJavaProject p2 = createJavaProject("P2", new String[] {""}, new String[] {"JCL_23_LIB"}, "bin", "23"); p2.setOption(JavaCore.COMPILER_ANNOTATION_RESOURCE_ANALYSIS, JavaCore.ENABLED); + p2.setOption(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, JavaCore.WARNING); addClasspathEntry(p2, JavaCore.newProjectEntry(p1.getPath())); // no access to the annotation lib, since not re-exported createFolder("/P2/client");