@@ -327,50 +327,40 @@ void ParentConditionalConformance::diagnoseConformanceStack(
327
327
}
328
328
329
329
namespace {
330
- // / Produce any additional syntactic diagnostics for the body of a function
331
- // / that had a result builder applied.
332
- class FunctionSyntacticDiagnosticWalker : public ASTWalker {
333
- SmallVector<DeclContext *, 4 > dcStack;
330
+ // / Produce any additional syntactic diagnostics for a SyntacticElementTarget.
331
+ class SyntacticDiagnosticWalker final : public ASTWalker {
332
+ const SyntacticElementTarget &Target;
333
+ bool IsTopLevelExprStmt;
334
+
335
+ SyntacticDiagnosticWalker (const SyntacticElementTarget &target,
336
+ bool isExprStmt)
337
+ : Target(target), IsTopLevelExprStmt(isExprStmt) {}
334
338
335
339
public:
336
- FunctionSyntacticDiagnosticWalker (DeclContext *dc) { dcStack.push_back (dc); }
340
+ static void check (const SyntacticElementTarget &target, bool isExprStmt) {
341
+ auto walker = SyntacticDiagnosticWalker (target, isExprStmt);
342
+ target.walk (walker);
343
+ }
337
344
338
345
MacroWalking getMacroWalkingBehavior () const override {
339
346
return MacroWalking::Expansion;
340
347
}
341
348
342
349
PreWalkResult<Expr *> walkToExprPre (Expr *expr) override {
343
- performSyntacticExprDiagnostics (expr, dcStack.back (), /* isExprStmt=*/ false );
344
-
345
- if (auto closure = dyn_cast<ClosureExpr>(expr)) {
346
- if (closure->isSeparatelyTypeChecked ()) {
347
- dcStack.push_back (closure);
348
- return Action::Continue (expr);
349
- }
350
- }
351
-
350
+ auto isExprStmt = (expr == Target.getAsExpr ()) ? IsTopLevelExprStmt : false ;
351
+ performSyntacticExprDiagnostics (expr, Target.getDeclContext (), isExprStmt);
352
352
return Action::SkipNode (expr);
353
353
}
354
354
355
- PostWalkResult<Expr *> walkToExprPost (Expr *expr) override {
356
- if (auto closure = dyn_cast<ClosureExpr>(expr)) {
357
- if (closure->isSeparatelyTypeChecked ()) {
358
- assert (dcStack.back () == closure);
359
- dcStack.pop_back ();
360
- }
361
- }
362
-
363
- return Action::Continue (expr);
364
- }
365
-
366
355
PreWalkResult<Stmt *> walkToStmtPre (Stmt *stmt) override {
367
- performStmtDiagnostics (stmt, dcStack. back ());
356
+ performStmtDiagnostics (stmt, Target. getDeclContext ());
368
357
return Action::Continue (stmt);
369
358
}
370
359
371
- PreWalkResult<Pattern *> walkToPatternPre (Pattern *pattern ) override {
372
- return Action::SkipNode (pattern );
360
+ PreWalkAction walkToDeclPre (Decl *D ) override {
361
+ return Action::VisitNodeIf (isa<PatternBindingDecl>(D) );
373
362
}
363
+
374
364
PreWalkAction walkToTypeReprPre (TypeRepr *typeRepr) override {
375
365
return Action::SkipNode ();
376
366
}
@@ -382,41 +372,7 @@ class FunctionSyntacticDiagnosticWalker : public ASTWalker {
382
372
383
373
void constraints::performSyntacticDiagnosticsForTarget (
384
374
const SyntacticElementTarget &target, bool isExprStmt) {
385
- auto *dc = target.getDeclContext ();
386
- switch (target.kind ) {
387
- case SyntacticElementTarget::Kind::expression: {
388
- // First emit diagnostics for the main expression.
389
- performSyntacticExprDiagnostics (target.getAsExpr (), dc, isExprStmt);
390
- return ;
391
- }
392
-
393
- case SyntacticElementTarget::Kind::forEachPreamble: {
394
- auto *stmt = target.getAsForEachStmt ();
395
-
396
- // First emit diagnostics for the main expression.
397
- performSyntacticExprDiagnostics (stmt->getTypeCheckedSequence (), dc,
398
- isExprStmt);
399
-
400
- if (auto *whereExpr = stmt->getWhere ())
401
- performSyntacticExprDiagnostics (whereExpr, dc, /* isExprStmt*/ false );
402
- return ;
403
- }
404
-
405
- case SyntacticElementTarget::Kind::function: {
406
- auto *body = target.getFunctionBody ();
407
- FunctionSyntacticDiagnosticWalker walker (dc);
408
- body->walk (walker);
409
- return ;
410
- }
411
- case SyntacticElementTarget::Kind::closure:
412
- case SyntacticElementTarget::Kind::stmtCondition:
413
- case SyntacticElementTarget::Kind::caseLabelItem:
414
- case SyntacticElementTarget::Kind::patternBinding:
415
- case SyntacticElementTarget::Kind::uninitializedVar:
416
- // Nothing to do for these.
417
- return ;
418
- }
419
- llvm_unreachable (" Unhandled case in switch!" );
375
+ SyntacticDiagnosticWalker::check (target, isExprStmt);
420
376
}
421
377
422
378
#pragma mark High-level entry points
0 commit comments