[CS] Walk UnresolvedDeclRefExprs in UnresolvedVarCollector

This matches what we do in VarRefCollector, and is
needed because we currently delay the pre-checking
of patterns due to the fact that we don't resolve
them until CSGen. We ought to consider changing
this, but until then, adjust the logic here to
ensure we properly connect an ExprPattern that
references an outer var with any type variables
it may involve.

rdar://112264204
This commit is contained in:
Hamish Knight
2023-07-17 17:32:46 +01:00
parent aad2b6e3e0
commit 09bb346b03
2 changed files with 36 additions and 0 deletions

View File

@@ -275,6 +275,27 @@ public:
}
}
}
// FIXME: We can see UnresolvedDeclRefExprs here because we don't walk into
// patterns when running preCheckExpression, since we don't resolve patterns
// until CSGen. We ought to consider moving pattern resolution into
// pre-checking, which would allow us to pre-check patterns normally.
if (auto *declRef = dyn_cast<UnresolvedDeclRefExpr>(expr)) {
auto name = declRef->getName();
auto loc = declRef->getLoc();
if (name.isSimpleName() && loc.isValid()) {
auto *varDecl =
dyn_cast_or_null<VarDecl>(ASTScope::lookupSingleLocalDecl(
CS.DC->getParentSourceFile(), name.getFullName(), loc));
if (varDecl) {
if (auto varType = CS.getTypeIfAvailable(varDecl)) {
SmallPtrSet<TypeVariableType *, 4> typeVars;
varType->getTypeVariables(typeVars);
Vars.insert(typeVars.begin(), typeVars.end());
}
}
}
}
return Action::Continue(expr);
}

View File

@@ -0,0 +1,15 @@
// RUN: %target-typecheck-verify-swift
// rdar://112264204: Make sure we can type-check this.
func foo(_ fn: (Int) -> Void) {}
func bar(_ x: Int) {
foo { [x] y in
switch y {
case x:
()
default:
()
}
}
}