Skip to content

Commit 2d20644

Browse files
authored
Merge pull request #64105 from hborla/pack-element-type-expr-folding
[PreCheckExpr] Allow pack references when resolving types during `TypeExpr` folding.
2 parents 791b2a0 + 34b720c commit 2d20644

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/Sema/CSGen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,6 +3078,12 @@ namespace {
30783078
if (auto *elementExpr = getAsExpr<PackElementExpr>(pack)) {
30793079
packType = CS.getType(elementExpr->getPackRefExpr());
30803080
} else if (auto *elementType = getAsTypeRepr<PackElementTypeRepr>(pack)) {
3081+
// OpenPackElementType sets types for 'each T' type reprs in
3082+
// expressions. Some invalid code won't make it there, and
3083+
// the constraint system won't have recorded a type.
3084+
if (!CS.hasType(elementType->getPackType()))
3085+
return Type();
3086+
30813087
packType = CS.getType(elementType->getPackType());
30823088
} else {
30833089
llvm_unreachable("unsupported pack reference ASTNode");

lib/Sema/PreCheckExpr.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,13 @@ TypeExpr *PreCheckExpression::simplifyNestedTypeExpr(UnresolvedDotExpr *UDE) {
14691469
// Fold 'T.U' into a nested type.
14701470

14711471
// Resolve the TypeRepr to get the base type for the lookup.
1472+
TypeResolutionOptions options(TypeResolverContext::InExpression);
1473+
// Pre-check always allows pack references during TypeExpr folding.
1474+
// CSGen will diagnose cases that appear outside of pack expansion
1475+
// expressions.
1476+
options |= TypeResolutionFlags::AllowPackReferences;
14721477
const auto BaseTy = TypeResolution::resolveContextualType(
1473-
InnerTypeRepr, DC, TypeResolverContext::InExpression,
1478+
InnerTypeRepr, DC, options,
14741479
[](auto unboundTy) {
14751480
// FIXME: Don't let unbound generic types escape type resolution.
14761481
// For now, just return the unbound generic type.

test/Constraints/pack-expansion-expressions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func typeReprPacks<each T>(_ t: repeat each T) where each T: ExpressibleByIntege
7070

7171
_ = Array<each T>() // expected-error {{pack reference 'T' can only appear in pack expansion or generic requirement}}
7272
_ = 1 as each T // expected-error {{pack reference 'T' can only appear in pack expansion or generic requirement}}
73+
repeat Invalid<String, each T>("") // expected-error {{cannot find 'Invalid' in scope}}
7374
}
7475

7576
func sameShapeDiagnostics<each T, each U>(t: repeat each T, u: repeat each U) {
@@ -105,3 +106,14 @@ func tupleExpansion<each T, each U>(
105106
_ = zip(repeat each tuple1.element, with: repeat each tuple2.element)
106107
// expected-error@-1 {{global function 'zip(_:with:)' requires the type packs 'U' and 'T' have the same shape}}
107108
}
109+
110+
protocol Generatable {
111+
static func generate() -> Self
112+
}
113+
114+
func generateTuple<each T : Generatable>() -> (repeat each T) {
115+
(each T).generate()
116+
// expected-error@-1 {{pack reference 'T' can only appear in pack expansion or generic requirement}}
117+
118+
return (repeat (each T).generate())
119+
}

0 commit comments

Comments
 (0)