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
@@ -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
)
@@ -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(
@@ -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;
}
}
@@ -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)
}
}
}
}