Skip to content

Commit 39a902b

Browse files
committed
SIL: Update lowerCaptureContextParameters() for PackElementExpr captures
1 parent beca517 commit 39a902b

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
@@ -1970,33 +1970,39 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
19701970
continue;
19711971
}
19721972

1973-
auto *varDecl = cast<VarDecl>(capture.getDecl());
1973+
auto options = SILParameterInfo::Options();
1974+
1975+
Type type;
1976+
VarDecl *varDecl = nullptr;
1977+
if (auto *expr = capture.getPackElement()) {
1978+
type = expr->getType();
1979+
} else {
1980+
varDecl = cast<VarDecl>(capture.getDecl());
1981+
type = varDecl->getTypeInContext();
1982+
1983+
// If we're capturing a parameter pack, wrap it in a tuple.
1984+
if (type->is<PackExpansionType>()) {
1985+
assert(!cast<ParamDecl>(varDecl)->supportsMutation() &&
1986+
"Cannot capture a pack as an lvalue");
1987+
1988+
SmallVector<TupleTypeElt, 1> elts;
1989+
elts.push_back(type);
1990+
type = TupleType::get(elts, TC.Context);
1991+
}
1992+
1993+
if (isolatedParam == varDecl) {
1994+
options |= SILParameterInfo::Isolated;
1995+
isolatedParam = nullptr;
1996+
}
1997+
}
19741998

1975-
auto type = varDecl->getTypeInContext();
19761999
assert(!type->hasLocalArchetype() ||
19772000
(genericSig && origGenericSig &&
19782001
!genericSig->isEqual(origGenericSig)));
19792002
type = mapTypeOutOfContext(type);
19802003

19812004
auto canType = type->getReducedType(
19822005
genericSig ? genericSig : origGenericSig);
1983-
1984-
auto options = SILParameterInfo::Options();
1985-
if (isolatedParam == varDecl) {
1986-
options |= SILParameterInfo::Isolated;
1987-
isolatedParam = nullptr;
1988-
}
1989-
1990-
// If we're capturing a parameter pack, wrap it in a tuple.
1991-
if (isa<PackExpansionType>(canType)) {
1992-
assert(!cast<ParamDecl>(varDecl)->supportsMutation() &&
1993-
"Cannot capture a pack as an lvalue");
1994-
1995-
SmallVector<TupleTypeElt, 1> elts;
1996-
elts.push_back(canType);
1997-
canType = CanTupleType(TupleType::get(elts, TC.Context));
1998-
}
1999-
20002006
auto &loweredTL =
20012007
TC.getTypeLowering(AbstractionPattern(genericSig, canType), canType,
20022008
expansion);
@@ -2018,6 +2024,8 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
20182024
break;
20192025
}
20202026
case CaptureKind::Box: {
2027+
assert(varDecl);
2028+
20212029
// The type in the box is lowered in the minimal context.
20222030
auto minimalLoweredTy =
20232031
TC.getTypeLowering(AbstractionPattern(genericSig, canType), canType,
@@ -2035,6 +2043,8 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
20352043
break;
20362044
}
20372045
case CaptureKind::ImmutableBox: {
2046+
assert(varDecl);
2047+
20382048
// The type in the box is lowered in the minimal context.
20392049
auto minimalLoweredTy =
20402050
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)