Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP vanilla identity conversion on Switch #2867

Draft
wants to merge 4 commits into
base: BETA_JAVA23
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ private Constant resolveCasePattern(BlockScope scope, TypeBinding caseType, Type
switchStatement.totalPattern = e;
}
e.isTotalTypeNode = true;
if (switchStatement.nullCase == null)
if (switchStatement.nullCase == null
&& SwitchStatement.IsNullRequiredWithPrimitivesInPatterns(scope, expressionType))
constant = IntConstant.fromValue(-1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Javadoc extends ASTNode {
// bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=153399
// Store value tag positions
public long valuePositions = -1;
public boolean isMarkdown;

public Javadoc(int sourceStart, int sourceEnd) {
this.sourceStart = sourceStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,8 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream) {
}
}
private void transformConstants() {
if (this.nullCase == null) {
if (this.nullCase == null
&& SwitchStatement.IsNullRequiredWithPrimitivesInPatterns(this.scope, this.expression.resolvedType)) {
for (int i = 0,l = this.otherConstants.length; i < l; ++i) {
if (this.otherConstants[i].e == this.totalPattern) {
this.otherConstants[i].index = -1;
Expand All @@ -945,6 +946,12 @@ private void transformConstants() {
this.constants[i] = this.otherConstants[i].index;
}
}
public static boolean IsNullRequiredWithPrimitivesInPatterns(BlockScope scope,
TypeBinding expressionType) {
return !(expressionType.isBaseType() && JavaFeature.PRIMITIVES_IN_PATTERNS.isSupported(
scope.compilerOptions().sourceLevel,
scope.compilerOptions().enablePreviewFeatures));
}
private void generateCodeSwitchPatternEpilogue(CodeStream codeStream) {
if (needPatternDispatchCopy()) {
codeStream.removeVariable(this.dispatchPatternCopy);
Expand All @@ -954,7 +961,7 @@ private void generateCodeSwitchPatternEpilogue(CodeStream codeStream) {

private void generateCodeSwitchPatternPrologue(BlockScope currentScope, CodeStream codeStream) {
this.expression.generateCode(currentScope, codeStream, true);
if ((this.switchBits & NullCase) == 0) {
if ((this.switchBits & NullCase) == 0 && !this.expression.resolvedType.isBaseType()) {
codeStream.dup();
codeStream.invokeJavaUtilObjectsrequireNonNull();
codeStream.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,21 @@ public boolean checkDeprecation(int commentPtr) {
this.javadocStart = this.sourceParser.scanner.commentStarts[commentPtr];
this.javadocEnd = this.sourceParser.scanner.commentStops[commentPtr]-1;
this.firstTagPosition = this.sourceParser.scanner.commentTagStarts[commentPtr];
this.markdown = this.sourceParser.scanner.commentIsMarkdown[commentPtr];
this.validValuePositions = -1;
this.invalidValuePositions = -1;
this.tagWaitingForDescription = NO_TAG_VALUE;

// Init javadoc if necessary
if (this.checkDocComment) {
this.docComment = new Javadoc(this.javadocStart, this.javadocEnd);
this.docComment.isMarkdown = this.markdown;
} else if (this.setJavadocPositions) {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=189459
// if annotation processors are there, javadoc object is required but
// they need not be resolved
this.docComment = new Javadoc(this.javadocStart, this.javadocEnd);
this.docComment.isMarkdown = this.markdown;
this.docComment.bits &= ~ASTNode.ResolveJavadoc;
} else {
this.docComment = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11499,22 +11499,14 @@ public int flushCommentsDefinedPriorTo(int position) {
break;
// move valid comment infos, overriding obsolete comment infos
case 2:
this.scanner.commentStarts[0] = this.scanner.commentStarts[index+1];
this.scanner.commentStops[0] = this.scanner.commentStops[index+1];
this.scanner.commentTagStarts[0] = this.scanner.commentTagStarts[index+1];
this.scanner.commentStarts[1] = this.scanner.commentStarts[index+2];
this.scanner.commentStops[1] = this.scanner.commentStops[index+2];
this.scanner.commentTagStarts[1] = this.scanner.commentTagStarts[index+2];
this.scanner.copyCommentInfo(0, index+1);
this.scanner.copyCommentInfo(1, index+2);
break;
case 1:
this.scanner.commentStarts[0] = this.scanner.commentStarts[index+1];
this.scanner.commentStops[0] = this.scanner.commentStops[index+1];
this.scanner.commentTagStarts[0] = this.scanner.commentTagStarts[index+1];
this.scanner.copyCommentInfo(0, index+1);
break;
default:
System.arraycopy(this.scanner.commentStarts, index + 1, this.scanner.commentStarts, 0, validCount);
System.arraycopy(this.scanner.commentStops, index + 1, this.scanner.commentStops, 0, validCount);
System.arraycopy(this.scanner.commentTagStarts, index + 1, this.scanner.commentTagStarts, 0, validCount);
this.scanner.copyAllCommentInfo(index+1, 0, validCount);
}
this.scanner.commentPtr = validCount - 1;
return position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class Scanner implements TerminalTokens {
public final static int COMMENT_ARRAYS_SIZE = 30;
public int[] commentStops = new int[COMMENT_ARRAYS_SIZE];
public int[] commentStarts = new int[COMMENT_ARRAYS_SIZE];
public boolean[] commentIsMarkdown = new boolean[COMMENT_ARRAYS_SIZE];
public int[] commentTagStarts = new int[COMMENT_ARRAYS_SIZE];
public int commentPtr = -1; // no comment test with commentPtr value -1
public int lastCommentLinePosition = -1;
Expand Down Expand Up @@ -3215,6 +3216,7 @@ public void recordComment(int token) {
// compute position
int commentStart = this.startPosition;
int stopPosition = this.currentPosition;
boolean isMarkdown = false;
switch (token) {
case TokenNameCOMMENT_LINE:
// both positions are negative
Expand All @@ -3226,19 +3228,19 @@ public void recordComment(int token) {
stopPosition = -this.currentPosition;
break;
case TokenNameCOMMENT_MARKDOWN:
isMarkdown = true;
break;
}

// a new comment is recorded
int length = this.commentStops.length;
if (++this.commentPtr >= length) {
int newLength = length + COMMENT_ARRAYS_SIZE*10;
System.arraycopy(this.commentStops, 0, this.commentStops = new int[newLength], 0, length);
System.arraycopy(this.commentStarts, 0, this.commentStarts = new int[newLength], 0, length);
System.arraycopy(this.commentTagStarts, 0, this.commentTagStarts = new int[newLength], 0, length);
growCommentInfoArrays(length, newLength);
}
this.commentStops[this.commentPtr] = stopPosition;
this.commentStarts[this.commentPtr] = commentStart;
this.commentIsMarkdown[this.commentPtr] = isMarkdown;
}

/**
Expand Down Expand Up @@ -5938,6 +5940,25 @@ public static InvalidInputException invalidInput() {
return new InvalidInputException();
}

public void copyCommentInfo(int to, int from) {
this.commentStarts[to] = this.commentStarts[from];
this.commentStops[to] = this.commentStops[from];
this.commentTagStarts[to] = this.commentTagStarts[from];
this.commentIsMarkdown[to] = this.commentIsMarkdown[from];
}

public void copyAllCommentInfo(int from, int to, int length) {
System.arraycopy(this.commentStarts, from, this.commentStarts, to, length);
System.arraycopy(this.commentStops, from, this.commentStops, to, length);
System.arraycopy(this.commentTagStarts, from, this.commentTagStarts, to, length);
System.arraycopy(this.commentIsMarkdown, from, this.commentIsMarkdown, 0, length);
}

protected void growCommentInfoArrays(int length, int newLength) {
System.arraycopy(this.commentStops, 0, this.commentStops = new int[newLength], 0, length);
System.arraycopy(this.commentStarts, 0, this.commentStarts = new int[newLength], 0, length);
System.arraycopy(this.commentIsMarkdown, 0, this.commentIsMarkdown = new boolean[newLength], 0, length);
System.arraycopy(this.commentTagStarts, 0, this.commentTagStarts = new int[newLength], 0, length);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class PrimitiveInPatternsTest extends AbstractRegressionTest9 {
static {
// TESTS_NUMBERS = new int [] { 1 };
// TESTS_RANGE = new int[] { 1, -1 };
// TESTS_NAMES = new String[] { "test267" };
TESTS_NAMES = new String[] { "test268" };
}
private String extraLibPath;
public static Class<?> testClass() {
Expand Down Expand Up @@ -6782,6 +6782,25 @@ public static void main(String[] args) {
},
"1");
}
// switch test cases
public void test268() {
runConformTest(new String[] {
"X.java",
"""
public class X {
public static int foo(int i) {
return switch (i) {
case int k -> 100;
};
}
public static void main(String argv[]) {
System.out.println(X.foo(0));
}
}
"""
},
"100");
}
public void testNonPrim001() {
runConformTest(new String[] {
"X.java",
Expand All @@ -6802,7 +6821,8 @@ public static void main(String argv[]) {
"""
},
"true");
} // test from spec
}
// test from spec
public void _testSpec001() {
runConformTest(new String[] {
"X.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3327,6 +3327,7 @@ void numberOfSpaces2() { }
Comment comment = (Comment) unitComments.get(0);
assertEquals("Comment should be javadoc", comment.getNodeType(), ASTNode.JAVADOC);
Javadoc docComment = (Javadoc) comment;
assertTrue(this.prefix+"should be markdown", docComment.isMarkdown());
assertEquals(this.prefix+"Wrong number of tags", 1, docComment.tags().size());

TagElement tagElement = (TagElement) docComment.tags().get(0);
Expand Down
8 changes: 8 additions & 0 deletions org.eclipse.jdt.core/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@
</message_arguments>
</filter>
</resource>
<resource path="dom/org/eclipse/jdt/core/dom/Javadoc.java" type="org.eclipse.jdt.core.dom.Javadoc">
<filter comment="Evolution: add property for markdown javadoc" id="336658481">
<message_arguments>
<message_argument value="org.eclipse.jdt.core.dom.Javadoc"/>
<message_argument value="MARKDOWN_PROPERTY"/>
</message_arguments>
</filter>
</resource>
<resource path="dom/org/eclipse/jdt/core/dom/MemberRef.java" type="org.eclipse.jdt.core.dom.MemberRef">
<filter id="576725006">
<message_arguments>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public boolean checkDeprecation(int commentPtr) {
@Override
protected boolean commentParse() {
this.docComment = new CompletionJavadoc(this.javadocStart, this.javadocEnd);
this.docComment.isMarkdown = this.markdown;
this.firstTagPosition = 1; // bug 429340: completion parser needs to parse the whole comment
return super.commentParse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public boolean checkDeprecation(int commentPtr) {
@Override
protected boolean commentParse() {
this.docComment = new SelectionJavadoc(this.javadocStart, this.javadocEnd);
this.docComment.isMarkdown = this.markdown;
return super.commentParse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public Javadoc parse(int start, int length) {
commentParse();
}
this.docComment.setSourceRange(start, length);
if (this.ast.apiLevel >= AST.JLS23_INTERNAL) {
this.docComment.setMarkdown(this.markdown);
}
if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
setComment(start, length); // backward compatibility
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public static List propertyDescriptors(int apiLevel) {
* @exception UnsupportedOperationException if this operation is used in
* an AST below JLS23
* @since 3.39
* @noreference preview feature
*/
public List modifiers() {
if (this.ast.apiLevel < AST.JLS23_INTERNAL)
Expand All @@ -202,6 +203,7 @@ public List modifiers() {
* @return the bit-wise "or" of <code>Modifier</code> constants
* @see Modifier
* @since 3.39
* @noreference preview feature
*/
public int getModifiers() {
if (this.modifiers == null) {
Expand Down
Loading