From c6a09dba4ed9b76ca4bc6d43c10f744281baa516 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 10 Jul 2019 18:50:54 +0300 Subject: [PATCH] FIR: consider Java default constructors as primary ones --- .../kotlin/fir/java/JavaSymbolProvider.kt | 9 ++++--- .../java/declarations/FirJavaConstructor.kt | 1 + .../java/scopes/JavaClassEnhancementScope.kt | 26 ++++++++++++++----- .../jvmInstanceFieldReference.fir.txt | 2 +- .../ir/irText/expressions/kt16904.fir.txt | 2 +- .../sam/samConversionsWithSmartCasts.fir.txt | 2 +- .../setFieldWithImplicitCast.fir.txt | 2 +- .../ir/irText/stubs/javaInnerClass.fir.txt | 2 +- .../stubs/javaSyntheticProperty.fir.txt | 2 +- 9 files changed, 33 insertions(+), 15 deletions(-) diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaSymbolProvider.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaSymbolProvider.kt index 6e979b0ce25..c2ba92c6e05 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaSymbolProvider.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaSymbolProvider.kt @@ -210,11 +210,14 @@ class JavaSymbolProvider( val javaClassDeclaredConstructors = javaClass.constructors val constructorId = CallableId(classId.packageFqName, classId.relativeClassName, classId.shortClassName) - fun addJavaConstructor(visibility: Visibility = this.visibility): FirJavaConstructor { + fun addJavaConstructor( + visibility: Visibility = this.visibility, + isPrimary: Boolean = false + ): FirJavaConstructor { val constructorSymbol = FirConstructorSymbol(constructorId) val classTypeParameters = javaClass.typeParameters.convertTypeParameters(javaTypeParameterStack) val firJavaConstructor = FirJavaConstructor( - this@JavaSymbolProvider.session, constructorSymbol, visibility, + this@JavaSymbolProvider.session, constructorSymbol, visibility, isPrimary, FirResolvedTypeRefImpl( this@JavaSymbolProvider.session, null, firSymbol.constructType( @@ -230,7 +233,7 @@ class JavaSymbolProvider( } if (javaClassDeclaredConstructors.isEmpty() && javaClass.classKind == ClassKind.CLASS) { - addJavaConstructor() + addJavaConstructor(isPrimary = true) } for (javaConstructor in javaClassDeclaredConstructors) { addJavaConstructor(javaConstructor.visibility).apply { diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaConstructor.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaConstructor.kt index 6e5adb08a04..b4b2f48120c 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaConstructor.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaConstructor.kt @@ -21,6 +21,7 @@ class FirJavaConstructor( session: FirSession, override val symbol: FirConstructorSymbol, visibility: Visibility, + override val isPrimary: Boolean, delegatedSelfTypeRef: FirTypeRef ) : FirAbstractCallableMember( session, diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt index 555de1c409a..19ea090e7fe 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt @@ -180,12 +180,26 @@ class JavaClassEnhancementScope( } } val function: FirMemberFunction<*> = when (firMethod) { - is FirJavaConstructor -> FirConstructorImpl( - this@JavaClassEnhancementScope.session, null, FirConstructorSymbol(methodId), - newReceiverTypeRef, newReturnTypeRef - ).apply { - this.valueParameters += newValueParameters - this.typeParameters += firMethod.typeParameters + is FirJavaConstructor -> { + val symbol = FirConstructorSymbol(methodId) + if (firMethod.isPrimary) { + FirPrimaryConstructorImpl( + this@JavaClassEnhancementScope.session, null, symbol, + firMethod.visibility, + isExpect = false, + isActual = false, + delegatedSelfTypeRef = newReturnTypeRef, + delegatedConstructor = null + ) + } else { + FirConstructorImpl( + this@JavaClassEnhancementScope.session, null, symbol, + newReceiverTypeRef, newReturnTypeRef + ) + }.apply { + this.valueParameters += newValueParameters + this.typeParameters += firMethod.typeParameters + } } else -> FirMemberFunctionImpl( this@JavaClassEnhancementScope.session, null, diff --git a/compiler/testData/ir/irText/expressions/jvmInstanceFieldReference.fir.txt b/compiler/testData/ir/irText/expressions/jvmInstanceFieldReference.fir.txt index 2a49176f42a..36a4007ac81 100644 --- a/compiler/testData/ir/irText/expressions/jvmInstanceFieldReference.fir.txt +++ b/compiler/testData/ir/irText/expressions/jvmInstanceFieldReference.fir.txt @@ -3,7 +3,7 @@ FILE fqName: fileName:/Derived.kt $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Derived CONSTRUCTOR visibility:public <> () returnType:.Derived [primary] BLOCK_BODY - DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in .Base' + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in .Base' INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.Base]' ANONYMOUS_INITIALIZER isStatic=false BLOCK_BODY diff --git a/compiler/testData/ir/irText/expressions/kt16904.fir.txt b/compiler/testData/ir/irText/expressions/kt16904.fir.txt index 69c3b78d693..7c9ce1f79a2 100644 --- a/compiler/testData/ir/irText/expressions/kt16904.fir.txt +++ b/compiler/testData/ir/irText/expressions/kt16904.fir.txt @@ -98,7 +98,7 @@ FILE fqName: fileName:/kt16904.kt $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Test2 CONSTRUCTOR visibility:public <> () returnType:.Test2 [primary] BLOCK_BODY - DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in .J' + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in .J' INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test2 modality:FINAL visibility:public superTypes:[.J]' ANONYMOUS_INITIALIZER isStatic=false BLOCK_BODY diff --git a/compiler/testData/ir/irText/expressions/sam/samConversionsWithSmartCasts.fir.txt b/compiler/testData/ir/irText/expressions/sam/samConversionsWithSmartCasts.fir.txt index 49fca780a5b..5c666568d00 100644 --- a/compiler/testData/ir/irText/expressions/sam/samConversionsWithSmartCasts.fir.txt +++ b/compiler/testData/ir/irText/expressions/sam/samConversionsWithSmartCasts.fir.txt @@ -82,5 +82,5 @@ FILE fqName: fileName:/samConversionsWithSmartCasts.kt FUN name:test9 visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY CALL 'public open fun run1 (r: java.lang.Runnable?): kotlin.Unit declared in .J' type=kotlin.Unit origin=null - $this: CONSTRUCTOR_CALL 'public constructor () declared in .J' type=.J origin=null + $this: CONSTRUCTOR_CALL 'public constructor () [primary] declared in .J' type=.J origin=null r: ERROR_CALL 'Unresolved reference: #' type=IrErrorType diff --git a/compiler/testData/ir/irText/expressions/setFieldWithImplicitCast.fir.txt b/compiler/testData/ir/irText/expressions/setFieldWithImplicitCast.fir.txt index f8b70b28047..a07a0d24202 100644 --- a/compiler/testData/ir/irText/expressions/setFieldWithImplicitCast.fir.txt +++ b/compiler/testData/ir/irText/expressions/setFieldWithImplicitCast.fir.txt @@ -3,7 +3,7 @@ FILE fqName: fileName:/Derived.kt $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Derived CONSTRUCTOR visibility:public <> () returnType:.Derived [primary] BLOCK_BODY - DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in .Base' + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in .Base' INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.Base]' FUN name:setValue visibility:public modality:FINAL <> ($this:.Derived, v:kotlin.Any) returnType:kotlin.Unit $this: VALUE_PARAMETER name: type:.Derived diff --git a/compiler/testData/ir/irText/stubs/javaInnerClass.fir.txt b/compiler/testData/ir/irText/stubs/javaInnerClass.fir.txt index 38d8bbfe460..e0737205759 100644 --- a/compiler/testData/ir/irText/stubs/javaInnerClass.fir.txt +++ b/compiler/testData/ir/irText/stubs/javaInnerClass.fir.txt @@ -3,7 +3,7 @@ FILE fqName: fileName:/javaInnerClass.kt $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Test1 CONSTRUCTOR visibility:public <> () returnType:.Test1 [primary] BLOCK_BODY - DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in .J' + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in .J' INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test1 modality:FINAL visibility:public superTypes:[.J]' PROPERTY name:test visibility:public modality:FINAL [val] FIELD PROPERTY_BACKING_FIELD name:test type:IrErrorType visibility:public [final] diff --git a/compiler/testData/ir/irText/stubs/javaSyntheticProperty.fir.txt b/compiler/testData/ir/irText/stubs/javaSyntheticProperty.fir.txt index 32c49e908ea..a72e0aaed5e 100644 --- a/compiler/testData/ir/irText/stubs/javaSyntheticProperty.fir.txt +++ b/compiler/testData/ir/irText/stubs/javaSyntheticProperty.fir.txt @@ -3,7 +3,7 @@ FILE fqName: fileName:/javaSyntheticProperty.kt FIELD PROPERTY_BACKING_FIELD name:test type:kotlin.String? visibility:public [final,static] EXPRESSION_BODY CALL 'public open fun getFoo (): kotlin.String? declared in .J' type=kotlin.String? origin=null - $this: CONSTRUCTOR_CALL 'public/*package*/ constructor () declared in .J' type=.J origin=null + $this: CONSTRUCTOR_CALL 'public/*package*/ constructor () [primary] declared in .J' type=.J origin=null FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:kotlin.String? correspondingProperty: PROPERTY name:test visibility:public modality:FINAL [val] BLOCK_BODY