JS: prohibit dynamic on RHS of in and !in operators (see KT-6579)

This commit is contained in:
Alexey Andreev
2016-12-08 17:31:20 +03:00
parent fc6eaa015f
commit a2d45153ba
9 changed files with 34 additions and 73 deletions
@@ -9,10 +9,6 @@ public final fun div(/*0*/ p0: dynamic): dynamic
public final fun mod(/*0*/ p0: dynamic): dynamic
public final fun rangeTo(/*0*/ p0: dynamic): dynamic
public final fun and(/*0*/ p0: dynamic): dynamic
public final fun contains(/*0*/ p0: dynamic): dynamic
public final fun contains(/*0*/ p0: dynamic): dynamic
public final fun contains(/*0*/ p0: dynamic): dynamic
public final fun contains(/*0*/ p0: dynamic): dynamic
public final fun get(/*0*/ p0: dynamic): dynamic
public final fun set(/*0*/ p0: dynamic, /*1*/ p1: dynamic): dynamic
public final fun get(/*0*/ p0: dynamic): dynamic
@@ -20,12 +20,6 @@ fun test(d: dynamic) {
d <!DEBUG_INFO_DYNAMIC!>and<!> d
d <!DEBUG_INFO_DYNAMIC!>in<!> d
d <!DEBUG_INFO_DYNAMIC!>!in<!> d
1 <!DEBUG_INFO_DYNAMIC!>in<!> d
1 <!DEBUG_INFO_DYNAMIC!>!in<!> d
<!DEBUG_INFO_DYNAMIC!>d[1]<!>
<!DEBUG_INFO_DYNAMIC!>d[1]<!> = 2
@@ -0,0 +1,4 @@
public final fun contains(/*0*/ p0: dynamic): dynamic
public final fun contains(/*0*/ p0: dynamic): dynamic
public final fun contains(/*0*/ p0: dynamic): dynamic
public final fun contains(/*0*/ p0: dynamic): dynamic
@@ -0,0 +1,12 @@
fun foo() {
val a: dynamic = Any()
println(a in setOf(1, 2))
println(1 <!WRONG_OPERATION_WITH_DYNAMIC!>in<!> a)
println(1 <!WRONG_OPERATION_WITH_DYNAMIC!>!in<!> a)
when (2) {
<!WRONG_OPERATION_WITH_DYNAMIC!>in<!> a -> println("ok")
}
when (3) {
<!WRONG_OPERATION_WITH_DYNAMIC!>!in<!> a -> println("ok")
}
}
@@ -0,0 +1,3 @@
package
public fun foo(): kotlin.Unit
@@ -146,6 +146,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
doTest(fileName);
}
@TestMetadata("inExpression.kt")
public void testInExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/inExpression.kt");
doTest(fileName);
}
@TestMetadata("indexedAccess.kt")
public void testIndexedAccess() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/indexedAccess.kt");
@@ -18,9 +18,12 @@ package org.jetbrains.kotlin.js.resolve.diagnostics
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtArrayAccessExpression
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtWhenConditionInRange
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.types.isDynamic
object JsDynamicCallChecker : CallChecker {
@@ -32,5 +35,11 @@ object JsDynamicCallChecker : CallChecker {
if (element is KtArrayAccessExpression && element.indexExpressions.size > 1) {
context.trace.report(ErrorsJs.WRONG_OPERATION_WITH_DYNAMIC.on(reportOn, "indexed access with more than one index"))
}
if (element is KtBinaryExpression && element.operationToken in OperatorConventions.IN_OPERATIONS ||
element is KtWhenConditionInRange
) {
context.trace.report(ErrorsJs.WRONG_OPERATION_WITH_DYNAMIC.on(reportOn, "`in` operation"))
}
}
}
@@ -1328,12 +1328,6 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("in.kt")
public void testIn() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/dynamic/in.kt");
doTest(fileName);
}
@TestMetadata("incrementAndDecrement.kt")
public void testIncrementAndDecrement() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/dynamic/incrementAndDecrement.kt");
-57
View File
@@ -1,57 +0,0 @@
package foo
fun box(): String {
testTrue { "num" in bar }
testTrue { "str" in bar }
testTrue { "obj" in bar }
testTrue { 0 in bar }
testTrue { 5 in bar }
testTrue { 1 in bar }
testTrue { "name" in bar.obj }
testTrue { "name" in baz }
testTrue { "length" in baz }
testTrue { "0" in arr }
testTrue { 0 in arr }
testTrue { 2 in arr }
testTrue { 3 in arr }
testTrue { "length" in arr }
testFalse { "num" !in bar }
testFalse { "str" !in bar }
testFalse { "obj" !in bar }
testFalse { 0 !in bar }
testFalse { 5 !in bar }
testFalse { 1 !in bar }
testFalse { "name" !in bar.obj }
testFalse { "name" !in baz }
testFalse { "length" !in baz }
testFalse { "0" !in arr }
testFalse { 0 !in arr }
testFalse { 2 !in arr }
testFalse { 3 !in arr }
testFalse { "length" !in arr }
testFalse { "num0" in bar }
testFalse { "STR" in bar }
testFalse { 2 in bar }
testFalse { "foo" in bar.obj }
testFalse { 3 in baz }
testFalse { "function" in baz }
testFalse { "6" in arr }
testFalse { 7 in arr }
testFalse { 10 in arr }
testFalse { "name" in arr }
testTrue { "num0" !in bar }
testTrue { "STR" !in bar }
testTrue { 2 !in bar }
testTrue { "foo" !in bar.obj }
testTrue { 3 !in baz }
testTrue { "function" !in baz }
testTrue { "6" !in arr }
testTrue { 7 !in arr }
testTrue { 10 !in arr }
testTrue { "name" !in arr }
return "OK"
}