diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt index 14dba211d4e..5e07434ce68 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt @@ -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 } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt index 58125615b85..e15748af0be 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt @@ -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() } } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExplicitTypeArgument.kt b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExplicitTypeArgument.kt new file mode 100644 index 00000000000..d676c39e012 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExplicitTypeArgument.kt @@ -0,0 +1,10 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_VARIABLE + +fun foo() { + val f = myRun { + 123 + } +} + +fun myRun(block: () -> R): R = block() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExplicitTypeArgument.txt b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExplicitTypeArgument.txt new file mode 100644 index 00000000000..a6943d120f7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExplicitTypeArgument.txt @@ -0,0 +1,4 @@ +package + +public fun foo(): kotlin.Unit +public fun myRun(/*0*/ block: () -> R): R diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index cdc21f7996f..599bc702ee9 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index daa2fbab270..577b1c63413 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -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");