From 71ef8c4f89b9a78566d641914ea4508abfcce12e Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 10 Nov 2016 19:08:15 +0300 Subject: [PATCH] JS: fix exception in JS front-end when checking JsName that was resolved with errors --- .../testsWithJsStdLib/name/jsNameWithoutParameter.kt | 7 +++++++ .../testsWithJsStdLib/name/jsNameWithoutParameter.txt | 6 ++++++ .../checkers/DiagnosticsTestWithJsStdLibGenerated.java | 6 ++++++ .../kotlin/js/translate/utils/AnnotationsUtils.java | 9 +++++---- .../js/test/semantics/JsCodegenBoxTestGenerated.java | 6 ++++++ 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameWithoutParameter.kt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameWithoutParameter.txt diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameWithoutParameter.kt b/compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameWithoutParameter.kt new file mode 100644 index 00000000000..318e81a7a0f --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameWithoutParameter.kt @@ -0,0 +1,7 @@ +// !DIAGNOSTICS: -NO_VALUE_FOR_PARAMETER, -CONSTANT_EXPECTED_TYPE_MISMATCH +// This should not crash, see KT-14752 +package foo + +@JsName fun foo(x: Int) = x + +@JsName(23) fun bar(x: Int) = x \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameWithoutParameter.txt b/compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameWithoutParameter.txt new file mode 100644 index 00000000000..a4f367a2d84 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameWithoutParameter.txt @@ -0,0 +1,6 @@ +package + +package foo { + @kotlin.js.JsName(name = 23) public fun bar(/*0*/ x: kotlin.Int): kotlin.Int + @kotlin.js.JsName public fun foo(/*0*/ x: kotlin.Int): kotlin.Int +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java index 554be45c7c7..e786c390a7f 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java @@ -386,6 +386,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes doTest(fileName); } + @TestMetadata("jsNameWithoutParameter.kt") + public void testJsNameWithoutParameter() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameWithoutParameter.kt"); + doTest(fileName); + } + @TestMetadata("methodAndMethod.kt") public void testMethodAndMethod() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.kt"); diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/translate/utils/AnnotationsUtils.java b/js/js.frontend/src/org/jetbrains/kotlin/js/translate/utils/AnnotationsUtils.java index 80751c7718c..6605011f511 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/translate/utils/AnnotationsUtils.java +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/translate/utils/AnnotationsUtils.java @@ -119,13 +119,14 @@ public final class AnnotationsUtils { @Nullable public static String getJsName(@NotNull DeclarationDescriptor descriptor) { AnnotationDescriptor annotation = getJsNameAnnotation(descriptor); - if (annotation == null) return null; + if (annotation == null || annotation.getAllValueArguments().isEmpty()) return null; ConstantValue value = annotation.getAllValueArguments().values().iterator().next(); - assert value != null : "JsName annotation should always declare string parameter"; + if (value == null) return null; Object result = value.getValue(); - assert result instanceof String : "Parameter of JsName annotation should be string"; + if (!(result instanceof String)) return null; + return (String) result; } @@ -143,7 +144,7 @@ public final class AnnotationsUtils { return false; } - public static boolean hasAnnotationOrInsideAnnotatedClass( + private static boolean hasAnnotationOrInsideAnnotatedClass( @NotNull DeclarationDescriptor descriptor, @NotNull PredefinedAnnotation annotation ) { 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 86d6d28eb17..34fa0ddf8fa 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 @@ -7082,6 +7082,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("kt13557.kt") + public void testKt13557() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/delegatedProperty/local/kt13557.kt"); + doTest(fileName); + } + @TestMetadata("localVal.kt") public void testLocalVal() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/delegatedProperty/local/localVal.kt");