From 73576c80e44132acab4ee3e6c79813adffc6dae1 Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Tue, 22 Dec 2020 12:31:17 -0800 Subject: [PATCH] FIR2IR: calculate IR parent for Java field ahead so as to cache type parameters from the parent if the field's return type is one of type parameters. #KT-44032 Fixed --- .../fir/backend/Fir2IrDeclarationStorage.kt | 8 +++-- .../kotlin/fir/Fir2IrTextTestGenerated.java | 5 +++ .../typeParameterFromJavaClass.fir.kt.txt | 6 ++++ .../typeParameterFromJavaClass.fir.txt | 15 +++++++++ .../firProblems/typeParameterFromJavaClass.kt | 33 +++++++++++++++++++ .../typeParameterFromJavaClass.kt.txt | 6 ++++ .../typeParameterFromJavaClass.txt | 16 +++++++++ .../kotlin/ir/IrTextTestCaseGenerated.java | 5 +++ 8 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.fir.kt.txt create mode 100644 compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.fir.txt create mode 100644 compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt create mode 100644 compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt.txt create mode 100644 compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.txt diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index a7477b4911c..787ae525cfe 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -1168,8 +1168,12 @@ class Fir2IrDeclarationStorage( fun getIrFieldSymbol(firFieldSymbol: FirFieldSymbol): IrSymbol { val fir = firFieldSymbol.fir - val irProperty = fieldCache[fir] ?: createIrField(fir).apply { - setAndModifyParent(findIrParent(fir)) + val irProperty = fieldCache[fir] ?: run { + // In case of type parameters from the parent as the field's return type, find the parent ahead to cache type parameters. + val irParent = findIrParent(fir) + createIrField(fir).apply { + setAndModifyParent(irParent) + } } return irProperty.symbol } diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java index a64bda8ea61..c462f751f70 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java @@ -1873,6 +1873,11 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest { runTest("compiler/testData/ir/irText/firProblems/throwableStackTrace.kt"); } + @TestMetadata("typeParameterFromJavaClass.kt") + public void testTypeParameterFromJavaClass() throws Exception { + runTest("compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt"); + } + @TestMetadata("typeVariableAfterBuildMap.kt") public void testTypeVariableAfterBuildMap() throws Exception { runTest("compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.kt"); diff --git a/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.fir.kt.txt b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.fir.kt.txt new file mode 100644 index 00000000000..2e0d59300f3 --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.fir.kt.txt @@ -0,0 +1,6 @@ +fun foo(movedPaths: MutableList>) { + movedPaths.forEach>(action = local fun (it: Couple) { + it.#second.getName() /*~> Unit */ + } +) +} diff --git a/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.fir.txt b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.fir.txt new file mode 100644 index 00000000000..1d0d71e73db --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.fir.txt @@ -0,0 +1,15 @@ +FILE fqName: fileName:/typeParameterFromJavaClass.kt + FUN name:foo visibility:public modality:FINAL <> (movedPaths:kotlin.collections.MutableList<.Couple<.FilePath>>) returnType:kotlin.Unit + VALUE_PARAMETER name:movedPaths index:0 type:kotlin.collections.MutableList<.Couple<.FilePath>> + BLOCK_BODY + CALL 'public final fun forEach (action: kotlin.Function1): kotlin.Unit [inline] declared in kotlin.collections' type=kotlin.Unit origin=null + : .Couple<.FilePath> + $receiver: GET_VAR 'movedPaths: kotlin.collections.MutableList<.Couple<.FilePath>> declared in .foo' type=kotlin.collections.MutableList<.Couple<.FilePath>> origin=null + action: FUN_EXPR type=kotlin.Function1<.Couple<.FilePath>, kotlin.Unit> origin=LAMBDA + FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> (it:.Couple<.FilePath>) returnType:kotlin.Unit + VALUE_PARAMETER name:it index:0 type:.Couple<.FilePath> + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public abstract fun getName (): kotlin.String? declared in .FilePath' type=kotlin.String? origin=GET_PROPERTY + $this: GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:second type:B of .Pair? visibility:public [final]' type=.FilePath? origin=GET_PROPERTY + receiver: GET_VAR 'it: .Couple<.FilePath> declared in .foo.' type=.Couple<.FilePath> origin=null diff --git a/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt new file mode 100644 index 00000000000..30faaaed089 --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt @@ -0,0 +1,33 @@ +// FILE: Pair.java + +public class Pair { + public final A first; + public final B second; + + public Pair(A first, B second) { + this.first = first; + this.second = second; + } +} + +// FILE: Couple.java + +public class Couple extends Pair { + public Couple(T first, T second) { + super(first, second); + } +} + +// FILE: FilePath.java + +public interface FilePath { + String getName(); +} + +// FILE: typeParameterFromJavaClass.kt + +// WITH_RUNTIME + +fun foo(movedPaths: MutableList>) { + movedPaths.forEach { it.second.name } +} diff --git a/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt.txt b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt.txt new file mode 100644 index 00000000000..b9d5db64a2c --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt.txt @@ -0,0 +1,6 @@ +fun foo(movedPaths: MutableList>) { + movedPaths.forEach>(action = local fun (it: Couple) { + itsuper.#second /*!! FilePath */.getName() /*~> Unit */ + } +) +} diff --git a/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.txt b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.txt new file mode 100644 index 00000000000..ca709a07f33 --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.txt @@ -0,0 +1,16 @@ +FILE fqName: fileName:/typeParameterFromJavaClass.kt + FUN name:foo visibility:public modality:FINAL <> (movedPaths:kotlin.collections.MutableList<.Couple<.FilePath>>) returnType:kotlin.Unit + VALUE_PARAMETER name:movedPaths index:0 type:kotlin.collections.MutableList<.Couple<.FilePath>> + BLOCK_BODY + CALL 'public final fun forEach (action: kotlin.Function1): kotlin.Unit [inline] declared in kotlin.collections' type=kotlin.Unit origin=null + : .Couple<.FilePath> + $receiver: GET_VAR 'movedPaths: kotlin.collections.MutableList<.Couple<.FilePath>> declared in .foo' type=kotlin.collections.MutableList<.Couple<.FilePath>> origin=null + action: FUN_EXPR type=kotlin.Function1<.Couple<.FilePath>, kotlin.Unit> origin=LAMBDA + FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> (it:.Couple<.FilePath>) returnType:kotlin.Unit + VALUE_PARAMETER name:it index:0 type:.Couple<.FilePath> + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public abstract fun getName (): @[FlexibleNullability] kotlin.String? declared in .FilePath' type=@[FlexibleNullability] kotlin.String? origin=GET_PROPERTY + $this: TYPE_OP type=.FilePath origin=IMPLICIT_NOTNULL typeOperand=.FilePath + GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:second type:@[FlexibleNullability] B of .Pair? visibility:public [final]' type=@[FlexibleNullability] .FilePath? origin=GET_PROPERTY + receiver: GET_VAR 'it: .Couple<.FilePath> declared in .foo.' type=.Couple<.FilePath> origin=null diff --git a/compiler/tests-gen/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java index 3e529ca8aaa..c2ce58a2055 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java @@ -1872,6 +1872,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { runTest("compiler/testData/ir/irText/firProblems/throwableStackTrace.kt"); } + @TestMetadata("typeParameterFromJavaClass.kt") + public void testTypeParameterFromJavaClass() throws Exception { + runTest("compiler/testData/ir/irText/firProblems/typeParameterFromJavaClass.kt"); + } + @TestMetadata("typeVariableAfterBuildMap.kt") public void testTypeVariableAfterBuildMap() throws Exception { runTest("compiler/testData/ir/irText/firProblems/typeVariableAfterBuildMap.kt");