Skip to content

Commit a6a10a7

Browse files
committed
SIL: Update lowerCaptureContextParameters() for PackElementExpr captures
1 parent 8b030e1 commit a6a10a7

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,33 +2007,39 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
20072007
continue;
20082008
}
20092009

2010-
auto *varDecl = cast<VarDecl>(capture.getDecl());
2010+
auto options = SILParameterInfo::Options();
2011+
2012+
Type type;
2013+
VarDecl *varDecl = nullptr;
2014+
if (auto *expr = capture.getPackElement()) {
2015+
type = expr->getType();
2016+
} else {
2017+
varDecl = cast<VarDecl>(capture.getDecl());
2018+
type = varDecl->getTypeInContext();
2019+
2020+
// If we're capturing a parameter pack, wrap it in a tuple.
2021+
if (type->is<PackExpansionType>()) {
2022+
assert(!cast<ParamDecl>(varDecl)->supportsMutation() &&
2023+
"Cannot capture a pack as an lvalue");
2024+
2025+
SmallVector<TupleTypeElt, 1> elts;
2026+
elts.push_back(type);
2027+
type = TupleType::get(elts, TC.Context);
2028+
}
2029+
2030+
if (isolatedParam == varDecl) {
2031+
options |= SILParameterInfo::Isolated;
2032+
isolatedParam = nullptr;
2033+
}
2034+
}
20112035

2012-
auto type = varDecl->getTypeInContext();
20132036
assert(!type->hasLocalArchetype() ||
20142037
(genericSig && origGenericSig &&
20152038
!genericSig->isEqual(origGenericSig)));
20162039
type = mapTypeOutOfContext(type);
20172040

20182041
auto canType = type->getReducedType(
20192042
genericSig ? genericSig : origGenericSig);
2020-
2021-
auto options = SILParameterInfo::Options();
2022-
if (isolatedParam == varDecl) {
2023-
options |= SILParameterInfo::Isolated;
2024-
isolatedParam = nullptr;
2025-
}
2026-
2027-
// If we're capturing a parameter pack, wrap it in a tuple.
2028-
if (isa<PackExpansionType>(canType)) {
2029-
assert(!cast<ParamDecl>(varDecl)->supportsMutation() &&
2030-
"Cannot capture a pack as an lvalue");
2031-
2032-
SmallVector<TupleTypeElt, 1> elts;
2033-
elts.push_back(canType);
2034-
canType = CanTupleType(TupleType::get(elts, TC.Context));
2035-
}
2036-
20372043
auto &loweredTL =
20382044
TC.getTypeLowering(AbstractionPattern(genericSig, canType), canType,
20392045
expansion);
@@ -2055,6 +2061,8 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
20552061
break;
20562062
}
20572063
case CaptureKind::Box: {
2064+
assert(varDecl);
2065+
20582066
// The type in the box is lowered in the minimal context.
20592067
auto minimalLoweredTy =
20602068
TC.getTypeLowering(AbstractionPattern(genericSig, canType), canType,
@@ -2072,6 +2080,8 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
20722080
break;
20732081
}
20742082
case CaptureKind::ImmutableBox: {
2083+
assert(varDecl);
2084+
20752085
// The type in the box is lowered in the minimal context.
20762086
auto minimalLoweredTy =
20772087
TC.getTypeLowering(AbstractionPattern(genericSig, canType), canType,

lib/SIL/IR/TypeLowering.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ static bool hasSingletonMetatype(CanType instanceType) {
118118

119119
CaptureKind TypeConverter::getDeclCaptureKind(CapturedValue capture,
120120
TypeExpansionContext expansion) {
121+
if (auto *expr = capture.getPackElement()) {
122+
auto contextTy = expr->getType();
123+
auto &lowering = getTypeLowering(
124+
contextTy, TypeExpansionContext::noOpaqueTypeArchetypesSubstitution(
125+
expansion.getResilienceExpansion()));
126+
127+
assert(!contextTy->isNoncopyable() && "Not implemented");
128+
if (!lowering.isAddressOnly())
129+
return CaptureKind::Constant;
130+
131+
return CaptureKind::Immutable;
132+
}
133+
121134
auto decl = capture.getDecl();
122135
auto *var = cast<VarDecl>(decl);
123136
assert(var->hasStorage() &&

0 commit comments

Comments
 (0)