@@ -687,6 +687,9 @@ class NodeAdder
687687
688688 NullablePtr<ASTScopeImpl> visitBraceStmt (BraceStmt *bs, ASTScopeImpl *p,
689689 ScopeCreator &scopeCreator) {
690+ if (bs->empty ())
691+ return p;
692+
690693 SmallVector<ValueDecl *, 2 > localFuncsAndTypes;
691694 SmallVector<VarDecl *, 2 > localVars;
692695
@@ -941,24 +944,26 @@ ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
941944 ASTScopeImpl *Scope::expandSpecifically (ScopeCreator &) { return this ; }
942945
943946CREATES_NEW_INSERTION_POINT (ASTSourceFileScope)
944- CREATES_NEW_INSERTION_POINT(ConditionalClauseScope)
945947CREATES_NEW_INSERTION_POINT(GuardStmtScope)
946948CREATES_NEW_INSERTION_POINT(PatternEntryDeclScope)
947949CREATES_NEW_INSERTION_POINT(GenericTypeOrExtensionScope)
948950CREATES_NEW_INSERTION_POINT(BraceStmtScope)
949951CREATES_NEW_INSERTION_POINT(TopLevelCodeScope)
952+ CREATES_NEW_INSERTION_POINT(ConditionalClausePatternUseScope)
950953
951954NO_NEW_INSERTION_POINT(FunctionBodyScope)
952955NO_NEW_INSERTION_POINT(AbstractFunctionDeclScope)
953956NO_NEW_INSERTION_POINT(AttachedPropertyWrapperScope)
954957NO_NEW_INSERTION_POINT(EnumElementScope)
958+ NO_NEW_INSERTION_POINT(GuardStmtBodyScope)
955959NO_NEW_INSERTION_POINT(ParameterListScope)
956960NO_NEW_INSERTION_POINT(PatternEntryInitializerScope)
957961
958962NO_NEW_INSERTION_POINT(CaptureListScope)
959963NO_NEW_INSERTION_POINT(CaseStmtScope)
960964NO_NEW_INSERTION_POINT(CaseLabelItemScope)
961965NO_NEW_INSERTION_POINT(CaseStmtBodyScope)
966+ NO_NEW_INSERTION_POINT(ConditionalClauseInitializerScope)
962967NO_NEW_INSERTION_POINT(ClosureParametersScope)
963968NO_NEW_INSERTION_POINT(DefaultArgumentInitializerScope)
964969NO_NEW_INSERTION_POINT(DoStmtScope)
@@ -974,8 +979,6 @@ NO_NEW_INSERTION_POINT(WhileStmtScope)
974979NO_EXPANSION(GenericParamScope)
975980NO_EXPANSION(SpecializeAttributeScope)
976981NO_EXPANSION(DifferentiableAttributeScope)
977- NO_EXPANSION(ConditionalClausePatternUseScope)
978- NO_EXPANSION(LookupParentDiversionScope)
979982
980983#undef CREATES_NEW_INSERTION_POINT
981984#undef NO_NEW_INSERTION_POINT
@@ -1057,42 +1060,49 @@ PatternEntryInitializerScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
10571060 this );
10581061}
10591062
1063+
10601064AnnotatedInsertionPoint
1061- ConditionalClauseScope ::expandAScopeThatCreatesANewInsertionPoint (
1065+ ConditionalClausePatternUseScope ::expandAScopeThatCreatesANewInsertionPoint (
10621066 ScopeCreator &scopeCreator) {
1063- const StmtConditionElement &sec = getStmtConditionElement ();
1064- switch (sec.getKind ()) {
1065- case StmtConditionElement::CK_Availability:
1066- return {this , " No introduced variables" };
1067- case StmtConditionElement::CK_Boolean:
1068- scopeCreator.addToScopeTree (sec.getBoolean (), this );
1069- return {this , " No introduced variables" };
1070- case StmtConditionElement::CK_PatternBinding:
1071- scopeCreator.addToScopeTree (sec.getInitializer (), this );
1072- auto *const ccPatternUseScope =
1073- scopeCreator.constructExpandAndInsertUncheckable <
1074- ConditionalClausePatternUseScope>(this , sec.getPattern (), endLoc);
1075- return {ccPatternUseScope,
1076- " Succeeding code must be in scope of conditional variables" };
1077- }
1078- ASTScope_unreachable (" Unhandled StmtConditionKind in switch" );
1067+ auto *initializer = sec.getInitializer ();
1068+ if (!isa<ErrorExpr>(initializer)) {
1069+ scopeCreator
1070+ .constructExpandAndInsertUncheckable <ConditionalClauseInitializerScope>(
1071+ this , initializer);
1072+ }
1073+
1074+ return {this ,
1075+ " Succeeding code must be in scope of conditional clause pattern bindings" };
1076+ }
1077+
1078+ void
1079+ ConditionalClauseInitializerScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
1080+ ScopeCreator &scopeCreator) {
1081+ scopeCreator.addToScopeTree (ASTNode (initializer), this );
1082+ }
1083+
1084+ void
1085+ GuardStmtBodyScope::expandAScopeThatDoesNotCreateANewInsertionPoint (ScopeCreator &
1086+ scopeCreator) {
1087+ scopeCreator.addToScopeTree (ASTNode (body), this );
10791088}
10801089
10811090AnnotatedInsertionPoint
10821091GuardStmtScope::expandAScopeThatCreatesANewInsertionPoint (ScopeCreator &
10831092 scopeCreator) {
1084-
10851093 ASTScopeImpl *conditionLookupParent =
1086- createNestedConditionalClauseScopes (scopeCreator, stmt->getBody ());
1094+ createNestedConditionalClauseScopes (scopeCreator, endLoc);
1095+
10871096 // Add a child for the 'guard' body, which always exits.
1088- // Parent is whole guard stmt scope, NOT the cond scopes
1089- scopeCreator.addToScopeTree (stmt->getBody (), this );
1097+ // The lookup parent is whole guard stmt scope, NOT the cond scopes
1098+ auto *body = stmt->getBody ();
1099+ if (!body->empty ()) {
1100+ scopeCreator
1101+ .constructExpandAndInsertUncheckable <GuardStmtBodyScope>(
1102+ conditionLookupParent, this , stmt->getBody ());
1103+ }
10901104
1091- auto *const lookupParentDiversionScope =
1092- scopeCreator
1093- .constructExpandAndInsertUncheckable <LookupParentDiversionScope>(
1094- this , conditionLookupParent, stmt->getEndLoc (), endLoc);
1095- return {lookupParentDiversionScope,
1105+ return {conditionLookupParent,
10961106 " Succeeding code must be in scope of guard variables" };
10971107}
10981108
@@ -1188,20 +1198,34 @@ void FunctionBodyScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
11881198
11891199void IfStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
11901200 ScopeCreator &scopeCreator) {
1201+ auto *thenStmt = stmt->getThenStmt ();
1202+ auto *elseStmt = stmt->getElseStmt ();
1203+
1204+ SourceLoc endLoc = thenStmt->getEndLoc ();
11911205 ASTScopeImpl *insertionPoint =
1192- createNestedConditionalClauseScopes (scopeCreator, stmt-> getThenStmt () );
1206+ createNestedConditionalClauseScopes (scopeCreator, endLoc );
11931207
11941208 // The 'then' branch
1195- scopeCreator.addToScopeTree (stmt->getThenStmt (), insertionPoint);
1209+ scopeCreator.addToScopeTree (thenStmt, insertionPoint);
1210+
1211+ // Result builders can add an 'else' block consisting entirely of
1212+ // implicit expressions. In this case, the end location of the
1213+ // 'then' block is equal to the start location of the 'else'
1214+ // block, and the 'else' block source range is empty.
1215+ if (elseStmt &&
1216+ thenStmt->getEndLoc () == elseStmt->getStartLoc () &&
1217+ elseStmt->getStartLoc () == elseStmt->getEndLoc ())
1218+ return ;
11961219
11971220 // Add the 'else' branch, if needed.
1198- scopeCreator.addToScopeTree (stmt-> getElseStmt () , this );
1221+ scopeCreator.addToScopeTree (elseStmt , this );
11991222}
12001223
12011224void WhileStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
12021225 ScopeCreator &scopeCreator) {
1226+ SourceLoc endLoc = stmt->getBody ()->getEndLoc ();
12031227 ASTScopeImpl *insertionPoint =
1204- createNestedConditionalClauseScopes (scopeCreator, stmt-> getBody () );
1228+ createNestedConditionalClauseScopes (scopeCreator, endLoc );
12051229 scopeCreator.addToScopeTree (stmt->getBody (), insertionPoint);
12061230}
12071231
@@ -1266,8 +1290,10 @@ void CaseStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
12661290 }
12671291 }
12681292
1269- scopeCreator.constructExpandAndInsertUncheckable <CaseStmtBodyScope>(
1270- this , stmt);
1293+ if (!stmt->getBody ()->empty ()) {
1294+ scopeCreator.constructExpandAndInsertUncheckable <CaseStmtBodyScope>(
1295+ this , stmt);
1296+ }
12711297}
12721298
12731299void CaseLabelItemScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
@@ -1409,14 +1435,23 @@ TypeAliasScope::createTrailingWhereClauseScope(ASTScopeImpl *parent,
14091435#pragma mark misc
14101436
14111437ASTScopeImpl *LabeledConditionalStmtScope::createNestedConditionalClauseScopes (
1412- ScopeCreator &scopeCreator, const Stmt * const afterConds ) {
1438+ ScopeCreator &scopeCreator, SourceLoc endLoc ) {
14131439 auto *stmt = getLabeledConditionalStmt ();
14141440 ASTScopeImpl *insertionPoint = this ;
1415- for (unsigned i = 0 ; i < stmt->getCond ().size (); ++i) {
1416- insertionPoint =
1417- scopeCreator
1418- .constructExpandAndInsertUncheckable <ConditionalClauseScope>(
1419- insertionPoint, stmt, i, afterConds->getStartLoc ());
1441+ for (auto &sec : stmt->getCond ()) {
1442+ switch (sec.getKind ()) {
1443+ case StmtConditionElement::CK_Availability:
1444+ break ;
1445+ case StmtConditionElement::CK_Boolean:
1446+ scopeCreator.addToScopeTree (sec.getBoolean (), insertionPoint);
1447+ break ;
1448+ case StmtConditionElement::CK_PatternBinding:
1449+ insertionPoint =
1450+ scopeCreator.constructExpandAndInsertUncheckable <
1451+ ConditionalClausePatternUseScope>(
1452+ insertionPoint, sec, endLoc);
1453+ break ;
1454+ }
14201455 }
14211456 return insertionPoint;
14221457}
0 commit comments