JS: prohibit passing native interfaces to reified type parameters

This commit is contained in:
Alexey Andreev
2016-09-30 14:54:50 +03:00
parent acf7fcaebf
commit 2eb54f234c
10 changed files with 124 additions and 1 deletions
@@ -0,0 +1,21 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
inline fun <reified T> foo(x: T) {
println(x)
}
@native interface I
@native class C : I
operator inline fun <reified T> C.plus(other: T) = this
fun bar() {
foo(C())
val c: I = C()
foo(<!NATIVE_INTERFACE_AS_REIFIED_TYPE_ARGUMENT!>c<!>)
foo<<!NATIVE_INTERFACE_AS_REIFIED_TYPE_ARGUMENT!>I<!>>(C())
C() + <!NATIVE_INTERFACE_AS_REIFIED_TYPE_ARGUMENT!>c<!>
}
@@ -0,0 +1,18 @@
package
public fun bar(): kotlin.Unit
public inline fun </*0*/ reified T> foo(/*0*/ x: T): kotlin.Unit
public operator inline fun </*0*/ reified T> C.plus(/*0*/ other: T): C
@kotlin.js.native() public final class C : I {
public constructor C()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@kotlin.js.native() public interface I {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -739,6 +739,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
doTest(fileName);
}
@TestMetadata("nativeInterfaceAsReifiedTypeArgument.kt")
public void testNativeInterfaceAsReifiedTypeArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/native/rtti/nativeInterfaceAsReifiedTypeArgument.kt");
doTest(fileName);
}
@TestMetadata("whenIsNativeInterface.kt")
public void testWhenIsNativeInterface() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/native/rtti/whenIsNativeInterface.kt");
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.js.resolve.diagnostics.JsCallChecker
import org.jetbrains.kotlin.js.resolve.diagnostics.JsNameChecker
import org.jetbrains.kotlin.js.resolve.diagnostics.JsNameClashChecker
import org.jetbrains.kotlin.js.resolve.diagnostics.JsNativeRttiChecker
import org.jetbrains.kotlin.js.resolve.diagnostics.JsReifiedNativeChecker
import org.jetbrains.kotlin.js.resolve.diagnostics.NativeInnerClassChecker
import org.jetbrains.kotlin.platform.PlatformToKotlinClassMap
import org.jetbrains.kotlin.resolve.IdentifierChecker
@@ -53,5 +54,6 @@ object JsPlatformConfigurator : PlatformConfigurator(
container.useInstance(JsNameClashChecker())
container.useImpl<JsReflectionAPICallChecker>()
container.useImpl<JsNativeRttiChecker>()
container.useImpl<JsReifiedNativeChecker>()
}
}
@@ -50,6 +50,7 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
put(ErrorsJs.JS_NAME_PROHIBITED_FOR_NAMED_NATIVE, "@JsName is prohibited for @native declaration with explicit name")
put(ErrorsJs.CANNOT_CHECK_FOR_NATIVE_INTERFACE, "Cannot check for native interface: {0}", RENDER_TYPE)
put(ErrorsJs.UNCHECKED_CAST_TO_NATIVE_INTERFACE, "Unchecked cast to native interface: {0} to {1}", RENDER_TYPE, RENDER_TYPE)
put(ErrorsJs.NATIVE_INTERFACE_AS_REIFIED_TYPE_ARGUMENT, "Cannot pass native interface {0} to reified type parameter", RENDER_TYPE)
this
}
@@ -54,6 +54,7 @@ public interface ErrorsJs {
DiagnosticFactory0<PsiElement> JS_NAME_PROHIBITED_FOR_NAMED_NATIVE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> CANNOT_CHECK_FOR_NATIVE_INTERFACE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<PsiElement, KotlinType, KotlinType> UNCHECKED_CAST_TO_NATIVE_INTERFACE = DiagnosticFactory2.create(WARNING);
DiagnosticFactory1<PsiElement, KotlinType> NATIVE_INTERFACE_AS_REIFIED_TYPE_ARGUMENT = DiagnosticFactory1.create(ERROR);
@SuppressWarnings("UnusedDeclaration")
Object _initializer = new Object() {
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.js.resolve.diagnostics
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.checkers.RttiExpressionChecker
import org.jetbrains.kotlin.resolve.calls.checkers.RttiExpressionInformation
import org.jetbrains.kotlin.resolve.calls.checkers.RttiOperation
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.resolve.diagnostics
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
class JsReifiedNativeChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
val typeArgumentList = resolvedCall.call.typeArgumentList?.arguments
for ((typeParam, typeArg) in resolvedCall.typeArguments) {
if (!typeParam.isReified) continue
val typeArgDescriptor = typeArg.constructor.declarationDescriptor
if (typeArgDescriptor != null && AnnotationsUtils.isNativeInterface(typeArgDescriptor)) {
val typeArgumentPsi = if (typeArgumentList != null) {
typeArgumentList[typeParam.index].typeReference
}
else {
resolvedCall.call.valueArguments[typeParam.index].getArgumentExpression()
}
context.trace.report(ErrorsJs.NATIVE_INTERFACE_AS_REIFIED_TYPE_ARGUMENT.on(typeArgumentPsi!!, typeArg))
}
}
}
}
@@ -5570,6 +5570,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("nativeClassAsReifiedTypeArgument.kt")
public void testNativeClassAsReifiedTypeArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/native/nativeClassAsReifiedTypeArgument.kt");
doTest(fileName);
}
@TestMetadata("nativeExtensionLikeMember.kt")
public void testNativeExtensionLikeMember() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/native/nativeExtensionLikeMember.kt");
@@ -0,0 +1,25 @@
//FILE: nativeClassAsReifiedTypeArgument.kt
var global = ""
inline fun <reified T> log(x: T) {
global += jsClass<T>().name + ": " + x
}
@native class C {
override fun toString() = noImpl
}
fun box(): String {
log(C())
if (global != "C: C instance") return "fail: $global"
return "OK"
}
//FILE: nativeClassAsReifiedTypeArgument.js
function C() {
}
C.prototype.toString = function() {
return "C instance"
}