Skip to content

Commit 62d25ba

Browse files
committed
[ConstraintSystem] Variadics: Pack expansion materialization operates on loaded tuples
If the base tuple of `.element` is l-value, it has to be loaded before pack expansion could be materialized.
1 parent b7a2fa2 commit 62d25ba

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3506,7 +3506,13 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
35063506
break;
35073507

35083508
case OverloadChoiceKind::MaterializePack: {
3509-
auto *tuple = choice.getBaseType()->castTo<TupleType>();
3509+
// Since `.element` is only applicable to single element tuples at the
3510+
// moment we can just look through l-value base to load it.
3511+
//
3512+
// In the future, _if_ the syntax allows for multiple expansions
3513+
// this code would have to be adjusted to project l-value from the
3514+
// base type just like TupleIndex does.
3515+
auto tuple = choice.getBaseType()->getRValueType()->castTo<TupleType>();
35103516
auto *expansion = tuple->getElementType(0)->castTo<PackExpansionType>();
35113517
adjustedRefType = expansion->getPatternType();
35123518
refType = adjustedRefType;

test/Constraints/pack-expansion-expressions.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,21 @@ do {
178178
test[data: repeat each args, "", 42] = 0
179179
}
180180
}
181+
182+
func test_pack_expansion_materialization_from_lvalue_base() {
183+
struct Data<Value> {}
184+
185+
struct Test<each T> {
186+
var data: (repeat Data<each T>)
187+
188+
init() {
189+
self.data = (repeat Data<each T>())
190+
_ = (repeat each data.element) // Ok
191+
192+
var tmp = (repeat Data<each T>()) // expected-warning {{never mutated}}
193+
_ = (repeat each tmp.element) // Ok
194+
195+
// TODO: Add subscript test-case when syntax is supported.
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)