diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index cf3121b61eb..394f56a0c0a 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -2672,6 +2672,15 @@ public class ExpressionCodegen extends KtVisitor impleme return invokeFunction(resolvedCall, receiver); } + @Override + public StackValue visitCollectionLiteralExpression( + @NotNull KtCollectionLiteralExpression expression, StackValue data + ) { + ResolvedCall resolvedCall = bindingContext.get(COLLECTION_LITERAL_CALL, expression); + assert resolvedCall != null : "No resolved call for " + PsiUtilsKt.getTextWithLocation(expression); + return invokeFunction(resolvedCall, data); + } + @Nullable private StackValue genSamInterfaceValue( @NotNull KtExpression probablyParenthesizedExpression, diff --git a/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsInArgumentPosition.kt b/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsInArgumentPosition.kt new file mode 100644 index 00000000000..4c82af53bdb --- /dev/null +++ b/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsInArgumentPosition.kt @@ -0,0 +1,43 @@ +// WITH_REFLECT + +// IGNORE_BACKEND: JS + +import java.util.Arrays +import kotlin.reflect.KClass +import kotlin.reflect.KFunction0 + +inline fun test(kFunction: KFunction0, test: T.() -> Unit) { + val annotation = kFunction.annotations.single() as T + annotation.test() +} + +fun check(b: Boolean, message: String) { + if (!b) throw RuntimeException(message) +} + +annotation class Foo(val a: FloatArray = [], val b: Array = [], val c: Array> = []) + +@Foo(a = [1f, 2f, 1 / 0f]) +fun test1() {} + +@Foo(b = ["Hello", ", ", "Kot" + "lin"]) +fun test2() {} + +@Foo(c = [Int::class, Array::class, Foo::class]) +fun test3() {} + +fun box(): String { + test(::test1) { + check(a.contentEquals(floatArrayOf(1f, 2f, Float.POSITIVE_INFINITY)), "Fail 1: ${a.joinToString()}") + } + + test(::test2) { + check(b.contentEquals(arrayOf("Hello", ", ", "Kotlin")), "Fail 2: ${b.joinToString()}") + } + + test(::test3) { + check(c.contentEquals(arrayOf(Int::class, Array::class, Foo::class)), "Fail 3: ${c.joinToString()}") + } + + return "OK" +} diff --git a/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithConstants.kt b/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithConstants.kt new file mode 100644 index 00000000000..ae30e2bfce0 --- /dev/null +++ b/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithConstants.kt @@ -0,0 +1,36 @@ +// WITH_REFLECT + +// IGNORE_BACKEND: JS + +import java.util.Arrays +import kotlin.reflect.KFunction0 + +inline fun test(kFunction: KFunction0, test: T.() -> Unit) { + val annotation = kFunction.annotations.single() as T + annotation.test() +} + +fun check(b: Boolean, message: String) { + if (!b) throw RuntimeException(message) +} + +annotation class Foo(val a: IntArray = [], val b: Array = []) + +const val ONE_INT = 1 +const val ONE_FLOAT = 1f +const val HELLO = "hello" +const val C_CHAR = 'c' + +@Foo( + a = [ONE_INT, ONE_INT + ONE_FLOAT.toInt(), ONE_INT + 10, (ONE_INT % 1.0).toInt()], + b = [HELLO, HELLO + C_CHAR, HELLO + ", Kotlin", C_CHAR.toString() + C_CHAR]) +fun test1() {} + +fun box(): String { + test(::test1) { + check(a.contentEquals(intArrayOf(1, 2, 11, 0)), "Fail 1: ${a.joinToString()}") + check(b.contentEquals(arrayOf("hello", "helloc", "hello, Kotlin", "cc")), "Fail 2: ${b.joinToString()}") + } + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/collectionLiterals/defaultAnnotationParameterValues.kt b/compiler/testData/codegen/box/collectionLiterals/defaultAnnotationParameterValues.kt new file mode 100644 index 00000000000..8befc9ddef8 --- /dev/null +++ b/compiler/testData/codegen/box/collectionLiterals/defaultAnnotationParameterValues.kt @@ -0,0 +1,38 @@ +// WITH_REFLECT + +// IGNORE_BACKEND: JS + +import java.util.Arrays +import kotlin.reflect.KClass +import kotlin.reflect.KFunction0 + +inline fun test(kFunction: KFunction0, test: T.() -> String): String { + val annotation = kFunction.annotations.single() as T + return annotation.test() +} + +fun check(b: Boolean, message: String) { + if (!b) throw RuntimeException(message) +} + +annotation class Foo( + val a: IntArray = [], + val b: IntArray = [1, 2, 3], + val c: Array = ["/"], + val d: Array> = [Int::class, Array::class], + val e: DoubleArray = [1.0] +) + +@Foo +fun withAnn() {} + +fun box(): String { + return test(::withAnn) { + check(a.contentEquals(intArrayOf()), "Fail 1: ${a.joinToString()}") + check(b.contentEquals(intArrayOf(1, 2, 3)), "Fail 2: ${b.joinToString()}") + check(c.contentEquals(arrayOf("/")), "Fail 3: ${c.joinToString()}") + check(d.contentEquals(arrayOf(Int::class, Array::class)), "Fail 4: ${d.joinToString()}") + check(e.contentEquals(doubleArrayOf(1.0)), "Fail 5: ${e.joinToString()}") + "OK" + } +} \ No newline at end of file diff --git a/compiler/testData/codegen/light-analysis/collectionLiterals/collectionLiteralsInArgumentPosition.txt b/compiler/testData/codegen/light-analysis/collectionLiterals/collectionLiteralsInArgumentPosition.txt new file mode 100644 index 00000000000..c79d613745d --- /dev/null +++ b/compiler/testData/codegen/light-analysis/collectionLiterals/collectionLiteralsInArgumentPosition.txt @@ -0,0 +1,17 @@ +@kotlin.Metadata +public final class CollectionLiteralsInArgumentPositionKt { + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String + public final static method check(p0: boolean, @org.jetbrains.annotations.NotNull p1: java.lang.String): void + private final static method test(p0: kotlin.reflect.KFunction, p1: kotlin.jvm.functions.Function1): void + public final static @Foo method test1(): void + public final static @Foo method test2(): void + public final static @Foo method test3(): void +} + +@java.lang.annotation.Retention +@kotlin.Metadata +public annotation class Foo { + public abstract method a(): float[] + public abstract method b(): java.lang.String[] + public abstract method c(): java.lang.Class[] +} diff --git a/compiler/testData/codegen/light-analysis/collectionLiterals/collectionLiteralsWithConstants.txt b/compiler/testData/codegen/light-analysis/collectionLiterals/collectionLiteralsWithConstants.txt new file mode 100644 index 00000000000..a8fb845c6b3 --- /dev/null +++ b/compiler/testData/codegen/light-analysis/collectionLiterals/collectionLiteralsWithConstants.txt @@ -0,0 +1,18 @@ +@kotlin.Metadata +public final class CollectionLiteralsWithConstantsKt { + public final static field C_CHAR: char + public final static @org.jetbrains.annotations.NotNull field HELLO: java.lang.String + public final static field ONE_FLOAT: float + public final static field ONE_INT: int + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String + public final static method check(p0: boolean, @org.jetbrains.annotations.NotNull p1: java.lang.String): void + private final static method test(p0: kotlin.reflect.KFunction, p1: kotlin.jvm.functions.Function1): void + public final static @Foo method test1(): void +} + +@java.lang.annotation.Retention +@kotlin.Metadata +public annotation class Foo { + public abstract method a(): int[] + public abstract method b(): java.lang.String[] +} diff --git a/compiler/testData/codegen/light-analysis/collectionLiterals/defaultAnnotationParameterValues.txt b/compiler/testData/codegen/light-analysis/collectionLiterals/defaultAnnotationParameterValues.txt new file mode 100644 index 00000000000..3a867e97a72 --- /dev/null +++ b/compiler/testData/codegen/light-analysis/collectionLiterals/defaultAnnotationParameterValues.txt @@ -0,0 +1,17 @@ +@kotlin.Metadata +public final class DefaultAnnotationParameterValuesKt { + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String + public final static method check(p0: boolean, @org.jetbrains.annotations.NotNull p1: java.lang.String): void + private final static method test(p0: kotlin.reflect.KFunction, p1: kotlin.jvm.functions.Function1): java.lang.String + public final static @Foo method withAnn(): void +} + +@java.lang.annotation.Retention +@kotlin.Metadata +public annotation class Foo { + public abstract method a(): int[] + public abstract method b(): int[] + public abstract method c(): java.lang.String[] + public abstract method d(): java.lang.Class[] + public abstract method e(): double[] +} diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 09f1909d332..2394a7f318b 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -3876,6 +3876,33 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes } } + @TestMetadata("compiler/testData/codegen/box/collectionLiterals") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class CollectionLiterals extends AbstractIrBlackBoxCodegenTest { + public void testAllFilesPresentInCollectionLiterals() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/collectionLiterals"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("collectionLiteralsInArgumentPosition.kt") + public void testCollectionLiteralsInArgumentPosition() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/collectionLiteralsInArgumentPosition.kt"); + doTest(fileName); + } + + @TestMetadata("collectionLiteralsWithConstants.kt") + public void testCollectionLiteralsWithConstants() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithConstants.kt"); + doTest(fileName); + } + + @TestMetadata("defaultAnnotationParameterValues.kt") + public void testDefaultAnnotationParameterValues() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/defaultAnnotationParameterValues.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/codegen/box/collections") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 04a52f30ff3..62c84d5705b 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -3876,6 +3876,33 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { } } + @TestMetadata("compiler/testData/codegen/box/collectionLiterals") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class CollectionLiterals extends AbstractBlackBoxCodegenTest { + public void testAllFilesPresentInCollectionLiterals() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/collectionLiterals"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("collectionLiteralsInArgumentPosition.kt") + public void testCollectionLiteralsInArgumentPosition() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/collectionLiteralsInArgumentPosition.kt"); + doTest(fileName); + } + + @TestMetadata("collectionLiteralsWithConstants.kt") + public void testCollectionLiteralsWithConstants() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithConstants.kt"); + doTest(fileName); + } + + @TestMetadata("defaultAnnotationParameterValues.kt") + public void testDefaultAnnotationParameterValues() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/defaultAnnotationParameterValues.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/codegen/box/collections") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index fbe40dd191d..778edfeeacf 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -3876,6 +3876,33 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes } } + @TestMetadata("compiler/testData/codegen/box/collectionLiterals") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class CollectionLiterals extends AbstractLightAnalysisModeTest { + public void testAllFilesPresentInCollectionLiterals() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/collectionLiterals"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("collectionLiteralsInArgumentPosition.kt") + public void testCollectionLiteralsInArgumentPosition() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/collectionLiteralsInArgumentPosition.kt"); + doTest(fileName); + } + + @TestMetadata("collectionLiteralsWithConstants.kt") + public void testCollectionLiteralsWithConstants() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithConstants.kt"); + doTest(fileName); + } + + @TestMetadata("defaultAnnotationParameterValues.kt") + public void testDefaultAnnotationParameterValues() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/defaultAnnotationParameterValues.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/codegen/box/collections") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 37fa7fa0690..b5f8c3c4e3c 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -4561,6 +4561,51 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { } } + @TestMetadata("compiler/testData/codegen/box/collectionLiterals") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class CollectionLiterals extends AbstractJsCodegenBoxTest { + public void testAllFilesPresentInCollectionLiterals() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/collectionLiterals"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); + } + + @TestMetadata("collectionLiteralsInArgumentPosition.kt") + public void testCollectionLiteralsInArgumentPosition() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/collectionLiteralsInArgumentPosition.kt"); + try { + doTest(fileName); + } + catch (Throwable ignore) { + return; + } + throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); + } + + @TestMetadata("collectionLiteralsWithConstants.kt") + public void testCollectionLiteralsWithConstants() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithConstants.kt"); + try { + doTest(fileName); + } + catch (Throwable ignore) { + return; + } + throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); + } + + @TestMetadata("defaultAnnotationParameterValues.kt") + public void testDefaultAnnotationParameterValues() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/collectionLiterals/defaultAnnotationParameterValues.kt"); + try { + doTest(fileName); + } + catch (Throwable ignore) { + return; + } + throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); + } + } + @TestMetadata("compiler/testData/codegen/box/collections") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)