From 07b2ab347deca99d69d19d951b4beafc96bb311e Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Sat, 3 Sep 2011 17:09:49 +0200 Subject: [PATCH] more tests for for- --- .../jet/codegen/ExpressionCodegen.java | 13 ++-- .../jet/codegen/FunctionCodegen.java | 4 +- .../codegen/controlStructures/forUserType.jet | 69 ++++++++++++++++--- 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index d3f56c7962a..c75ea5a5549 100644 --- a/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -1,9 +1,7 @@ package org.jetbrains.jet.codegen; import com.intellij.openapi.editor.Document; -import com.intellij.openapi.project.Project; import com.intellij.psi.*; -import com.intellij.psi.search.ProjectScope; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiTreeUtil; import gnu.trove.THashSet; @@ -34,6 +32,7 @@ import java.util.*; /** * @author max * @author yole + * @author alex.tkachman */ public class ExpressionCodegen extends JetVisitor { private static final String CLASS_OBJECT = "java/lang/Object"; @@ -308,6 +307,7 @@ public class ExpressionCodegen extends JetVisitor { v.mark(end); int paramIndex = myMap.leave(parameterDescriptor); + //noinspection ConstantConditions v.visitLocalVariable(loopParameter.getName(), asmParamType.getDescriptor(), null, begin, end, paramIndex); myMap.leaveTemp(); myBreakTargets.pop(); @@ -363,6 +363,7 @@ public class ExpressionCodegen extends JetVisitor { cleanupTemp(); final int paramIndex = myMap.leave(parameterDescriptor); + //noinspection ConstantConditions v.visitLocalVariable(expression.getLoopParameter().getName(), asmParamType.getDescriptor(), null, condition, end, paramIndex); myBreakTargets.pop(); myContinueTargets.pop(); @@ -822,8 +823,10 @@ public class ExpressionCodegen extends JetVisitor { setter = null; } else { + //noinspection ConstantConditions getter = isInsideClass && (propertyDescriptor.getGetter() == null || propertyDescriptor.getGetter().isDefault()) ? null : typeMapper.mapGetterSignature(propertyDescriptor); + //noinspection ConstantConditions setter = isInsideClass && (propertyDescriptor.getSetter() == null || propertyDescriptor.getSetter().isDefault()) ? null : typeMapper.mapSetterSignature(propertyDescriptor); } @@ -1434,12 +1437,15 @@ public class ExpressionCodegen extends JetVisitor { value.dupReceiver(v, 0); value.put(asmType, v); if (asmType == Type.LONG_TYPE) { + //noinspection UnnecessaryBoxing v.aconst(Long.valueOf(increment)); } else if (asmType == Type.FLOAT_TYPE) { + //noinspection UnnecessaryBoxing v.aconst(Float.valueOf(increment)); } else if (asmType == Type.DOUBLE_TYPE) { + //noinspection UnnecessaryBoxing v.aconst(Double.valueOf(increment)); } else { @@ -1741,7 +1747,6 @@ public class ExpressionCodegen extends JetVisitor { v.mark(lblCheck); for (int i = 0; i < entries.size(); i++) { - final boolean isLast = i == entries.size() - 1; final StackValue tupleField = StackValue.field(OBJECT_TYPE, tupleClassName, "_" + (i + 1), false); final StackValue stackValue = generatePatternMatch(entries.get(i).getPattern(), false, tupleField, nextEntry); stackValue.condJump(lblPopAndFail, true, v); @@ -1956,7 +1961,7 @@ public class ExpressionCodegen extends JetVisitor { final String className = "jet/Tuple" + entries.size(); Type tupleType = Type.getObjectType(className); StringBuilder signature = new StringBuilder("("); - for (JetExpression entry : entries) { + for (int i = 0; i != entries.size(); ++i) { signature.append("Ljava/lang/Object;"); } signature.append(")V"); diff --git a/idea/src/org/jetbrains/jet/codegen/FunctionCodegen.java b/idea/src/org/jetbrains/jet/codegen/FunctionCodegen.java index 51d53ad7b9c..f3e71709f1a 100644 --- a/idea/src/org/jetbrains/jet/codegen/FunctionCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/FunctionCodegen.java @@ -133,8 +133,8 @@ public class FunctionCodegen { if(overriddenFunctions.size() > 0) { for (FunctionDescriptor overriddenFunction : overriddenFunctions) { // TODO should we check params here as well? - if(!JetTypeImpl.equalTypeClasses(overriddenFunction.getReturnType(), functionDescriptor.getReturnType())) { - generateBridgeMethod(jvmSignature, state.getTypeMapper().mapSignature(overriddenFunction.getName(), overriddenFunction)); + if(!JetTypeImpl.equalTypeClasses(overriddenFunction.getOriginal().getReturnType(), functionDescriptor.getReturnType())) { + generateBridgeMethod(jvmSignature, state.getTypeMapper().mapSignature(overriddenFunction.getName(), overriddenFunction.getOriginal())); } } } diff --git a/idea/testData/codegen/controlStructures/forUserType.jet b/idea/testData/codegen/controlStructures/forUserType.jet index cb070bb1c68..be95e3b437e 100644 --- a/idea/testData/codegen/controlStructures/forUserType.jet +++ b/idea/testData/codegen/controlStructures/forUserType.jet @@ -1,25 +1,70 @@ fun box() : String { + var sum : Int = 0 + var i = 0 + val c6 = MyCollection4() + sum = 0 for (el in c6) { - System.out?.println(el) + sum = sum + el } + if(sum != 15) return "c6 failed" val c5 = MyCollection3() + sum = 0 for (el in c5) { - System.out?.println(el) + sum = sum + (el ?: 0) } + if(sum != 15) return "c5 failed" val c1: java.lang.Iterable = MyCollection1() - for (el in c1) {} + sum = 0 + for (el in c1) { + sum = sum + el + } + if(sum != 15) return "c1 failed" val c2 = MyCollection1() - for (el in c2) {} + sum = 0 + for (el in c2) { + sum = sum + el + } + if(sum != 15) return "c2 failed" val c3: Iterable = MyCollection2() - for (el in c3) {} + sum = 0 + for (el in c3) { + sum = sum + el + } + if(sum != 15) return "c3 failed" val c4 = MyCollection2() - for (el in c4) {} + sum = 0 + for (el in c4) { + sum = sum + el + } + if(sum != 15) return "c4 failed" + + val a : Array = Array (5) + for(el in 0..4) { + a[i] = i++ + } + sum = 0 + for (el in a) { + sum = sum + el + } + if(sum != 10) return "a failed" + + val b : Array = Array (5) + i = 0 + while(i < 5) { + b[i] = i++ + } + sum = 0 + for (el in b) { + sum = sum + (el ?: 0) + } + System.out?.println(sum) + if(sum != 10) return "b failed" return "OK" } @@ -28,8 +73,10 @@ class MyCollection1(): java.lang.Iterable { fun iterator(): java.util.Iterator = MyIterator() class MyIterator(): java.util.Iterator { - fun next() = 0 - fun hasNext() = false + var k : Int = 5 + + fun next() : Int = k-- + fun hasNext() = k > 0 } } @@ -37,8 +84,10 @@ class MyCollection2(): Iterable { fun iterator(): Iterator = MyIterator() class MyIterator(): Iterator { - fun next() = 0 - fun hasNext() = false + var k : Int = 5 + + fun next() : Int = k-- + fun hasNext() = k > 0 } }