diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintIncorporator.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintIncorporator.kt index acc88f32af5..ccac288165e 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintIncorporator.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintIncorporator.kt @@ -1,27 +1,18 @@ /* - * Copyright 2010-2016 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 -import org.jetbrains.kotlin.resolve.calls.inference.model.* +import org.jetbrains.kotlin.resolve.calls.inference.model.Constraint +import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind +import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable +import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.CaptureStatus import org.jetbrains.kotlin.types.checker.NewCapturedType import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor -import org.jetbrains.kotlin.types.typeUtil.asTypeProjection import org.jetbrains.kotlin.types.typeUtil.contains import org.jetbrains.kotlin.utils.SmartSet import org.jetbrains.kotlin.utils.addIfNotNull @@ -147,14 +138,11 @@ class ConstraintIncorporator(val typeApproximator: TypeApproximator) { } private fun UnwrappedType.substitute(typeVariable: NewTypeVariable, value: UnwrappedType): UnwrappedType { - val substitutor = TypeSubstitutor.create(mapOf(typeVariable.freshTypeConstructor to value.asTypeProjection())) - val type = substitutor.substitute(this, Variance.INVARIANT) ?: error("Impossible to substitute in $this: $typeVariable -> $value") - return type.unwrap() + val substitutor = NewTypeSubstitutorByConstructorMap(mapOf(typeVariable.freshTypeConstructor to value)) + return substitutor.safeSubstitute(this) } private fun approximateCapturedTypes(type: UnwrappedType, toSuper: Boolean): UnwrappedType = if (toSuper) typeApproximator.approximateToSuperType(type, TypeApproximatorConfiguration.IncorporationConfiguration) ?: type else typeApproximator.approximateToSubType(type, TypeApproximatorConfiguration.IncorporationConfiguration) ?: type - - -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.kt b/compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.kt new file mode 100644 index 00000000000..c2f2f51f2ac --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.kt @@ -0,0 +1,17 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +// FILE: JClass.java + +public class JClass { + public void foo(Key key, K value) {} +} + +// FILE: test.kt + +class Key + +fun select(x: S, y: S): S = x + +fun setValue(key: Key, value: T, j: JClass) { + j.foo(key, select(value, null)) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.txt b/compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.txt new file mode 100644 index 00000000000..3bc3e3be36f --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.txt @@ -0,0 +1,19 @@ +package + +public fun select(/*0*/ x: S, /*1*/ y: S): S +public fun setValue(/*0*/ key: Key, /*1*/ value: T, /*2*/ j: JClass): 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 fun foo(/*0*/ key: Key!, /*1*/ value: K!): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class Key { + public constructor Key() + 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 +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index befb198e9e3..b5b33109a24 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -9301,6 +9301,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("correctSubstitutionForIncorporationConstraint.kt") + public void testCorrectSubstitutionForIncorporationConstraint() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.kt"); + doTest(fileName); + } + @TestMetadata("declarationsBoundsViolation.kt") public void testDeclarationsBoundsViolation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/declarationsBoundsViolation.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index e0fcc23e54a..e1afb270cb9 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -9301,6 +9301,12 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing doTest(fileName); } + @TestMetadata("correctSubstitutionForIncorporationConstraint.kt") + public void testCorrectSubstitutionForIncorporationConstraint() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/correctSubstitutionForIncorporationConstraint.kt"); + doTest(fileName); + } + @TestMetadata("declarationsBoundsViolation.kt") public void testDeclarationsBoundsViolation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/declarationsBoundsViolation.kt");