avoid unnecessary null checks when receiver of '!!' or 'as' operator is not nullable
This commit is contained in:
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
|
|||||||
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
||||||
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
|
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
|
||||||
import org.jetbrains.jet.lang.resolve.constants.NullValue;
|
import org.jetbrains.jet.lang.resolve.constants.NullValue;
|
||||||
|
import org.jetbrains.jet.lang.types.JetType;
|
||||||
import org.jetbrains.jet.lexer.JetTokens;
|
import org.jetbrains.jet.lexer.JetTokens;
|
||||||
import org.jetbrains.k2js.translate.context.TemporaryVariable;
|
import org.jetbrains.k2js.translate.context.TemporaryVariable;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
@@ -360,10 +361,11 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
|
|||||||
if (expression.getOperationReference().getReferencedNameElementType() != JetTokens.AS_KEYWORD)
|
if (expression.getOperationReference().getReferencedNameElementType() != JetTokens.AS_KEYWORD)
|
||||||
return jsExpression.source(expression);
|
return jsExpression.source(expression);
|
||||||
|
|
||||||
JetTypeReference type = expression.getRight();
|
JetType rightType = BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.TYPE, expression.getRight());
|
||||||
assert type != null;
|
JetType leftType = BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.EXPRESSION_TYPE, expression.getLeft());
|
||||||
if (BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.TYPE, type).isNullable())
|
if (rightType.isNullable() || !leftType.isNullable()) {
|
||||||
return jsExpression.source(expression);
|
return jsExpression.source(expression);
|
||||||
|
}
|
||||||
|
|
||||||
// KT-2670
|
// KT-2670
|
||||||
// we actually do not care for types in js
|
// we actually do not care for types in js
|
||||||
|
|||||||
+9
-1
@@ -21,8 +21,12 @@ import com.google.dart.compiler.backend.js.ast.JsExpression;
|
|||||||
import com.intellij.psi.tree.IElementType;
|
import com.intellij.psi.tree.IElementType;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
|
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
|
||||||
|
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||||
import org.jetbrains.jet.lang.psi.JetUnaryExpression;
|
import org.jetbrains.jet.lang.psi.JetUnaryExpression;
|
||||||
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
|
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
||||||
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
||||||
|
import org.jetbrains.jet.lang.types.JetType;
|
||||||
import org.jetbrains.jet.lexer.JetTokens;
|
import org.jetbrains.jet.lexer.JetTokens;
|
||||||
import org.jetbrains.k2js.translate.callTranslator.CallTranslator;
|
import org.jetbrains.k2js.translate.callTranslator.CallTranslator;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
@@ -45,8 +49,12 @@ public final class UnaryOperationTranslator {
|
|||||||
) {
|
) {
|
||||||
IElementType operationToken = expression.getOperationReference().getReferencedNameElementType();
|
IElementType operationToken = expression.getOperationReference().getReferencedNameElementType();
|
||||||
if (operationToken == JetTokens.EXCLEXCL) {
|
if (operationToken == JetTokens.EXCLEXCL) {
|
||||||
return sure(translateAsExpression(getBaseExpression(expression), context), context);
|
JetExpression baseExpression = getBaseExpression(expression);
|
||||||
|
JetType type = BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.EXPRESSION_TYPE, baseExpression);
|
||||||
|
JsExpression translatedExpression = translateAsExpression(baseExpression, context);
|
||||||
|
return type.isNullable() ? sure(translatedExpression, context) : translatedExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IncrementTranslator.isIncrement(operationToken)) {
|
if (IncrementTranslator.isIncrement(operationToken)) {
|
||||||
return IncrementTranslator.translate(expression, context);
|
return IncrementTranslator.translate(expression, context);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package foo
|
|||||||
|
|
||||||
class A
|
class A
|
||||||
|
|
||||||
|
fun <T> Any.cast() = this!! as T
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
val a = null
|
val a = null
|
||||||
val s = a as A?
|
val s = a as A?
|
||||||
|
|||||||
Reference in New Issue
Block a user