Skip to content

Commit aef8306

Browse files
committed
[TypeChecker] Teach declaration type-checking about LeaveClosureBodyUnchecked flag
Propagate `LeaveClosureBodyUnchecked` flag from `typeCheckASTNodeAtLoc` down to declaration checker to skip checking closures associated with pattern binding entry initializers. This is no-op until multi-statement inference becomes enabled by default.
1 parent d7984f4 commit aef8306

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,11 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
431431
: CTP_DefaultParameter});
432432
}
433433

434-
bool TypeChecker::typeCheckBinding(
435-
Pattern *&pattern, Expr *&initializer, DeclContext *DC,
436-
Type patternType, PatternBindingDecl *PBD, unsigned patternNumber) {
434+
bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
435+
DeclContext *DC, Type patternType,
436+
PatternBindingDecl *PBD,
437+
unsigned patternNumber,
438+
TypeCheckExprOptions options) {
437439
SolutionApplicationTarget target =
438440
PBD ? SolutionApplicationTarget::forInitialization(
439441
initializer, DC, patternType, PBD, patternNumber,
@@ -442,7 +444,6 @@ bool TypeChecker::typeCheckBinding(
442444
initializer, DC, patternType, pattern,
443445
/*bindPatternVarsOneWay=*/false);
444446

445-
auto options = TypeCheckExprOptions();
446447
if (DC->getASTContext().LangOpts.CheckAPIAvailabilityOnly &&
447448
PBD && !DC->getAsDecl()) {
448449
// Skip checking the initializer for non-public decls when
@@ -491,7 +492,8 @@ bool TypeChecker::typeCheckBinding(
491492

492493
bool TypeChecker::typeCheckPatternBinding(PatternBindingDecl *PBD,
493494
unsigned patternNumber,
494-
Type patternType) {
495+
Type patternType,
496+
TypeCheckExprOptions options) {
495497
Pattern *pattern = PBD->getPattern(patternNumber);
496498
Expr *init = PBD->getInit(patternNumber);
497499

@@ -520,8 +522,8 @@ bool TypeChecker::typeCheckPatternBinding(PatternBindingDecl *PBD,
520522
}
521523
}
522524

523-
bool hadError = TypeChecker::typeCheckBinding(
524-
pattern, init, DC, patternType, PBD, patternNumber);
525+
bool hadError = TypeChecker::typeCheckBinding(pattern, init, DC, patternType,
526+
PBD, patternNumber, options);
525527
if (!init) {
526528
PBD->setInvalid();
527529
return true;

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
16701670
ASTContext &Ctx;
16711671
SourceFile *SF;
16721672

1673-
explicit DeclChecker(ASTContext &ctx, SourceFile *SF) : Ctx(ctx), SF(SF) {}
1673+
bool LeaveClosureBodiesUnchecked;
1674+
1675+
explicit DeclChecker(ASTContext &ctx, SourceFile *SF,
1676+
bool LeaveClosureBodiesUnchecked = false)
1677+
: Ctx(ctx), SF(SF),
1678+
LeaveClosureBodiesUnchecked(LeaveClosureBodiesUnchecked) {}
16741679

16751680
ASTContext &getASTContext() const { return Ctx; }
16761681
void addDelayedFunction(AbstractFunctionDecl *AFD) {
@@ -2044,7 +2049,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20442049
continue;
20452050

20462051
if (!PBD->isInitializerChecked(i)) {
2047-
TypeChecker::typeCheckPatternBinding(PBD, i);
2052+
TypeCheckExprOptions options;
2053+
2054+
if (LeaveClosureBodiesUnchecked)
2055+
options |= TypeCheckExprFlags::LeaveClosureBodyUnchecked;
2056+
2057+
TypeChecker::typeCheckPatternBinding(PBD, i, /*patternType=*/Type(),
2058+
options);
20482059
}
20492060

20502061
if (!PBD->isInvalid()) {
@@ -3164,9 +3175,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
31643175
};
31653176
} // end anonymous namespace
31663177

3167-
void TypeChecker::typeCheckDecl(Decl *D) {
3178+
void TypeChecker::typeCheckDecl(Decl *D, bool LeaveClosureBodiesUnchecked) {
31683179
auto *SF = D->getDeclContext()->getParentSourceFile();
3169-
DeclChecker(D->getASTContext(), SF).visit(D);
3180+
DeclChecker(D->getASTContext(), SF, LeaveClosureBodiesUnchecked).visit(D);
31703181
}
31713182

31723183
void TypeChecker::checkParameterList(ParameterList *params,

lib/Sema/TypeCheckStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ void StmtChecker::typeCheckASTNode(ASTNode &node) {
15141514

15151515
// Type check the declaration.
15161516
if (auto *D = node.dyn_cast<Decl *>()) {
1517-
TypeChecker::typeCheckDecl(D);
1517+
TypeChecker::typeCheckDecl(D, LeaveBraceStmtBodyUnchecked);
15181518
return;
15191519
}
15201520

lib/Sema/TypeChecker.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,15 +445,20 @@ swift::handleSILGenericParams(GenericParamList *genericParams,
445445
}
446446

447447
void swift::typeCheckPatternBinding(PatternBindingDecl *PBD,
448-
unsigned bindingIndex) {
448+
unsigned bindingIndex,
449+
bool leaveClosureBodiesUnchecked) {
449450
assert(!PBD->isInitializerChecked(bindingIndex) &&
450451
PBD->getInit(bindingIndex));
451452

452453
auto &Ctx = PBD->getASTContext();
453454
DiagnosticSuppression suppression(Ctx.Diags);
454-
(void)evaluateOrDefault(
455-
Ctx.evaluator, PatternBindingEntryRequest{PBD, bindingIndex}, nullptr);
456-
TypeChecker::typeCheckPatternBinding(PBD, bindingIndex);
455+
456+
TypeCheckExprOptions options;
457+
if (leaveClosureBodiesUnchecked)
458+
options |= TypeCheckExprFlags::LeaveClosureBodyUnchecked;
459+
460+
TypeChecker::typeCheckPatternBinding(PBD, bindingIndex,
461+
/*patternType=*/Type(), options);
457462
}
458463

459464
bool swift::typeCheckASTNodeAtLoc(DeclContext *DC, SourceLoc TargetLoc) {

lib/Sema/TypeChecker.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ Type typeCheckParameterDefault(Expr *&defaultValue, DeclContext *DC,
400400

401401
void typeCheckTopLevelCodeDecl(TopLevelCodeDecl *TLCD);
402402

403-
void typeCheckDecl(Decl *D);
403+
void typeCheckDecl(Decl *D, bool LeaveClosureBodiesUnchecked = false);
404404

405405
void addImplicitDynamicAttribute(Decl *D);
406406
void checkDeclAttributes(Decl *D);
@@ -665,9 +665,11 @@ void coerceParameterListToType(ParameterList *P, AnyFunctionType *FN);
665665
bool typeCheckBinding(Pattern *&P, Expr *&Init, DeclContext *DC,
666666
Type patternType,
667667
PatternBindingDecl *PBD = nullptr,
668-
unsigned patternNumber = 0);
668+
unsigned patternNumber = 0,
669+
TypeCheckExprOptions options = {});
669670
bool typeCheckPatternBinding(PatternBindingDecl *PBD, unsigned patternNumber,
670-
Type patternType = Type());
671+
Type patternType = Type(),
672+
TypeCheckExprOptions options = {});
671673

672674
/// Type-check a for-each loop's pattern binding and sequence together.
673675
///

0 commit comments

Comments
 (0)