@@ -2058,48 +2058,38 @@ namespace {
20582058 // parameter or return type is omitted, a fresh type variable is used to
20592059 // stand in for that parameter or return type, allowing it to be inferred
20602060 // from context.
2061- auto getExplicitResultType = [&]() -> Type {
2062- if (!closure->hasExplicitResultType ()) {
2063- return Type ();
2064- }
2061+ Type resultTy = [&] {
2062+ if (closure->hasExplicitResultType ()) {
2063+ if (auto declaredTy = closure->getExplicitResultType ()) {
2064+ return declaredTy;
2065+ }
20652066
2066- if (auto declaredTy = closure->getExplicitResultType ()) {
2067- return declaredTy;
2067+ const auto resolvedTy = resolveTypeReferenceInExpression (
2068+ closure->getExplicitResultTypeRepr (),
2069+ TypeResolverContext::InExpression,
2070+ // Introduce type variables for unbound generics.
2071+ OpenUnboundGenericType (
2072+ CS, CS.getConstraintLocator (
2073+ closure, ConstraintLocator::ClosureResult)));
2074+ if (resolvedTy)
2075+ return resolvedTy;
20682076 }
20692077
2070- return resolveTypeReferenceInExpression (
2071- closure->getExplicitResultTypeRepr (),
2072- TypeResolverContext::InExpression, nullptr );
2073- };
2074-
2075- Type resultTy;
2076- auto *resultLoc =
2077- CS.getConstraintLocator (closure, ConstraintLocator::ClosureResult);
2078- if (auto explicityTy = getExplicitResultType ()) {
2079- resultTy = explicityTy;
2080- } else {
2081- auto getContextualResultType = [&]() -> Type {
2082- if (auto contextualType = CS.getContextualType (closure)) {
2083- if (auto fnType = contextualType->getAs <FunctionType>())
2084- return fnType->getResult ();
2085- }
2086- return Type ();
2087- };
2088-
2089- if (auto contextualResultTy = getContextualResultType ()) {
2090- resultTy = contextualResultTy;
2091- } else {
2092- // If no return type was specified, create a fresh type
2093- // variable for it and mark it as possible hole.
2094- //
2095- // If this is a multi-statement closure, let's mark result
2096- // as potential hole right away.
2097- resultTy = CS.createTypeVariable (
2098- resultLoc,
2099- shouldTypeCheckInEnclosingExpression (closure)
2100- ? 0 : TVO_CanBindToHole);
2078+ if (auto contextualType = CS.getContextualType (closure)) {
2079+ if (auto fnType = contextualType->getAs <FunctionType>())
2080+ return fnType->getResult ();
21012081 }
2102- }
2082+
2083+ // If no return type was specified, create a fresh type
2084+ // variable for it and mark it as possible hole.
2085+ //
2086+ // If this is a multi-statement closure, let's mark result
2087+ // as potential hole right away.
2088+ return Type (CS.createTypeVariable (
2089+ CS.getConstraintLocator (closure, ConstraintLocator::ClosureResult),
2090+ shouldTypeCheckInEnclosingExpression (closure) ? 0
2091+ : TVO_CanBindToHole));
2092+ }();
21032093
21042094 return FunctionType::get (closureParams, resultTy, extInfo);
21052095 }
0 commit comments