From 58765f2364748ca1704be434ed1255381e1695d4 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Sun, 4 Sep 2011 16:27:54 +0200 Subject: [PATCH] fix for KT-237. Unit as value part --- .../jet/codegen/ExpressionCodegen.java | 5 ++++ .../jetbrains/jet/codegen/JetTypeMapper.java | 20 +++++++++----- .../org/jetbrains/jet/codegen/StackValue.java | 2 +- idea/testData/codegen/regressions/kt237.jet | 27 ++++++++++++++++--- stdlib/src/jet/Tuple0.java | 26 ++++++++++++++++++ 5 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 stdlib/src/jet/Tuple0.java diff --git a/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index df3972e60d0..b3b00f2016a 100644 --- a/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -1970,6 +1970,11 @@ public class ExpressionCodegen extends JetVisitor { if (entries.size() > 22) { throw new UnsupportedOperationException("tuple too large"); } + if(entries.size() == 0) { + v.visitFieldInsn(Opcodes.GETSTATIC, "jet/Tuple0", "INSTANCE", "Ljet/Tuple0;"); + return StackValue.onStack(Type.getObjectType("jet/Tuple0")); + } + final String className = "jet/Tuple" + entries.size(); Type tupleType = Type.getObjectType(className); StringBuilder signature = new StringBuilder("("); diff --git a/idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java b/idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java index 5713d75b728..df0860a370c 100644 --- a/idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java +++ b/idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java @@ -273,14 +273,22 @@ public class JetTypeMapper { return owner; } + public Type mapReturnType(@NotNull final JetType jetType, OwnerKind kind) { + if (jetType.equals(JetStandardClasses.getUnitType()) || jetType.equals(JetStandardClasses.getNothingType())) { + return Type.VOID_TYPE; + } + return mapType(jetType, kind); + } + + public Type mapReturnType(final JetType jetType) { + return mapReturnType(jetType, OwnerKind.INTERFACE); + } + public Type mapType(final JetType jetType) { return mapType(jetType, OwnerKind.INTERFACE); } public Type mapType(@NotNull final JetType jetType, OwnerKind kind) { - if (jetType.equals(JetStandardClasses.getUnitType()) || jetType.equals(JetStandardClasses.getNothingType())) { - return Type.VOID_TYPE; - } if (jetType.equals(standardLibrary.getIntType())) { return Type.INT_TYPE; } @@ -410,10 +418,10 @@ public class JetTypeMapper { if (returnTypeRef == null) { final FunctionDescriptor functionDescriptor = bindingContext.get(BindingContext.FUNCTION, f); final JetType type = functionDescriptor.getReturnType(); - returnType = mapType(type); + returnType = mapReturnType(type); } else { - returnType = mapType(bindingContext.get(BindingContext.TYPE, returnTypeRef)); + returnType = mapReturnType(bindingContext.get(BindingContext.TYPE, returnTypeRef)); } return new Method(f.getName(), returnType, parameterTypes.toArray(new Type[parameterTypes.size()])); } @@ -470,7 +478,7 @@ public class JetTypeMapper { for (ValueParameterDescriptor parameter : parameters) { parameterTypes.add(mapType(parameter.getOutType())); } - Type returnType = mapType(f.getReturnType()); + Type returnType = mapReturnType(f.getReturnType()); return new Method(name, returnType, parameterTypes.toArray(new Type[parameterTypes.size()])); } diff --git a/idea/src/org/jetbrains/jet/codegen/StackValue.java b/idea/src/org/jetbrains/jet/codegen/StackValue.java index 396ff664e85..800bb5be3b7 100644 --- a/idea/src/org/jetbrains/jet/codegen/StackValue.java +++ b/idea/src/org/jetbrains/jet/codegen/StackValue.java @@ -164,7 +164,7 @@ public abstract class StackValue { } else if (type.getSort() != Type.VOID && this.type.getSort() == Type.VOID) { if(type.getSort() == Type.OBJECT) - v.aconst(null); + v.visitFieldInsn(Opcodes.GETSTATIC, "jet/Tuple0", "INSTANCE", "Ljet/Tuple0;"); else if(type == Type.LONG_TYPE) v.lconst(0); else if(type == Type.FLOAT_TYPE) diff --git a/idea/testData/codegen/regressions/kt237.jet b/idea/testData/codegen/regressions/kt237.jet index 1c19cf08e45..adc16c93b60 100644 --- a/idea/testData/codegen/regressions/kt237.jet +++ b/idea/testData/codegen/regressions/kt237.jet @@ -1,20 +1,41 @@ -fun foreach(array: Array, action: fun(Int): Unit) { +fun main(args: Array?) { + val y: Unit = () //do not compile + A() //do not compile + C(()) //do not compile + //do not compile + System.out?.println(fff(())) //do not compile + System.out?.println(id(y)) //do not compile + System.out?.println(fff(id(y)) == id(foreach(Array(0),{(e : Int) : Unit => }))) //do not compile +} +class A() + +class C(val value: T) { + fun foo(): T = value +} + +fun fff(x: T) : T { return x } + +fun id(value: T): T = value + +fun foreach(array: Array?, action: fun(Int): Unit) { for (el in array) { action(el) //exception through compilation (see below) } } -/* + fun almostFilter(array: Array, action: fun(Int): Int) { for (el in array) { action(el) } } -*/ + fun box() : String { val a = Array (3) a[0] = 0 a[1] = 1 a[2] = 2 foreach(a, { (el : Int) : Unit => System.out?.println(el) }) + almostFilter(a, { (el : Int) : Int => el }) + main(null) return "OK" } \ No newline at end of file diff --git a/stdlib/src/jet/Tuple0.java b/stdlib/src/jet/Tuple0.java new file mode 100644 index 00000000000..8d9db829584 --- /dev/null +++ b/stdlib/src/jet/Tuple0.java @@ -0,0 +1,26 @@ +package jet; + +/** + * @author alex.tkachman + */ +public class Tuple0 { + public static final Tuple0 INSTANCE = new Tuple0(); + + private Tuple0() { + } + + @Override + public String toString() { + return "()"; + } + + @Override + public boolean equals(Object o) { + return o == INSTANCE; + } + + @Override + public int hashCode() { + return 239; + } +} \ No newline at end of file