Refactoring: TypeInfoFactory.createTypeInfo() without type -> noTypeInfo(), getNotNullType -> getTypeNotNull, nullability refined, style fixes

This commit is contained in:
Mikhail Glukhikh
2015-04-21 18:26:26 +03:00
parent 3e1c3e9dfe
commit b40fe9d5ba
18 changed files with 78 additions and 71 deletions
@@ -82,7 +82,7 @@ public class BindingContextUtils {
} }
@NotNull @NotNull
public static JetType getNotNullType( public static JetType getTypeNotNull(
@NotNull BindingContext bindingContext, @NotNull BindingContext bindingContext,
@NotNull JetExpression expression @NotNull JetExpression expression
) { ) {
@@ -160,7 +160,7 @@ public class BindingContextUtils {
// NB: should never return null if expression is already processed // NB: should never return null if expression is already processed
if (!Boolean.TRUE.equals(context.get(BindingContext.PROCESSED, expression))) return null; if (!Boolean.TRUE.equals(context.get(BindingContext.PROCESSED, expression))) return null;
JetTypeInfo result = context.get(BindingContext.EXPRESSION_TYPE_INFO, expression); JetTypeInfo result = context.get(BindingContext.EXPRESSION_TYPE_INFO, expression);
return result != null ? result : TypeInfoFactoryPackage.createTypeInfo(DataFlowInfo.EMPTY); return result != null ? result : TypeInfoFactoryPackage.noTypeInfo(DataFlowInfo.EMPTY);
} }
public static boolean isExpressionWithValidReference( public static boolean isExpressionWithValidReference(
@@ -31,7 +31,7 @@ import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.createTypeInfo import org.jetbrains.kotlin.types.expressions.typeInfoFactory.noTypeInfo
public fun JetReturnExpression.getTargetFunctionDescriptor(context: BindingContext): FunctionDescriptor? { public fun JetReturnExpression.getTargetFunctionDescriptor(context: BindingContext): FunctionDescriptor? {
val targetLabel = getTargetLabel() val targetLabel = getTargetLabel()
@@ -63,7 +63,8 @@ public fun <C : ResolutionContext<C>> ResolutionContext<C>.recordScopeAndDataFlo
trace.record(BindingContext.EXPRESSION_TYPE_INFO, expression, typeInfo.replaceDataFlowInfo(dataFlowInfo)) trace.record(BindingContext.EXPRESSION_TYPE_INFO, expression, typeInfo.replaceDataFlowInfo(dataFlowInfo))
} }
else if (dataFlowInfo != DataFlowInfo.EMPTY) { else if (dataFlowInfo != DataFlowInfo.EMPTY) {
trace.record(BindingContext.EXPRESSION_TYPE_INFO, expression, createTypeInfo(dataFlowInfo)) // Don't store anything in BindingTrace if it's simply an empty DataFlowInfo
trace.record(BindingContext.EXPRESSION_TYPE_INFO, expression, noTypeInfo(dataFlowInfo))
} }
} }
@@ -192,7 +192,7 @@ public class ArgumentTypeResolver {
@NotNull ResolveArgumentsMode resolveArgumentsMode @NotNull ResolveArgumentsMode resolveArgumentsMode
) { ) {
if (expression == null) { if (expression == null) {
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
if (isFunctionLiteralArgument(expression, context)) { if (isFunctionLiteralArgument(expression, context)) {
return getFunctionLiteralTypeInfo(expression, getFunctionLiteralArgument(expression, context), context, resolveArgumentsMode); return getFunctionLiteralTypeInfo(expression, getFunctionLiteralArgument(expression, context), context, resolveArgumentsMode);
@@ -158,7 +158,7 @@ public class CallExpressionResolver {
} }
temporaryForVariable.commit(); temporaryForVariable.commit();
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
@NotNull @NotNull
@@ -201,7 +201,7 @@ public class CallExpressionResolver {
context.trace.report(FUNCTION_CALL_EXPECTED.on(callExpression, callExpression, hasValueParameters)); context.trace.report(FUNCTION_CALL_EXPECTED.on(callExpression, callExpression, hasValueParameters));
} }
if (functionDescriptor == null) { if (functionDescriptor == null) {
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
if (functionDescriptor instanceof ConstructorDescriptor && if (functionDescriptor instanceof ConstructorDescriptor &&
DescriptorUtils.isAnnotationClass(functionDescriptor.getContainingDeclaration())) { DescriptorUtils.isAnnotationClass(functionDescriptor.getContainingDeclaration())) {
@@ -226,11 +226,11 @@ public class CallExpressionResolver {
temporaryForVariable.commit(); temporaryForVariable.commit();
context.trace.report(FUNCTION_EXPECTED.on(calleeExpression, calleeExpression, context.trace.report(FUNCTION_EXPECTED.on(calleeExpression, calleeExpression,
type != null ? type : ErrorUtils.createErrorType(""))); type != null ? type : ErrorUtils.createErrorType("")));
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
} }
temporaryForFunction.commit(); temporaryForFunction.commit();
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
private static boolean canInstantiateAnnotationClass(@NotNull JetCallExpression expression) { private static boolean canInstantiateAnnotationClass(@NotNull JetCallExpression expression) {
@@ -265,7 +265,7 @@ public class CallExpressionResolver {
else if (selectorExpression != null) { else if (selectorExpression != null) {
context.trace.report(ILLEGAL_SELECTOR.on(selectorExpression, selectorExpression.getText())); context.trace.report(ILLEGAL_SELECTOR.on(selectorExpression, selectorExpression.getText()));
} }
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
/** /**
@@ -66,7 +66,7 @@ public fun <D : CallableDescriptor> ResolvedCall<D>.getParameterForArgument(valu
// call // call
public fun <C: ResolutionContext<C>> Call.hasUnresolvedArguments(context: ResolutionContext<C>): Boolean { public fun <C: ResolutionContext<C>> Call.hasUnresolvedArguments(context: ResolutionContext<C>): Boolean {
val arguments = getValueArguments().map { it?.getArgumentExpression() } val arguments = getValueArguments().map { it.getArgumentExpression() }
return arguments.any { return arguments.any {
argument -> argument ->
val expressionType = argument?.let { context.trace.getBindingContext().getType(it) } val expressionType = argument?.let { context.trace.getBindingContext().getType(it) }
@@ -135,7 +135,7 @@ public fun JetElement.getCall(context: BindingContext): Call? {
} }
public fun JetElement.getParentCall(context: BindingContext, strict: Boolean = true): Call? { public fun JetElement.getParentCall(context: BindingContext, strict: Boolean = true): Call? {
val callExpressionTypes = array<Class<out JetElement>?>( val callExpressionTypes = arrayOf<Class<out JetElement>?>(
javaClass<JetSimpleNameExpression>(), javaClass<JetCallElement>(), javaClass<JetBinaryExpression>(), javaClass<JetSimpleNameExpression>(), javaClass<JetCallElement>(), javaClass<JetBinaryExpression>(),
javaClass<JetUnaryExpression>(), javaClass<JetArrayAccessExpression>()) javaClass<JetUnaryExpression>(), javaClass<JetArrayAccessExpression>())
@@ -111,7 +111,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
public JetTypeInfo visitParenthesizedExpression(@NotNull JetParenthesizedExpression expression, ExpressionTypingContext context) { public JetTypeInfo visitParenthesizedExpression(@NotNull JetParenthesizedExpression expression, ExpressionTypingContext context) {
JetExpression innerExpression = expression.getExpression(); JetExpression innerExpression = expression.getExpression();
if (innerExpression == null) { if (innerExpression == null) {
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
return facade.getTypeInfo(innerExpression, context.replaceScope(context.scope)); return facade.getTypeInfo(innerExpression, context.replaceScope(context.scope));
} }
@@ -287,7 +287,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
if (superTypeQualifier != null) { if (superTypeQualifier != null) {
components.expressionTypingServices.getTypeResolver().resolveType(context.scope, superTypeQualifier, context.trace, true); components.expressionTypingServices.getTypeResolver().resolveType(context.scope, superTypeQualifier, context.trace, true);
} }
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
private JetType checkPossiblyQualifiedSuper( private JetType checkPossiblyQualifiedSuper(
@@ -812,7 +812,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
: contextWithExpectedType.replaceContextDependency(INDEPENDENT).replaceExpectedType(NO_EXPECTED_TYPE); : contextWithExpectedType.replaceContextDependency(INDEPENDENT).replaceExpectedType(NO_EXPECTED_TYPE);
JetExpression baseExpression = expression.getBaseExpression(); JetExpression baseExpression = expression.getBaseExpression();
if (baseExpression == null) return TypeInfoFactoryPackage.createTypeInfo(context); if (baseExpression == null) return TypeInfoFactoryPackage.noTypeInfo(context);
JetSimpleNameExpression operationSign = expression.getOperationReference(); JetSimpleNameExpression operationSign = expression.getOperationReference();
@@ -953,7 +953,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
boolean isStatement boolean isStatement
) { ) {
JetExpression baseExpression = expression.getBaseExpression(); JetExpression baseExpression = expression.getBaseExpression();
if (baseExpression == null) return TypeInfoFactoryPackage.createTypeInfo(context); if (baseExpression == null) return TypeInfoFactoryPackage.noTypeInfo(context);
return facade.getTypeInfo(baseExpression, context, isStatement); return facade.getTypeInfo(baseExpression, context, isStatement);
} }
@@ -1079,7 +1079,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
} }
else { else {
context.trace.report(UNSUPPORTED.on(operationSign, "Unknown operation")); context.trace.report(UNSUPPORTED.on(operationSign, "Unknown operation"));
result = TypeInfoFactoryPackage.createTypeInfo(context); result = TypeInfoFactoryPackage.noTypeInfo(context);
} }
CompileTimeConstant<?> value = ConstantExpressionEvaluator.evaluate( CompileTimeConstant<?> value = ConstantExpressionEvaluator.evaluate(
expression, contextWithExpectedType.trace, contextWithExpectedType.expectedType expression, contextWithExpectedType.trace, contextWithExpectedType.expectedType
@@ -1222,7 +1222,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
if (left == null || right == null) { if (left == null || right == null) {
getTypeInfoOrNullType(left, context, facade); getTypeInfoOrNullType(left, context, facade);
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
Call call = createCallForSpecialConstruction(expression, expression.getOperationReference(), Lists.newArrayList(left, right)); Call call = createCallForSpecialConstruction(expression, expression.getOperationReference(), Lists.newArrayList(left, right));
@@ -1245,7 +1245,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
dataFlowInfo = dataFlowInfo.disequate(value, DataFlowValue.NULL); dataFlowInfo = dataFlowInfo.disequate(value, DataFlowValue.NULL);
} }
JetType type = resolvedCall.getResultingDescriptor().getReturnType(); JetType type = resolvedCall.getResultingDescriptor().getReturnType();
if (type == null || rightType == null) return TypeInfoFactoryPackage.createTypeInfo(dataFlowInfo); if (type == null || rightType == null) return TypeInfoFactoryPackage.noTypeInfo(dataFlowInfo);
// Sometimes return type for special call for elvis operator might be nullable, // Sometimes return type for special call for elvis operator might be nullable,
// but result is not nullable if the right type is not nullable // but result is not nullable if the right type is not nullable
@@ -1255,6 +1255,8 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
if (context.contextDependency == DEPENDENT) { if (context.contextDependency == DEPENDENT) {
return TypeInfoFactoryPackage.createTypeInfo(type, dataFlowInfo); return TypeInfoFactoryPackage.createTypeInfo(type, dataFlowInfo);
} }
// If break or continue was possible, take condition check info as the jump info
return TypeInfoFactoryPackage.createTypeInfo(DataFlowUtils.checkType(type, expression, context), return TypeInfoFactoryPackage.createTypeInfo(DataFlowUtils.checkType(type, expression, context),
dataFlowInfo, dataFlowInfo,
loopBreakContinuePossible, loopBreakContinuePossible,
@@ -1273,7 +1275,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
ExpressionTypingContext contextWithNoExpectedType = context.replaceExpectedType(NO_EXPECTED_TYPE); ExpressionTypingContext contextWithNoExpectedType = context.replaceExpectedType(NO_EXPECTED_TYPE);
if (right == null) { if (right == null) {
if (left != null) facade.getTypeInfo(left, contextWithNoExpectedType); if (left != null) facade.getTypeInfo(left, contextWithNoExpectedType);
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
JetTypeInfo rightTypeInfo = facade.getTypeInfo(right, contextWithNoExpectedType); JetTypeInfo rightTypeInfo = facade.getTypeInfo(right, contextWithNoExpectedType);
@@ -1350,7 +1352,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
private JetTypeInfo assignmentIsNotAnExpressionError(JetBinaryExpression expression, ExpressionTypingContext context) { private JetTypeInfo assignmentIsNotAnExpressionError(JetBinaryExpression expression, ExpressionTypingContext context) {
facade.checkStatementType(expression, context); facade.checkStatementType(expression, context);
context.trace.report(ASSIGNMENT_IN_EXPRESSION_CONTEXT.on(expression)); context.trace.report(ASSIGNMENT_IN_EXPRESSION_CONTEXT.on(expression));
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
@Override @Override
@@ -1370,7 +1372,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
//left here is a receiver, so it doesn't depend on expected type //left here is a receiver, so it doesn't depend on expected type
typeInfo = facade.getTypeInfo(left, context.replaceContextDependency(INDEPENDENT).replaceExpectedType(NO_EXPECTED_TYPE)); typeInfo = facade.getTypeInfo(left, context.replaceContextDependency(INDEPENDENT).replaceExpectedType(NO_EXPECTED_TYPE));
} else { } else {
typeInfo = TypeInfoFactoryPackage.createTypeInfo(context); typeInfo = TypeInfoFactoryPackage.noTypeInfo(context);
} }
ExpressionTypingContext contextWithDataFlow = context.replaceDataFlowInfo(typeInfo.getDataFlowInfo()); ExpressionTypingContext contextWithDataFlow = context.replaceDataFlowInfo(typeInfo.getDataFlowInfo());
@@ -1396,7 +1398,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
@Override @Override
public JetTypeInfo visitDeclaration(@NotNull JetDeclaration dcl, ExpressionTypingContext context) { public JetTypeInfo visitDeclaration(@NotNull JetDeclaration dcl, ExpressionTypingContext context) {
context.trace.report(DECLARATION_IN_ILLEGAL_CONTEXT.on(dcl)); context.trace.report(DECLARATION_IN_ILLEGAL_CONTEXT.on(dcl));
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
@Override @Override
@@ -1404,10 +1406,9 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
if (!JetPsiUtil.isLHSOfDot(expression)) { if (!JetPsiUtil.isLHSOfDot(expression)) {
context.trace.report(PACKAGE_IS_NOT_AN_EXPRESSION.on(expression)); context.trace.report(PACKAGE_IS_NOT_AN_EXPRESSION.on(expression));
} }
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
private class StringTemplateVisitor extends JetVisitorVoid { private class StringTemplateVisitor extends JetVisitorVoid {
final ExpressionTypingContext context; final ExpressionTypingContext context;
@@ -1416,7 +1417,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
StringTemplateVisitor(ExpressionTypingContext context) { StringTemplateVisitor(ExpressionTypingContext context) {
this.context = context; this.context = context;
this.typeInfo = TypeInfoFactoryPackage.createTypeInfo(context); this.typeInfo = TypeInfoFactoryPackage.noTypeInfo(context);
} }
@Override @Override
@@ -1460,7 +1461,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
JetExpression baseExpression = expression.getBaseExpression(); JetExpression baseExpression = expression.getBaseExpression();
if (baseExpression == null) { if (baseExpression == null) {
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
return facade.getTypeInfo(baseExpression, context, isStatement); return facade.getTypeInfo(baseExpression, context, isStatement);
} }
@@ -1468,7 +1469,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
@Override @Override
public JetTypeInfo visitJetElement(@NotNull JetElement element, ExpressionTypingContext context) { public JetTypeInfo visitJetElement(@NotNull JetElement element, ExpressionTypingContext context) {
context.trace.report(UNSUPPORTED.on(element, getClass().getCanonicalName())); context.trace.report(UNSUPPORTED.on(element, getClass().getCanonicalName()));
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
@NotNull @NotNull
@@ -1488,7 +1489,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
@NotNull BindingTrace traceForResolveResult, @NotNull BindingTrace traceForResolveResult,
boolean isGet) { boolean isGet) {
JetExpression arrayExpression = arrayAccessExpression.getArrayExpression(); JetExpression arrayExpression = arrayAccessExpression.getArrayExpression();
if (arrayExpression == null) return TypeInfoFactoryPackage.createTypeInfo(oldContext); if (arrayExpression == null) return TypeInfoFactoryPackage.noTypeInfo(oldContext);
JetTypeInfo arrayTypeInfo = facade.safeGetTypeInfo(arrayExpression, oldContext.replaceExpectedType(NO_EXPECTED_TYPE) JetTypeInfo arrayTypeInfo = facade.safeGetTypeInfo(arrayExpression, oldContext.replaceExpectedType(NO_EXPECTED_TYPE)
@@ -111,8 +111,8 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
: result; : result;
} }
return TypeInfoFactoryPackage.createTypeInfo(DataFlowUtils.checkImplicitCast( return TypeInfoFactoryPackage.createTypeInfo(DataFlowUtils.checkImplicitCast(
components.builtIns.getUnitType(), ifExpression, components.builtIns.getUnitType(), ifExpression,
contextWithExpectedType, isStatement contextWithExpectedType, isStatement
), ),
thenInfo.or(elseInfo) thenInfo.or(elseInfo)
); );
@@ -221,8 +221,9 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
bodyTypeInfo = components.expressionTypingServices.getBlockReturnedTypeWithWritableScope( bodyTypeInfo = components.expressionTypingServices.getBlockReturnedTypeWithWritableScope(
scopeToExtend, Collections.singletonList(body), scopeToExtend, Collections.singletonList(body),
CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(conditionInfo)); CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(conditionInfo));
} else { }
bodyTypeInfo = TypeInfoFactoryPackage.createTypeInfo(conditionInfo); else {
bodyTypeInfo = TypeInfoFactoryPackage.noTypeInfo(conditionInfo);
} }
// Condition is false at this point only if there is no jumps outside // Condition is false at this point only if there is no jumps outside
@@ -331,7 +332,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
writableScope, block, CoercionStrategy.NO_COERCION, context); writableScope, block, CoercionStrategy.NO_COERCION, context);
} }
else { else {
bodyTypeInfo = TypeInfoFactoryPackage.createTypeInfo(context); bodyTypeInfo = TypeInfoFactoryPackage.noTypeInfo(context);
} }
JetExpression condition = expression.getCondition(); JetExpression condition = expression.getCondition();
DataFlowInfo conditionDataFlowInfo = checkCondition(conditionScope, condition, context); DataFlowInfo conditionDataFlowInfo = checkCondition(conditionScope, condition, context);
@@ -380,7 +381,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
} }
} }
else { else {
loopRangeInfo = TypeInfoFactoryPackage.createTypeInfo(context); loopRangeInfo = TypeInfoFactoryPackage.noTypeInfo(context);
} }
WritableScope loopScope = newWritableScopeImpl(context, "Scope with for-loop index"); WritableScope loopScope = newWritableScopeImpl(context, "Scope with for-loop index");
@@ -480,7 +481,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
} }
} }
JetTypeInfo result = TypeInfoFactoryPackage.createTypeInfo(context); JetTypeInfo result = TypeInfoFactoryPackage.noTypeInfo(context);
if (finallyBlock != null) { if (finallyBlock != null) {
result = facade.getTypeInfo(finallyBlock.getFinalExpression(), result = facade.getTypeInfo(finallyBlock.getFinalExpression(),
context.replaceExpectedType(NO_EXPECTED_TYPE)); context.replaceExpectedType(NO_EXPECTED_TYPE));
@@ -225,7 +225,7 @@ public class DataFlowUtils {
facade.checkStatementType( facade.checkStatementType(
expression, context.replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE).replaceContextDependency(INDEPENDENT)); expression, context.replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE).replaceContextDependency(INDEPENDENT));
context.trace.report(EXPRESSION_EXPECTED.on(expression, expression)); context.trace.report(EXPRESSION_EXPECTED.on(expression, expression));
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
@NotNull @NotNull
@@ -315,13 +315,13 @@ public class ExpressionTypingServices {
@NotNull ExpressionTypingContext context @NotNull ExpressionTypingContext context
) { ) {
if (block.isEmpty()) { if (block.isEmpty()) {
return new JetTypeInfo(builtIns.getUnitType(), context.dataFlowInfo, false, context.dataFlowInfo); return TypeInfoFactoryPackage.createTypeInfo(builtIns.getUnitType(), context);
} }
ExpressionTypingInternals blockLevelVisitor = ExpressionTypingVisitorDispatcher.createForBlock(expressionTypingComponents, scope); ExpressionTypingInternals blockLevelVisitor = ExpressionTypingVisitorDispatcher.createForBlock(expressionTypingComponents, scope);
ExpressionTypingContext newContext = context.replaceScope(scope).replaceExpectedType(NO_EXPECTED_TYPE); ExpressionTypingContext newContext = context.replaceScope(scope).replaceExpectedType(NO_EXPECTED_TYPE);
JetTypeInfo result = TypeInfoFactoryPackage.createTypeInfo(context); JetTypeInfo result = TypeInfoFactoryPackage.noTypeInfo(context);
// Jump point data flow info // Jump point data flow info
DataFlowInfo beforeJumpInfo = newContext.dataFlowInfo; DataFlowInfo beforeJumpInfo = newContext.dataFlowInfo;
boolean jumpOutPossible = false; boolean jumpOutPossible = false;
@@ -336,7 +336,7 @@ public class ExpressionTypingUtils {
) { ) {
return expression != null return expression != null
? facade.getTypeInfo(expression, context) ? facade.getTypeInfo(expression, context)
: TypeInfoFactoryPackage.createTypeInfo(context); : TypeInfoFactoryPackage.noTypeInfo(context);
} }
@SuppressWarnings("SuspiciousMethodCalls") @SuppressWarnings("SuspiciousMethodCalls")
@@ -99,8 +99,9 @@ public class ExpressionTypingVisitorDispatcher extends JetVisitor<JetTypeInfo, E
if (typeInfo.getType() != null) { if (typeInfo.getType() != null) {
return typeInfo; return typeInfo;
} }
return typeInfo.replaceType(ErrorUtils.createErrorType("Type for " + expression.getText())).replaceDataFlowInfo( return typeInfo
context.dataFlowInfo); .replaceType(ErrorUtils.createErrorType("Type for " + expression.getText()))
.replaceDataFlowInfo(context.dataFlowInfo);
} }
@Override @Override
@@ -131,7 +132,7 @@ public class ExpressionTypingVisitorDispatcher extends JetVisitor<JetTypeInfo, E
} }
@NotNull @NotNull
static private JetTypeInfo getTypeInfo(@NotNull JetExpression expression, ExpressionTypingContext context, JetVisitor<JetTypeInfo, ExpressionTypingContext> visitor) { private static JetTypeInfo getTypeInfo(@NotNull JetExpression expression, ExpressionTypingContext context, JetVisitor<JetTypeInfo, ExpressionTypingContext> visitor) {
try { try {
JetTypeInfo recordedTypeInfo = BindingContextUtils.getRecordedTypeInfo(expression, context.trace.getBindingContext()); JetTypeInfo recordedTypeInfo = BindingContextUtils.getRecordedTypeInfo(expression, context.trace.getBindingContext());
if (recordedTypeInfo != null) { if (recordedTypeInfo != null) {
@@ -141,7 +142,8 @@ public class ExpressionTypingVisitorDispatcher extends JetVisitor<JetTypeInfo, E
try { try {
result = expression.accept(visitor, context); result = expression.accept(visitor, context);
// Some recursive definitions (object expressions) must put their types in the cache manually: // Some recursive definitions (object expressions) must put their types in the cache manually:
if (Boolean.TRUE.equals(context.trace.get(BindingContext.PROCESSED, expression))) { //noinspection ConstantConditions
if (context.trace.get(BindingContext.PROCESSED, expression)) {
JetType type = context.trace.getBindingContext().getType(expression); JetType type = context.trace.getBindingContext().getType(expression);
return result.replaceType(type); return result.replaceType(type);
} }
@@ -153,7 +155,7 @@ public class ExpressionTypingVisitorDispatcher extends JetVisitor<JetTypeInfo, E
} }
catch (ReenteringLazyValueComputationException e) { catch (ReenteringLazyValueComputationException e) {
context.trace.report(TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM.on(expression)); context.trace.report(TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM.on(expression));
result = TypeInfoFactoryPackage.createTypeInfo(context); result = TypeInfoFactoryPackage.noTypeInfo(context);
} }
context.trace.record(BindingContext.PROCESSED, expression); context.trace.record(BindingContext.PROCESSED, expression);
@@ -155,7 +155,7 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
} }
} }
else { else {
typeInfo = TypeInfoFactoryPackage.createTypeInfo(context); typeInfo = TypeInfoFactoryPackage.noTypeInfo(context);
} }
{ {
@@ -176,13 +176,13 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
JetExpression initializer = multiDeclaration.getInitializer(); JetExpression initializer = multiDeclaration.getInitializer();
if (initializer == null) { if (initializer == null) {
context.trace.report(INITIALIZER_REQUIRED_FOR_MULTIDECLARATION.on(multiDeclaration)); context.trace.report(INITIALIZER_REQUIRED_FOR_MULTIDECLARATION.on(multiDeclaration));
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
ExpressionReceiver expressionReceiver = ExpressionTypingUtils.getExpressionReceiver( ExpressionReceiver expressionReceiver = ExpressionTypingUtils.getExpressionReceiver(
facade, initializer, context.replaceExpectedType(NO_EXPECTED_TYPE).replaceContextDependency(INDEPENDENT)); facade, initializer, context.replaceExpectedType(NO_EXPECTED_TYPE).replaceContextDependency(INDEPENDENT));
JetTypeInfo typeInfo = facade.getTypeInfo(initializer, context); JetTypeInfo typeInfo = facade.getTypeInfo(initializer, context);
if (expressionReceiver == null) { if (expressionReceiver == null) {
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
components.expressionTypingUtils.defineLocalVariablesFromMultiDeclaration(scope, multiDeclaration, expressionReceiver, initializer, context); components.expressionTypingUtils.defineLocalVariablesFromMultiDeclaration(scope, multiDeclaration, expressionReceiver, initializer, context);
return typeInfo.replaceType(DataFlowUtils.checkStatementType(multiDeclaration, context)); return typeInfo.replaceType(DataFlowUtils.checkStatementType(multiDeclaration, context));
@@ -339,7 +339,7 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
JetExpression right = expression.getRight(); JetExpression right = expression.getRight();
if (left instanceof JetArrayAccessExpression) { if (left instanceof JetArrayAccessExpression) {
JetArrayAccessExpression arrayAccessExpression = (JetArrayAccessExpression) left; JetArrayAccessExpression arrayAccessExpression = (JetArrayAccessExpression) left;
if (right == null) return TypeInfoFactoryPackage.createTypeInfo(context); if (right == null) return TypeInfoFactoryPackage.noTypeInfo(context);
JetTypeInfo typeInfo = basic.resolveArrayAccessSetMethod(arrayAccessExpression, right, context, context.trace); JetTypeInfo typeInfo = basic.resolveArrayAccessSetMethod(arrayAccessExpression, right, context, context.trace);
basic.checkLValue(context.trace, context, arrayAccessExpression, right); basic.checkLValue(context.trace, context, arrayAccessExpression, right);
return typeInfo.replaceType(checkAssignmentType(typeInfo.getType(), expression, contextWithExpectedType)); return typeInfo.replaceType(checkAssignmentType(typeInfo.getType(), expression, contextWithExpectedType));
@@ -347,25 +347,25 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
JetTypeInfo leftInfo = ExpressionTypingUtils.getTypeInfoOrNullType(left, context, facade); JetTypeInfo leftInfo = ExpressionTypingUtils.getTypeInfoOrNullType(left, context, facade);
JetType leftType = leftInfo.getType(); JetType leftType = leftInfo.getType();
DataFlowInfo dataFlowInfo = leftInfo.getDataFlowInfo(); DataFlowInfo dataFlowInfo = leftInfo.getDataFlowInfo();
JetTypeInfo rightInfo; JetTypeInfo resultInfo;
if (right != null) { if (right != null) {
rightInfo = facade.getTypeInfo(right, context.replaceDataFlowInfo(dataFlowInfo).replaceExpectedType(leftType)); resultInfo = facade.getTypeInfo(right, context.replaceDataFlowInfo(dataFlowInfo).replaceExpectedType(leftType));
dataFlowInfo = rightInfo.getDataFlowInfo(); dataFlowInfo = resultInfo.getDataFlowInfo();
JetType rightType = rightInfo.getType(); JetType rightType = resultInfo.getType();
if (left != null && leftType != null && rightType != null) { if (left != null && leftType != null && rightType != null) {
DataFlowValue leftValue = DataFlowValueFactory.createDataFlowValue(left, leftType, context); DataFlowValue leftValue = DataFlowValueFactory.createDataFlowValue(left, leftType, context);
DataFlowValue rightValue = DataFlowValueFactory.createDataFlowValue(right, rightType, context); DataFlowValue rightValue = DataFlowValueFactory.createDataFlowValue(right, rightType, context);
// We cannot say here anything new about rightValue except it has the same value as leftValue // We cannot say here anything new about rightValue except it has the same value as leftValue
rightInfo = rightInfo.replaceDataFlowInfo(dataFlowInfo.assign(leftValue, rightValue)); resultInfo = resultInfo.replaceDataFlowInfo(dataFlowInfo.assign(leftValue, rightValue));
} }
} }
else { else {
rightInfo = leftInfo; resultInfo = leftInfo;
} }
if (leftType != null && leftOperand != null) { //if leftType == null, some other error has been generated if (leftType != null && leftOperand != null) { //if leftType == null, some other error has been generated
basic.checkLValue(context.trace, context, leftOperand, right); basic.checkLValue(context.trace, context, leftOperand, right);
} }
return rightInfo.replaceType(DataFlowUtils.checkStatementType(expression, contextWithExpectedType)); return resultInfo.replaceType(DataFlowUtils.checkStatementType(expression, contextWithExpectedType));
} }
@@ -377,7 +377,7 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
@Override @Override
public JetTypeInfo visitJetElement(@NotNull JetElement element, ExpressionTypingContext context) { public JetTypeInfo visitJetElement(@NotNull JetElement element, ExpressionTypingContext context) {
context.trace.report(UNSUPPORTED.on(element, "in a block")); context.trace.report(UNSUPPORTED.on(element, "in a block"));
return TypeInfoFactoryPackage.createTypeInfo(context); return TypeInfoFactoryPackage.noTypeInfo(context);
} }
@Override @Override
@@ -42,6 +42,7 @@ import org.jetbrains.kotlin.types.checker.JetTypeChecker
import org.jetbrains.kotlin.types.expressions.CoercionStrategy.COERCION_TO_UNIT import org.jetbrains.kotlin.types.expressions.CoercionStrategy.COERCION_TO_UNIT
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.createCheckedTypeInfo import org.jetbrains.kotlin.types.expressions.typeInfoFactory.createCheckedTypeInfo
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.createTypeInfo import org.jetbrains.kotlin.types.expressions.typeInfoFactory.createTypeInfo
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.noTypeInfo
import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.addIfNotNull
public class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : ExpressionTypingVisitor(facade) { public class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : ExpressionTypingVisitor(facade) {
@@ -51,8 +51,9 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
@Override @Override
public JetTypeInfo visitIsExpression(@NotNull JetIsExpression expression, ExpressionTypingContext contextWithExpectedType) { public JetTypeInfo visitIsExpression(@NotNull JetIsExpression expression, ExpressionTypingContext contextWithExpectedType) {
ExpressionTypingContext context = contextWithExpectedType.replaceExpectedType(NO_EXPECTED_TYPE).replaceContextDependency( ExpressionTypingContext context = contextWithExpectedType
INDEPENDENT); .replaceExpectedType(NO_EXPECTED_TYPE)
.replaceContextDependency(INDEPENDENT);
JetExpression leftHandSide = expression.getLeftHandSide(); JetExpression leftHandSide = expression.getLeftHandSide();
JetTypeInfo typeInfo = facade.safeGetTypeInfo(leftHandSide, context.replaceScope(context.scope)); JetTypeInfo typeInfo = facade.safeGetTypeInfo(leftHandSide, context.replaceScope(context.scope));
JetType knownType = typeInfo.getType(); JetType knownType = typeInfo.getType();
@@ -22,22 +22,22 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.types.expressions.JetTypeInfo import org.jetbrains.kotlin.types.expressions.JetTypeInfo
/** /*
* Functions in this file are intended to create type info instances in different circumstances * Functions in this file are intended to create type info instances in different circumstances
*/ */
public fun createTypeInfo(type: JetType?): JetTypeInfo = createTypeInfo(type, DataFlowInfo.EMPTY)
public fun createTypeInfo(dataFlowInfo: DataFlowInfo): JetTypeInfo = JetTypeInfo(null, dataFlowInfo)
public fun createTypeInfo(context: ResolutionContext<*>): JetTypeInfo = createTypeInfo(context.dataFlowInfo)
public fun createTypeInfo(type: JetType?, dataFlowInfo: DataFlowInfo): JetTypeInfo = JetTypeInfo(type, dataFlowInfo) public fun createTypeInfo(type: JetType?, dataFlowInfo: DataFlowInfo): JetTypeInfo = JetTypeInfo(type, dataFlowInfo)
public fun createTypeInfo(type: JetType?, context: ResolutionContext<*>): JetTypeInfo = createTypeInfo(type, context.dataFlowInfo)
public fun createTypeInfo(type: JetType?, dataFlowInfo: DataFlowInfo, jumpPossible: Boolean, jumpFlowInfo: DataFlowInfo): JetTypeInfo = public fun createTypeInfo(type: JetType?, dataFlowInfo: DataFlowInfo, jumpPossible: Boolean, jumpFlowInfo: DataFlowInfo): JetTypeInfo =
JetTypeInfo(type, dataFlowInfo, jumpPossible, jumpFlowInfo) JetTypeInfo(type, dataFlowInfo, jumpPossible, jumpFlowInfo)
public fun createTypeInfo(type: JetType?): JetTypeInfo = createTypeInfo(type, DataFlowInfo.EMPTY)
public fun createTypeInfo(type: JetType?, context: ResolutionContext<*>): JetTypeInfo = createTypeInfo(type, context.dataFlowInfo)
public fun noTypeInfo(dataFlowInfo: DataFlowInfo): JetTypeInfo = createTypeInfo(null, dataFlowInfo)
public fun noTypeInfo(context: ResolutionContext<*>): JetTypeInfo = noTypeInfo(context.dataFlowInfo)
public fun createCheckedTypeInfo(type: JetType?, context: ResolutionContext<*>, expression: JetExpression): JetTypeInfo = public fun createCheckedTypeInfo(type: JetType?, context: ResolutionContext<*>, expression: JetExpression): JetTypeInfo =
createTypeInfo(type, context).checkType(expression, context) createTypeInfo(type, context).checkType(expression, context)
@@ -356,7 +356,7 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
assert right != null; assert right != null;
JetType rightType = BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.TYPE, right); JetType rightType = BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.TYPE, right);
JetType leftType = BindingContextUtils.getNotNullType(context.bindingContext(), expression.getLeft()); JetType leftType = BindingContextUtils.getTypeNotNull(context.bindingContext(), expression.getLeft());
if (TypeUtils.isNullableType(rightType) || !TypeUtils.isNullableType(leftType)) { if (TypeUtils.isNullableType(rightType) || !TypeUtils.isNullableType(leftType)) {
return jsExpression.source(expression); return jsExpression.source(expression);
} }
@@ -55,7 +55,7 @@ public final class UnaryOperationTranslator {
IElementType operationToken = expression.getOperationReference().getReferencedNameElementType(); IElementType operationToken = expression.getOperationReference().getReferencedNameElementType();
if (operationToken == JetTokens.EXCLEXCL) { if (operationToken == JetTokens.EXCLEXCL) {
JetExpression baseExpression = getBaseExpression(expression); JetExpression baseExpression = getBaseExpression(expression);
JetType type = BindingContextUtils.getNotNullType(context.bindingContext(), baseExpression); JetType type = BindingContextUtils.getTypeNotNull(context.bindingContext(), baseExpression);
JsExpression translatedExpression = translateAsExpression(baseExpression, context); JsExpression translatedExpression = translateAsExpression(baseExpression, context);
return type.isMarkedNullable() ? sure(translatedExpression, context) : translatedExpression; return type.isMarkedNullable() ? sure(translatedExpression, context) : translatedExpression;
} }
@@ -210,7 +210,7 @@ public final class BindingUtils {
@NotNull @NotNull
public static JetType getTypeForExpression(@NotNull BindingContext context, public static JetType getTypeForExpression(@NotNull BindingContext context,
@NotNull JetExpression expression) { @NotNull JetExpression expression) {
return BindingContextUtils.getNotNullType(context, expression); return BindingContextUtils.getTypeNotNull(context, expression);
} }
@NotNull @NotNull