JS: don't omit guard for catch with Throwable type (KT-13616); don't generate guard for catch with dynamic type (KT-13615)
This commit is contained in:
@@ -2977,6 +2977,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("tryCatchDynamic.kt")
|
||||
public void testTryCatchDynamic() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/expression/try/tryCatchDynamic.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("tryCatchExpr.kt")
|
||||
public void testTryCatchExpr() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/expression/try/tryCatchExpr.kt");
|
||||
|
||||
+4
-9
@@ -17,9 +17,7 @@
|
||||
package org.jetbrains.kotlin.js.translate.expression
|
||||
|
||||
import com.google.dart.compiler.backend.js.ast.*
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.js.descriptorUtils.getJetTypeFqName
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext
|
||||
import org.jetbrains.kotlin.js.translate.general.AbstractTranslator
|
||||
import org.jetbrains.kotlin.js.translate.general.Translation.patternTranslator
|
||||
@@ -31,6 +29,7 @@ import org.jetbrains.kotlin.psi.KtCatchClause
|
||||
import org.jetbrains.kotlin.psi.KtTypeReference
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull
|
||||
import org.jetbrains.kotlin.types.isDynamic
|
||||
|
||||
class CatchTranslator(
|
||||
val catches: List<KtCatchClause>,
|
||||
@@ -105,7 +104,7 @@ class CatchTranslator(
|
||||
val thenBlock = translateCatchBody(context, catch)
|
||||
thenBlock.statements.addAll(0, additionalStatements)
|
||||
|
||||
if (paramType.isThrowable) return thenBlock
|
||||
if (paramType.isDynamic) return thenBlock
|
||||
|
||||
// translateIsCheck won't ever return `null` if its second argument is `null`
|
||||
val typeCheck = with (patternTranslator(nextContext)) {
|
||||
@@ -128,10 +127,6 @@ class CatchTranslator(
|
||||
return convertToBlock(jsCatchBody)
|
||||
}
|
||||
|
||||
private val KtTypeReference.isThrowable: Boolean
|
||||
get() {
|
||||
val jetType = getNotNull(bindingContext(), BindingContext.TYPE, this)
|
||||
val jetTypeName = jetType.getJetTypeFqName(false)
|
||||
return jetTypeName == KotlinBuiltIns.FQ_NAMES.throwable.asString()
|
||||
}
|
||||
private val KtTypeReference.isDynamic: Boolean
|
||||
get() = getNotNull(bindingContext(), BindingContext.TYPE, this).isDynamic()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
|
||||
var s: String = ""
|
||||
|
||||
try {
|
||||
js("throw null")
|
||||
} catch (e: Throwable) {
|
||||
s = "Throwable"
|
||||
} catch (e: dynamic) {
|
||||
s = "dynamic"
|
||||
}
|
||||
assertEquals("dynamic", s)
|
||||
|
||||
s = ""
|
||||
try {
|
||||
try {
|
||||
js("throw null")
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
s = "Throwable"
|
||||
}
|
||||
} catch (e: dynamic) {
|
||||
s = "dynamic"
|
||||
}
|
||||
assertEquals("dynamic", s)
|
||||
|
||||
s = ""
|
||||
try {
|
||||
js("throw Object.create(null)")
|
||||
}
|
||||
catch (e: dynamic) {
|
||||
s = "dynamic"
|
||||
}
|
||||
assertEquals("dynamic", s)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user