[CSSyntacticElement] Bring type variables from outer pack expansion into scope

If closure appears inside of a pack expansion, the elements
that reference pack elements have to bring expansion's shape
type in scope to make sure that the shapes match.
This commit is contained in:
Pavel Yaskevich
2024-05-22 14:43:10 -07:00
parent 888ab81c1e
commit bc3b2748bc
2 changed files with 12 additions and 3 deletions

View File

@@ -153,6 +153,17 @@ public:
} }
} }
// If closure appears inside of a pack expansion, the elements
// that reference pack elements have to bring expansion's shape
// type in scope to make sure that the shapes match.
if (auto *packElement = getAsExpr<PackElementExpr>(expr)) {
if (auto *outerEnvironment = CS.getPackEnvironment(packElement)) {
auto *expansionTy = CS.simplifyType(CS.getType(outerEnvironment))
->castTo<PackExpansionType>();
expansionTy->getCountType()->getTypeVariables(ReferencedVars);
}
}
return Action::Continue(expr); return Action::Continue(expr);
} }

View File

@@ -748,13 +748,11 @@ do {
(repeat takesClosure { each t }) // Ok (repeat takesClosure { each t }) // Ok
} }
// FIXME: multi-statement closures should type-check.
func testMultiStmtClosure<each T>(_ t: repeat each T) -> (repeat each T) { func testMultiStmtClosure<each T>(_ t: repeat each T) -> (repeat each T) {
(repeat takesClosure { (repeat takesClosure {
// expected-error@-1 {{pack expansion requires that '_' and 'each T' have the same shape}}
let v = each t let v = each t
return v return v
}) }) // Ok
} }
func takesAutoclosure<T>(_ fn: @autoclosure () -> T) -> T { return fn() } func takesAutoclosure<T>(_ fn: @autoclosure () -> T) -> T { return fn() }