KT-15574 Can't instantiate Array through Type Alias

Array instatiation code should handle type alias constructors properly.

So far, we don't have constructors with type parameters
different from the type parameters of the resulting type,
so we can safely take type arguments of the underlying type.
This commit is contained in:
Dmitry Petrov
2017-02-01 12:27:42 +03:00
parent f5a431490c
commit 7600f89f0b
9 changed files with 120 additions and 1 deletions
@@ -2998,7 +2998,8 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
@NotNull
CallGenerator getOrCreateCallGenerator(@NotNull ResolvedCall<?> resolvedCall, @NotNull CallableDescriptor descriptor) {
Map<TypeParameterDescriptor, KotlinType> typeArguments = resolvedCall.getTypeArguments();
Map<TypeParameterDescriptor, KotlinType> typeArguments = getTypeArgumentsForResolvedCall(resolvedCall, descriptor);
TypeParameterMappings mappings = new TypeParameterMappings();
for (Map.Entry<TypeParameterDescriptor, KotlinType> entry : typeArguments.entrySet()) {
TypeParameterDescriptor key = entry.getKey();
@@ -3023,9 +3024,38 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
);
}
}
return getOrCreateCallGenerator(descriptor, resolvedCall.getCall().getCallElement(), mappings, false);
}
@NotNull
private static Map<TypeParameterDescriptor, KotlinType> getTypeArgumentsForResolvedCall(
@NotNull ResolvedCall<?> resolvedCall,
@NotNull CallableDescriptor descriptor
) {
if (!(descriptor instanceof TypeAliasConstructorDescriptor)) {
return resolvedCall.getTypeArguments();
}
TypeAliasConstructorDescriptor typeAliasConstructorDescriptor = (TypeAliasConstructorDescriptor) descriptor;
ClassConstructorDescriptor underlyingConstructorDescriptor = typeAliasConstructorDescriptor.getUnderlyingConstructorDescriptor();
KotlinType resultingType = typeAliasConstructorDescriptor.getReturnType();
List<TypeProjection> typeArgumentsForReturnType = resultingType.getArguments();
List<TypeParameterDescriptor> typeParameters = underlyingConstructorDescriptor.getTypeParameters();
assert typeParameters.size() == typeArgumentsForReturnType.size() :
"Type parameters of the underlying constructor " + underlyingConstructorDescriptor +
"should correspond to type arguments for the resulting type " + resultingType;
Map<TypeParameterDescriptor, KotlinType> typeArgumentsMap = Maps.newHashMapWithExpectedSize(typeParameters.size());
for (TypeParameterDescriptor typeParameter: typeParameters) {
KotlinType typeArgument = typeArgumentsForReturnType.get(typeParameter.getIndex()).getType();
typeArgumentsMap.put(typeParameter, typeArgument);
}
return typeArgumentsMap;
}
@Nullable
private static Pair<TypeParameterDescriptor, ReificationArgument> extractReificationArgument(@NotNull KotlinType type) {
@@ -0,0 +1,8 @@
class Pair<T1, T2>(val x1: T1, val x2: T2)
typealias ST<T> = Pair<String, T>
fun box(): String {
val st = ST<String>("O", "K")
return st.x1 + st.x2
}
@@ -0,0 +1,16 @@
typealias BoolArray = Array<Boolean>
typealias IArray = IntArray
typealias MyArray<T> = Array<T>
fun box(): String {
val ba = BoolArray(1) { true }
if (!ba[0]) return "Fail #1"
val ia = IArray(1) { 42 }
if (ia[0] != 42) return "Fail #2"
val ma = MyArray<Int>(1) { 42 }
if (ma[0] != 42) return "Fail #2"
return "OK"
}
@@ -0,0 +1,13 @@
@kotlin.Metadata
public final class GenericTypeAliasConstructor2Kt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@kotlin.Metadata
public final class Pair {
private final field x1: java.lang.Object
private final field x2: java.lang.Object
public method <init>(p0: java.lang.Object, p1: java.lang.Object): void
public final method getX1(): java.lang.Object
public final method getX2(): java.lang.Object
}
@@ -0,0 +1,4 @@
@kotlin.Metadata
public final class TypeAliasConstructorForArrayKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@@ -17303,6 +17303,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("genericTypeAliasConstructor2.kt")
public void testGenericTypeAliasConstructor2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/genericTypeAliasConstructor2.kt");
doTest(fileName);
}
@TestMetadata("innerClassTypeAliasConstructor.kt")
public void testInnerClassTypeAliasConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/innerClassTypeAliasConstructor.kt");
@@ -17345,6 +17351,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("typeAliasConstructorForArray.kt")
public void testTypeAliasConstructorForArray() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructorForArray.kt");
doTest(fileName);
}
@TestMetadata("typeAliasConstructorInSuperCall.kt")
public void testTypeAliasConstructorInSuperCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructorInSuperCall.kt");
@@ -17303,6 +17303,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("genericTypeAliasConstructor2.kt")
public void testGenericTypeAliasConstructor2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/genericTypeAliasConstructor2.kt");
doTest(fileName);
}
@TestMetadata("innerClassTypeAliasConstructor.kt")
public void testInnerClassTypeAliasConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/innerClassTypeAliasConstructor.kt");
@@ -17345,6 +17351,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("typeAliasConstructorForArray.kt")
public void testTypeAliasConstructorForArray() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructorForArray.kt");
doTest(fileName);
}
@TestMetadata("typeAliasConstructorInSuperCall.kt")
public void testTypeAliasConstructorInSuperCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructorInSuperCall.kt");
@@ -17303,6 +17303,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis
doTest(fileName);
}
@TestMetadata("genericTypeAliasConstructor2.kt")
public void testGenericTypeAliasConstructor2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/genericTypeAliasConstructor2.kt");
doTest(fileName);
}
@TestMetadata("innerClassTypeAliasConstructor.kt")
public void testInnerClassTypeAliasConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/innerClassTypeAliasConstructor.kt");
@@ -17345,6 +17351,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis
doTest(fileName);
}
@TestMetadata("typeAliasConstructorForArray.kt")
public void testTypeAliasConstructorForArray() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructorForArray.kt");
doTest(fileName);
}
@TestMetadata("typeAliasConstructorInSuperCall.kt")
public void testTypeAliasConstructorInSuperCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructorInSuperCall.kt");
@@ -21771,6 +21771,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("genericTypeAliasConstructor2.kt")
public void testGenericTypeAliasConstructor2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/genericTypeAliasConstructor2.kt");
doTest(fileName);
}
@TestMetadata("innerClassTypeAliasConstructor.kt")
public void testInnerClassTypeAliasConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/innerClassTypeAliasConstructor.kt");
@@ -21813,6 +21819,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("typeAliasConstructorForArray.kt")
public void testTypeAliasConstructorForArray() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructorForArray.kt");
doTest(fileName);
}
@TestMetadata("typeAliasConstructorInSuperCall.kt")
public void testTypeAliasConstructorInSuperCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructorInSuperCall.kt");