diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index dda07b90f43..4ee50ec847e 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -5555,6 +5555,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/DeepCopyIrTree.kt"); } + @Test + @TestMetadata("definitelyNotNullCast.kt") + public void testDefinitelyNotNullCast() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.kt"); + } + @Test @TestMetadata("EnumMapGet.kt") public void testEnumMapGet() throws Exception { diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.fir.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.fir.txt new file mode 100644 index 00000000000..bb861cb196e --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.fir.txt @@ -0,0 +1,14 @@ +FILE: test.kt + public open class Value : R|kotlin/Any| { + public constructor(s: R|kotlin/String|): R|Value| { + super() + } + + public final val s: R|kotlin/String| = R|/s| + public get(): R|kotlin/String| + + } + public final val generator: R|Generator| = R|/Generator.Generator|() + public get(): R|Generator| + public final val y: R|Value| = (R|/generator|.R|/Generator.createValue||>(String(Omega)) as R|Value|) + public get(): R|Value| diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.kt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.kt new file mode 100644 index 00000000000..b7c7ceabf59 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.kt @@ -0,0 +1,17 @@ +// FILE: Generator.java + +import org.jetbrains.annotations.NotNull; + +public class Generator { + @NotNull + public T createValue(@NotNull String content) { + return (T) (new Value(content)); + } +} + +// FILE: test.kt +open class Value(val s: String) + +val generator = Generator() + +val y = generator.createValue("Omega") as Value diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index 13e40e6c944..5c7103a82cf 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -5555,6 +5555,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/DeepCopyIrTree.kt"); } + @Test + @TestMetadata("definitelyNotNullCast.kt") + public void testDefinitelyNotNullCast() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.kt"); + } + @Test @TestMetadata("EnumMapGet.kt") public void testEnumMapGet() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 8f3be700607..57586303376 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -5555,6 +5555,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/DeepCopyIrTree.kt"); } + @Test + @TestMetadata("definitelyNotNullCast.kt") + public void testDefinitelyNotNullCast() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.kt"); + } + @Test @TestMetadata("EnumMapGet.kt") public void testEnumMapGet() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt index 7642484cecf..7420159972b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt @@ -345,14 +345,18 @@ internal fun FirFunction.isFunctionForExpectTypeFromCastFeature(): Boolean { val returnType = returnTypeRef.coneTypeSafe() ?: return false - if ((returnType.lowerBoundIfFlexible() as? ConeTypeParameterType)?.lookupTag != typeParameter.symbol.toLookupTag()) return false + if ((returnType.unwrap() as? ConeTypeParameterType)?.lookupTag != typeParameter.symbol.toLookupTag()) return false fun FirTypeRef.isBadType() = coneTypeSafe() - ?.contains { (it.lowerBoundIfFlexible() as? ConeTypeParameterType)?.lookupTag == typeParameter.symbol.toLookupTag() } != false + ?.contains { (it.unwrap() as? ConeTypeParameterType)?.lookupTag == typeParameter.symbol.toLookupTag() } != false if (valueParameters.any { it.returnTypeRef.isBadType() } || receiverTypeRef?.isBadType() == true) return false return true } +private fun ConeKotlinType.unwrap(): ConeSimpleKotlinType = lowerBoundIfFlexible().let { + if (it is ConeDefinitelyNotNullType) it.original.unwrap() else it +} +