@@ -687,6 +687,9 @@ class NodeAdder
687
687
688
688
NullablePtr<ASTScopeImpl> visitBraceStmt (BraceStmt *bs, ASTScopeImpl *p,
689
689
ScopeCreator &scopeCreator) {
690
+ if (bs->empty ())
691
+ return p;
692
+
690
693
SmallVector<ValueDecl *, 2 > localFuncsAndTypes;
691
694
SmallVector<VarDecl *, 2 > localVars;
692
695
@@ -941,24 +944,26 @@ ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
941
944
ASTScopeImpl *Scope::expandSpecifically (ScopeCreator &) { return this ; }
942
945
943
946
CREATES_NEW_INSERTION_POINT (ASTSourceFileScope)
944
- CREATES_NEW_INSERTION_POINT(ConditionalClauseScope)
945
947
CREATES_NEW_INSERTION_POINT(GuardStmtScope)
946
948
CREATES_NEW_INSERTION_POINT(PatternEntryDeclScope)
947
949
CREATES_NEW_INSERTION_POINT(GenericTypeOrExtensionScope)
948
950
CREATES_NEW_INSERTION_POINT(BraceStmtScope)
949
951
CREATES_NEW_INSERTION_POINT(TopLevelCodeScope)
952
+ CREATES_NEW_INSERTION_POINT(ConditionalClausePatternUseScope)
950
953
951
954
NO_NEW_INSERTION_POINT(FunctionBodyScope)
952
955
NO_NEW_INSERTION_POINT(AbstractFunctionDeclScope)
953
956
NO_NEW_INSERTION_POINT(AttachedPropertyWrapperScope)
954
957
NO_NEW_INSERTION_POINT(EnumElementScope)
958
+ NO_NEW_INSERTION_POINT(GuardStmtBodyScope)
955
959
NO_NEW_INSERTION_POINT(ParameterListScope)
956
960
NO_NEW_INSERTION_POINT(PatternEntryInitializerScope)
957
961
958
962
NO_NEW_INSERTION_POINT(CaptureListScope)
959
963
NO_NEW_INSERTION_POINT(CaseStmtScope)
960
964
NO_NEW_INSERTION_POINT(CaseLabelItemScope)
961
965
NO_NEW_INSERTION_POINT(CaseStmtBodyScope)
966
+ NO_NEW_INSERTION_POINT(ConditionalClauseInitializerScope)
962
967
NO_NEW_INSERTION_POINT(ClosureParametersScope)
963
968
NO_NEW_INSERTION_POINT(DefaultArgumentInitializerScope)
964
969
NO_NEW_INSERTION_POINT(DoStmtScope)
@@ -974,8 +979,6 @@ NO_NEW_INSERTION_POINT(WhileStmtScope)
974
979
NO_EXPANSION(GenericParamScope)
975
980
NO_EXPANSION(SpecializeAttributeScope)
976
981
NO_EXPANSION(DifferentiableAttributeScope)
977
- NO_EXPANSION(ConditionalClausePatternUseScope)
978
- NO_EXPANSION(LookupParentDiversionScope)
979
982
980
983
#undef CREATES_NEW_INSERTION_POINT
981
984
#undef NO_NEW_INSERTION_POINT
@@ -1057,42 +1060,49 @@ PatternEntryInitializerScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1057
1060
this );
1058
1061
}
1059
1062
1063
+
1060
1064
AnnotatedInsertionPoint
1061
- ConditionalClauseScope ::expandAScopeThatCreatesANewInsertionPoint (
1065
+ ConditionalClausePatternUseScope ::expandAScopeThatCreatesANewInsertionPoint (
1062
1066
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 );
1079
1088
}
1080
1089
1081
1090
AnnotatedInsertionPoint
1082
1091
GuardStmtScope::expandAScopeThatCreatesANewInsertionPoint (ScopeCreator &
1083
1092
scopeCreator) {
1084
-
1085
1093
ASTScopeImpl *conditionLookupParent =
1086
- createNestedConditionalClauseScopes (scopeCreator, stmt->getBody ());
1094
+ createNestedConditionalClauseScopes (scopeCreator, endLoc);
1095
+
1087
1096
// 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
+ }
1090
1104
1091
- auto *const lookupParentDiversionScope =
1092
- scopeCreator
1093
- .constructExpandAndInsertUncheckable <LookupParentDiversionScope>(
1094
- this , conditionLookupParent, stmt->getEndLoc (), endLoc);
1095
- return {lookupParentDiversionScope,
1105
+ return {conditionLookupParent,
1096
1106
" Succeeding code must be in scope of guard variables" };
1097
1107
}
1098
1108
@@ -1188,20 +1198,34 @@ void FunctionBodyScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1188
1198
1189
1199
void IfStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
1190
1200
ScopeCreator &scopeCreator) {
1201
+ auto *thenStmt = stmt->getThenStmt ();
1202
+ auto *elseStmt = stmt->getElseStmt ();
1203
+
1204
+ SourceLoc endLoc = thenStmt->getEndLoc ();
1191
1205
ASTScopeImpl *insertionPoint =
1192
- createNestedConditionalClauseScopes (scopeCreator, stmt-> getThenStmt () );
1206
+ createNestedConditionalClauseScopes (scopeCreator, endLoc );
1193
1207
1194
1208
// 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 ;
1196
1219
1197
1220
// Add the 'else' branch, if needed.
1198
- scopeCreator.addToScopeTree (stmt-> getElseStmt () , this );
1221
+ scopeCreator.addToScopeTree (elseStmt , this );
1199
1222
}
1200
1223
1201
1224
void WhileStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
1202
1225
ScopeCreator &scopeCreator) {
1226
+ SourceLoc endLoc = stmt->getBody ()->getEndLoc ();
1203
1227
ASTScopeImpl *insertionPoint =
1204
- createNestedConditionalClauseScopes (scopeCreator, stmt-> getBody () );
1228
+ createNestedConditionalClauseScopes (scopeCreator, endLoc );
1205
1229
scopeCreator.addToScopeTree (stmt->getBody (), insertionPoint);
1206
1230
}
1207
1231
@@ -1266,8 +1290,10 @@ void CaseStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1266
1290
}
1267
1291
}
1268
1292
1269
- scopeCreator.constructExpandAndInsertUncheckable <CaseStmtBodyScope>(
1270
- this , stmt);
1293
+ if (!stmt->getBody ()->empty ()) {
1294
+ scopeCreator.constructExpandAndInsertUncheckable <CaseStmtBodyScope>(
1295
+ this , stmt);
1296
+ }
1271
1297
}
1272
1298
1273
1299
void CaseLabelItemScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
@@ -1409,14 +1435,23 @@ TypeAliasScope::createTrailingWhereClauseScope(ASTScopeImpl *parent,
1409
1435
#pragma mark misc
1410
1436
1411
1437
ASTScopeImpl *LabeledConditionalStmtScope::createNestedConditionalClauseScopes (
1412
- ScopeCreator &scopeCreator, const Stmt * const afterConds ) {
1438
+ ScopeCreator &scopeCreator, SourceLoc endLoc ) {
1413
1439
auto *stmt = getLabeledConditionalStmt ();
1414
1440
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
+ }
1420
1455
}
1421
1456
return insertionPoint;
1422
1457
}
0 commit comments