[CS] Set the naming pattern in markInvalid

This normally gets populated by successful type-checking, we still want
to populate it if we fail though to avoid attempting to type-check the
parent statement again.
This commit is contained in:
Hamish Knight
2025-09-09 13:48:40 +01:00
parent 84847bcd06
commit 10ed17549c
3 changed files with 8 additions and 2 deletions

View File

@@ -307,6 +307,12 @@ void SyntacticElementTarget::markInvalid() const {
PreWalkResult<Pattern *> walkToPatternPre(Pattern *P) override {
P->setType(ErrorType::get(Ctx));
// For a named pattern, set it on the variable. This stops us from
// attempting to double-type-check variables we've already type-checked.
if (auto *NP = dyn_cast<NamedPattern>(P))
NP->getDecl()->setNamingPattern(NP);
return Action::Continue(P);
}

View File

@@ -4,7 +4,7 @@
func baz(y: [Int], z: Int) -> Int {
switch z {
case y[let z]: // expected-error 2{{'let' binding pattern cannot appear in an expression}}
case y[let z]: // expected-error {{'let' binding pattern cannot appear in an expression}}
z
default:
z

View File

@@ -406,7 +406,7 @@ func testNonBinding5(_ x: Int, y: [Int]) {
func testNonBinding6(y: [Int], z: Int) -> Int {
switch 0 {
// We treat 'z' here as a binding, which is invalid.
case let y[z]: // expected-error 2{{pattern variable binding cannot appear in an expression}}
case let y[z]: // expected-error {{pattern variable binding cannot appear in an expression}}
z
case y[z]: // This is fine
0