Skip to content

Commit 2e65a14

Browse files
authored
Merge pull request swiftlang#28372 from slavapestov/typecheck-decl-primary
Kill ClosuresWithUncomputedCaptures and split off TypeCheckDeclPrimary.cpp from TypeCheckDecl.cpp
2 parents ffe180f + 15b5820 commit 2e65a14

20 files changed

+2584
-2544
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ class ASTContext final {
419419

420420
void installGlobalTypeChecker(TypeChecker *TC);
421421
public:
422+
/// Returns if semantic AST queries are enabled. This generally means module
423+
/// loading and name lookup can take place.
424+
bool areSemanticQueriesEnabled() const;
425+
422426
/// Retrieve the global \c TypeChecker instance associated with this context.
423427
TypeChecker *getLegacyGlobalTypeChecker() const;
424428

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,10 @@ RC<syntax::SyntaxArena> ASTContext::getSyntaxArena() const {
616616
return getImpl().TheSyntaxArena;
617617
}
618618

619+
bool ASTContext::areSemanticQueriesEnabled() const {
620+
return getLegacyGlobalTypeChecker() != nullptr;
621+
}
622+
619623
TypeChecker *ASTContext::getLegacyGlobalTypeChecker() const {
620624
return getImpl().Checker;
621625
}

lib/AST/Decl.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,10 +2892,7 @@ bool ValueDecl::isRecursiveValidation() const {
28922892
Type ValueDecl::getInterfaceType() const {
28932893
auto &ctx = getASTContext();
28942894

2895-
// N.B. This assertion exists to catch new broken callers. It can be removed
2896-
// with the LazyResolver when the time comes.
2897-
assert(ctx.getLegacyGlobalTypeChecker()
2898-
&& "The type checker must be installed to make semantic queries!");
2895+
assert(ctx.areSemanticQueriesEnabled());
28992896

29002897
if (auto type =
29012898
evaluateOrDefault(ctx.evaluator,
@@ -5697,7 +5694,7 @@ StaticSpellingKind AbstractStorageDecl::getCorrectStaticSpelling() const {
56975694

56985695
llvm::TinyPtrVector<CustomAttr *> VarDecl::getAttachedPropertyWrappers() const {
56995696
auto &ctx = getASTContext();
5700-
if (!ctx.getLegacyGlobalTypeChecker()) {
5697+
if (!ctx.areSemanticQueriesEnabled()) {
57015698
return { };
57025699
}
57035700

@@ -6642,7 +6639,7 @@ ObjCSelector
66426639
AbstractFunctionDecl::getObjCSelector(DeclName preferredName,
66436640
bool skipIsObjCResolution) const {
66446641
// FIXME: Forces computation of the Objective-C selector.
6645-
if (getASTContext().getLegacyGlobalTypeChecker() && !skipIsObjCResolution)
6642+
if (getASTContext().areSemanticQueriesEnabled() && !skipIsObjCResolution)
66466643
(void)isObjC();
66476644

66486645
// If there is an @objc attribute with a name, use that name.

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ bool DeclContext::lookupQualified(ArrayRef<NominalTypeDecl *> typeDecls,
16601660
tracker->addUsedMember({current, member.getBaseName()},isLookupCascading);
16611661

16621662
// Make sure we've resolved implicit members, if we need them.
1663-
if (ctx.getLegacyGlobalTypeChecker()) {
1663+
if (ctx.areSemanticQueriesEnabled()) {
16641664
current->synthesizeSemanticMembersIfNeeded(member);
16651665
installPropertyWrapperMembersIfNeeded(current, member);
16661666
}

lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ add_swift_host_library(swiftSema STATIC
4646
TypeCheckDecl.cpp
4747
TypeCheckDeclObjC.cpp
4848
TypeCheckDeclOverride.cpp
49+
TypeCheckDeclPrimary.cpp
4950
TypeCheckError.cpp
5051
TypeCheckExpr.cpp
5152
TypeCheckExprObjC.cpp

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,10 +4494,6 @@ namespace {
44944494
outerClosure->setType(outerClosureTy);
44954495
cs.cacheType(outerClosure);
44964496

4497-
// The inner closure at least will definitely have a capture.
4498-
cs.getTypeChecker().ClosuresWithUncomputedCaptures.push_back(outerClosure);
4499-
cs.getTypeChecker().ClosuresWithUncomputedCaptures.push_back(closure);
4500-
45014497
// let outerApply = "\( outerClosure )( \(E) )"
45024498
auto outerApply = CallExpr::createImplicit(ctx, outerClosure, {E}, {});
45034499
outerApply->setType(closureTy);
@@ -5411,8 +5407,7 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, AnyFunctionType *funcType,
54115407
arg, closureType->getResult(),
54125408
locator.withPathElement(ConstraintLocator::AutoclosureResult));
54135409

5414-
convertedArg = TypeChecker::buildAutoClosureExpr(dc, arg, closureType);
5415-
cs.cacheExprTypes(convertedArg);
5410+
convertedArg = cs.buildAutoClosureExpr(arg, closureType);
54165411
} else {
54175412
convertedArg = coerceToType(
54185413
arg, paramType,
@@ -7269,12 +7264,6 @@ namespace {
72697264
ClosuresToTypeCheck.push_back(closure);
72707265
}
72717266

7272-
// Don't try to register captures if constraint system is used to
7273-
// produce diagnostics for one of the sub-expressions.
7274-
if (!cs.Options.contains(
7275-
ConstraintSystemFlags::SubExpressionDiagnostics))
7276-
cs.getTypeChecker().ClosuresWithUncomputedCaptures.push_back(closure);
7277-
72787267
return { false, closure };
72797268
}
72807269

lib/Sema/CSDiagnostics.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ class FailureDiagnostic {
121121
InFlightDiagnostic emitDiagnostic(ArgTypes &&... Args) const;
122122

123123
protected:
124-
TypeChecker &getTypeChecker() const { return CS.getTypeChecker(); }
125-
126124
DeclContext *getDC() const { return CS.DC; }
127125

128126
ASTContext &getASTContext() const { return CS.getASTContext(); }

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ namespace {
21352135
if (!ty || ty->is<TypeVariableType>())
21362136
ty = CS.createTypeVariable(CS.getConstraintLocator(locator),
21372137
TVO_CanBindToNoEscape);
2138-
return CS.getTypeChecker().getOptionalType(var->getLoc(), ty);
2138+
return TypeChecker::getOptionalType(var->getLoc(), ty);
21392139
case ReferenceOwnershipOptionality::Allowed:
21402140
case ReferenceOwnershipOptionality::Disallowed:
21412141
break;
@@ -2852,7 +2852,7 @@ namespace {
28522852
/// diagnosing ill-formed standard libraries, so it really isn't
28532853
/// worth QoI efforts.
28542854
Type getOptionalType(SourceLoc optLoc, Type valueTy) {
2855-
auto optTy = CS.getTypeChecker().getOptionalType(optLoc, valueTy);
2855+
auto optTy = TypeChecker::getOptionalType(optLoc, valueTy);
28562856
if (!optTy ||
28572857
TypeChecker::requireOptionalIntrinsics(CS.getASTContext(), optLoc))
28582858
return Type();
@@ -3863,8 +3863,7 @@ getMemberDecls(InterestedMemberKind Kind) {
38633863
ResolvedMemberResult
38643864
swift::resolveValueMember(DeclContext &DC, Type BaseTy, DeclName Name) {
38653865
ResolvedMemberResult Result;
3866-
assert(DC.getASTContext().getLegacyGlobalTypeChecker() &&
3867-
"Must have type checker to make global query!");
3866+
assert(DC.getASTContext().areSemanticQueriesEnabled());
38683867
ConstraintSystem CS(&DC, None);
38693868

38703869
// Look up all members of BaseTy with the given Name.

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6166,7 +6166,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
61666166
auto innerTV = createTypeVariable(locator,
61676167
TVO_CanBindToLValue |
61686168
TVO_CanBindToNoEscape);
6169-
Type optTy = getTypeChecker().getOptionalType(SourceLoc(), innerTV);
6169+
Type optTy = TypeChecker::getOptionalType(SourceLoc(), innerTV);
61706170
SmallVector<Constraint *, 2> optionalities;
61716171
auto nonoptionalResult = Constraint::createFixed(
61726172
*this, ConstraintKind::Bind,

lib/Sema/ConstraintSystem.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,3 +3474,37 @@ 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+
3498+
Expr *result = closure;
3499+
3500+
if (!newClosureType->isEqual(closureType)) {
3501+
assert(isInDefaultArgumentContext);
3502+
assert(newClosureType
3503+
->withExtInfo(newClosureType->getExtInfo().withNoEscape(true))
3504+
->isEqual(closureType));
3505+
result = new (Context) FunctionConversionExpr(closure, closureType);
3506+
}
3507+
3508+
cacheExprTypes(result);
3509+
return result;
3510+
}

0 commit comments

Comments
 (0)