-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Constraint solver] Reject trailing closures matching non-closure-parameters #24403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,11 @@ bool MatchCallArgumentListener::relabelArguments(ArrayRef<Identifier> newNames){ | |
return true; | ||
} | ||
|
||
bool MatchCallArgumentListener::trailingClosureMismatch( | ||
unsigned paramIdx, unsigned argIdx) { | ||
return true; | ||
} | ||
|
||
/// Produce a score (smaller is better) comparing a parameter name and | ||
/// potentially-typo'd argument name. | ||
/// | ||
|
@@ -205,6 +210,21 @@ static ConstraintSystem::TypeMatchOptions getDefaultDecompositionOptions( | |
return flags | ConstraintSystem::TMF_GenerateConstraints; | ||
} | ||
|
||
/// Determine whether the given parameter can accept a trailing closure. | ||
static bool acceptsTrailingClosure(const AnyFunctionType::Param ¶m) { | ||
Type paramTy = param.getPlainType(); | ||
if (!paramTy) | ||
return true; | ||
|
||
paramTy = paramTy->lookThroughAllOptionalTypes(); | ||
return paramTy->isTypeParameter() || | ||
paramTy->is<ArchetypeType>() || | ||
paramTy->is<AnyFunctionType>() || | ||
paramTy->isTypeVariableOrMember() || | ||
paramTy->is<UnresolvedType>() || | ||
paramTy->isAny(); | ||
} | ||
|
||
// FIXME: This should return ConstraintSystem::TypeMatchResult instead | ||
// to give more information to the solver about the failure. | ||
bool constraints:: | ||
|
@@ -425,6 +445,14 @@ matchCallArguments(ArrayRef<AnyFunctionType::Param> args, | |
|
||
// If we have a trailing closure, it maps to the last parameter. | ||
if (hasTrailingClosure && numParams > 0) { | ||
// If there is no suitable last parameter to accept the trailing closure, | ||
// notify the listener and bail if we need to. | ||
if (!acceptsTrailingClosure(params[numParams - 1])) { | ||
if (listener.trailingClosureMismatch(numParams - 1, numArgs - 1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the spirit of |
||
return true; | ||
} | ||
|
||
// Claim the parameter/argument pair. | ||
claimedArgs[numArgs-1] = true; | ||
++numClaimedArgs; | ||
parameterBindings[numParams-1].push_back(numArgs-1); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How precise do you want to get here? If it has any constraints at all, it can't accept a function value (just like non-Any existentials)