Skip to content

Commit a4eb591

Browse files
authored
Merge pull request #85142 from hamishknight/invalidate-skipped
[CS] Invalidate nested unresolved VarDecls when ignoring completion argument
2 parents b5627c9 + 62f0926 commit a4eb591

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

include/swift/AST/Expr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
506506
/// walk into the body of it (unless it is single-expression).
507507
void forEachChildExpr(llvm::function_ref<Expr *(Expr *)> callback);
508508

509+
/// Apply the specified function to all variables referenced in any
510+
/// child UnresolvedPatternExprs.
511+
void forEachUnresolvedVariable(llvm::function_ref<void(VarDecl *)> f) const;
512+
509513
/// Determine whether this expression refers to a type by name.
510514
///
511515
/// This distinguishes static references to types, like Int, from metatype

include/swift/AST/Pattern.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class alignas(8) Pattern : public ASTAllocated<Pattern> {
192192
/// Collect the set of variables referenced in the given pattern.
193193
void collectVariables(SmallVectorImpl<VarDecl *> &variables) const;
194194

195-
/// apply the specified function to all variables referenced in this
195+
/// Apply the specified function to all variables referenced in this
196196
/// pattern.
197197
void forEachVariable(llvm::function_ref<void(VarDecl *)> f) const;
198198

lib/AST/Pattern.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ namespace {
192192
const std::function<void(VarDecl*)> &fn;
193193
public:
194194

195-
WalkToVarDecls(const std::function<void(VarDecl*)> &fn)
196-
: fn(fn) {}
195+
WalkToVarDecls(const std::function<void(VarDecl*)> &fn) : fn(fn) {}
197196

198197
/// Walk everything that's available; there shouldn't be macro expansions
199198
/// that matter anyway.
@@ -239,6 +238,9 @@ namespace {
239238
};
240239
} // end anonymous namespace
241240

241+
void Expr::forEachUnresolvedVariable(llvm::function_ref<void(VarDecl *)> f) const {
242+
const_cast<Expr *>(this)->walk(WalkToVarDecls(f));
243+
}
242244

243245
/// apply the specified function to all variables referenced in this
244246
/// pattern.

lib/Sema/CSGen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4354,6 +4354,11 @@ namespace {
43544354
auto &CS = CG.getConstraintSystem();
43554355

43564356
if (CS.isArgumentIgnoredForCodeCompletion(expr)) {
4357+
// Make sure we invalidate any nested VarDecls to ensure the body
4358+
// VarDecl for a case statement still gets a type assigned.
4359+
expr->forEachUnresolvedVariable([&](VarDecl *VD) {
4360+
CS.setType(VD, ErrorType::get(CS.getASTContext()));
4361+
});
43574362
CG.setTypeForArgumentIgnoredForCompletion(expr);
43584363
return Action::SkipNode(expr);
43594364
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// {"kind":"complete","signature":"swift::constraints::ConstraintSystem::getType(swift::ASTNode) const","signatureAssert":"Assertion failed: (found != NodeTypes.end() && \"Expected type to have been set!\"), function getType"}
2-
// RUN: not --crash %target-swift-ide-test -code-completion -batch-code-completion -skip-filecheck -code-completion-diagnostics -source-filename %s
2+
// RUN: %target-swift-ide-test -code-completion -batch-code-completion -skip-filecheck -code-completion-diagnostics -source-filename %s
33
{ switch { case let c(#^COMPLETE^# b) a

0 commit comments

Comments
 (0)