fix for KT-237. Unit as value part

This commit is contained in:
Alex Tkachman
2011-09-04 16:27:54 +02:00
parent 22e2caf7f8
commit 58765f2364
5 changed files with 70 additions and 10 deletions
@@ -1970,6 +1970,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
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("(");
@@ -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()]));
}
@@ -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)
+24 -3
View File
@@ -1,20 +1,41 @@
fun foreach(array: Array<Int>, action: fun(Int): Unit) {
fun main(args: Array<String>?) {
val y: Unit = () //do not compile
A<Unit>() //do not compile
C<Unit>(()) //do not compile
//do not compile
System.out?.println(fff<Unit>(())) //do not compile
System.out?.println(id<Unit>(y)) //do not compile
System.out?.println(fff<Unit>(id<Unit>(y)) == id<Unit>(foreach(Array<Int>(0),{(e : Int) : Unit => }))) //do not compile
}
class A<T>()
class C<T>(val value: T) {
fun foo(): T = value
}
fun <T> fff(x: T) : T { return x }
fun <T> id(value: T): T = value
fun foreach(array: Array<Int>?, action: fun(Int): Unit) {
for (el in array) {
action(el) //exception through compilation (see below)
}
}
/*
fun almostFilter(array: Array<Int>, action: fun(Int): Int) {
for (el in array) {
action(el)
}
}
*/
fun box() : String {
val a = Array<Int> (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"
}
+26
View File
@@ -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;
}
}