Skip to content

Commit 9e03b71

Browse files
committed
Use flexible upper bound of parameter's type for vararg argument
See the issue and the test. The problem was that when generating call to `foo` method in member scope of `AT<*>` its resulting descriptor after substitution and approximation was: fun foo(x: Nothing..Array<out Nothing>). This signature is correct, but when using this parameter type for generating a vararg argument the assertion is violated that the type of the argument must be an array (by default we're using lower flexible bound everywhere) The solution is using upper bound for flexible types that should always have a form of Array<out T> for varargs (even for such corner cases) both for Kotlin and Java declarations. #KT-14607 Fixed
1 parent 14dad61 commit 9e03b71

File tree

6 files changed

+47
-1
lines changed

6 files changed

+47
-1
lines changed

compiler/backend/src/org/jetbrains/kotlin/codegen/CallBasedArgumentGenerator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.jetbrains.kotlin.psi.KtExpression;
2222
import org.jetbrains.kotlin.psi.ValueArgument;
2323
import org.jetbrains.kotlin.resolve.calls.model.*;
24+
import org.jetbrains.kotlin.types.FlexibleTypesKt;
2425
import org.jetbrains.org.objectweb.asm.Type;
2526

2627
import java.util.List;
@@ -84,7 +85,9 @@ protected void generateDefault(int i, @NotNull DefaultValueArgument argument) {
8485
protected void generateVararg(int i, @NotNull VarargValueArgument argument) {
8586
ValueParameterDescriptor parameter = valueParameters.get(i);
8687
Type type = valueParameterTypes.get(i);
87-
codegen.genVarargs(argument, parameter.getType());
88+
// Upper bound for type of vararg parameter should always have a form of 'Array<out T>',
89+
// while its lower bound may be Nothing-typed after approximation
90+
codegen.genVarargs(argument, FlexibleTypesKt.upperIfFlexible(parameter.getType()));
8891
callGenerator.afterParameterPut(type, null, i);
8992
}
9093

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// FILE: AT.java
2+
3+
public class AT<G> {
4+
public String result = "fail";
5+
public void foo(G ...y) {
6+
result = "OK";
7+
}
8+
}
9+
10+
// FILE: main.kt
11+
12+
fun AT<*>.bar() {
13+
foo()
14+
}
15+
16+
fun box(): String {
17+
val a = AT<String>()
18+
a.bar()
19+
return a.result
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@kotlin.Metadata
2+
public final class MainKt {
3+
public final static method bar(@org.jetbrains.annotations.NotNull p0: AT): void
4+
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
5+
}

compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,12 @@ public void testStdlib() throws Exception {
617617
doTest(fileName);
618618
}
619619

620+
@TestMetadata("varargsWithJava.kt")
621+
public void testVarargsWithJava() throws Exception {
622+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/arrays/varargsWithJava.kt");
623+
doTest(fileName);
624+
}
625+
620626
@TestMetadata("compiler/testData/codegen/box/arrays/multiDecl")
621627
@TestDataPath("$PROJECT_ROOT")
622628
@RunWith(JUnit3RunnerWithInners.class)

compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,12 @@ public void testStdlib() throws Exception {
617617
doTest(fileName);
618618
}
619619

620+
@TestMetadata("varargsWithJava.kt")
621+
public void testVarargsWithJava() throws Exception {
622+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/arrays/varargsWithJava.kt");
623+
doTest(fileName);
624+
}
625+
620626
@TestMetadata("compiler/testData/codegen/box/arrays/multiDecl")
621627
@TestDataPath("$PROJECT_ROOT")
622628
@RunWith(JUnit3RunnerWithInners.class)

js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,12 @@ public void testStdlib() throws Exception {
809809
doTest(fileName);
810810
}
811811

812+
@TestMetadata("varargsWithJava.kt")
813+
public void testVarargsWithJava() throws Exception {
814+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/arrays/varargsWithJava.kt");
815+
doTest(fileName);
816+
}
817+
812818
@TestMetadata("compiler/testData/codegen/box/arrays/multiDecl")
813819
@TestDataPath("$PROJECT_ROOT")
814820
@RunWith(JUnit3RunnerWithInners.class)

0 commit comments

Comments
 (0)