Underlying constructor for type alias should always be substituted

Otherwise PSI2IR fails because of type mismatch.
This commit is contained in:
Dmitry Petrov
2018-03-16 17:11:24 +03:00
parent bfcf1584a2
commit eb5904ea8e
8 changed files with 81 additions and 15 deletions
@@ -1117,7 +1117,7 @@ public class KotlinTypeMapper {
}
if (f instanceof TypeAliasConstructorDescriptor) {
return mapSignature(((TypeAliasConstructorDescriptor) f).getUnderlyingConstructorDescriptor(), kind, skipGenericSignature);
return mapSignature(((TypeAliasConstructorDescriptor) f).getUnderlyingConstructorDescriptor().getOriginal(), kind, skipGenericSignature);
}
if (f instanceof FunctionImportedFromObject) {
@@ -100,7 +100,8 @@ class SymbolTable {
private var currentScope: Scope? = null
override fun get(d: D): S? {
val scope = currentScope ?: throw AssertionError("No active scope")
val scope = currentScope
?: throw AssertionError("No active scope")
return scope[d]
}
@@ -0,0 +1,5 @@
class Cell<T>(val value: T)
typealias IntAlias = Cell<Int>
fun test() = IntAlias(42)
@@ -0,0 +1,43 @@
FILE fqName:<root> fileName:/specializedTypeAliasConstructorCall.kt
CLASS CLASS name:Cell modality:FINAL visibility:public flags:
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:Cell<T> flags:
superClasses:
CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Any modality:OPEN visibility:public flags:
TYPE_PARAMETER name:T index:0 variance: upperBounds:[kotlin.Any?]
superClassifiers:
CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Any modality:OPEN visibility:public flags:
CONSTRUCTOR visibility:public <> (value:T) returnType:Cell<T> flags:
VALUE_PARAMETER name:value index:0 type:T flags:
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
INSTANCE_INITIALIZER_CALL classDescriptor='Cell'
PROPERTY name:value type:T visibility:public modality:FINAL flags:val
FIELD PROPERTY_BACKING_FIELD name:value type:T visibility:public
EXPRESSION_BODY
GET_VAR 'value-parameter value: T' type=T origin=INITIALIZE_PROPERTY_FROM_PARAMETER
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-value> visibility:public modality:FINAL <> ($this:Cell<T>) returnType:T flags:
$this: VALUE_PARAMETER name:<this> type:Cell<T> flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-value>(): T'
GET_FIELD 'value: T' type=T origin=null
receiver: GET_VAR 'this@Cell: Cell<T>' type=Cell<T> origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:Boolean flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:Boolean flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Any flags:
VALUE_PARAMETER name:other index:0 type:kotlin.Any? flags:
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:Int flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:Int flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Any flags:
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:String flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:String flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Any flags:
TYPEALIAS typealias IntAlias = Cell<Int> type=Cell<kotlin.Int>
FUN name:test visibility:public modality:FINAL <> () returnType:Cell<Int> flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='test(): IntAlias /* = Cell<Int> */'
CALL 'constructor Cell(Int)' type=Cell<kotlin.Int> origin=null
<T>: null
value: CONST Int type=kotlin.Int value=42
@@ -951,6 +951,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
doTest(fileName);
}
@TestMetadata("specializedTypeAliasConstructorCall.kt")
public void testSpecializedTypeAliasConstructorCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/specializedTypeAliasConstructorCall.kt");
doTest(fileName);
}
@TestMetadata("stringComparisons.kt")
public void testStringComparisons() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/stringComparisons.kt");
@@ -599,6 +599,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
final TypeSubstitutor substitutor = DescriptorSubstitutor.substituteTypeParameters(
unsubstitutedTypeParameters, configuration.substitution, substitutedDescriptor, substitutedTypeParameters, wereChanges
);
if (substitutor == null) return null;
KotlinType substitutedReceiverParameterType = null;
if (configuration.newExtensionReceiverParameterType != null) {
@@ -768,6 +769,15 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
return visitor.visitFunctionDescriptor(this, data);
}
@Nullable
public static List<ValueParameterDescriptor> getSubstitutedValueParameters(
FunctionDescriptor substitutedDescriptor,
@NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
@NotNull TypeSubstitutor substitutor
) {
return getSubstitutedValueParameters(substitutedDescriptor, unsubstitutedValueParameters, substitutor, false, false, null);
}
@Nullable
public static List<ValueParameterDescriptor> getSubstitutedValueParameters(
FunctionDescriptor substitutedDescriptor,
@@ -175,27 +175,26 @@ class TypeAliasConstructorDescriptorImpl private constructor(
constructor: ClassConstructorDescriptor
): TypeAliasConstructorDescriptor? {
val substitutorForUnderlyingClass = typeAliasDescriptor.getTypeSubstitutorForUnderlyingClass() ?: return null
val substitutedConstructor = constructor.substitute(substitutorForUnderlyingClass) ?: return null
val typeAliasConstructor =
TypeAliasConstructorDescriptorImpl(
storageManager, typeAliasDescriptor, constructor, null, constructor.annotations,
storageManager, typeAliasDescriptor,
substitutedConstructor,
null, constructor.annotations,
constructor.kind, typeAliasDescriptor.source
)
val valueParameters =
FunctionDescriptorImpl.getSubstitutedValueParameters(
typeAliasConstructor, constructor.valueParameters, substitutorForUnderlyingClass, false, false,
null
typeAliasConstructor, constructor.valueParameters, substitutorForUnderlyingClass
) ?: return null
val returnType = run {
val returnTypeNoAbbreviation = substitutorForUnderlyingClass.substitute(constructor.returnType, Variance.INVARIANT)
?: return null
returnTypeNoAbbreviation.unwrap().lowerIfFlexible().withAbbreviation(typeAliasDescriptor.defaultType)
}
val returnType = substitutedConstructor.returnType.unwrap().lowerIfFlexible().withAbbreviation(typeAliasDescriptor.defaultType)
val receiverParameterType =
constructor.dispatchReceiverParameter?.let { substitutorForUnderlyingClass.safeSubstitute(it.type, Variance.INVARIANT) }
val receiverParameterType = constructor.dispatchReceiverParameter?.let {
substitutorForUnderlyingClass.safeSubstitute(it.type, Variance.INVARIANT)
}
typeAliasConstructor.initialize(
receiverParameterType,
@@ -40,10 +40,12 @@ public class DescriptorSubstitutor {
@NotNull DeclarationDescriptor newContainingDeclaration,
@NotNull @Mutable List<TypeParameterDescriptor> result
) {
return substituteTypeParameters(typeParameters, originalSubstitution, newContainingDeclaration, result, null);
TypeSubstitutor substitutor = substituteTypeParameters(typeParameters, originalSubstitution, newContainingDeclaration, result, null);
if (substitutor == null) throw new AssertionError("Substitution failed");
return substitutor;
}
@NotNull
@Nullable
public static TypeSubstitutor substituteTypeParameters(
@ReadOnly @NotNull List<TypeParameterDescriptor> typeParameters,
@NotNull TypeSubstitution originalSubstitution,
@@ -80,7 +82,7 @@ public class DescriptorSubstitutor {
TypeParameterDescriptorImpl substituted = substitutedMap.get(descriptor);
for (KotlinType upperBound : descriptor.getUpperBounds()) {
KotlinType substitutedBound = substitutor.substitute(upperBound, Variance.IN_VARIANCE);
assert substitutedBound != null : "Upper bound failed to substitute: " + descriptor;
if (substitutedBound == null) return null;
if (substitutedBound != upperBound && wereChanges != null) {
wereChanges[0] = true;