diff --git a/compiler/testData/diagnostics/testsWithJava8/nullForOptionalOrElse.kt b/compiler/testData/diagnostics/testsWithJava8/nullForOptionalOrElse.kt new file mode 100644 index 00000000000..e27ded0f05b --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJava8/nullForOptionalOrElse.kt @@ -0,0 +1,7 @@ +import java.util.* +import java.util.stream.Stream + +fun Stream?.getIfSingle() = + this?.map { Optional.ofNullable(it) } + ?.reduce(Optional.empty()) { _, _ -> Optional.empty() } + ?.orElse(null) // <<---- should not be an error diff --git a/compiler/testData/diagnostics/testsWithJava8/nullForOptionalOrElse.txt b/compiler/testData/diagnostics/testsWithJava8/nullForOptionalOrElse.txt new file mode 100644 index 00000000000..fecbc90fedf --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJava8/nullForOptionalOrElse.txt @@ -0,0 +1,3 @@ +package + +public fun java.util.stream.Stream?.getIfSingle(): T? diff --git a/compiler/tests-java8/tests/org/jetbrains/kotlin/checkers/DiagnosticsWithJava8TestGenerated.java b/compiler/tests-java8/tests/org/jetbrains/kotlin/checkers/DiagnosticsWithJava8TestGenerated.java index a73b51f2b73..a2e0435d6f7 100644 --- a/compiler/tests-java8/tests/org/jetbrains/kotlin/checkers/DiagnosticsWithJava8TestGenerated.java +++ b/compiler/tests-java8/tests/org/jetbrains/kotlin/checkers/DiagnosticsWithJava8TestGenerated.java @@ -48,6 +48,12 @@ public class DiagnosticsWithJava8TestGenerated extends AbstractDiagnosticsWithFu doTest(fileName); } + @TestMetadata("nullForOptionalOrElse.kt") + public void testNullForOptionalOrElse() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJava8/nullForOptionalOrElse.kt"); + doTest(fileName); + } + @TestMetadata("samWithConsumer.kt") public void testSamWithConsumer() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJava8/samWithConsumer.kt"); diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt index a63fa53e5a2..ae8d9af53d7 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt @@ -243,7 +243,8 @@ internal class NotNullTypeParameter(override val delegate: SimpleType) : CustomT } override fun replaceAnnotations(newAnnotations: Annotations) = NotNullTypeParameter(delegate.replaceAnnotations(newAnnotations)) - override fun makeNullableAsSpecified(newNullability: Boolean) = this + override fun makeNullableAsSpecified(newNullability: Boolean) = + if (newNullability) delegate.makeNullableAsSpecified(true) else this override val isError: Boolean get() = false