diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java index a9bfc03f889..4804f046098 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java @@ -724,7 +724,7 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes @RunWith(JUnit3RunnerWithInners.class) public static class Rtti extends AbstractDiagnosticsTestWithJsStdLib { public void testAllFilesPresentInRtti() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithJsStdLib/native/rtti"), Pattern.compile("^(.+)\\.kt$"), true); + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithJsStdLib/native/rtti"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } @TestMetadata("castToNativeInterface.kt") diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java index b479765e9ae..c90ca4a1803 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java @@ -5504,6 +5504,18 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest { doTest(fileName); } + @TestMetadata("castToNativeInterfaceChecked.kt") + public void testCastToNativeInterfaceChecked() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/native/castToNativeInterfaceChecked.kt"); + doTest(fileName); + } + + @TestMetadata("castToNullableNativeInterface.kt") + public void testCastToNullableNativeInterface() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/native/castToNullableNativeInterface.kt"); + doTest(fileName); + } + @TestMetadata("castToTypeParamBoundedByNativeInterface.kt") public void testCastToTypeParamBoundedByNativeInterface() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/native/castToTypeParamBoundedByNativeInterface.kt"); @@ -5648,6 +5660,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest { doTest(fileName); } + @TestMetadata("safeCastToNativeInterface.kt") + public void testSafeCastToNativeInterface() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/native/safeCastToNativeInterface.kt"); + doTest(fileName); + } + @TestMetadata("secondaryConstructor.kt") public void testSecondaryConstructor() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/native/secondaryConstructor.kt"); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/PatternTranslator.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/PatternTranslator.java index b42e20e20c4..2866822474f 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/PatternTranslator.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/PatternTranslator.java @@ -159,7 +159,7 @@ public final class PatternTranslator extends AbstractTranslator { private JsExpression doGetIsTypeCheckCallable(@NotNull KotlinType type) { ClassifierDescriptor targetDescriptor = type.getConstructor().getDeclarationDescriptor(); if (targetDescriptor != null && AnnotationsUtils.isNativeInterface(targetDescriptor)) { - return null; + return type.isMarkedNullable() ? null : namer().isAny(); } JsExpression builtinCheck = getIsTypeCheckCallableForBuiltin(type); diff --git a/js/js.translator/testData/box/native/castToNativeInterfaceChecked.kt b/js/js.translator/testData/box/native/castToNativeInterfaceChecked.kt new file mode 100644 index 00000000000..96b2a42f311 --- /dev/null +++ b/js/js.translator/testData/box/native/castToNativeInterfaceChecked.kt @@ -0,0 +1,15 @@ +@native interface I { + fun foo(): String +} + +fun createObject(): Any? = null + +fun box(): String { + try { + (createObject() as I).foo() + return "fail: exception not thrown" + } + catch (e: ClassCastException) { + return "OK" + } +} \ No newline at end of file diff --git a/js/js.translator/testData/box/native/castToNullableNativeInterface.kt b/js/js.translator/testData/box/native/castToNullableNativeInterface.kt new file mode 100644 index 00000000000..9ffac1c230e --- /dev/null +++ b/js/js.translator/testData/box/native/castToNullableNativeInterface.kt @@ -0,0 +1,10 @@ +@native interface I { + fun foo(): String +} + +fun createObject(): Any? = null + +fun box(): String { + val result = (createObject() as I?)?.foo() + return if (result == null) "OK" else "fail" +} diff --git a/js/js.translator/testData/box/native/nativeClassAsReifiedTypeArgument.kt b/js/js.translator/testData/box/native/nativeClassAsReifiedTypeArgument.kt index cb42a9dec6b..39a734821e9 100644 --- a/js/js.translator/testData/box/native/nativeClassAsReifiedTypeArgument.kt +++ b/js/js.translator/testData/box/native/nativeClassAsReifiedTypeArgument.kt @@ -2,8 +2,8 @@ var global = "" -inline fun log(x: T) { - global += jsClass().name + ": " + x +inline fun log(x: T) { + global += T::class.js.name + ": " + x } @native class C { diff --git a/js/js.translator/testData/box/native/safeCastToNativeInterface.kt b/js/js.translator/testData/box/native/safeCastToNativeInterface.kt new file mode 100644 index 00000000000..67890280809 --- /dev/null +++ b/js/js.translator/testData/box/native/safeCastToNativeInterface.kt @@ -0,0 +1,9 @@ +@native interface I { + fun foo(): String +} +fun createObject(): Any? = null + +fun box(): String { + val result = (createObject() as? I)?.foo() + return if (result == null) "OK" else "fail" +} \ No newline at end of file