Skip to content

Commit 80f6ed7

Browse files
authored
Report usage of preview API only when the project compliance is at latest #3943 (#3947)
* Report warning about preview API being used instead of error when target platform is not latest. Keep everything else same. #3947
1 parent 6798c4b commit 80f6ed7

File tree

2 files changed

+101
-27
lines changed
  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem
  • org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression

2 files changed

+101
-27
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9560,33 +9560,31 @@ public void previewAPIUsed(Scope scope, int sourceStart, int sourceEnd, IBinaryA
95609560
}
95619561
} else
95629562
if (CharOperation.equals(valuePair.getName(), ConstantPool.REFLECTIVE)) {
9563-
if (valuePair.getValue() instanceof BooleanConstant bool)
9564-
isReflective = bool.booleanValue();
9563+
if (valuePair.getValue() instanceof BooleanConstant bool)
9564+
isReflective = bool.booleanValue();
95659565
}
95669566
}
95679567

9568-
9569-
String[] arguments = { featureName };
95709568
int problemId = -1;
95719569
int severity = -1;
95729570
if (!this.options.enablePreviewFeatures) {
9573-
problemId = IProblem.PreviewAPIDisabled;
9574-
severity = isReflective ? ProblemSeverities.Warning : ProblemSeverities.Error;
9571+
if (this.options.complianceLevel < ClassFileConstants.getLatestJDKLevel()) {
9572+
problemId = IProblem.PreviewAPIUsed;
9573+
severity = ProblemSeverities.Warning;
9574+
} else {
9575+
problemId = IProblem.PreviewAPIDisabled;
9576+
severity = isReflective ? ProblemSeverities.Warning : ProblemSeverities.Error;
9577+
}
95759578
} else {
95769579
this.referenceContext.compilationResult().usesPreview = true;
95779580
if (this.options.isAnyEnabled(IrritantSet.PREVIEW)) {
95789581
severity = ProblemSeverities.Warning;
95799582
problemId = IProblem.PreviewAPIUsed;
95809583
}
95819584
}
9582-
if (problemId != -1 && severity != -1)
9583-
this.handle(
9584-
problemId,
9585-
arguments,
9586-
arguments,
9587-
severity,
9588-
sourceStart,
9589-
sourceEnd);
9585+
if (problemId == -1 || severity == -1) return;
9586+
String[] arguments = { featureName };
9587+
this.handle(problemId, arguments, arguments, severity, sourceStart, sourceEnd);
95909588
}
95919589
//Returns true if the problem is handled and reported (only errors considered and not warnings)
95929590
private boolean validateRestrictedKeywords(char[] name, int start, int end, boolean reportSyntaxError) {

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

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class PreviewFlagTest extends AbstractRegressionTest9 {
2828
static {
2929
// TESTS_NUMBERS = new int [] { 1 };
3030
// TESTS_RANGE = new int[] { 1, -1 };
31-
// TESTS_NAMES = new String[] { "testIssue3614_001" };
31+
// TESTS_NAMES = new String[] { "testIssue3943_001" };
3232
// TESTS_NAMES = new String[] { "testIssue3614_001" };
3333
}
3434
private String extraLibPath;
@@ -151,15 +151,21 @@ public static void main(String[] args) {
151151
ScopedValue<Integer> si = ScopedValue.newInstance();
152152
System.out.println(si == null ? "hello" : "world");
153153
}
154+
public int foo() {}
154155
}
155156
"""
156157
},
157-
"----------\n" +
158-
"1. ERROR in X.java (at line 3)\n" +
159-
" ScopedValue<Integer> si = ScopedValue.newInstance();\n" +
160-
" ^^^^^^^^^^^\n" +
161-
"This API is part of the preview feature 'Scoped Values' which is disabled by default. Use --enable-preview to enable\n" +
162-
"----------\n",
158+
"----------\n" +
159+
"1. ERROR in X.java (at line 3)\n" +
160+
" ScopedValue<Integer> si = ScopedValue.newInstance();\n" +
161+
" ^^^^^^^^^^^\n" +
162+
"This API is part of the preview feature 'Scoped Values' which is disabled by default. Use --enable-preview to enable\n" +
163+
"----------\n" +
164+
"2. ERROR in X.java (at line 6)\n" +
165+
" public int foo() {}\n" +
166+
" ^^^^^\n" +
167+
"This method must return a result of type int\n" +
168+
"----------\n",
163169
null,
164170
true,
165171
options);
@@ -181,12 +187,12 @@ public static void main(String... args) {}
181187
"""
182188
};
183189
runner.expectedCompilerLog =
184-
"----------\n" +
185-
"1. WARNING in X.java (at line 4)\n" +
186-
" return tree.isModule();\n" +
187-
" ^^^^^^^^^^^^^^^\n" +
188-
"This API is part of the preview feature 'Module Import Declarations' which is disabled by default. Use --enable-preview to enable\n" +
189-
"----------\n";
190+
"----------\n" +
191+
"1. WARNING in X.java (at line 4)\n" +
192+
" return tree.isModule();\n" +
193+
" ^^^^^^^^^^^^^^^\n" +
194+
"This API is part of the preview feature 'Module Import Declarations' which is disabled by default. Use --enable-preview to enable\n" +
195+
"----------\n";
190196
runner.runConformTest();
191197
}
192198
public void testIssue3614_003_enabled() throws Exception {
@@ -218,4 +224,74 @@ public void main(String... args) {
218224
runner.expectedOutputString = "42";
219225
runner.runConformTest();
220226
}
227+
public void testIssue3943_001() throws IOException, ClassFormatException {
228+
Map<String, String> options = getCompilerOptions();
229+
String str = options.get(CompilerOptions.OPTION_Compliance);
230+
options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_23);
231+
options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_23);
232+
options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_23);
233+
options.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.DISABLED);
234+
runNegativeTest(new String[] {
235+
"X.java",
236+
"""
237+
public class X {
238+
public static void main(String[] args) {
239+
ScopedValue<Integer> si = ScopedValue.newInstance();
240+
System.out.println(si == null ? "hello" : "world");
241+
}
242+
public int foo() {}
243+
}
244+
"""},
245+
"----------\n" +
246+
"1. WARNING in X.java (at line 3)\n" +
247+
" ScopedValue<Integer> si = ScopedValue.newInstance();\n" +
248+
" ^^^^^^^^^^^\n" +
249+
"You are using an API that is part of the preview feature \'Scoped Values\' and may be removed in future\n" +
250+
"----------\n" +
251+
"2. ERROR in X.java (at line 6)\n" +
252+
" public int foo() {}\n" +
253+
" ^^^^^\n" +
254+
"This method must return a result of type int\n" +
255+
"----------\n",
256+
null,
257+
true,
258+
options);
259+
options.put(CompilerOptions.OPTION_Compliance, str);
260+
options.put(CompilerOptions.OPTION_Source, str);
261+
options.put(CompilerOptions.OPTION_TargetPlatform, str);
262+
options.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED);
263+
}
264+
public void testIssue3943_002() throws IOException, ClassFormatException {
265+
Map<String, String> options = getCompilerOptions();
266+
String str = options.get(CompilerOptions.OPTION_Compliance);
267+
options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_23);
268+
options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_23);
269+
options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_23);
270+
options.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.DISABLED);
271+
runNegativeTest(new String[] {
272+
"X.java",
273+
"""
274+
public class X {
275+
@SuppressWarnings("preview")
276+
public static void main(String[] args) {
277+
ScopedValue<Integer> si = ScopedValue.newInstance();
278+
System.out.println(si == null ? "hello" : "world");
279+
}
280+
public int foo() {}
281+
}
282+
"""},
283+
"----------\n" +
284+
"1. ERROR in X.java (at line 7)\n" +
285+
" public int foo() {}\n" +
286+
" ^^^^^\n" +
287+
"This method must return a result of type int\n" +
288+
"----------\n",
289+
null,
290+
true,
291+
options);
292+
options.put(CompilerOptions.OPTION_Compliance, str);
293+
options.put(CompilerOptions.OPTION_Source, str);
294+
options.put(CompilerOptions.OPTION_TargetPlatform, str);
295+
options.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED);
296+
}
221297
}

0 commit comments

Comments
 (0)