@@ -109,14 +109,16 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
109
109
SmallPtrSet<DeclRefExpr*, 4 > AlreadyDiagnosedBitCasts;
110
110
111
111
bool IsExprStmt;
112
+ unsigned ExprNestingDepth;
112
113
bool HasReachedSemanticsProvidingExpr;
113
114
114
115
ASTContext &Ctx;
115
116
const DeclContext *DC;
116
117
117
118
public:
118
119
DiagnoseWalker (const DeclContext *DC, bool isExprStmt)
119
- : IsExprStmt(isExprStmt), HasReachedSemanticsProvidingExpr(false ),
120
+ : IsExprStmt(isExprStmt), ExprNestingDepth(0 ),
121
+ HasReachedSemanticsProvidingExpr (false ),
120
122
Ctx(DC->getASTContext ()), DC(DC) {}
121
123
122
124
std::pair<bool , Pattern*> walkToPatternPre (Pattern *P) override {
@@ -130,6 +132,10 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
130
132
bool shouldWalkIntoTapExpression () override { return false ; }
131
133
132
134
std::pair<bool , Expr *> walkToExprPre (Expr *E) override {
135
+ if (isa<OpenExistentialExpr>(E)) {
136
+ // Don't increase ExprNestingDepth.
137
+ return { true , E };
138
+ }
133
139
134
140
if (auto collection = dyn_cast<CollectionExpr>(E)) {
135
141
if (collection->isTypeDefaulted ()) {
@@ -275,9 +281,11 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
275
281
}
276
282
277
283
// Diagnose 'self.init' or 'super.init' nested in another expression
278
- // or closure.
284
+ // or closure. The ExprNestingDepth thing is to allow this to be nested
285
+ // inside of an OpenExistentialExpr that is at the top level.
279
286
if (auto *rebindSelfExpr = dyn_cast<RebindSelfInConstructorExpr>(E)) {
280
- if (!Parent.isNull () || !IsExprStmt || DC->getParent ()->isLocalContext ()) {
287
+ if (ExprNestingDepth > 0 || !IsExprStmt ||
288
+ DC->getParent ()->isLocalContext ()) {
281
289
bool isChainToSuper;
282
290
(void )rebindSelfExpr->getCalledConstructor (isChainToSuper);
283
291
Ctx.Diags .diagnose (E->getLoc (), diag::init_delegation_nested,
@@ -346,9 +354,20 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
346
354
HasReachedSemanticsProvidingExpr = true ;
347
355
}
348
356
357
+ ++ExprNestingDepth;
358
+
349
359
return { true , E };
350
360
}
351
361
362
+ Expr *walkToExprPost (Expr *E) override {
363
+ if (isa<OpenExistentialExpr>(E))
364
+ return E;
365
+
366
+ assert (ExprNestingDepth != 0 );
367
+ --ExprNestingDepth;
368
+ return E;
369
+ }
370
+
352
371
// / Visit each component of the keypath and emit a diagnostic if they
353
372
// / refer to a member that has effects.
354
373
void checkForEffectfulKeyPath (KeyPathExpr *keyPath) {
@@ -1500,6 +1519,7 @@ static void diagRecursivePropertyAccess(const Expr *E, const DeclContext *DC) {
1500
1519
};
1501
1520
1502
1521
DiagnoseWalker walker (var, fn);
1522
+
1503
1523
const_cast <Expr *>(E)->walk(walker);
1504
1524
}
1505
1525
0 commit comments