Fix for KT-10313: ClassCastException with Generics

#KT-10313 Fixed
This commit is contained in:
Anton Sukhonosenko
2015-12-15 08:54:09 +03:00
committed by Michael Bogdanov
parent edf6a2142b
commit 0073257841
12 changed files with 140 additions and 2 deletions
@@ -1242,12 +1242,21 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
@Nullable
public static ConstantValue<?> getCompileTimeConstant(@NotNull KtExpression expression, @NotNull BindingContext bindingContext) {
return getCompileTimeConstant(expression, bindingContext, false);
}
@Nullable
public static ConstantValue<?> getCompileTimeConstant(
@NotNull KtExpression expression,
@NotNull BindingContext bindingContext,
boolean checkPure
) {
CompileTimeConstant<?> compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, bindingContext);
if (compileTimeValue == null) {
return null;
}
if (compileTimeValue.getUsesNonConstValAsConstant()) return null;
if (compileTimeValue.getUsesNonConstValAsConstant() || (checkPure && !compileTimeValue.getParameters().isPure())) return null;
KotlinType expectedType = bindingContext.getType(expression);
return compileTimeValue.toConstantValue(expectedType);
@@ -2893,6 +2902,11 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
expression.getRight(), reference);
}
else {
ConstantValue<?> compileTimeConstant = getCompileTimeConstant(expression, bindingContext, true);
if (compileTimeConstant != null) {
return StackValue.constant(compileTimeConstant.getValue(), expressionType(expression));
}
ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCallWithAssert(expression, bindingContext);
FunctionDescriptor descriptor = (FunctionDescriptor) resolvedCall.getResultingDescriptor();
@@ -3165,6 +3179,11 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
@Override
public StackValue visitPrefixExpression(@NotNull KtPrefixExpression expression, @NotNull StackValue receiver) {
ConstantValue<?> compileTimeConstant = getCompileTimeConstant(expression, bindingContext, true);
if (compileTimeConstant != null) {
return StackValue.constant(compileTimeConstant.getValue(), expressionType(expression));
}
DeclarationDescriptor originalOperation = bindingContext.get(REFERENCE_TARGET, expression.getOperationReference());
ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCallWithAssert(expression, bindingContext);
CallableDescriptor op = resolvedCall.getResultingDescriptor();