From 14d0475d597cabacf638a69ac88a24baa069f759 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Thu, 17 Aug 2023 09:27:24 +0200 Subject: [PATCH] Type inference for switch --- resources/bytecode/javFiles/Switch.jav | 2 +- .../de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/resources/bytecode/javFiles/Switch.jav b/resources/bytecode/javFiles/Switch.jav index 95de49e1..03308375 100644 --- a/resources/bytecode/javFiles/Switch.jav +++ b/resources/bytecode/javFiles/Switch.jav @@ -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; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 5c68974d..7f1a9159 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -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 }