Skip to content

Commit 8482516

Browse files
committed
[Constraint system] Properly deal with "as" patterns.
Teach pattern matching involving "as" patterns to work properly in function builders. The code almost handled this, but prematurely prechecking expressions in patterns broke it.
1 parent 6ad4b25 commit 8482516

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class BuilderClosureVisitor
331331
addChild(captureExpr(expr, /*oneWay=*/true, node.get<Expr *>()));
332332
}
333333

334-
if (!cs)
334+
if (!cs || hadError)
335335
return nullptr;
336336

337337
// Call Builder.buildBlock(... args ...)
@@ -1354,6 +1354,11 @@ class PreCheckFunctionBuilderApplication : public ASTWalker {
13541354
// Otherwise, recurse into the statement normally.
13551355
return std::make_pair(true, S);
13561356
}
1357+
1358+
/// Ignore patterns.
1359+
std::pair<bool, Pattern*> walkToPatternPre(Pattern *pat) override {
1360+
return { false, pat };
1361+
}
13571362
};
13581363

13591364
}

test/Constraints/function_builder.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,30 @@ tuplify(true) { c in
536536
// CHECK-SAME: Optional("matched without payload")
537537
// CHECK-SAME: "matched with payload", "hello!", 34
538538
// CHECK-SAME: "intentional mismatch"
539+
540+
class Super { }
541+
542+
class Sub : Super {
543+
func subMethod() -> String {
544+
return "subMethod"
545+
}
546+
}
547+
548+
func getSuper(wantSubclass: Bool) -> Super {
549+
return wantSubclass ? Sub() : Super()
550+
}
551+
552+
tuplify(true) { c in
553+
"testIfLetAsMatching"
554+
if case let sub as Sub = getSuper(wantSubclass: true) {
555+
sub.subMethod()
556+
}
557+
if case let sub as Sub = getSuper(wantSubclass: false) {
558+
fatalError("cannot match this")
559+
} else {
560+
"Superclass instance"
561+
}
562+
}
563+
// CHECK: testIfLetAsMatching
564+
// CHECK-SAME: "subMethod"
565+
// CHECK-SAME: "Superclass instance"

0 commit comments

Comments
 (0)