Skip to content

Commit 61d86d5

Browse files
committed
[NFC] CSGen: Clean up some flow in inferClosureType
1 parent 80560da commit 61d86d5

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

lib/Sema/CSGen.cpp

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,52 +2062,38 @@ namespace {
20622062
// parameter or return type is omitted, a fresh type variable is used to
20632063
// stand in for that parameter or return type, allowing it to be inferred
20642064
// from context.
2065-
auto getExplicitResultType = [&]() -> Type {
2066-
if (!closure->hasExplicitResultType()) {
2067-
return Type();
2068-
}
2065+
Type resultTy = [&] {
2066+
if (closure->hasExplicitResultType()) {
2067+
if (auto declaredTy = closure->getExplicitResultType()) {
2068+
return declaredTy;
2069+
}
20692070

2070-
if (auto declaredTy = closure->getExplicitResultType()) {
2071-
return declaredTy;
2071+
const auto resolvedTy = resolveTypeReferenceInExpression(
2072+
closure->getExplicitResultTypeRepr(),
2073+
TypeResolverContext::InExpression,
2074+
// Introduce type variables for unbound generics.
2075+
OpenUnboundGenericType(
2076+
CS, CS.getConstraintLocator(
2077+
closure, ConstraintLocator::ClosureResult)));
2078+
if (resolvedTy)
2079+
return resolvedTy;
20722080
}
20732081

2074-
return resolveTypeReferenceInExpression(
2075-
closure->getExplicitResultTypeRepr(),
2076-
TypeResolverContext::InExpression,
2077-
// Introduce type variables for unbound generics.
2078-
OpenUnboundGenericType(
2079-
CS, CS.getConstraintLocator(closure,
2080-
ConstraintLocator::ClosureResult)));
2081-
};
2082-
2083-
Type resultTy;
2084-
if (auto explicityTy = getExplicitResultType()) {
2085-
resultTy = explicityTy;
2086-
} else {
2087-
auto getContextualResultType = [&]() -> Type {
2088-
if (auto contextualType = CS.getContextualType(closure)) {
2089-
if (auto fnType = contextualType->getAs<FunctionType>())
2090-
return fnType->getResult();
2091-
}
2092-
return Type();
2093-
};
2094-
2095-
if (auto contextualResultTy = getContextualResultType()) {
2096-
resultTy = contextualResultTy;
2097-
} else {
2098-
// If no return type was specified, create a fresh type
2099-
// variable for it and mark it as possible hole.
2100-
//
2101-
// If this is a multi-statement closure, let's mark result
2102-
// as potential hole right away.
2103-
resultTy = CS.createTypeVariable(
2104-
CS.getConstraintLocator(closure,
2105-
ConstraintLocator::ClosureResult),
2106-
shouldTypeCheckInEnclosingExpression(closure)
2107-
? 0
2108-
: TVO_CanBindToHole);
2082+
if (auto contextualType = CS.getContextualType(closure)) {
2083+
if (auto fnType = contextualType->getAs<FunctionType>())
2084+
return fnType->getResult();
21092085
}
2110-
}
2086+
2087+
// If no return type was specified, create a fresh type
2088+
// variable for it and mark it as possible hole.
2089+
//
2090+
// If this is a multi-statement closure, let's mark result
2091+
// as potential hole right away.
2092+
return Type(CS.createTypeVariable(
2093+
CS.getConstraintLocator(closure, ConstraintLocator::ClosureResult),
2094+
shouldTypeCheckInEnclosingExpression(closure) ? 0
2095+
: TVO_CanBindToHole));
2096+
}();
21112097

21122098
return FunctionType::get(closureParams, resultTy, extInfo);
21132099
}

0 commit comments

Comments
 (0)