Skip to content

Commit 1923037

Browse files
committed
[CS] Invalidate unresolved variables in ExprPatterns early
Make sure we invalidate when we initially visit the ExprPattern to ensure that we don't run into issues when generating constraints for a `where` clause before the constraints for the ExprPattern. We ought to change the constraint generation there to use a conjunction to ensure that the pattern is solved before the `where` clause, but I want to keep this as a quick low risk fix that we can cherry-pick. I'll switch it to a conjunction in a follow-up.
1 parent e7f5ca9 commit 1923037

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

lib/Sema/CSGen.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,6 +3167,16 @@ namespace {
31673167
}
31683168

31693169
case PatternKind::Expr: {
3170+
// Make sure we invalidate any nested VarDecls early since generating
3171+
// constraints for a `where` clause may happen before we've generated
3172+
// constraints for the ExprPattern. We'll record a fix when visiting
3173+
// the UnresolvedPatternExpr.
3174+
// FIXME: We ought to use a conjunction for switch cases, then we
3175+
// wouldn't need this logic.
3176+
auto *EP = cast<ExprPattern>(pattern);
3177+
EP->getSubExpr()->forEachUnresolvedVariable([&](VarDecl *VD) {
3178+
CS.setType(VD, ErrorType::get(CS.getASTContext()));
3179+
});
31703180
// We generate constraints for ExprPatterns in a separate pass. For
31713181
// now, just create a type variable.
31723182
return setType(CS.createTypeVariable(CS.getConstraintLocator(locator),
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// {"kind":"typecheck","signature":"swift::constraints::ConstraintSystem::getTypeOfReferencePre(swift::constraints::OverloadChoice, swift::DeclContext*, swift::constraints::ConstraintLocatorBuilder, swift::constraints::PreparedOverloadBuilder*)","signatureAssert":"Assertion failed: (func->isOperator() && \"Lookup should only find operators\"), function getTypeOfReferencePre"}
2+
// RUN: not %target-swift-frontend -typecheck %s
3+
{
4+
switch if .random() else {
5+
}
6+
{
7+
case let !a where a:
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// {"kind":"typecheck","signature":"swift::constraints::TypeVarRefCollector::walkToStmtPre(swift::Stmt*)","signatureAssert":"Assertion failed: (result), function getClosureType"}
2+
// RUN: not %target-swift-frontend -typecheck %s
3+
{
4+
switch if <#expression#> {
5+
return
6+
}
7+
{
8+
case let !a where a:
9+
}
10+
}

0 commit comments

Comments
 (0)