Skip to content
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

[ConstraintSystem] Handle pack expansion materialization on l-value base #64299

Merged
merged 2 commits into from Mar 11, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Sema/CSApply.cpp
Expand Up @@ -3415,6 +3415,14 @@ namespace {
}

case OverloadChoiceKind::MaterializePack: {
auto baseTy = solution.getResolvedType(base);

// Load the base tuple if necessary, materialization
// operates on r-value types only.
if (baseTy->is<LValueType>())
base = coerceToType(base, baseTy->getRValueType(),
cs.getConstraintLocator(base));

auto packType = solution.getResolvedType(expr);
return cs.cacheType(
MaterializePackExpr::create(cs.getASTContext(),
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Expand Up @@ -3506,7 +3506,13 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
break;

case OverloadChoiceKind::MaterializePack: {
auto *tuple = choice.getBaseType()->castTo<TupleType>();
// Since `.element` is only applicable to single element tuples at the
// moment we can just look through l-value base to load it.
//
// In the future, _if_ the syntax allows for multiple expansions
// this code would have to be adjusted to project l-value from the
// base type just like TupleIndex does.
auto tuple = choice.getBaseType()->getRValueType()->castTo<TupleType>();
auto *expansion = tuple->getElementType(0)->castTo<PackExpansionType>();
adjustedRefType = expansion->getPatternType();
refType = adjustedRefType;
Expand Down
18 changes: 18 additions & 0 deletions test/Constraints/pack-expansion-expressions.swift
Expand Up @@ -178,3 +178,21 @@ do {
test[data: repeat each args, "", 42] = 0
}
}

func test_pack_expansion_materialization_from_lvalue_base() {
struct Data<Value> {}

struct Test<each T> {
var data: (repeat Data<each T>)

init() {
self.data = (repeat Data<each T>())
_ = (repeat each data.element) // Ok

var tmp = (repeat Data<each T>()) // expected-warning {{never mutated}}
_ = (repeat each tmp.element) // Ok

// TODO: Add subscript test-case when syntax is supported.
}
}
}