JS: prohibit is checks against native interfaces. Warn about casts to native interfaces. Fix #KT-14037, fix #KT-14038
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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.resolve.calls.checkers
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.resolve.BindingTrace
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
interface RttiExpressionChecker {
|
||||
fun check(rttiInformation: RttiExpressionInformation, reportOn: PsiElement, trace: BindingTrace)
|
||||
}
|
||||
|
||||
enum class RttiOperation {
|
||||
IS,
|
||||
NOT_IS,
|
||||
AS,
|
||||
SAFE_AS
|
||||
}
|
||||
|
||||
class RttiExpressionInformation(
|
||||
val subject: KtExpression,
|
||||
val sourceType: KotlinType?,
|
||||
val targetType: KotlinType?,
|
||||
val operation: RttiOperation
|
||||
)
|
||||
+14
-3
@@ -42,8 +42,7 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilsKt;
|
||||
import org.jetbrains.kotlin.resolve.calls.ArgumentTypeResolver;
|
||||
import org.jetbrains.kotlin.resolve.calls.CallExpressionResolver;
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt;
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker;
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext;
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.*;
|
||||
import org.jetbrains.kotlin.resolve.calls.model.DataFlowInfoForArgumentsImpl;
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCallImpl;
|
||||
@@ -315,7 +314,19 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
|
||||
}
|
||||
|
||||
KotlinType result = operationType == AS_SAFE ? TypeUtils.makeNullable(targetType) : targetType;
|
||||
return components.dataFlowAnalyzer.checkType(typeInfo.replaceType(result), expression, context);
|
||||
KotlinTypeInfo resultTypeInfo = components.dataFlowAnalyzer.checkType(typeInfo.replaceType(result), expression, context);
|
||||
|
||||
RttiExpressionInformation rttiInformation = new RttiExpressionInformation(
|
||||
expression.getLeft(),
|
||||
subjectType,
|
||||
result,
|
||||
operationType == AS_SAFE ? RttiOperation.SAFE_AS : RttiOperation.AS
|
||||
);
|
||||
for (RttiExpressionChecker checker : components.rttiExpressionCheckers) {
|
||||
checker.check(rttiInformation, expression, context.trace);
|
||||
}
|
||||
|
||||
return resultTypeInfo;
|
||||
}
|
||||
|
||||
private void checkBinaryWithTypeRHS(
|
||||
|
||||
+7
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.resolve.*;
|
||||
import org.jetbrains.kotlin.resolve.calls.CallExpressionResolver;
|
||||
import org.jetbrains.kotlin.resolve.calls.CallResolver;
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker;
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.RttiExpressionChecker;
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -58,6 +59,7 @@ public class ExpressionTypingComponents {
|
||||
/*package*/ LookupTracker lookupTracker;
|
||||
/*package*/ OverloadChecker overloadChecker;
|
||||
/*package*/ LanguageVersionSettings languageVersionSettings;
|
||||
/*package*/ Iterable<RttiExpressionChecker> rttiExpressionCheckers;
|
||||
|
||||
@Inject
|
||||
public void setGlobalContext(@NotNull GlobalContext globalContext) {
|
||||
@@ -193,4 +195,9 @@ public class ExpressionTypingComponents {
|
||||
public void setLanguageVersionSettings(@NotNull LanguageVersionSettings languageVersionSettings) {
|
||||
this.languageVersionSettings = languageVersionSettings;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setRttiExpressionCheckers(@NotNull Iterable<RttiExpressionChecker> rttiExpressionCheckers) {
|
||||
this.rttiExpressionCheckers = rttiExpressionCheckers;
|
||||
}
|
||||
}
|
||||
|
||||
+31
-1
@@ -26,6 +26,8 @@ import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.*
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.*
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.RttiExpressionInformation
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.RttiOperation
|
||||
import org.jetbrains.kotlin.resolve.calls.context.ContextDependency.INDEPENDENT
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue
|
||||
@@ -53,7 +55,23 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping
|
||||
val newDataFlowInfo = conditionInfo.and(typeInfo.dataFlowInfo)
|
||||
context.trace.record(BindingContext.DATAFLOW_INFO_AFTER_CONDITION, expression, newDataFlowInfo)
|
||||
}
|
||||
return components.dataFlowAnalyzer.checkType(typeInfo.replaceType(components.builtIns.booleanType), expression, contextWithExpectedType)
|
||||
|
||||
val resultTypeInfo = components.dataFlowAnalyzer.checkType(typeInfo.replaceType(components.builtIns.booleanType), expression, contextWithExpectedType)
|
||||
|
||||
if (typeReference != null) {
|
||||
val rhsType = context.trace[BindingContext.TYPE, typeReference]
|
||||
val rttiInformation = RttiExpressionInformation(
|
||||
subject = leftHandSide,
|
||||
sourceType = knownType,
|
||||
targetType = rhsType,
|
||||
operation = RttiOperation.IS
|
||||
)
|
||||
components.rttiExpressionCheckers.forEach {
|
||||
it.check(rttiInformation, expression, context.trace)
|
||||
}
|
||||
}
|
||||
|
||||
return resultTypeInfo
|
||||
}
|
||||
|
||||
override fun visitWhenExpression(expression: KtWhenExpression, context: ExpressionTypingContext) =
|
||||
@@ -314,6 +332,18 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping
|
||||
else {
|
||||
newDataFlowInfo = result
|
||||
}
|
||||
val rhsType = context.trace[BindingContext.TYPE, typeReference]
|
||||
if (subjectExpression != null) {
|
||||
val rttiInformation = RttiExpressionInformation(
|
||||
subject = subjectExpression,
|
||||
sourceType = subjectType,
|
||||
targetType = rhsType,
|
||||
operation = if (condition.isNegated) RttiOperation.NOT_IS else RttiOperation.IS
|
||||
)
|
||||
components.rttiExpressionCheckers.forEach {
|
||||
it.check(rttiInformation, condition, context.trace)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
@native interface I
|
||||
|
||||
fun box(a: Any, b: Any): Pair<I, I?> {
|
||||
return Pair(<!UNCHECKED_CAST_TO_NATIVE_INTERFACE!>a as I<!>, <!UNCHECKED_CAST_TO_NATIVE_INTERFACE!>b as? I<!>)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package
|
||||
|
||||
public fun box(/*0*/ a: kotlin.Any, /*1*/ b: kotlin.Any): kotlin.Pair<I, I?>
|
||||
|
||||
@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
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
@native interface I
|
||||
|
||||
fun box(a: Any, b: Any): Boolean {
|
||||
return <!CANNOT_CHECK_FOR_NATIVE_INTERFACE!>a is I<!> && <!CANNOT_CHECK_FOR_NATIVE_INTERFACE!>b !is I<!>
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package
|
||||
|
||||
public fun box(/*0*/ a: kotlin.Any, /*1*/ b: kotlin.Any): kotlin.Boolean
|
||||
|
||||
@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
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
@native interface I
|
||||
|
||||
@native interface J
|
||||
|
||||
fun box(a: Any) = when (a) {
|
||||
<!CANNOT_CHECK_FOR_NATIVE_INTERFACE!>is I<!> -> 0
|
||||
<!CANNOT_CHECK_FOR_NATIVE_INTERFACE!>!is J<!> -> 1
|
||||
else -> 2
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package
|
||||
|
||||
public fun box(/*0*/ a: kotlin.Any): kotlin.Int
|
||||
|
||||
@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
|
||||
}
|
||||
|
||||
@kotlin.js.native() public interface J {
|
||||
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
|
||||
}
|
||||
+27
@@ -719,6 +719,33 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/testsWithJsStdLib/native/rtti")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@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);
|
||||
}
|
||||
|
||||
@TestMetadata("castToNativeInterface.kt")
|
||||
public void testCastToNativeInterface() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/native/rtti/castToNativeInterface.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("checkForNativeInterface.kt")
|
||||
public void testCheckForNativeInterface() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/native/rtti/checkForNativeInterface.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("whenIsNativeInterface.kt")
|
||||
public void testWhenIsNativeInterface() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/native/rtti/whenIsNativeInterface.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/testsWithJsStdLib/native/unusedParam")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user