@NotNull private static KtBinaryExpression createVariableAssignment(@NotNull KtProperty property) { String propertyName = property.getName(); assert propertyName != null : "Property should have a name " + property.getText(); KtBinaryExpression assignment = (KtBinaryExpression) KtPsiFactoryKt.KtPsiFactory(property).createExpression(propertyName + " = x"); KtExpression right = assignment.getRight(); assert right != null : "Created binary expression should have a right part " + assignment.getText(); KtExpression initializer = property.getInitializer(); assert initializer != null : "Initializer should exist for property " + property.getText(); right.replace(initializer); return assignment; }
private KotlinTypeInfo getTypeOfLastExpressionInBlock( @NotNull KtExpression statementExpression, @NotNull ExpressionTypingContext context, @NotNull CoercionStrategy coercionStrategyForLastExpression, @NotNull ExpressionTypingInternals blockLevelVisitor) { if (context.expectedType != NO_EXPECTED_TYPE) { KotlinType expectedType; if (context.expectedType == UNIT_EXPECTED_TYPE || // the first check is necessary to avoid invocation 'isUnit(UNIT_EXPECTED_TYPE)' (coercionStrategyForLastExpression == COERCION_TO_UNIT && KotlinBuiltIns.isUnit(context.expectedType))) { expectedType = UNIT_EXPECTED_TYPE; } else { expectedType = context.expectedType; } return blockLevelVisitor.getTypeInfo( statementExpression, context.replaceExpectedType(expectedType), true); } KotlinTypeInfo result = blockLevelVisitor.getTypeInfo(statementExpression, context, true); if (coercionStrategyForLastExpression == COERCION_TO_UNIT) { boolean mightBeUnit = false; if (statementExpression instanceof KtDeclaration) { mightBeUnit = true; } if (statementExpression instanceof KtBinaryExpression) { KtBinaryExpression binaryExpression = (KtBinaryExpression) statementExpression; IElementType operationType = binaryExpression.getOperationToken(); //noinspection SuspiciousMethodCalls if (operationType == KtTokens.EQ || OperatorConventions.ASSIGNMENT_OPERATIONS.containsKey(operationType)) { mightBeUnit = true; } } if (mightBeUnit) { // ExpressionTypingVisitorForStatements should return only null or Unit for declarations and // assignments, // but (for correct assignment / initialization analysis) data flow info must be preserved assert result.getType() == null || KotlinBuiltIns.isUnit(result.getType()); result = result.replaceType(expressionTypingComponents.builtIns.getUnitType()); } } return result; }