-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IR: fix SAM conversion for types with contravariant intersection argu…
…ment type In the added test, the problem was that the SAM type as computed by `SamTypeFactory.createByValueParameter` was `Consumer<{BaseClass & BaseInterface}>`, which was latter approximated in psi2ir during the KotlinType->IrType conversion to `Consumer<out Any?>` (here: https://github.com/JetBrains/kotlin/blob/3034d9d791cf1f9033104e12448e0d262d3bc3ce/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ArgumentsGenerationUtils.kt#L606), because intersection type argument is approximated to `out Any?`. To avoid this, replace intersection type in immediate arguments of a SAM type with the common supertype of its components at the same place where we're getting rid of projections. #KT-45945 Fixed
- Loading branch information
Showing
16 changed files
with
450 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
.../tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
compiler/testData/codegen/box/funInterface/contravariantIntersectionType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// IGNORE_BACKEND: WASM | ||
// WASM_MUTE_REASON: SAM_CONVERSIONS | ||
|
||
// CHECK_BYTECODE_TEXT | ||
// 0 java/lang/invoke/LambdaMetafactory | ||
|
||
abstract class BaseClass | ||
interface BaseInterface | ||
|
||
class ConcreteType : BaseClass(), BaseInterface | ||
class ConcreteType2 : BaseClass(), BaseInterface | ||
|
||
fun box(): String { | ||
example(0) | ||
return "OK" | ||
} | ||
|
||
fun example(input: Int) { | ||
val instance = when (input) { | ||
0 -> GenericHolder<ConcreteType>() | ||
else -> GenericHolder<ConcreteType2>() | ||
} | ||
|
||
instance.doOnSuccess {} | ||
instance.doOnSuccess(::functionReference) | ||
} | ||
|
||
fun functionReference(x: Any) {} | ||
|
||
class GenericHolder<T> { | ||
fun doOnSuccess(onSuccess: Consumer<in T>) { | ||
onSuccess.accept(object : BaseClass() {} as T) | ||
} | ||
} | ||
|
||
fun interface Consumer<T> { | ||
fun accept(t: T) | ||
} |
42 changes: 42 additions & 0 deletions
42
...ta/codegen/box/funInterface/contravariantIntersectionTypeWithNonTrivialCommonSupertype.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// IGNORE_BACKEND: WASM | ||
// WASM_MUTE_REASON: SAM_CONVERSIONS | ||
|
||
// CHECK_BYTECODE_TEXT | ||
// 0 java/lang/invoke/LambdaMetafactory | ||
|
||
interface Top | ||
|
||
interface Common : Top | ||
|
||
abstract class BaseClass : Common | ||
interface BaseInterface : Common | ||
|
||
class ConcreteType : BaseClass(), BaseInterface | ||
class ConcreteType2 : BaseClass(), BaseInterface | ||
|
||
fun box(): String { | ||
example(0) | ||
return "OK" | ||
} | ||
|
||
fun example(input: Int) { | ||
val instance = when (input) { | ||
0 -> GenericHolder<ConcreteType>() | ||
else -> GenericHolder<ConcreteType2>() | ||
} | ||
|
||
instance.doOnSuccess {} | ||
instance.doOnSuccess(::functionReference) | ||
} | ||
|
||
fun functionReference(x: Any) {} | ||
|
||
class GenericHolder<T : Top> { | ||
fun doOnSuccess(onSuccess: Consumer<in T>) { | ||
onSuccess.accept(object : BaseClass() {} as T) | ||
} | ||
} | ||
|
||
fun interface Consumer<T : Top> { | ||
fun accept(t: T) | ||
} |
34 changes: 34 additions & 0 deletions
34
...a/codegen/box/funInterface/contravariantIntersectionTypeWithNonTrivialCommonSupertype2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// IGNORE_BACKEND: WASM | ||
// WASM_MUTE_REASON: SAM_CONVERSIONS | ||
|
||
// CHECK_BYTECODE_TEXT | ||
// 0 java/lang/invoke/LambdaMetafactory | ||
|
||
interface Top | ||
interface Unrelated | ||
|
||
interface A : Top, Unrelated | ||
interface B : Top, Unrelated | ||
|
||
fun box(): String { | ||
val g = when ("".length) { | ||
0 -> G<A>() | ||
else -> G<B>() | ||
} | ||
|
||
g.check {} | ||
g.check(::functionReference) | ||
return "OK" | ||
} | ||
|
||
fun functionReference(x: Any) {} | ||
|
||
class G<T : Top> { | ||
fun check(x: IFoo<in T>) { | ||
x.accept(object : A {} as T) | ||
} | ||
} | ||
|
||
fun interface IFoo<T : Top> { | ||
fun accept(t: T) | ||
} |
42 changes: 42 additions & 0 deletions
42
compiler/testData/codegen/box/sam/contravariantIntersectionType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// TARGET_BACKEND: JVM | ||
|
||
// CHECK_BYTECODE_TEXT | ||
// JVM_IR_TEMPLATES | ||
// 2 java/lang/invoke/LambdaMetafactory | ||
|
||
// FILE: test.kt | ||
|
||
abstract class BaseClass | ||
interface BaseInterface | ||
|
||
class ConcreteType : BaseClass(), BaseInterface | ||
class ConcreteType2 : BaseClass(), BaseInterface | ||
|
||
fun box(): String { | ||
example(0) | ||
return "OK" | ||
} | ||
|
||
fun example(input: Int) { | ||
val instance = when (input) { | ||
0 -> GenericHolder<ConcreteType>() | ||
else -> GenericHolder<ConcreteType2>() | ||
} | ||
|
||
instance.doOnSuccess {} | ||
instance.doOnSuccess(::functionReference) | ||
} | ||
|
||
fun functionReference(x: Any) {} | ||
|
||
class GenericHolder<T> { | ||
fun doOnSuccess(onSuccess: Consumer<in T>) { | ||
onSuccess.accept(object : BaseClass() {} as T) | ||
} | ||
} | ||
|
||
// FILE: Consumer.java | ||
|
||
public interface Consumer<T> { | ||
void accept(T t); | ||
} |
46 changes: 46 additions & 0 deletions
46
...er/testData/codegen/box/sam/contravariantIntersectionTypeWithNonTrivialCommonSupertype.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// TARGET_BACKEND: JVM | ||
|
||
// CHECK_BYTECODE_TEXT | ||
// JVM_IR_TEMPLATES | ||
// 2 java/lang/invoke/LambdaMetafactory | ||
|
||
// FILE: test.kt | ||
|
||
interface Top | ||
|
||
interface Common : Top | ||
|
||
abstract class BaseClass : Common | ||
interface BaseInterface : Common | ||
|
||
class ConcreteType : BaseClass(), BaseInterface | ||
class ConcreteType2 : BaseClass(), BaseInterface | ||
|
||
fun box(): String { | ||
example(0) | ||
return "OK" | ||
} | ||
|
||
fun example(input: Int) { | ||
val instance = when (input) { | ||
0 -> GenericHolder<ConcreteType>() | ||
else -> GenericHolder<ConcreteType2>() | ||
} | ||
|
||
instance.doOnSuccess {} | ||
instance.doOnSuccess(::functionReference) | ||
} | ||
|
||
fun functionReference(x: Any) {} | ||
|
||
class GenericHolder<T : Top> { | ||
fun doOnSuccess(onSuccess: Consumer<in T>) { | ||
onSuccess.accept(object : BaseClass() {} as T) | ||
} | ||
} | ||
|
||
// FILE: Consumer.java | ||
|
||
public interface Consumer<T extends Top> { | ||
void accept(T t); | ||
} |
38 changes: 38 additions & 0 deletions
38
...r/testData/codegen/box/sam/contravariantIntersectionTypeWithNonTrivialCommonSupertype2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// TARGET_BACKEND: JVM | ||
|
||
// CHECK_BYTECODE_TEXT | ||
// JVM_IR_TEMPLATES | ||
// 2 java/lang/invoke/LambdaMetafactory | ||
|
||
// FILE: test.kt | ||
|
||
interface Top | ||
interface Unrelated | ||
|
||
interface A : Top, Unrelated | ||
interface B : Top, Unrelated | ||
|
||
fun box(): String { | ||
val g = when ("".length) { | ||
0 -> G<A>() | ||
else -> G<B>() | ||
} | ||
|
||
g.check {} | ||
g.check(::functionReference) | ||
return "OK" | ||
} | ||
|
||
fun functionReference(x: Any) {} | ||
|
||
class G<T : Top> { | ||
fun check(x: IFoo<in T>) { | ||
x.accept(object : A {} as T) | ||
} | ||
} | ||
|
||
// FILE: IFoo.java | ||
|
||
public interface IFoo<T extends Top> { | ||
void accept(T t); | ||
} |
36 changes: 36 additions & 0 deletions
36
...new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.