Skip to content

Commit

Permalink
Partially fix testBug531714_015 - switch(i) { case 1 -> list } needs …
Browse files Browse the repository at this point in the history
…yield statement Part 1 and 2

Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
Rob Stryker committed May 14, 2024
1 parent e4930ed commit 3550c1a
Showing 1 changed file with 75 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@
import java.util.function.Predicate;

import javax.lang.model.type.TypeKind;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;

import org.eclipse.core.runtime.ILog;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ModuleModifier.ModuleModifierKeyword;
import org.eclipse.jdt.core.dom.PrefixExpression.Operator;
import org.eclipse.jdt.core.dom.PrimitiveType.Code;
import org.eclipse.jdt.internal.javac.JavacProblemConverter;
import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner;

import com.sun.source.tree.CaseTree.CaseKind;
import com.sun.source.tree.ModuleTree.ModuleKind;
Expand Down Expand Up @@ -100,6 +97,7 @@
import com.sun.tools.javac.tree.JCTree.JCSkip;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCSwitch;
import com.sun.tools.javac.tree.JCTree.JCSwitchExpression;
import com.sun.tools.javac.tree.JCTree.JCSynchronized;
import com.sun.tools.javac.tree.JCTree.JCThrow;
import com.sun.tools.javac.tree.JCTree.JCTry;
Expand Down Expand Up @@ -994,7 +992,7 @@ private void setJavadocForNode(JCTree javac, ASTNode node) {
}
}

private Expression convertExpression(JCExpression javac) {
private Expression convertExpressionImpl(JCExpression javac) {
if (javac instanceof JCIdent ident) {
if (Objects.equals(ident.name, Names.instance(this.context)._this)) {
ThisExpression res = this.ast.newThisExpression();
Expand Down Expand Up @@ -1138,21 +1136,6 @@ private Expression convertExpression(JCExpression javac) {
}
return res;
}
if (javac instanceof JCErroneous error) {
if (error.getErrorTrees().size() == 1) {
JCTree tree = error.getErrorTrees().get(0);
if (tree instanceof JCExpression nestedExpr) {
try {
return convertExpression(nestedExpr);
} catch (Exception ex) {
// pass-through: do not break when attempting such reconcile
}
}
}
ParenthesizedExpression substitute = this.ast.newParenthesizedExpression();
commonSettings(substitute, error);
return substitute;
}
if (javac instanceof JCBinary binary) {
InfixExpression res = this.ast.newInfixExpression();
commonSettings(res, javac);
Expand Down Expand Up @@ -1379,10 +1362,79 @@ private Expression convertExpression(JCExpression javac) {
commonSettings(res, javac);
return res;
}
if (javac instanceof JCSwitchExpression jcSwitch) {
SwitchExpression res = this.ast.newSwitchExpression();
commonSettings(res, javac);
JCExpression switchExpr = jcSwitch.getExpression();
if( switchExpr instanceof JCParens jcp) {
switchExpr = jcp.getExpression();
}
res.setExpression(convertExpression(switchExpr));

List<JCCase> cases = jcSwitch.getCases();
Iterator<JCCase> it = cases.iterator();
ArrayList<JCTree> bodyList = new ArrayList<>();
while(it.hasNext()) {
JCCase switchCase = it.next();
bodyList.add(switchCase);
if( switchCase.getCaseKind() == CaseKind.STATEMENT ) {
if( switchCase.getStatements() != null && switchCase.getStatements().size() > 0 ) {
bodyList.addAll(switchCase.getStatements());
}
} else {
bodyList.add(switchCase.getBody());
}
}

Iterator<JCTree> stmtIterator = bodyList.iterator();
while(stmtIterator.hasNext()) {
JCTree next = stmtIterator.next();
if( next instanceof JCStatement jcs) {
Statement s1 = convertStatement(jcs, res);
if( s1 != null ) {
res.statements().add(s1);
}
} else if( next instanceof JCExpression jce) {
Expression s1 = convertExpression(jce);
if( s1 != null ) {
// make a yield statement out of it??
YieldStatement r1 = this.ast.newYieldStatement();
commonSettings(r1, javac);
r1.setExpression(s1);
res.statements().add(r1);
}
}
}
return res;
}
return null;
}

private Expression convertExpressionOrNull(JCExpression javac) {
return convertExpressionImpl(javac);
}

private Expression convertExpression(JCExpression javac) {
Expression ret = convertExpressionImpl(javac);
if( ret != null )
return ret;

// Handle errors or default situation
if (javac instanceof JCErroneous error) {
if (error.getErrorTrees().size() == 1) {
JCTree tree = error.getErrorTrees().get(0);
if (tree instanceof JCExpression nestedExpr) {
try {
return convertExpression(nestedExpr);
} catch (Exception ex) {
// pass-through: do not break when attempting such reconcile
}
}
}
return this.ast.newSimpleName(new String(RecoveryScanner.FAKE_IDENTIFIER));
}
ILog.get().error("Unsupported " + javac + " of type" + (javac == null ? "null" : javac.getClass()));
ParenthesizedExpression substitute = this.ast.newParenthesizedExpression();
commonSettings(substitute, javac);
return substitute;
return this.ast.newSimpleName(new String(RecoveryScanner.FAKE_IDENTIFIER));
}

private Pattern convert(JCPattern jcPattern) {
Expand Down

0 comments on commit 3550c1a

Please sign in to comment.