Skip to content

[CS] Don't walk into local decls in TypeVariableRefFinder #71276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ class TypeVariableRefFinder : public ASTWalker {
return Action::Continue(stmt);
}

PreWalkAction walkToDeclPre(Decl *D) override {
/// Decls get type-checked separately, except for PatternBindingDecls,
/// whose initializers we want to walk into.
return Action::VisitChildrenIf(isa<PatternBindingDecl>(D));
}

private:
DeclContext *currentClosureDC() const {
return ClosureDCs.empty() ? nullptr : ClosureDCs.back();
Expand Down
21 changes: 21 additions & 0 deletions test/Constraints/issue-71273.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-typecheck-verify-swift

// https://github.com/apple/swift/issues/71273

func bar<R>(_ fn: () -> R) {}

// Make sure we don't error here.
func testLocalFn() {
bar() {
func foo() -> Int { return 0 }
return ()
}
}

func testLocalBinding() {
bar() {
let _ = if .random() { return () } else { 0 }
// expected-error@-1 {{cannot 'return' in 'if' when used as expression}}
return ()
}
}