Native: fix ^KT-49384

Don't report errors for projections in nested type arguments of
SAM conversion target types.
This commit is contained in:
Svyatoslav Scherbina
2021-10-25 16:03:51 +03:00
committed by Space
parent 65bea27431
commit 99447a11c1
4 changed files with 58 additions and 4 deletions
@@ -199,12 +199,19 @@ internal val copyDefaultValuesToActualPhase = konanUnitPhase(
*/
internal val checkSamSuperTypesPhase = konanUnitPhase(
op = {
// Handling types in current module not recursively:
// psi2ir can produce SAM conversions with variances in type arguments of type arguments.
// See https://youtrack.jetbrains.com/issue/KT-49384.
// So don't go deeper than top-level arguments to avoid the compiler emitting false-positive errors.
// Lowerings can handle this.
// Also such variances are allowed in the language for manual implementations of interfaces.
irModule!!.files
.forEach { SamSuperTypesChecker(this, it, mode = SamSuperTypesChecker.Mode.THROW).run() }
.forEach { SamSuperTypesChecker(this, it, mode = SamSuperTypesChecker.Mode.THROW, recurse = false).run() }
// TODO: This is temporary for handling klibs produced with earlier compiler versions.
// Handling types in dependencies recursively, just to be extra safe: don't change something that works.
irModules.values
.flatMap { it.files }
.forEach { SamSuperTypesChecker(this, it, mode = SamSuperTypesChecker.Mode.ERASE).run() }
.forEach { SamSuperTypesChecker(this, it, mode = SamSuperTypesChecker.Mode.ERASE, recurse = true).run() }
},
name = "CheckSamSuperTypes",
description = "Check SAM conversions super types"
@@ -23,7 +23,8 @@ import org.jetbrains.kotlin.types.Variance
internal class SamSuperTypesChecker(private val context: Context,
private val irFile: IrFile,
private val mode: Mode) {
private val mode: Mode,
private val recurse: Boolean) {
enum class Mode {
ERASE,
THROW
@@ -43,7 +44,13 @@ internal class SamSuperTypesChecker(private val context: Context,
context.reportCompilationError(
"Unexpected variance in super type argument: ${argument.variance} @$index", irFile, owner)
}
makeTypeProjection(argument.type.eraseProjections(owner), Variance.INVARIANT)
val newArgumentType = if (recurse) {
argument.type.eraseProjections(owner)
} else {
// See the explanation at the SamSuperTypesChecker constructor call sites.
argument.type
}
makeTypeProjection(newArgumentType, Variance.INVARIANT)
}
}
}
@@ -3181,6 +3181,10 @@ task funInterface_kt43887(type: KonanLocalTest) {
source = "codegen/funInterface/kt43887.kt"
}
task funInterface_kt49384(type: KonanLocalTest) {
source = "codegen/funInterface/kt49384.kt"
}
task objectExpression1(type: KonanLocalTest) {
useGoldenData = true
source = "codegen/objectExpression/expr1.kt"
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package codegen.funInterface.kt49384
import kotlin.test.*
interface A<T>
// https://youtrack.jetbrains.com/issue/KT-49384
class B<T> {
init {
mutableListOf<A<out T>>()
.sortWith { _, _ -> 1 }
}
}
@Test
fun test1() {
val b = B<Any>()
assertEquals(b, b) // Just to ensure B is not deleted by DCE
}
fun interface Foo<T> {
fun same(obj: T): T
}
fun getSame(obj: A<out Any>, foo: Foo<A<out Any>>) = foo.same(obj)
@Test
fun test2() {
val obj = object : A<Any> {}
assertSame(obj, getSame(obj) { it })
}