[FIR Plugin prototype] Fix AllPropertiesConstructor test

^KT-56861 Fixed
This commit is contained in:
Kirill Rakhman
2023-02-24 16:48:42 +01:00
committed by Space Team
parent 2139914061
commit 59c61cdd07
5 changed files with 23 additions and 62 deletions
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.plugin.generators
import org.jetbrains.kotlin.GeneratedDeclarationKey
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.containingClassLookupTag
import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.extensions.FirDeclarationPredicateRegistrar
@@ -30,7 +31,9 @@ class AllPropertiesConstructorMetadataProvider(session: FirSession) : FirDeclara
override fun provideDeclarationsForClass(klass: FirClass, scopeSession: ScopeSession): List<FirDeclaration> {
if (!session.predicateBasedProvider.matches(PREDICATE, klass)) return emptyList()
val scope = klass.unsubstitutedScope(session, scopeSession, withForcedTypeCalculator = false)
val properties = scope.getCallableNames().flatMap { scope.getProperties(it) }
val properties = scope.getCallableNames()
.flatMap { scope.getProperties(it) }
.sortedBy { it.containingClassLookupTag() == klass.symbol.toLookupTag() }
val constructor = createConstructor(klass.symbol, Key) {
for (property in properties) {
valueParameter(property.name, property.resolvedReturnType)
@@ -55,7 +55,7 @@ class AllPropertiesConstructorIrGenerator(val context: IrPluginContext) : IrElem
superClass.constructors.singleOrNull { it.valueParameters.isNotEmpty() }
}
} ?: error("All properies constructor not found")
Unit
context.irFactory.buildConstructor {
startOffset = SYNTHETIC_OFFSET
endOffset = SYNTHETIC_OFFSET
@@ -235,43 +235,13 @@ FILE fqName:<root> fileName:/main.kt
CONSTRUCTOR_CALL 'public constructor <init> (s: kotlin.String) [primary] declared in <root>.C' type=<root>.C origin=null
s: CONST String type=kotlin.String value="c"
RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in <root>'
CALL 'public final fun with <T, R> (receiver: T of kotlin.StandardKt.with, block: @[ExtensionFunctionType] kotlin.Function1<T of kotlin.StandardKt.with, R of kotlin.StandardKt.with>): R of kotlin.StandardKt.with [inline] declared in kotlin.StandardKt' type=kotlin.String origin=null
<T>: @[FlexibleNullability] <root>.Derived?
<R>: kotlin.String
receiver: GET_VAR 'val derived: @[FlexibleNullability] <root>.Derived? [val] declared in <root>.box' type=@[FlexibleNullability] <root>.Derived? origin=null
block: FUN_EXPR type=@[ExtensionFunctionType] kotlin.Function1<@[FlexibleNullability] <root>.Derived?, kotlin.String> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> ($receiver:@[FlexibleNullability] <root>.Derived?) returnType:kotlin.String
$receiver: VALUE_PARAMETER name:$this$with type:@[FlexibleNullability] <root>.Derived?
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.String declared in <root>.box'
WHEN type=kotlin.String origin=IF
BRANCH
if: WHEN type=kotlin.Boolean origin=ANDAND
BRANCH
if: WHEN type=kotlin.Boolean origin=ANDAND
BRANCH
if: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ
arg0: CALL 'public final fun <get-s> (): kotlin.String declared in <root>.A' type=kotlin.String origin=GET_PROPERTY
$this: CALL 'public final fun <get-a> (): <root>.A [fake_override] declared in <root>.Derived' type=<root>.A origin=GET_PROPERTY
$this: GET_VAR '$this$with: @[FlexibleNullability] <root>.Derived? declared in <root>.box.<anonymous>' type=@[FlexibleNullability] <root>.Derived? origin=null
arg1: CONST String type=kotlin.String value="a"
then: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ
arg0: CALL 'public final fun <get-s> (): kotlin.String declared in <root>.B' type=kotlin.String origin=GET_PROPERTY
$this: CALL 'public final fun <get-b> (): <root>.B [fake_override] declared in <root>.Derived' type=<root>.B origin=GET_PROPERTY
$this: GET_VAR '$this$with: @[FlexibleNullability] <root>.Derived? declared in <root>.box.<anonymous>' type=@[FlexibleNullability] <root>.Derived? origin=null
arg1: CONST String type=kotlin.String value="b"
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: CONST Boolean type=kotlin.Boolean value=false
then: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ
arg0: CALL 'public final fun <get-s> (): kotlin.String declared in <root>.C' type=kotlin.String origin=GET_PROPERTY
$this: CALL 'public final fun <get-c> (): <root>.C declared in <root>.Derived' type=<root>.C origin=GET_PROPERTY
$this: GET_VAR '$this$with: @[FlexibleNullability] <root>.Derived? declared in <root>.box.<anonymous>' type=@[FlexibleNullability] <root>.Derived? origin=null
arg1: CONST String type=kotlin.String value="c"
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: CONST Boolean type=kotlin.Boolean value=false
then: CONST String type=kotlin.String value="OK"
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: CONST String type=kotlin.String value="Error"
WHEN type=kotlin.String origin=IF
BRANCH
if: CALL 'public final fun not (): kotlin.Boolean [operator] declared in kotlin.Boolean' type=kotlin.Boolean origin=EXCLEQ
$this: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EXCLEQ
arg0: GET_VAR 'val derived: @[FlexibleNullability] <root>.Derived? [val] declared in <root>.box' type=@[FlexibleNullability] <root>.Derived? origin=null
arg1: CONST Null type=kotlin.Nothing? value=null
then: CONST String type=kotlin.String value="OK"
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: CONST String type=kotlin.String value="Error"
@@ -57,16 +57,13 @@ FILE: main.kt
}
).R|kotlin/reflect/jvm/javaConstructor|<R|Derived|>!!
lval derived: R|Derived!| = R|<local>/constructor|.R|SubstitutionOverride<java/lang/reflect/Constructor.newInstance: R|Derived!|>|(vararg(R|/A.A|(String(a)), R|/B.B|(String(b)), R|/C.C|(String(c))))
^box R|kotlin/with|<R|Derived!|, R|kotlin/String|>(R|<local>/derived|, <L> = with@fun R|Derived!|.<anonymous>(): R|kotlin/String| <inline=Inline, kind=EXACTLY_ONCE> {
^ when () {
==(this@R|special/anonymous|.R|/Base.a|.R|/A.s|, String(a)) && ==(this@R|special/anonymous|.R|/Base.b|.R|/B.s|, String(b)) && ==(this@R|special/anonymous|.R|/Derived.c|.R|/C.s|, String(c)) -> {
String(OK)
}
else -> {
String(Error)
}
^box when () {
!=(R|<local>/derived|, Null(null)) -> {
String(OK)
}
else -> {
String(Error)
}
}
)
}
@@ -3,9 +3,6 @@
// WITH_REFLECT
// FULL_JDK
// ignored, see KT-56861, looks like the bytecode for the generated constructor is invalid and blackbox test fails on runtime
// IGNORE_BACKEND_K2: JVM_IR
// MODULE: a
import org.jetbrains.kotlin.fir.plugin.AllPropertiesConstructor
@@ -35,11 +32,5 @@ import kotlin.reflect.jvm.javaConstructor
fun box(): String {
val constructor = Derived::class.constructors.first { it.valueParameters.size == 3 }.javaConstructor!!
val derived = constructor.newInstance(A("a"), B("b"), C("c"))
return with (derived) {
if (a.s == "a" && b.s == "b" && c.s == "c") {
"OK"
} else {
"Error"
}
}
return if (derived != null) "OK" else "Error"
}