Skip to content

Commit 0bb323b

Browse files
committed
Sema: Move buildAutoClosureExpr() from TypeChecker to ConstraintSystem
1 parent dca3056 commit 0bb323b

File tree

6 files changed

+44
-49
lines changed

6 files changed

+44
-49
lines changed

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5411,8 +5411,7 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, AnyFunctionType *funcType,
54115411
arg, closureType->getResult(),
54125412
locator.withPathElement(ConstraintLocator::AutoclosureResult));
54135413

5414-
convertedArg = TypeChecker::buildAutoClosureExpr(dc, arg, closureType);
5415-
cs.cacheExprTypes(convertedArg);
5414+
convertedArg = cs.buildAutoClosureExpr(arg, closureType);
54165415
} else {
54175416
convertedArg = coerceToType(
54185417
arg, paramType,

lib/Sema/ConstraintSystem.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,3 +3474,38 @@ ConstraintSystem::isConversionEphemeral(ConversionRestrictionKind conversion,
34743474
return ConversionEphemeralness::NonEphemeral;
34753475
}
34763476
}
3477+
3478+
Expr *ConstraintSystem::buildAutoClosureExpr(Expr *expr,
3479+
FunctionType *closureType) {
3480+
auto &Context = DC->getASTContext();
3481+
bool isInDefaultArgumentContext = false;
3482+
if (auto *init = dyn_cast<Initializer>(DC))
3483+
isInDefaultArgumentContext =
3484+
init->getInitializerKind() == InitializerKind::DefaultArgument;
3485+
3486+
auto info = closureType->getExtInfo();
3487+
auto newClosureType = closureType;
3488+
3489+
if (isInDefaultArgumentContext && info.isNoEscape())
3490+
newClosureType = closureType->withExtInfo(info.withNoEscape(false))
3491+
->castTo<FunctionType>();
3492+
3493+
auto *closure = new (Context) AutoClosureExpr(
3494+
expr, newClosureType, AutoClosureExpr::InvalidDiscriminator, DC);
3495+
3496+
closure->setParameterList(ParameterList::createEmpty(Context));
3497+
getTypeChecker().ClosuresWithUncomputedCaptures.push_back(closure);
3498+
3499+
Expr *result = closure;
3500+
3501+
if (!newClosureType->isEqual(closureType)) {
3502+
assert(isInDefaultArgumentContext);
3503+
assert(newClosureType
3504+
->withExtInfo(newClosureType->getExtInfo().withNoEscape(true))
3505+
->isEqual(closureType));
3506+
result = new (Context) FunctionConversionExpr(closure, closureType);
3507+
}
3508+
3509+
cacheExprTypes(result);
3510+
return result;
3511+
}

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,6 +3026,10 @@ class ConstraintSystem {
30263026
ConstraintLocator *memberLocator,
30273027
bool includeInaccessibleMembers);
30283028

3029+
/// Build implicit autoclosure expression wrapping a given expression.
3030+
/// Given expression represents computed result of the closure.
3031+
Expr *buildAutoClosureExpr(Expr *expr, FunctionType *closureType);
3032+
30293033
private:
30303034
/// Determines whether or not a given conversion at a given locator requires
30313035
/// the creation of a temporary value that's only valid for a limited scope.

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,24 +2308,21 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
23082308

23092309
if (isAutoClosure) {
23102310
class AutoClosureListener : public ExprTypeCheckListener {
2311-
DeclContext *DC;
23122311
FunctionType *ParamType;
23132312

23142313
public:
2315-
AutoClosureListener(DeclContext *DC, FunctionType *paramType)
2316-
: DC(DC), ParamType(paramType) {}
2314+
AutoClosureListener(FunctionType *paramType)
2315+
: ParamType(paramType) {}
23172316

23182317
Expr *appliedSolution(constraints::Solution &solution,
23192318
Expr *expr) override {
23202319
auto &cs = solution.getConstraintSystem();
2321-
auto *closure = TypeChecker::buildAutoClosureExpr(DC, expr, ParamType);
2322-
cs.cacheExprTypes(closure);
2323-
return closure;
2320+
return cs.buildAutoClosureExpr(expr, ParamType);
23242321
}
23252322
};
23262323

23272324
auto *fnType = paramType->castTo<FunctionType>();
2328-
AutoClosureListener listener(DC, fnType);
2325+
AutoClosureListener listener(fnType);
23292326
return typeCheckExpression(defaultValue, DC,
23302327
TypeLoc::withoutLoc(fnType->getResult()),
23312328
canFail ? CTP_DefaultParameter : CTP_CannotFail,

lib/Sema/TypeCheckExpr.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -617,41 +617,6 @@ Expr *TypeChecker::buildRefExpr(ArrayRef<ValueDecl *> Decls,
617617
return result;
618618
}
619619

620-
Expr *TypeChecker::buildAutoClosureExpr(DeclContext *DC, Expr *expr,
621-
FunctionType *closureType) {
622-
auto &Context = DC->getASTContext();
623-
bool isInDefaultArgumentContext = false;
624-
if (auto *init = dyn_cast<Initializer>(DC))
625-
isInDefaultArgumentContext =
626-
init->getInitializerKind() == InitializerKind::DefaultArgument;
627-
628-
auto info = closureType->getExtInfo();
629-
auto newClosureType = closureType;
630-
631-
if (isInDefaultArgumentContext && info.isNoEscape())
632-
newClosureType = closureType->withExtInfo(info.withNoEscape(false))
633-
->castTo<FunctionType>();
634-
635-
auto *closure = new (Context) AutoClosureExpr(
636-
expr, newClosureType, AutoClosureExpr::InvalidDiscriminator, DC);
637-
638-
closure->setParameterList(ParameterList::createEmpty(Context));
639-
640-
// FIXME: Remove global type checker dependency.
641-
auto *TC = Context.getLegacyGlobalTypeChecker();
642-
TC->ClosuresWithUncomputedCaptures.push_back(closure);
643-
644-
if (!newClosureType->isEqual(closureType)) {
645-
assert(isInDefaultArgumentContext);
646-
assert(newClosureType
647-
->withExtInfo(newClosureType->getExtInfo().withNoEscape(true))
648-
->isEqual(closureType));
649-
return new (Context) FunctionConversionExpr(closure, closureType);
650-
}
651-
652-
return closure;
653-
}
654-
655620
static Type lookupDefaultLiteralType(const DeclContext *dc,
656621
StringRef name) {
657622
auto &ctx = dc->getASTContext();

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,11 +1333,6 @@ class TypeChecker final {
13331333
static Expr *buildRefExpr(ArrayRef<ValueDecl *> Decls, DeclContext *UseDC,
13341334
DeclNameLoc NameLoc, bool Implicit,
13351335
FunctionRefKind functionRefKind);
1336-
1337-
/// Build implicit autoclosure expression wrapping a given expression.
1338-
/// Given expression represents computed result of the closure.
1339-
static Expr *buildAutoClosureExpr(DeclContext *DC, Expr *expr,
1340-
FunctionType *closureType);
13411336
/// @}
13421337

13431338
/// Retrieve a specific, known protocol.

0 commit comments

Comments
 (0)