From 003aea2e68ad3285cd7cf538259ddbda456800f2 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Mon, 10 Feb 2020 16:43:42 +0300 Subject: [PATCH] FIR: Consider abbreviation's nullability when expanding types --- .../kotlin/fir/resolve/ResolveUtils.kt | 8 +++++++- .../resolve/stdlib/concurrentMapOfAliases.kt | 12 ++++++++++++ .../resolve/stdlib/concurrentMapOfAliases.txt | 18 ++++++++++++++++++ .../FirDiagnosticsWithStdlibTestGenerated.java | 5 +++++ .../tests/inference/regressions/kt702.fir.kt | 6 +++--- .../diagnostics/tests/regressions/kt701.fir.kt | 6 +++--- .../tests/typealias/inGenerics.fir.kt | 2 +- 7 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 compiler/fir/resolve/testData/resolve/stdlib/concurrentMapOfAliases.kt create mode 100644 compiler/fir/resolve/testData/resolve/stdlib/concurrentMapOfAliases.txt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolveUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolveUtils.kt index e80c63d96f7..19564c51448 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolveUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolveUtils.kt @@ -98,11 +98,17 @@ fun ConeClassLikeType.directExpansionType( .toSymbol(useSiteSession) ?.safeAs()?.fir ?: return null - val resultType = expandedConeType(typeAlias) ?: return null + val resultType = expandedConeType(typeAlias)?.applyNullabilityFrom(this) ?: return null + if (resultType.typeArguments.isEmpty()) return resultType return mapTypeAliasArguments(typeAlias, this, resultType) as? ConeClassLikeType } +private fun ConeClassLikeType.applyNullabilityFrom(abbreviation: ConeClassLikeType): ConeClassLikeType { + if (abbreviation.isMarkedNullable) return withNullability(ConeNullability.NULLABLE) + return this +} + private fun mapTypeAliasArguments( typeAlias: FirTypeAlias, abbreviatedType: ConeClassLikeType, diff --git a/compiler/fir/resolve/testData/resolve/stdlib/concurrentMapOfAliases.kt b/compiler/fir/resolve/testData/resolve/stdlib/concurrentMapOfAliases.kt new file mode 100644 index 00000000000..6c9ee359a92 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/stdlib/concurrentMapOfAliases.kt @@ -0,0 +1,12 @@ +// FULL_JDK +private typealias MyAlias = CharSequence + +class A { + private val foo = java.util.concurrent.ConcurrentHashMap() + + private fun bar() { + foo["dd"]?.baz() + } + + private fun MyAlias.baz() {} +} diff --git a/compiler/fir/resolve/testData/resolve/stdlib/concurrentMapOfAliases.txt b/compiler/fir/resolve/testData/resolve/stdlib/concurrentMapOfAliases.txt new file mode 100644 index 00000000000..9463d1b9e58 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/stdlib/concurrentMapOfAliases.txt @@ -0,0 +1,18 @@ +FILE: concurrentMapOfAliases.kt + private final typealias MyAlias = R|kotlin/CharSequence| + public final class A : R|kotlin/Any| { + public constructor(): R|A| { + super() + } + + private final val foo: R|java/util/concurrent/ConcurrentHashMap| = Q|java/util/concurrent|.R|java/util/concurrent/ConcurrentHashMap.ConcurrentHashMap|() + private get(): R|java/util/concurrent/ConcurrentHashMap| + + private final fun bar(): R|kotlin/Unit| { + (this@R|/A|, this@R|/A|.R|/A.foo|.R|FakeOverride|(String(dd)))?.R|/A.baz|() + } + + private final fun R|MyAlias|.baz(): R|kotlin/Unit| { + } + + } diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java index 22902f391d4..291f0b055c8 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java @@ -78,6 +78,11 @@ public class FirDiagnosticsWithStdlibTestGenerated extends AbstractFirDiagnostic runTest("compiler/fir/resolve/testData/resolve/stdlib/concurrent.kt"); } + @TestMetadata("concurrentMapOfAliases.kt") + public void testConcurrentMapOfAliases() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/stdlib/concurrentMapOfAliases.kt"); + } + @TestMetadata("emptyArray.kt") public void testEmptyArray() throws Exception { runTest("compiler/fir/resolve/testData/resolve/stdlib/emptyArray.kt"); diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt702.fir.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt702.fir.kt index 8cd37411b95..d5b256555be 100644 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt702.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt702.fir.kt @@ -13,8 +13,8 @@ public class Throwables() { } } public fun propagateIfPossible(throwable : Throwable?) : Unit { - propagateIfInstanceOf(throwable, getJavaClass()) // Type inference failed: Mismatch while expanding constraints - propagateIfInstanceOf(throwable, getJavaClass()) // Type inference failed: Mismatch while expanding constraints + propagateIfInstanceOf(throwable, getJavaClass()) // Type inference failed: Mismatch while expanding constraints + propagateIfInstanceOf(throwable, getJavaClass()) // Type inference failed: Mismatch while expanding constraints } } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/regressions/kt701.fir.kt b/compiler/testData/diagnostics/tests/regressions/kt701.fir.kt index 509e0f33460..56f432a40d0 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt701.fir.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt701.fir.kt @@ -10,8 +10,8 @@ public class Throwables() { } } public fun propagateIfPossible(throwable : Throwable?) { - propagateIfInstanceOf(throwable, getJavaClass()) //; Type inference failed: Mismatch while expanding constraints - propagateIfInstanceOf(throwable, getJavaClass()) // Type inference failed: Mismatch while expanding constraints + propagateIfInstanceOf(throwable, getJavaClass()) //; Type inference failed: Mismatch while expanding constraints + propagateIfInstanceOf(throwable, getJavaClass()) // Type inference failed: Mismatch while expanding constraints } } -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/typealias/inGenerics.fir.kt b/compiler/testData/diagnostics/tests/typealias/inGenerics.fir.kt index 567455e61df..6ba006782bd 100644 --- a/compiler/testData/diagnostics/tests/typealias/inGenerics.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/inGenerics.fir.kt @@ -9,4 +9,4 @@ val ms: MyString = "MyString" val msn: MyString? = null val msc: MyStringContainer = Container(ms) -val msc1 = MyStringContainer(null) \ No newline at end of file +val msc1 = MyStringContainer(null)