Skip to content

Commit 8214e18

Browse files
committed
SILGen: Clean up invariants around capture lists
1 parent e462626 commit 8214e18

File tree

4 files changed

+20
-64
lines changed

4 files changed

+20
-64
lines changed

include/swift/SIL/TypeLowering.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -946,24 +946,6 @@ class TypeConverter {
946946
CaptureInfo getLoweredLocalCaptures(SILDeclRef fn);
947947
bool hasLoweredLocalCaptures(SILDeclRef fn);
948948

949-
#ifndef NDEBUG
950-
/// If \c false, \c childDC is in a context it cannot capture variables from,
951-
/// so it is expected that Sema may not have computed its \c CaptureInfo.
952-
///
953-
/// This call exists for use in assertions; do not use it to skip capture
954-
/// processing.
955-
static bool canCaptureFromParent(DeclContext *childDC) {
956-
// This call was added because Sema leaves the captures of functions that
957-
// cannot capture anything uncomputed.
958-
// TODO: Make Sema set them to CaptureInfo::empty() instead.
959-
960-
if (childDC)
961-
if (auto decl = childDC->getAsDecl())
962-
return decl->getDeclContext()->isLocalContext();
963-
return true;
964-
}
965-
#endif
966-
967949
enum class ABIDifference : uint8_t {
968950
// Types have compatible calling conventions and representations, so can
969951
// be trivially bitcast.

lib/SIL/TypeLowering.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,20 @@ CaptureInfo
21722172
TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
21732173
PrettyStackTraceSILLocation stack("getting lowered local captures",
21742174
fn.getAsRegularLocation(), Context);
2175+
// If we're guaranteed to never have local captures, bail out now.
2176+
switch (fn.kind) {
2177+
case SILDeclRef::Kind::StoredPropertyInitializer:
2178+
case SILDeclRef::Kind::PropertyWrapperBackingInitializer:
2179+
return CaptureInfo::empty();
2180+
2181+
default:
2182+
if (fn.hasDecl()) {
2183+
if (!fn.getDecl()->isLocalCapture())
2184+
return CaptureInfo::empty();
2185+
}
2186+
2187+
break;
2188+
}
21752189

21762190
fn.isForeign = 0;
21772191
fn.isCurried = 0;
@@ -2199,8 +2213,7 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
21992213
std::function<void (SILDeclRef)> collectConstantCaptures;
22002214

22012215
collectCaptures = [&](CaptureInfo captureInfo, DeclContext *dc) {
2202-
assert(captureInfo.hasBeenComputed() ||
2203-
!TypeConverter::canCaptureFromParent(dc));
2216+
assert(captureInfo.hasBeenComputed());
22042217

22052218
if (captureInfo.hasGenericParamCaptures())
22062219
capturesGenericParams = true;
@@ -2325,6 +2338,9 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
23252338
};
23262339

23272340
collectFunctionCaptures = [&](AnyFunctionRef curFn) {
2341+
if (!curFn.getBody())
2342+
return;
2343+
23282344
if (!visitedFunctions.insert(curFn).second)
23292345
return;
23302346

@@ -2392,15 +2408,6 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
23922408
resultingCaptures.push_back(*selfCapture);
23932409
}
23942410

2395-
// It is an error for a member of a nominal type to have any captures
2396-
// at all. If we just clear them out here, SILGen will diagnose the
2397-
// problem.
2398-
if (fn.hasDecl()) {
2399-
auto *dc = fn.getDecl()->getDeclContext();
2400-
if (dc->isTypeContext())
2401-
resultingCaptures.clear();
2402-
}
2403-
24042411
// Cache the uniqued set of transitive captures.
24052412
CaptureInfo info{Context, resultingCaptures, capturesDynamicSelf,
24062413
capturesOpaqueValue, capturesGenericParams};

lib/SILGen/SILGenFunction.cpp

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -695,31 +695,6 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
695695
}
696696
}
697697

698-
#ifndef NDEBUG
699-
/// If \c false, \c function is either a declaration that inherently cannot
700-
/// capture variables, or it is in a context it cannot capture variables from.
701-
/// In either case, it is expected that Sema may not have computed its
702-
/// \c CaptureInfo.
703-
///
704-
/// This call exists for use in assertions; do not use it to skip capture
705-
/// processing.
706-
static bool canCaptureFromParent(SILDeclRef function) {
707-
switch (function.kind) {
708-
case SILDeclRef::Kind::StoredPropertyInitializer:
709-
case SILDeclRef::Kind::PropertyWrapperBackingInitializer:
710-
return false;
711-
712-
default:
713-
if (function.hasDecl()) {
714-
if (auto dc = dyn_cast<DeclContext>(function.getDecl())) {
715-
return TypeConverter::canCaptureFromParent(dc);
716-
}
717-
}
718-
return false;
719-
}
720-
}
721-
#endif
722-
723698
void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value,
724699
bool EmitProfilerIncrement) {
725700
auto *dc = function.getDecl()->getInnermostDeclContext();
@@ -757,14 +732,7 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value,
757732
params = ParameterList::create(ctx, SourceLoc(), {param}, SourceLoc());
758733
}
759734

760-
CaptureInfo captureInfo;
761-
if (function.getAnyFunctionRef())
762-
captureInfo = SGM.M.Types.getLoweredLocalCaptures(function);
763-
else {
764-
assert(!canCaptureFromParent(function));
765-
captureInfo = CaptureInfo::empty();
766-
}
767-
735+
auto captureInfo = SGM.M.Types.getLoweredLocalCaptures(function);
768736
auto interfaceType = value->getType()->mapTypeOutOfContext();
769737
emitProlog(captureInfo, params, /*selfParam=*/nullptr,
770738
dc, interfaceType, /*throws=*/false, SourceLoc());

lib/SILGen/SILGenProlog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,7 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
445445

446446
// Emit the capture argument variables. These are placed last because they
447447
// become the first curry level of the SIL function.
448-
assert((captureInfo.hasBeenComputed() ||
449-
!TypeConverter::canCaptureFromParent(DC)) &&
448+
assert(captureInfo.hasBeenComputed() &&
450449
"can't emit prolog of function with uncomputed captures");
451450
for (auto capture : captureInfo.getCaptures()) {
452451
if (capture.isDynamicSelfMetadata()) {

0 commit comments

Comments
 (0)