JS: prohibit is checks against native interfaces. Warn about casts to native interfaces. Fix #KT-14037, fix #KT-14038

This commit is contained in:
Alexey Andreev
2016-09-28 19:32:25 +03:00
parent 2bb81e6a18
commit acf7fcaebf
21 changed files with 341 additions and 9 deletions
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.js.translate.context.TranslationContext;
import org.jetbrains.kotlin.js.translate.general.AbstractTranslator;
import org.jetbrains.kotlin.js.translate.general.Translation;
import org.jetbrains.kotlin.js.translate.intrinsic.functions.factories.TopLevelFIF;
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils;
import org.jetbrains.kotlin.js.translate.reference.ReferenceTranslator;
import org.jetbrains.kotlin.js.translate.utils.BindingUtils;
import org.jetbrains.kotlin.js.translate.utils.TranslationUtils;
@@ -124,6 +125,10 @@ public final class PatternTranslator extends AbstractTranslator {
if (sourceType != null && !DynamicTypesKt.isDynamic(sourceType) && TypeUtilsKt.isSubtypeOf(sourceType, targetType)) return null;
JsExpression checkFunReference = doGetIsTypeCheckCallable(targetType);
if (checkFunReference == null) {
return null;
}
boolean isReifiedType = isReifiedTypeParameter(targetType);
if (!isReifiedType && isNullableType(targetType) ||
isReifiedType && findChildByType(targetTypeReference, KtNodeTypes.NULLABLE_TYPE) != null
@@ -137,6 +142,9 @@ public final class PatternTranslator extends AbstractTranslator {
@NotNull
public JsExpression getIsTypeCheckCallable(@NotNull KotlinType type) {
JsExpression callable = doGetIsTypeCheckCallable(type);
assert callable != null : "This method should be called only to translate reified type parameters. " +
"`callable` should never be null for reified type parameters. " +
"Actual type: " + type;
// If the type is reified, rely on the corresponding is type callable.
// Otherwise make sure that passing null yields true
@@ -147,8 +155,13 @@ public final class PatternTranslator extends AbstractTranslator {
return callable;
}
@NotNull
@Nullable
private JsExpression doGetIsTypeCheckCallable(@NotNull KotlinType type) {
ClassifierDescriptor targetDescriptor = type.getConstructor().getDeclarationDescriptor();
if (targetDescriptor != null && AnnotationsUtils.isNativeInterface(targetDescriptor)) {
return null;
}
JsExpression builtinCheck = getIsTypeCheckCallableForBuiltin(type);
if (builtinCheck != null) return builtinCheck;
@@ -164,13 +177,13 @@ public final class PatternTranslator extends AbstractTranslator {
JsExpression result = null;
for (KotlinType upperBound : typeParameterDescriptor.getUpperBounds()) {
JsExpression next = doGetIsTypeCheckCallable(upperBound);
result = result != null ? namer().andPredicate(result, next) : next;
if (next != null) {
result = result != null ? namer().andPredicate(result, next) : next;
}
}
assert result != null : "KotlinType is expected to return at least one upper bound: " + type;
return result;
}
ClassDescriptor referencedClass = DescriptorUtils.getClassDescriptorForType(type);
JsExpression typeName = ReferenceTranslator.translateAsTypeReference(referencedClass, context());
return namer().isInstanceOf(typeName);
@@ -0,0 +1,29 @@
// FILE: castToNativeClassChecked.kt
@native abstract class S() {
abstract fun foo(): String
}
@native class A(x: String) {
fun foo(): String = noImpl
}
fun createObject(): Any = A("fail: CCE not thrown")
fun box(): String {
try {
return (createObject() as S).foo()
}
catch (e: ClassCastException) {
return "OK"
}
}
// FILE: castToNativeClassChecked.js
function S() {
}
function A(x) {
this.x = x;
}
A.prototype.foo = function() {
return this.x;
}
@@ -0,0 +1,20 @@
// FILE: castToNativeInterface.kt
@native interface I {
fun foo(): String
}
@native class A(x: String) : I {
override fun foo(): String = noImpl
}
fun createObject(): Any = A("OK")
fun box() = (createObject() as I).foo()
// FILE: castToNativeInterface.js
function A(x) {
this.x = x;
}
A.prototype.foo = function() {
return this.x;
}
@@ -0,0 +1,32 @@
// FILE: castToTypeParamBoundedByNativeInterface.kt
@native interface I {
fun foo(): String
}
interface J {
fun bar(): String
}
@native abstract class B() : I
@native class A(x: String) : B() {
override fun foo(): String = noImpl
}
fun createObject(): Any = A("OK")
fun <T> castToI(o: Any): T where T : I, T : B = o as T
fun box() = castToI<A>(createObject()).foo()
// FILE: castToTypeParamBoundedByNativeInterface.js
function B() {
}
function A(x) {
this.x = x;
}
A.prototype = Object.create(B.prototype);
A.prototype.foo = function() {
return this.x;
}