[NI] Fix coercion to Unit for explicitly specified type argument

#KT-25424 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2018-11-21 18:22:28 +03:00
parent c6712ff861
commit 7a9fb3ca26
6 changed files with 28 additions and 4 deletions
@@ -25,7 +25,7 @@ class PostponedArgumentsAnalyzer(
// type can be proper if it not contains not fixed type variables
fun canBeProper(type: UnwrappedType): Boolean
fun hasUpperUnitConstraint(type: UnwrappedType): Boolean
fun hasUpperOrEqualUnitConstraint(type: UnwrappedType): Boolean
// mutable operations
fun addOtherSystem(otherSystem: ConstraintStorage)
@@ -76,7 +76,7 @@ class PostponedArgumentsAnalyzer(
c.canBeProper(rawReturnType) -> substitute(rawReturnType)
// For Unit-coercion
c.hasUpperUnitConstraint(rawReturnType) -> lambda.returnType.builtIns.unitType
c.hasUpperOrEqualUnitConstraint(rawReturnType) -> lambda.returnType.builtIns.unitType
else -> null
}
@@ -289,11 +289,11 @@ class NewConstraintSystemImpl(
}
// PostponedArgumentsAnalyzer.Context
override fun hasUpperUnitConstraint(type: UnwrappedType): Boolean {
override fun hasUpperOrEqualUnitConstraint(type: UnwrappedType): Boolean {
checkState(State.BUILDING, State.COMPLETION, State.FREEZED)
val constraints = storage.notFixedTypeVariables[type.constructor]?.constraints ?: return false
return constraints.any { it.kind == ConstraintKind.UPPER && it.type.isUnit() }
return constraints.any { (it.kind == ConstraintKind.UPPER || it.kind == ConstraintKind.EQUALITY) && it.type.isUnit() }
}
}
@@ -0,0 +1,10 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_VARIABLE
fun foo() {
val f = myRun<Unit> {
123
}
}
fun <R> myRun(block: () -> R): R = block()
@@ -0,0 +1,4 @@
package
public fun foo(): kotlin.Unit
public fun </*0*/ R> myRun(/*0*/ block: () -> R): R
@@ -9685,6 +9685,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExpectedTypeAndBound.kt");
}
@TestMetadata("coercionWithExplicitTypeArgument.kt")
public void testCoercionWithExplicitTypeArgument() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExplicitTypeArgument.kt");
}
@TestMetadata("coercionWithoutExpectedType.kt")
public void testCoercionWithoutExpectedType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithoutExpectedType.kt");
@@ -9685,6 +9685,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExpectedTypeAndBound.kt");
}
@TestMetadata("coercionWithExplicitTypeArgument.kt")
public void testCoercionWithExplicitTypeArgument() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExplicitTypeArgument.kt");
}
@TestMetadata("coercionWithoutExpectedType.kt")
public void testCoercionWithoutExpectedType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithoutExpectedType.kt");