@@ -2453,6 +2453,49 @@ namespace {
24532453 auto resultLocator =
24542454 CS.getConstraintLocator (closure, ConstraintLocator::ClosureResult);
24552455
2456+ // FIXME: Need a better locator.
2457+ auto thrownErrorLocator =
2458+ CS.getConstraintLocator (closure, ConstraintLocator::ClosureThrownError);
2459+
2460+ // Determine the thrown error type, when appropriate.
2461+ Type thrownErrorTy = [&] {
2462+ // Explicitly-specified thrown type.
2463+ if (auto thrownTypeRepr = closure->getExplicitThrownTypeRepr ()) {
2464+ Type resolvedTy = resolveTypeReferenceInExpression (
2465+ thrownTypeRepr, TypeResolverContext::InExpression,
2466+ thrownErrorLocator);
2467+ if (resolvedTy)
2468+ return resolvedTy;
2469+ }
2470+
2471+ // Thrown type inferred from context.
2472+ if (auto contextualType = CS.getContextualType (
2473+ closure, /* forConstraint=*/ false )) {
2474+ if (auto fnType = contextualType->getAs <AnyFunctionType>()) {
2475+ if (Type thrownErrorTy = fnType->getThrownError ())
2476+ return thrownErrorTy;
2477+ }
2478+ }
2479+
2480+ // We do not try to infer a thrown error type if one isn't immediately
2481+ // available. We could attempt this in the future.
2482+ return Type ();
2483+ }();
2484+
2485+ if (thrownErrorTy) {
2486+ // Record the thrown error type in the extended info for the function
2487+ // type of the closure.
2488+ extInfo = extInfo.withThrows (true , thrownErrorTy);
2489+
2490+ // Ensure that the thrown error type conforms to Error.
2491+ if (auto errorProto =
2492+ CS.getASTContext ().getProtocol (KnownProtocolKind::Error)) {
2493+ CS.addConstraint (
2494+ ConstraintKind::ConformsTo, thrownErrorTy,
2495+ errorProto->getDeclaredInterfaceType (), thrownErrorLocator);
2496+ }
2497+ }
2498+
24562499 // Closure expressions always have function type. In cases where a
24572500 // parameter or return type is omitted, a fresh type variable is used to
24582501 // stand in for that parameter or return type, allowing it to be inferred
0 commit comments