Type inference for switch

This commit is contained in:
Daniel Holle 2023-08-17 09:27:24 +02:00
parent 762d344e42
commit 14d0475d59
2 changed files with 5 additions and 2 deletions

View File

@ -5,7 +5,7 @@ import java.lang.Float;
record Rec(Integer a, Object b) {}
public class Switch {
Integer main(Object o) {
main(o) {
return switch (o) {
case Rec(Integer a, Integer b) -> { yield a + b; }
case Rec(Integer a, Float b) -> { yield a + 10; }

View File

@ -735,6 +735,9 @@ public class TYPEStmt implements StatementVisitor {
public void visit(Switch switchStmt) {
switchStack.push(switchStmt);
for (var child : switchStmt.getBlocks()) {
for (var label : child.getLabels()) if (label.getExpression() instanceof Pattern)
constraintsSet.addUndConstraint(new Pair(label.getExpression().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT));
child.accept(this);
constraintsSet.addUndConstraint(new Pair(child.getType(), switchStmt.getType(), PairOperator.SMALLERDOT));
}
@ -756,7 +759,7 @@ public class TYPEStmt implements StatementVisitor {
@Override
public void visit(Yield aYield) {
aYield.retexpr.accept(this);
constraintsSet.addUndConstraint(new Pair(aYield.getType(), switchStack.peek().getType(), PairOperator.SMALLERDOT));
constraintsSet.addUndConstraint(new Pair(aYield.getType(), switchStack.peek().getType(), PairOperator.EQUALSDOT));
// TODO Auto-generated method stub
}