From 2a0bb68e1c87d615cf629f1a41631de243e79374 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Fri, 19 Jan 2018 14:53:49 +0300 Subject: [PATCH] [NI] Fix substitution of `NotNullTypeParameter` --- .../components/NewTypeSubstitutor.kt | 18 +++++------------ .../notnullTypesFromJavaWithSmartcast.kt | 20 +++++++++++++++++++ .../notnullTypesFromJavaWithSmartcast.txt | 13 ++++++++++++ .../methodTypeParameter.kt | 6 +++--- .../checkers/DiagnosticsTestGenerated.java | 6 ++++++ .../DiagnosticsUsingJavacTestGenerated.java | 6 ++++++ .../org/jetbrains/kotlin/types/TypeUtils.java | 18 ++++------------- 7 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.kt create mode 100644 compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.txt diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt index 8b451ca68a7..effb55bab55 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.resolve.calls.inference.components @@ -122,6 +111,9 @@ interface NewTypeSubstitutor { if (type.isDefinitelyNotNullType) { replacement = replacement.makeDefinitelyNotNullOrNotNull() } + if (type is CustomTypeVariable) { + replacement = type.substitutionResult(replacement).unwrap() + } return replacement } diff --git a/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.kt b/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.kt new file mode 100644 index 00000000000..122a6448558 --- /dev/null +++ b/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.kt @@ -0,0 +1,20 @@ +// FILE: JClass.java + +import org.jetbrains.annotations.NotNull; + +public class JClass { + @NotNull + public static T getNotNullT() { + return null; + } +} + +// FILE: test.kt +fun test() { + var value: T? = null + if (value == null) { + value = JClass.getNotNullT() + } + + value.hashCode() // unsafe call error +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.txt b/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.txt new file mode 100644 index 00000000000..cb4e22e5c49 --- /dev/null +++ b/compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.txt @@ -0,0 +1,13 @@ +package + +public fun test(): kotlin.Unit + +public open class JClass { + public constructor JClass() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + // Static members + @org.jetbrains.annotations.NotNull public open fun getNotNullT(): T +} diff --git a/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/methodTypeParameter.kt b/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/methodTypeParameter.kt index 2ee0161e1da..0c63b22b23e 100644 --- a/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/methodTypeParameter.kt +++ b/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/methodTypeParameter.kt @@ -11,9 +11,9 @@ public class A { // FILE: k.kt fun test() { - A.bar(null, "") + A.bar(null, "") A.bar(null, "") - A.bar(null, "") - A.bar(null, A.platformString()) + A.bar(null, "") + A.bar(null, A.platformString()) } diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 0d9c7fbbd9a..9af5962a335 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -14822,6 +14822,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("notnullTypesFromJavaWithSmartcast.kt") + public void testNotnullTypesFromJavaWithSmartcast() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.kt"); + doTest(fileName); + } + @TestMetadata("NullableNothingIsExactlyNull.kt") public void testNullableNothingIsExactlyNull() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/NullableNothingIsExactlyNull.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 3c4d9bdfc5f..fbcc9d5a850 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -14822,6 +14822,12 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing doTest(fileName); } + @TestMetadata("notnullTypesFromJavaWithSmartcast.kt") + public void testNotnullTypesFromJavaWithSmartcast() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/notnullTypesFromJavaWithSmartcast.kt"); + doTest(fileName); + } + @TestMetadata("NullableNothingIsExactlyNull.kt") public void testNullableNothingIsExactlyNull() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/nullabilityAndSmartCasts/NullableNothingIsExactlyNull.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java index 6d624f161d2..9c228a146e1 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.types; @@ -29,6 +18,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations; import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor; import org.jetbrains.kotlin.resolve.scopes.MemberScope; import org.jetbrains.kotlin.types.checker.KotlinTypeChecker; +import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor; import java.util.*; @@ -467,7 +457,7 @@ public class TypeUtils { } public static boolean isTypeParameter(@NotNull KotlinType type) { - return getTypeParameterDescriptorOrNull(type) != null; + return getTypeParameterDescriptorOrNull(type) != null || type.getConstructor() instanceof NewTypeVariableConstructor; } public static boolean isReifiedTypeParameter(@NotNull KotlinType type) {