mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[CodeCompletion] Don’t report a call pattern as convertible if its result type doesn’t match
To compute the expected type of a call pattern (which is the return type of the function if that call pattern is being used), we called `getTypeForCompletion` for the entire call, in the same way that we do for the code completion token. However, this pattern does not generally work. For the code completion token it worked because the code completion expression doesn’t have an inherent type and it inherits the type solely from its context. Calls, however, have an inherent return type and that type gets assigned as the `typeForCompletion`. Implement targeted checks for the two most common cases where an expected type exists: If the call that we suggest call patterns for is itself an argument to another function or if it is used in a place that has a contextual type in the constraint system (eg. a variable binding or a `return` statement). This means that we no longer return `Convertible` for call patterns in some more complex scenarios. But given that this information was computed based on incorrect results and that in those cases all call patterns had a `Convertible` type relation, I think that’s acceptable. Fixing this would require recording more information in the constraints system, which is out-of-scope for now.
This commit is contained in:
@@ -89,43 +89,6 @@ bool ArgumentTypeCheckCompletionCallback::addPossibleParams(
|
||||
return ShowGlobalCompletions;
|
||||
}
|
||||
|
||||
/// Applies heuristic to determine whether the result type of \p E is
|
||||
/// unconstrained, that is if the constraint system is satisfiable for any
|
||||
/// result type of \p E.
|
||||
static bool isExpressionResultTypeUnconstrained(const Solution &S, Expr *E) {
|
||||
ConstraintSystem &CS = S.getConstraintSystem();
|
||||
if (auto ParentExpr = CS.getParentExpr(E)) {
|
||||
if (auto Assign = dyn_cast<AssignExpr>(ParentExpr)) {
|
||||
if (isa<DiscardAssignmentExpr>(Assign->getDest())) {
|
||||
// _ = <expr> is unconstrained
|
||||
return true;
|
||||
}
|
||||
} else if (isa<RebindSelfInConstructorExpr>(ParentExpr)) {
|
||||
// super.init() is unconstrained (it always produces the correct result
|
||||
// by definition)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
auto target = S.getTargetFor(E);
|
||||
if (!target)
|
||||
return false;
|
||||
|
||||
assert(target->kind == SyntacticElementTarget::Kind::expression);
|
||||
switch (target->getExprContextualTypePurpose()) {
|
||||
case CTP_Unused:
|
||||
// If we aren't using the contextual type, its unconstrained by definition.
|
||||
return true;
|
||||
case CTP_Initialization: {
|
||||
// let x = <expr> is unconstrained
|
||||
auto contextualType = target->getExprContextualType();
|
||||
return !contextualType || contextualType->is<UnresolvedType>();
|
||||
}
|
||||
default:
|
||||
// Assume that it's constrained by default.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether `E` has a parent expression with arguments.
|
||||
static bool hasParentCallLikeExpr(Expr *E, ConstraintSystem &CS) {
|
||||
E = CS.getParentExpr(E);
|
||||
@@ -172,10 +135,21 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
|
||||
auto ArgIdx = ArgInfo->completionIdx;
|
||||
|
||||
Type ExpectedCallType;
|
||||
if (!isExpressionResultTypeUnconstrained(S, ParentCall)) {
|
||||
ExpectedCallType = getTypeForCompletion(S, ParentCall);
|
||||
if (auto ArgLoc = S.getConstraintSystem().getArgumentLocator(ParentCall)) {
|
||||
if (auto FuncArgApplyInfo = S.getFunctionArgApplyInfo(ArgLoc)) {
|
||||
Type ParamType = FuncArgApplyInfo->getParamInterfaceType();
|
||||
ExpectedCallType = S.simplifyTypeForCodeCompletion(ParamType);
|
||||
}
|
||||
}
|
||||
|
||||
if (!ExpectedCallType) {
|
||||
if (auto ContextualType = S.getContextualType(ParentCall)) {
|
||||
ExpectedCallType = ContextualType;
|
||||
}
|
||||
}
|
||||
if (ExpectedCallType && ExpectedCallType->hasUnresolvedType()) {
|
||||
ExpectedCallType = Type();
|
||||
}
|
||||
|
||||
auto *CallLocator = CS.getConstraintLocator(ParentCall);
|
||||
auto *CalleeLocator = S.getCalleeLocator(CallLocator);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user