[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:
Alex Hoppen
2024-04-10 16:06:47 -07:00
parent 2ef1aa5525
commit 278ecef284
9 changed files with 52 additions and 62 deletions

View File

@@ -908,7 +908,7 @@ private:
bool lookThroughAutoclosure) const {
auto param = fnTy->getParams()[ParamIdx];
auto paramTy = param.getPlainType();
if (lookThroughAutoclosure && param.isAutoClosure())
if (lookThroughAutoclosure && param.isAutoClosure() && paramTy->is<FunctionType>())
paramTy = paramTy->castTo<FunctionType>()->getResult();
return paramTy;
}