[Typed throws] Handle key-path literal used with a function that has typed throws

When providing a key-path literal for a parameter of function type
where that function type has a generic parameter for its thrown error
type, infer `Never` for the generic argument because key paths don't
throw.

Thanks to @xedin for realizing that this would be an issue.
This commit is contained in:
Doug Gregor
2023-10-26 14:54:59 -07:00
parent a67b81843b
commit ff45fec1d8
2 changed files with 20 additions and 1 deletions

View File

@@ -12248,6 +12248,16 @@ ConstraintSystem::simplifyKeyPathConstraint(
// { root in root[keyPath: kp] }.
boundRoot = fnTy->getParams()[0].getParameterType();
boundValue = fnTy->getResult();
// Key paths never throw, so if the function has a thrown error type
// that is a type variable, infer it to be Never.
if (auto thrownError = fnTy->getThrownError()) {
if (thrownError->isTypeVariableOrMember()) {
(void)matchTypes(
thrownError, getASTContext().getNeverType(),
ConstraintKind::Equal, TMF_GenerateConstraints, locator);
}
}
}
if (boundRoot &&