Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
- Show an error message when the unicode escaped version of underscore
  is used as a local variable in a Java 21 project with preview disabled
- Test error messages for underscore for Java 21 with and without
  preview enabled
- Fix test failure related to resolving type of "var" variable in foreach loops

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Jan 19, 2024
1 parent 09cbc6a commit 2d2f140
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void setFieldIndex(int depth) {
* @param scope used to determine source level
*/
public boolean isUnnamed(BlockScope scope) {
return this.name.length == 1 && this.name[0] == '_'
return ((this.name.length == 1 && this.name[0] == '_') || ("\\u005F".equals(this.name.toString()))) //$NON-NLS-1$
&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK21
&& scope.compilerOptions().enablePreviewFeatures;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public void resolve(BlockScope upperScope) {
// use the scope that will hold the init declarations
this.scope = new BlockScope(upperScope);
this.scope.blockStatement = this;
this.elementVariable.resolve(this.scope, false, true); // collection expression can see itemVariable
this.elementVariable.resolve(this.scope); // collection expression can see itemVariable
LocalVariableBinding[] patternVariablesInTrueScope = null;

if (this.pattern != null && JavaFeature.RECORD_PATTERNS.isSupported(upperScope.compilerOptions())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,12 @@ private static Expression findPolyExpression(Expression e) {

@Override
public void resolve(BlockScope scope) {
resolve(scope, false, false);
resolve(scope, false);
}
public void resolve(BlockScope scope, boolean isPatternVariable) {
resolve(scope, isPatternVariable, false);
}
public void resolve(BlockScope scope, boolean isPatternVariable, boolean isEnhancedForLoopElement) {
// prescan NNBD
public void resolve(BlockScope scope, boolean isPatternVariable) { // prescan NNBD
handleNonNullByDefault(scope, this.annotations, this);

if (!isPatternVariable && !isEnhancedForLoopElement && this.initialization == null && this.isUnnamed(scope)) {
if (!isPatternVariable && (this.bits & ASTNode.IsForeachElementVariable) == 0 && this.initialization == null && this.isUnnamed(scope)) {
scope.problemReporter().unnamedVariableMustHaveInitializer(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ public static TestSuite getTestSuite(boolean addComplianceDiagnoseTest) {
tests_21.addAll(TEST_CLASSES_1_5);
addJava16Tests(tests_21);
tests_21.add(EmbeddedExpressionSelectionTest.class);
tests_21.add(JEP443UnnamedVariableTest.class);
// Reset forgotten subsets tests
TestCase.TESTS_PREFIX = null;
TestCase.TESTS_NAMES = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ public static Test suite() {
since_21.add(SwitchPatternTest.class);
since_21.add(RecordPatternTest.class);
since_21.add(UnnamedPatternsAndVariablesTest.class);
since_21.add(UseOfUnderscoreWithPreviewTest.class);
since_21.add(NullAnnotationTests21.class);
since_21.add(NullAnnotationTests18.class);
since_21.add(StringTemplateTest.class);
since_21.add(BatchCompilerTest_21.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.eclipse.jdt.core.tests.compiler.regression;

import java.util.Map;

import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

Expand All @@ -17,15 +15,6 @@ public UseOfUnderscoreTest(String name) {
super(name);
}

@Override
protected Map<String, String> getCompilerOptions() {
CompilerOptions compilerOptions = new CompilerOptions(super.getCompilerOptions());
if (compilerOptions.sourceLevel == ClassFileConstants.JDK21) {
compilerOptions.enablePreviewFeatures = true;
}
return compilerOptions.getMap();
}

public void testReportsUnderscoreInstanceMemberAsError() {
CompilerOptions options = new CompilerOptions(getCompilerOptions());

Expand All @@ -38,7 +27,7 @@ public void testReportsUnderscoreInstanceMemberAsError() {
message = "'_' is a keyword from source level 9 onwards, cannot be used as identifier";
errorLevel = "ERROR";
} else {
message = "As of release 21, '_' is only allowed to declare unnamed patterns, local variables, exception parameters or lambda parameters";
message = "Unnamed Patterns and Variables is a preview feature and disabled by default. Use --enable-preview to enable";
errorLevel = "ERROR";
}

Expand Down Expand Up @@ -71,7 +60,7 @@ public void testReportsUnicodeEscapeUnderscoreInstanceMemberAsError() {
message = "'_' is a keyword from source level 9 onwards, cannot be used as identifier";
errorLevel = "ERROR";
} else {
message = "As of release 21, '_' is only allowed to declare unnamed patterns, local variables, exception parameters or lambda parameters";
message = "Unnamed Patterns and Variables is a preview feature and disabled by default. Use --enable-preview to enable";
errorLevel = "ERROR";
}

Expand Down Expand Up @@ -103,7 +92,7 @@ public void testReportsUnderscoreParameterAsError() {
message = "'_' is a keyword from source level 9 onwards, cannot be used as identifier";
errorLevel = "ERROR";
} else {
message = "As of release 21, '_' is only allowed to declare unnamed patterns, local variables, exception parameters or lambda parameters";
message = "Unnamed Patterns and Variables is a preview feature and disabled by default. Use --enable-preview to enable";
errorLevel = "ERROR";
}

Expand Down Expand Up @@ -137,7 +126,7 @@ public void testReportsUnderscoreParameterAsErrorUnicodeEscape() {
message = "'_' is a keyword from source level 9 onwards, cannot be used as identifier";
errorLevel = "ERROR";
} else {
message = "As of release 21, '_' is only allowed to declare unnamed patterns, local variables, exception parameters or lambda parameters";
message = "Unnamed Patterns and Variables is a preview feature and disabled by default. Use --enable-preview to enable";
errorLevel = "ERROR";
}

Expand All @@ -159,4 +148,49 @@ public static void foo(int \\u005F) {
"----------\n");
}

public void testReportsUnderscoreLocalVariableAsErrorUnicodeEscape() {
CompilerOptions options = new CompilerOptions(getCompilerOptions());

String SOURCE_CODE = """
public class A {
public static void main(String[] args) {
int \\u005F = 12;
System.out.println("hello, world");
}
}
""";

if (options.sourceLevel < ClassFileConstants.JDK9) {
runNegativeTest(new String[] { "A.java", SOURCE_CODE },
"""
----------
1. WARNING in A.java (at line 3)
int \\u005F = 12;
^^^^^^
'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on
----------
""");
} else if (options.sourceLevel < ClassFileConstants.JDK21) {
runNegativeTest(new String[] { "A.java", SOURCE_CODE },
"""
----------
1. ERROR in A.java (at line 3)
int \\u005F = 12;
^^^^^^
'_' is a keyword from source level 9 onwards, cannot be used as identifier
----------
""");
} else {
runNegativeTest(new String[] { "A.java", SOURCE_CODE },
"""
----------
1. ERROR in A.java (at line 3)
int \\u005F = 12;
^^^^^^
Unnamed Patterns and Variables is a preview feature and disabled by default. Use --enable-preview to enable
----------
""");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package org.eclipse.jdt.core.tests.compiler.regression;

import java.util.Map;

import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

import junit.framework.Test;

public class UseOfUnderscoreWithPreviewTest extends AbstractBatchCompilerTest {

public static Test suite() {
return buildMinimalComplianceTestSuite(UseOfUnderscoreWithPreviewTest.class, F_21);
}

public UseOfUnderscoreWithPreviewTest(String name) {
super(name);
}

@Override
protected Map<String, String> getCompilerOptions() {
CompilerOptions compilerOptions = new CompilerOptions(super.getCompilerOptions());
if (compilerOptions.sourceLevel == ClassFileConstants.JDK21) {
compilerOptions.enablePreviewFeatures = true;
}
return compilerOptions.getMap();
}

public void testReportsUnderscoreInstanceMemberAsError() {
String message = "As of release 21, '_' is only allowed to declare unnamed patterns, local variables, exception parameters or lambda parameters";
String errorLevel = "ERROR";

runNegativeTest(new String[] { "A.java", """
public class A {
int _ = 1;
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
""" },

"----------\n" +
"1. " + errorLevel + " in A.java (at line 2)\n" +
" int _ = 1;\n" +
" ^\n" +
message + "\n" +
"----------\n");
}

public void testReportsUnicodeEscapeUnderscoreInstanceMemberAsError() {
String message = "As of release 21, '_' is only allowed to declare unnamed patterns, local variables, exception parameters or lambda parameters";
String errorLevel = "ERROR";

runNegativeTest(new String[] { "A.java", """
public class A {
int \\u005F = 1;
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
""" },
"----------\n" +
"1. " + errorLevel + " in A.java (at line 2)\n" +
" int \\u005F = 1;\n" +
" ^^^^^^\n" +
message + "\n" +
"----------\n");
}

public void testReportsUnderscoreParameterAsError() {
String message = "As of release 21, '_' is only allowed to declare unnamed patterns, local variables, exception parameters or lambda parameters";
String errorLevel = "ERROR";

runNegativeTest(new String[] { "A.java", """
public class A {
public static void main(String[] args) {
foo(1);
}
public static void foo(int _) {
System.out.println("Hello, World!");
}
}
""" },
"----------\n" +
"1. " + errorLevel + " in A.java (at line 5)\n" +
" public static void foo(int _) {\n" +
" ^\n" +
message + "\n" +
"----------\n");
}

public void testReportsUnderscoreParameterAsErrorUnicodeEscape() {
String message = "As of release 21, '_' is only allowed to declare unnamed patterns, local variables, exception parameters or lambda parameters";
String errorLevel = "ERROR";

runNegativeTest(new String[] { "A.java", """
public class A {
public static void main(String[] args) {
foo(1);
}
public static void foo(int \\u005F) {
System.out.println("Hello, World!");
}
}
""" },
"----------\n" +
"1. " + errorLevel + " in A.java (at line 5)\n" +
" public static void foo(int \\u005F) {\n" +
" ^^^^^^\n" +
message + "\n" +
"----------\n");
}

public void testReportsUnderscoreLocalVariableAsErrorUnicodeEscape() {
runConformTest(new String[] { "A.java",
"""
public class A {
public static void main(String[] args) {
int \\u005F = 12;
System.out.println("hello, world");
}
}
"""},
"hello, world", null, new String[] { "--enable-preview" });
}

}
Binary file modified org.eclipse.jdt.core/jdtCompilerAdapter.jar
Binary file not shown.

0 comments on commit 2d2f140

Please sign in to comment.