@@ -2671,18 +2671,28 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
2671
2671
// function, we unfortunately need to type-check everything since we need to
2672
2672
// apply the solution.
2673
2673
// FIXME: We ought to see if we can do better in that case.
2674
- if (auto *CE = DC->getInnermostClosureForCaptures ()) {
2675
- if (CE->getBodyState () == ClosureExpr::BodyState::Parsed) {
2676
- swift::typeCheckASTNodeAtLoc (
2677
- TypeCheckASTNodeAtLocContext::declContext (CE->getParent ()),
2678
- CE->getLoc ());
2679
-
2680
- // If the context itself is a ClosureExpr, we should have type-checked
2681
- // the completion expression now. If it's a nested local declaration,
2682
- // fall through to type-check the AST node now that we've type-checked
2683
- // the surrounding closure.
2684
- if (isa<ClosureExpr>(DC))
2685
- return false ;
2674
+ //
2675
+ // We don't need to do this for unattached nodes since we already would have
2676
+ // type-checked the surrounding context, and unattached nodes cannot be
2677
+ // typechecked via DeclContext since they aren't actually part of the AST.
2678
+ if (!typeCheckCtx.isForUnattachedNode ()) {
2679
+ if (auto *CE = DC->getInnermostClosureForCaptures ()) {
2680
+ // Walk up to the parent-most closure.
2681
+ while (auto *parent = dyn_cast_or_null<ClosureExpr>(CE->getParent ()))
2682
+ CE = parent;
2683
+
2684
+ if (CE->getBodyState () == ClosureExpr::BodyState::Parsed) {
2685
+ swift::typeCheckASTNodeAtLoc (
2686
+ TypeCheckASTNodeAtLocContext::declContext (CE->getParent ()),
2687
+ CE->getLoc ());
2688
+
2689
+ // If the context itself is a ClosureExpr, we should have type-checked
2690
+ // the completion expression now. If it's a nested local declaration,
2691
+ // fall through to type-check the AST node now that we've type-checked
2692
+ // the surrounding closure.
2693
+ if (isa<ClosureExpr>(DC))
2694
+ return false ;
2695
+ }
2686
2696
}
2687
2697
}
2688
2698
0 commit comments