KT-313 Bug in substitutions in a function returning its type parameter T
This commit is contained in:
+1
-1
@@ -144,7 +144,7 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement
|
||||
defaultType = new JetTypeImpl(
|
||||
Collections.<AnnotationDescriptor>emptyList(),
|
||||
getTypeConstructor(),
|
||||
TypeUtils.hasNullableBound(this),
|
||||
TypeUtils.hasNullableLowerBound(this),
|
||||
Collections.<TypeProjection>emptyList(),
|
||||
new LazyScopeAdapter(new LazyValue<JetScope>() {
|
||||
@Override
|
||||
|
||||
@@ -78,7 +78,7 @@ public class TypeResolver {
|
||||
result[0] = new JetTypeImpl(
|
||||
annotations,
|
||||
typeParameterDescriptor.getTypeConstructor(),
|
||||
nullable || TypeUtils.hasNullableBound(typeParameterDescriptor),
|
||||
nullable || TypeUtils.hasNullableLowerBound(typeParameterDescriptor),
|
||||
Collections.<TypeProjection>emptyList(),
|
||||
getScopeForTypeParameter(typeParameterDescriptor)
|
||||
);
|
||||
|
||||
@@ -97,12 +97,14 @@ public class JetTypeChecker {
|
||||
if (!supertype.isNullable() && subtype.isNullable()) {
|
||||
return false;
|
||||
}
|
||||
subtype = TypeUtils.makeNotNullable(subtype);
|
||||
supertype = TypeUtils.makeNotNullable(supertype);
|
||||
if (JetStandardClasses.isNothingOrNullableNothing(subtype)) {
|
||||
return true;
|
||||
}
|
||||
@Nullable JetType closestSupertype = findCorrespondingSupertype(subtype, supertype);
|
||||
if (closestSupertype == null) {
|
||||
if (!constraintBuilder.noCorrespondingSupertype(subtype, supertype)) return false;
|
||||
return constraintBuilder.noCorrespondingSupertype(subtype, supertype); // if this returns true, there still isn't any supertype to continue with
|
||||
}
|
||||
|
||||
return checkSubtypeForTheSameConstructor(closestSupertype, supertype);
|
||||
|
||||
@@ -130,7 +130,7 @@ public class TypeSubstitutor {
|
||||
if (value != null) {
|
||||
assert constructor.getDeclarationDescriptor() instanceof TypeParameterDescriptor;
|
||||
|
||||
return substitutionResult((TypeParameterDescriptor) constructor.getDeclarationDescriptor(), howThisTypeIsUsed, Variance.INVARIANT, value).getType();
|
||||
return TypeUtils.makeNullableIfNeeded(substitutionResult((TypeParameterDescriptor) constructor.getDeclarationDescriptor(), howThisTypeIsUsed, Variance.INVARIANT, value).getType(), type.isNullable());
|
||||
|
||||
// if (!allows(howThisTypeIsUsed, value.getProjectionKind())) {
|
||||
// throw new SubstitutionException("!!" + value.toString());
|
||||
|
||||
@@ -385,8 +385,8 @@ public class TypeUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean hasNullableBound(@NotNull TypeParameterDescriptor typeParameterDescriptor) {
|
||||
for (JetType bound : typeParameterDescriptor.getUpperBounds()) {
|
||||
public static boolean hasNullableLowerBound(@NotNull TypeParameterDescriptor typeParameterDescriptor) {
|
||||
for (JetType bound : typeParameterDescriptor.getLowerBounds()) {
|
||||
if (bound.isNullable()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
+7
-4
@@ -209,7 +209,7 @@ public class ConstraintSystemImpl implements ConstraintSystem {
|
||||
public boolean noCorrespondingSupertype(@NotNull JetType subtype, @NotNull JetType supertype) {
|
||||
boolean result = delegate.noCorrespondingSupertype(subtype, supertype);
|
||||
if (!result) {
|
||||
println("-- " + subtype + " has supertype corresponding to " + supertype);
|
||||
println("-- " + subtype + " has no supertype corresponding to " + supertype);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -273,9 +273,12 @@ public class ConstraintSystemImpl implements ConstraintSystem {
|
||||
DeclarationDescriptor declarationDescriptor = type.getConstructor().getDeclarationDescriptor();
|
||||
if (declarationDescriptor instanceof TypeParameterDescriptor) {
|
||||
TypeParameterDescriptor typeParameterDescriptor = (TypeParameterDescriptor) declarationDescriptor;
|
||||
UnknownType unknownType = unknownTypes.get(typeParameterDescriptor);
|
||||
if (unknownType != null) {
|
||||
return unknownType;
|
||||
// Checking that this is not a T?, but exactly T
|
||||
if (typeParameterDescriptor.getDefaultType().isNullable() == type.isNullable()) {
|
||||
UnknownType unknownType = unknownTypes.get(typeParameterDescriptor);
|
||||
if (unknownType != null) {
|
||||
return unknownType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,8 +18,8 @@ fun foo(c: Consumer<Int>, p: Producer<Int>, u: Usual<Int>) {
|
||||
}
|
||||
|
||||
//Arrays copy example
|
||||
class Array<T>(val length : Int) {
|
||||
fun get(index : Int) : T { return null }
|
||||
class Array<T>(val length : Int, val t : T) {
|
||||
fun get(index : Int) : T { return t }
|
||||
fun set(index : Int, value : T) { /* ... */ }
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// KT-313 Bug in substitutions in a function returning its type parameter T
|
||||
|
||||
fun <T> Iterable<T>.join(separator : String?) : String {
|
||||
return separator.npe()
|
||||
}
|
||||
|
||||
fun <T : Any> T?.npe() : T {
|
||||
if (this == null)
|
||||
throw NullPointerException()
|
||||
return this;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
namespace test
|
||||
|
||||
class List<T>(len: Int) {
|
||||
val a : Array<T> = Array<T>(len)
|
||||
val a : Array<T?> = Array<T?>(len)
|
||||
|
||||
fun reverse() {
|
||||
var i = 0
|
||||
@@ -29,7 +29,7 @@ fun box() : String {
|
||||
|
||||
val c = List<Array<Int>>(1)
|
||||
c.a[0] = Array<Int>(4,{-1})
|
||||
println(c.a[0].size)
|
||||
println(c.a[0]?.size)
|
||||
|
||||
val e = List<Int>(5)
|
||||
e.a[0] = 0
|
||||
|
||||
@@ -36,8 +36,8 @@ trait WriteOnlyArray<in T> : ISized {
|
||||
}
|
||||
}
|
||||
|
||||
class MutableArray<T>(length: Int) : ReadOnlyArray<T>, WriteOnlyArray<T> {
|
||||
private val array = Array<T>(length)
|
||||
class MutableArray<T>(length: Int, init : fun(Int) : T) : ReadOnlyArray<T>, WriteOnlyArray<T> {
|
||||
private val array = Array<T>(length, init)
|
||||
|
||||
override fun get(index : Int) : T = array[index]
|
||||
override fun set(index : Int, value : T) : Unit { array[index] = value }
|
||||
@@ -47,7 +47,7 @@ class MutableArray<T>(length: Int) : ReadOnlyArray<T>, WriteOnlyArray<T> {
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
var a = MutableArray<Int> (4)
|
||||
var a = MutableArray<Int> (4, {0})
|
||||
a [0] = 10
|
||||
a.set(1, 2, 13)
|
||||
a [3] = 40
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ArrayGenTest extends CodegenTestCase {
|
||||
}
|
||||
|
||||
public void testCreateMultiGenerics () throws Exception {
|
||||
loadText("class L<T>() { val a = Array<T>(5) } fun foo() = L<Int>.a");
|
||||
loadText("class L<T>() { val a = Array<T?>(5) } fun foo() = L<Int>.a");
|
||||
System.out.println(generateToText());
|
||||
Method foo = generateFunction();
|
||||
Object invoke = foo.invoke(null);
|
||||
|
||||
@@ -18,8 +18,8 @@ fun foo(c: Consumer<Int>, p: Producer<Int>, u: Usual<Int>) {
|
||||
}
|
||||
|
||||
//Arrays copy example
|
||||
class Array<T>(val length : Int) {
|
||||
fun get(index : Int) : T { return null }
|
||||
class Array<T>(val length : Int, val t : T) {
|
||||
fun get(index : Int) : T { return t }
|
||||
fun set(index : Int, value : T) { /* ... */ }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user