From 4c85b69c88978d9a30192f0ce09ce63d9f3c3d32 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Sat, 17 Oct 2015 16:37:01 +0300 Subject: [PATCH] Report type inference error when inference failed with 'NoInfer' annotation --- .../annotationsForResolve/noInferAnnotation.kt | 12 +++++++----- .../annotationsForResolve/noInferAnnotation.txt | 2 +- .../kotlin/resolve/annotationsForResolve.kt | 6 +++--- .../resolve/calls/inference/ConstraintSystemImpl.kt | 3 ++- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/noInferAnnotation.kt b/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/noInferAnnotation.kt index 1c63d70c5b0..a600e821cfc 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/noInferAnnotation.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/noInferAnnotation.kt @@ -10,21 +10,23 @@ fun @kotlin.internal.NoInfer T.test2(t1: T): T = t1 fun test3(t1: @kotlin.internal.NoInfer T): T = t1 fun usage() { - test1(1, "312") - 1.test2("") + test1(1, "312") + 1.test2("") test3("") } @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") fun List.contains1(e: @kotlin.internal.NoInfer T): Boolean = true -fun test(l: List) { - l.contains1("") +fun test(i: Int?, a: Any, l: List) { + l.contains1(a) + l.contains1("") + l.contains1(i) } @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") fun assertEquals1(e1: T, e2: @kotlin.internal.NoInfer T): Boolean = true fun test(s: String) { - assertEquals1(s, 11) + assertEquals1(s, 11) } \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/noInferAnnotation.txt b/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/noInferAnnotation.txt index 6d3aef8c670..bb9cc6bd390 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/noInferAnnotation.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/inference/annotationsForResolve/noInferAnnotation.txt @@ -1,7 +1,7 @@ package @kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) public fun assertEquals1(/*0*/ e1: T, /*1*/ e2: T): kotlin.Boolean -public fun test(/*0*/ l: kotlin.List): kotlin.Unit +public fun test(/*0*/ i: kotlin.Int?, /*1*/ a: kotlin.Any, /*2*/ l: kotlin.List): kotlin.Unit public fun test(/*0*/ s: kotlin.String): kotlin.Unit @kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) public fun test1(/*0*/ t1: T, /*1*/ t2: T): T @kotlin.Suppress(names = {"INVISIBLE_MEMBER", "INVISIBLE_REFERENCE"}) public fun test3(/*0*/ t1: T): T diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/annotationsForResolve.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/annotationsForResolve.kt index 592ae4d0de3..6cb14f27dc8 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/annotationsForResolve.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/annotationsForResolve.kt @@ -18,16 +18,16 @@ package org.jetbrains.kotlin.resolve.descriptorUtil import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor -import org.jetbrains.kotlin.descriptors.annotations.Annotated import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.types.JetType private val NO_INFER_ANNOTATION_FQ_NAME = FqName("kotlin.internal.NoInfer") -public fun Annotated.hasNoInferAnnotation(): Boolean = annotations.hasAnnotation(NO_INFER_ANNOTATION_FQ_NAME) +public fun JetType.hasNoInferAnnotation(): Boolean = annotations.hasAnnotation(NO_INFER_ANNOTATION_FQ_NAME) private val EXACT_ANNOTATION_FQ_NAME = FqName("kotlin.internal.Exact") -public fun Annotated.hasExactAnnotation(): Boolean = annotations.hasAnnotation(EXACT_ANNOTATION_FQ_NAME) +public fun JetType.hasExactAnnotation(): Boolean = annotations.hasAnnotation(EXACT_ANNOTATION_FQ_NAME) private val LOW_PRIORITY_IN_OVERLOAD_RESOLUTION_FQ_NAME = FqName("kotlin.internal.LowPriorityInOverloadResolution") diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt index 21061d4bc89..5a5de396eea 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt @@ -293,7 +293,6 @@ public class ConstraintSystemImpl : ConstraintSystem { if (isErrorOrSpecialType(subType, constraintPosition) || isErrorOrSpecialType(superType, constraintPosition)) return if (subType == null || superType == null) return - if (subType.hasNoInferAnnotation() || superType.hasNoInferAnnotation()) return if ((subType.hasExactAnnotation() || superType.hasExactAnnotation()) && (constraintKind != EQUAL)) { return doAddConstraint(EQUAL, subType, superType, constraintContext, typeCheckingProcedure) } @@ -340,6 +339,8 @@ public class ConstraintSystemImpl : ConstraintSystem { if (constraintContext.initial) { storeInitialConstraint(constraintKind, subType, superType, constraintPosition) } + if (subType.hasNoInferAnnotation() || superType.hasNoInferAnnotation()) return + simplifyConstraint(newSubType, superType) }