use explicit type of delegated property as expected
while resolving constraint system for delegated expression
This commit is contained in:
@@ -755,4 +755,25 @@ public class JetPsiUtil {
|
||||
public static boolean isInComment(PsiElement element) {
|
||||
return CommentUtilCore.isComment(element) || element instanceof KDocElement;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static JetExpression getCalleeExpressionIfAny(@NotNull JetExpression expression) {
|
||||
if (expression instanceof JetCallElement) {
|
||||
JetCallElement callExpression = (JetCallElement) expression;
|
||||
return callExpression.getCalleeExpression();
|
||||
}
|
||||
if (expression instanceof JetQualifiedExpression) {
|
||||
JetExpression selectorExpression = ((JetQualifiedExpression) expression).getSelectorExpression();
|
||||
if (selectorExpression != null) {
|
||||
return getCalleeExpressionIfAny(selectorExpression);
|
||||
}
|
||||
}
|
||||
if (expression instanceof JetUnaryExpression) {
|
||||
return ((JetUnaryExpression) expression).getOperationReference();
|
||||
}
|
||||
if (expression instanceof JetBinaryExpression) {
|
||||
return ((JetBinaryExpression) expression).getOperationReference();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
|
||||
import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintSystemCompleter;
|
||||
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
||||
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
@@ -82,6 +83,7 @@ public interface BindingContext {
|
||||
new BasicWritableSlice<JetReferenceExpression, DeclarationDescriptor>(DO_NOTHING);
|
||||
WritableSlice<JetElement, ResolvedCall<? extends CallableDescriptor>> RESOLVED_CALL =
|
||||
new BasicWritableSlice<JetElement, ResolvedCall<? extends CallableDescriptor>>(DO_NOTHING);
|
||||
WritableSlice<JetElement, ConstraintSystemCompleter> CONSTRAINT_SYSTEM_COMPLETER = new BasicWritableSlice<JetElement, ConstraintSystemCompleter>(DO_NOTHING);
|
||||
WritableSlice<JetElement, Call> CALL = new BasicWritableSlice<JetElement, Call>(DO_NOTHING);
|
||||
|
||||
WritableSlice<JetReferenceExpression, Collection<? extends DeclarationDescriptor>> AMBIGUOUS_REFERENCE_TARGET =
|
||||
|
||||
@@ -26,6 +26,10 @@ import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.descriptors.impl.FunctionDescriptorUtil;
|
||||
import org.jetbrains.jet.lang.descriptors.impl.MutableClassDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintPosition;
|
||||
import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintSystem;
|
||||
import org.jetbrains.jet.lang.resolve.calls.inference.ConstraintSystemCompleter;
|
||||
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
||||
import org.jetbrains.jet.lang.resolve.calls.util.CallMaker;
|
||||
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
|
||||
import org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResults;
|
||||
@@ -46,7 +50,8 @@ import java.util.*;
|
||||
|
||||
import static org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
|
||||
import static org.jetbrains.jet.lang.diagnostics.Errors.*;
|
||||
import static org.jetbrains.jet.lang.resolve.BindingContext.DEFERRED_TYPE;
|
||||
import static org.jetbrains.jet.lang.resolve.BindingContext.*;
|
||||
import static org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResults.Code;
|
||||
import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE;
|
||||
|
||||
public class BodyResolver {
|
||||
@@ -487,10 +492,25 @@ public class BodyResolver {
|
||||
|
||||
JetScope propertyDeclarationInnerScope = descriptorResolver.getPropertyDeclarationInnerScopeForInitializer(
|
||||
propertyScope, propertyDescriptor.getTypeParameters(), NO_RECEIVER_PARAMETER, trace);
|
||||
JetType delegateType = expressionTypingServices.safeGetType(propertyDeclarationInnerScope, delegateExpression, NO_EXPECTED_TYPE,
|
||||
DataFlowInfo.EMPTY, trace);
|
||||
TemporaryBindingTrace traceToResolveDelegatedProperty = TemporaryBindingTrace.create(trace, "Trace to resolve delegated property");
|
||||
JetScope accessorScope = JetScopeUtils.makeScopeForPropertyAccessor(
|
||||
propertyDescriptor, parentScopeForAccessor, descriptorResolver, trace);
|
||||
|
||||
JetExpression calleeExpression = JetPsiUtil.getCalleeExpressionIfAny(delegateExpression);
|
||||
ConstraintSystemCompleter completer =
|
||||
createConstraintSystemCompleter(jetProperty, propertyDescriptor, delegateExpression, accessorScope);
|
||||
if (calleeExpression != null) {
|
||||
traceToResolveDelegatedProperty.record(CONSTRAINT_SYSTEM_COMPLETER, calleeExpression, completer);
|
||||
}
|
||||
JetType delegateType = expressionTypingServices.safeGetType(propertyDeclarationInnerScope, delegateExpression, NO_EXPECTED_TYPE,
|
||||
DataFlowInfo.EMPTY, traceToResolveDelegatedProperty);
|
||||
traceToResolveDelegatedProperty.commit(new TraceEntryFilter() {
|
||||
@Override
|
||||
public boolean accept(@NotNull WritableSlice<?, ?> slice, Object key) {
|
||||
return slice != CONSTRAINT_SYSTEM_COMPLETER;
|
||||
}
|
||||
}, true);
|
||||
|
||||
JetScope accessorScope = JetScopeUtils.makeScopeForPropertyAccessor(propertyDescriptor, parentScopeForAccessor, descriptorResolver, trace);
|
||||
DelegatedPropertyUtils.resolveDelegatedPropertyGetMethod(propertyDescriptor, delegateExpression, delegateType,
|
||||
expressionTypingServices, trace, accessorScope);
|
||||
|
||||
@@ -500,6 +520,73 @@ public class BodyResolver {
|
||||
}
|
||||
}
|
||||
|
||||
private ConstraintSystemCompleter createConstraintSystemCompleter(
|
||||
JetProperty property,
|
||||
final PropertyDescriptor propertyDescriptor,
|
||||
final JetExpression delegateExpression,
|
||||
final JetScope accessorScope
|
||||
) {
|
||||
final JetType expectedType = property.getTypeRef() != null ? propertyDescriptor.getType() : NO_EXPECTED_TYPE;
|
||||
return new ConstraintSystemCompleter() {
|
||||
@Override
|
||||
public void completeConstraintSystem(
|
||||
@NotNull ConstraintSystem constraintSystem, @NotNull ResolvedCall<?> resolvedCall
|
||||
) {
|
||||
JetType returnType = resolvedCall.getCandidateDescriptor().getReturnType();
|
||||
if (returnType == null) return;
|
||||
|
||||
TemporaryBindingTrace traceToResolveConventionMethods =
|
||||
TemporaryBindingTrace.create(trace, "Trace to resolve delegated property convention methods");
|
||||
OverloadResolutionResults<FunctionDescriptor>
|
||||
getMethodResults = DelegatedPropertyUtils.getDelegatedPropertyConventionMethod(
|
||||
propertyDescriptor, delegateExpression, returnType, expressionTypingServices,
|
||||
traceToResolveConventionMethods, accessorScope, true);
|
||||
|
||||
if (conventionMethodFound(getMethodResults)) {
|
||||
FunctionDescriptor descriptor = getMethodResults.getResultingDescriptor();
|
||||
JetType returnTypeOfGetMethod = descriptor.getReturnType();
|
||||
if (returnTypeOfGetMethod != null) {
|
||||
constraintSystem.addSupertypeConstraint(expectedType, returnTypeOfGetMethod, ConstraintPosition.FROM_COMPLETER);
|
||||
}
|
||||
addConstraintForThisValue(constraintSystem, descriptor);
|
||||
}
|
||||
if (propertyDescriptor.isVar()) {
|
||||
OverloadResolutionResults<FunctionDescriptor> setMethodResults =
|
||||
DelegatedPropertyUtils.getDelegatedPropertyConventionMethod(
|
||||
propertyDescriptor, delegateExpression, returnType, expressionTypingServices,
|
||||
traceToResolveConventionMethods, accessorScope, false);
|
||||
|
||||
if (conventionMethodFound(setMethodResults)) {
|
||||
FunctionDescriptor descriptor = setMethodResults.getResultingDescriptor();
|
||||
List<ValueParameterDescriptor> valueParameters = descriptor.getValueParameters();
|
||||
if (valueParameters.size() == 3) {
|
||||
ValueParameterDescriptor valueParameterForThis = valueParameters.get(2);
|
||||
constraintSystem.addSubtypeConstraint(expectedType, valueParameterForThis.getType(), ConstraintPosition.FROM_COMPLETER);
|
||||
addConstraintForThisValue(constraintSystem, descriptor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean conventionMethodFound(@NotNull OverloadResolutionResults<FunctionDescriptor> results) {
|
||||
return results.isSuccess() ||
|
||||
(results.isSingleResult() && results.getResultCode() == Code.SINGLE_CANDIDATE_ARGUMENT_MISMATCH);
|
||||
}
|
||||
|
||||
private void addConstraintForThisValue(ConstraintSystem constraintSystem, FunctionDescriptor resultingDescriptor) {
|
||||
ReceiverParameterDescriptor receiverParameter = propertyDescriptor.getReceiverParameter();
|
||||
ReceiverParameterDescriptor thisObject = propertyDescriptor.getExpectedThisObject();
|
||||
if (receiverParameter == null && thisObject == null) return;
|
||||
|
||||
List<ValueParameterDescriptor> valueParameters = resultingDescriptor.getValueParameters();
|
||||
if (valueParameters.isEmpty()) return;
|
||||
ValueParameterDescriptor valueParameterForThis = valueParameters.get(0);
|
||||
JetType typeOfThis = receiverParameter != null ? receiverParameter.getType() : thisObject.getType();
|
||||
constraintSystem.addSubtypeConstraint(typeOfThis, valueParameterForThis.getType(), ConstraintPosition.FROM_COMPLETER);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void resolvePropertyInitializer(
|
||||
@NotNull JetProperty property,
|
||||
@NotNull PropertyDescriptor propertyDescriptor,
|
||||
|
||||
@@ -235,6 +235,21 @@ public class CandidateResolver {
|
||||
|
||||
constraintSystem.addSupertypeConstraint(context.expectedType, descriptor.getReturnType(), ConstraintPosition.EXPECTED_TYPE_POSITION);
|
||||
|
||||
ConstraintSystemCompleter constraintSystemCompleter = context.trace.get(
|
||||
BindingContext.CONSTRAINT_SYSTEM_COMPLETER, context.call.getCalleeExpression());
|
||||
if (constraintSystemCompleter != null) {
|
||||
ConstraintSystemImpl backup = (ConstraintSystemImpl) constraintSystem.copy();
|
||||
|
||||
//todo improve error reporting with errors in constraints from completer
|
||||
constraintSystemCompleter.completeConstraintSystem(constraintSystem, resolvedCall);
|
||||
if (constraintSystem.hasTypeConstructorMismatchAt(ConstraintPosition.FROM_COMPLETER) ||
|
||||
(constraintSystem.hasContradiction() && !backup.hasContradiction())) {
|
||||
|
||||
constraintSystem = backup;
|
||||
resolvedCall.setConstraintSystem(backup);
|
||||
}
|
||||
}
|
||||
|
||||
if (constraintSystem.hasContradiction()) {
|
||||
return reportInferenceError(context);
|
||||
}
|
||||
|
||||
+1
@@ -24,6 +24,7 @@ public class ConstraintPosition {
|
||||
public static final ConstraintPosition RECEIVER_POSITION = new ConstraintPosition("RECEIVER_POSITION");
|
||||
public static final ConstraintPosition EXPECTED_TYPE_POSITION = new ConstraintPosition("EXPECTED_TYPE_POSITION");
|
||||
public static final ConstraintPosition BOUND_CONSTRAINT_POSITION = new ConstraintPosition("BOUND_CONSTRAINT_POSITION");
|
||||
public static final ConstraintPosition FROM_COMPLETER = new ConstraintPosition("FROM_COMPLETER");
|
||||
|
||||
private static final Map<Integer, ConstraintPosition> valueParameterPositions = Maps.newHashMap();
|
||||
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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.jet.lang.resolve.calls.inference;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
||||
|
||||
public interface ConstraintSystemCompleter {
|
||||
void completeConstraintSystem(
|
||||
@NotNull ConstraintSystem constraintSystem,
|
||||
@NotNull ResolvedCall<?> resolvedCall
|
||||
);
|
||||
}
|
||||
+45
-27
@@ -123,8 +123,48 @@ public class DelegatedPropertyUtils {
|
||||
PropertyAccessorDescriptor accessor = isGet ? propertyDescriptor.getGetter() : propertyDescriptor.getSetter();
|
||||
assert accessor != null : "Delegated property should have getter/setter " + propertyDescriptor + " " + delegateExpression.getText();
|
||||
|
||||
if (trace.getBindingContext().get(DELEGATED_PROPERTY_CALL, accessor) != null) return;
|
||||
|
||||
OverloadResolutionResults<FunctionDescriptor> functionResults = getDelegatedPropertyConventionMethod(
|
||||
propertyDescriptor, delegateExpression, delegateType, expressionTypingServices, trace, scope, isGet);
|
||||
Call call = trace.getBindingContext().get(DELEGATED_PROPERTY_CALL, accessor);
|
||||
if (call != null) return;
|
||||
assert call != null : "'getDelegatedPropertyConventionMethod' didn't record a call";
|
||||
|
||||
if (!functionResults.isSuccess()) {
|
||||
String expectedFunction = renderCall(call, trace.getBindingContext());
|
||||
if (functionResults.isIncomplete()) {
|
||||
trace.report(DELEGATE_SPECIAL_FUNCTION_MISSING.on(delegateExpression, expectedFunction, delegateType));
|
||||
}
|
||||
else if (functionResults.isSingleResult() ||
|
||||
functionResults.getResultCode() == OverloadResolutionResults.Code.MANY_FAILED_CANDIDATES) {
|
||||
trace.report(DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE
|
||||
.on(delegateExpression, expectedFunction, functionResults.getResultingCalls()));
|
||||
}
|
||||
else if (functionResults.isAmbiguity()) {
|
||||
trace.report(DELEGATE_SPECIAL_FUNCTION_AMBIGUITY
|
||||
.on(delegateExpression, expectedFunction, functionResults.getResultingCalls()));
|
||||
}
|
||||
else {
|
||||
trace.report(DELEGATE_SPECIAL_FUNCTION_MISSING.on(delegateExpression, expectedFunction, delegateType));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
trace.record(DELEGATED_PROPERTY_RESOLVED_CALL, accessor, functionResults.getResultingCall());
|
||||
}
|
||||
|
||||
/* Resolve get() or set() methods from delegate */
|
||||
public static OverloadResolutionResults<FunctionDescriptor> getDelegatedPropertyConventionMethod(
|
||||
@NotNull PropertyDescriptor propertyDescriptor,
|
||||
@NotNull JetExpression delegateExpression,
|
||||
@NotNull JetType delegateType,
|
||||
@NotNull ExpressionTypingServices expressionTypingServices,
|
||||
@NotNull BindingTrace trace,
|
||||
@NotNull JetScope scope,
|
||||
boolean isGet
|
||||
) {
|
||||
PropertyAccessorDescriptor accessor = isGet ? propertyDescriptor.getGetter() : propertyDescriptor.getSetter();
|
||||
assert accessor != null : "Delegated property should have getter/setter " + propertyDescriptor + " " + delegateExpression.getText();
|
||||
|
||||
ExpressionTypingContext context = ExpressionTypingContext.newContext(
|
||||
expressionTypingServices, trace, scope,
|
||||
@@ -144,39 +184,17 @@ public class DelegatedPropertyUtils {
|
||||
propertyDescriptor.getType());
|
||||
arguments.add(fakeArgument);
|
||||
List<ValueParameterDescriptor> valueParameters = accessor.getValueParameters();
|
||||
context.trace.record(REFERENCE_TARGET, fakeArgument, valueParameters.get(0));
|
||||
trace.record(REFERENCE_TARGET, fakeArgument, valueParameters.get(0));
|
||||
}
|
||||
|
||||
Name functionName = Name.identifier(isGet ? "get" : "set");
|
||||
JetReferenceExpression fakeCalleeExpression = createSimpleName(project, functionName.asString());
|
||||
|
||||
ExpressionReceiver receiver = new ExpressionReceiver(delegateExpression, delegateType);
|
||||
call = CallMaker.makeCallWithExpressions(fakeCalleeExpression, receiver, null, fakeCalleeExpression, arguments, Call.CallType.DEFAULT);
|
||||
context.trace.record(BindingContext.DELEGATED_PROPERTY_CALL, accessor, call);
|
||||
Call call = CallMaker.makeCallWithExpressions(fakeCalleeExpression, receiver, null, fakeCalleeExpression, arguments, Call.CallType.DEFAULT);
|
||||
trace.record(BindingContext.DELEGATED_PROPERTY_CALL, accessor, call);
|
||||
|
||||
OverloadResolutionResults<FunctionDescriptor> functionResults = context.resolveCallWithGivenName(call, fakeCalleeExpression, functionName);
|
||||
|
||||
if (!functionResults.isSuccess()) {
|
||||
String expectedFunction = renderCall(call, trace.getBindingContext());
|
||||
if (functionResults.isIncomplete()) {
|
||||
context.trace.report(DELEGATE_SPECIAL_FUNCTION_MISSING.on(delegateExpression, expectedFunction, delegateType));
|
||||
}
|
||||
else if (functionResults.isSingleResult() ||
|
||||
functionResults.getResultCode() == OverloadResolutionResults.Code.MANY_FAILED_CANDIDATES) {
|
||||
context.trace.report(DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE
|
||||
.on(delegateExpression, expectedFunction, functionResults.getResultingCalls()));
|
||||
}
|
||||
else if (functionResults.isAmbiguity()) {
|
||||
context.trace.report(DELEGATE_SPECIAL_FUNCTION_AMBIGUITY
|
||||
.on(delegateExpression, expectedFunction, functionResults.getResultingCalls()));
|
||||
}
|
||||
else {
|
||||
context.trace.report(DELEGATE_SPECIAL_FUNCTION_MISSING.on(delegateExpression, expectedFunction, delegateType));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
context.trace.record(DELEGATED_PROPERTY_RESOLVED_CALL, accessor, functionResults.getResultingCall());
|
||||
return context.resolveCallWithGivenName(call, fakeCalleeExpression, functionName);
|
||||
}
|
||||
|
||||
private static String renderCall(@NotNull Call call, @NotNull BindingContext context) {
|
||||
|
||||
Reference in New Issue
Block a user