Skip to content

Commit 40b757a

Browse files
committed
SILGen: Use TypeConverter::getSubstitutionMapWithCapturedEnvironments()
1 parent 9d1e50e commit 40b757a

File tree

3 files changed

+20
-34
lines changed

3 files changed

+20
-34
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,9 +1289,8 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
12891289
auto captureInfo = SGF.SGM.Types.getLoweredLocalCaptures(constant);
12901290
SGF.SGM.Types.setCaptureTypeExpansionContext(constant, SGF.SGM.M);
12911291

1292-
if (afd->getDeclContext()->isLocalContext() &&
1293-
!captureInfo.hasGenericParamCaptures())
1294-
subs = SubstitutionMap();
1292+
subs = SGF.SGM.Types.getSubstitutionMapWithCapturedEnvironments(
1293+
constant, captureInfo, subs);
12951294

12961295
// Check whether we have to dispatch to the original implementation of a
12971296
// dynamically_replaceable inside of a dynamic_replacement(for:) function.
@@ -1368,17 +1367,14 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
13681367

13691368
// A directly-called closure can be emitted as a direct call instead of
13701369
// really producing a closure object.
1371-
1372-
auto captureInfo = SGF.SGM.M.Types.getLoweredLocalCaptures(constant);
1373-
13741370
SubstitutionMap subs;
1375-
if (captureInfo.hasGenericParamCaptures())
1376-
subs = SGF.getForwardingSubstitutionMap();
1371+
std::tie(std::ignore, std::ignore, subs)
1372+
= SGF.SGM.Types.getForwardingSubstitutionsForLowering(constant);
13771373

13781374
setCallee(Callee::forDirect(SGF, constant, subs, e));
13791375

13801376
// If the closure requires captures, emit them.
1381-
if (!captureInfo.getCaptures().empty()) {
1377+
if (SGF.SGM.Types.hasLoweredLocalCaptures(constant)) {
13821378
SmallVector<ManagedValue, 4> captures;
13831379
SGF.emitCaptures(e, constant, CaptureEmission::ImmediateApplication,
13841380
captures);
@@ -6771,14 +6767,9 @@ static Callee getBaseAccessorFunctionRef(SILGenFunction &SGF,
67716767
subs, loc, true);
67726768
}
67736769

6774-
// The accessor might be a local function that does not capture any
6775-
// generic parameters, in which case we don't want to pass in any
6776-
// substitutions.
67776770
auto captureInfo = SGF.SGM.Types.getLoweredLocalCaptures(constant);
6778-
if (decl->getDeclContext()->isLocalContext() &&
6779-
!captureInfo.hasGenericParamCaptures()) {
6780-
subs = SubstitutionMap();
6781-
}
6771+
subs = SGF.SGM.Types.getSubstitutionMapWithCapturedEnvironments(
6772+
constant, captureInfo, subs);
67826773

67836774
// If this is a method in a protocol, generate it as a protocol call.
67846775
if (isa<ProtocolDecl>(decl->getDeclContext())) {

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3056,14 +3056,11 @@ RValueEmitter::emitClosureReference(AbstractClosureExpr *e,
30563056
// Emit the closure body.
30573057
SGF.SGM.emitClosure(e, contextInfo);
30583058

3059-
SubstitutionMap subs;
3060-
if (e->getCaptureInfo().hasGenericParamCaptures())
3061-
subs = SGF.getForwardingSubstitutionMap();
3062-
30633059
// Generate the closure value (if any) for the closure expr's function
30643060
// reference.
30653061
SILLocation loc = e;
3066-
return SGF.emitClosureValue(loc, SILDeclRef(e), contextInfo, subs);
3062+
return SGF.emitClosureValue(loc, SILDeclRef(e), contextInfo,
3063+
SubstitutionMap());
30673064
}
30683065

30693066
RValue RValueEmitter::

lib/SILGen/SILGenFunction.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -977,28 +977,26 @@ SILGenFunction::emitClosureValue(SILLocation loc, SILDeclRef constant,
977977
// Apply substitutions.
978978
auto pft = constantInfo.SILFnType;
979979

980-
auto closure = *constant.getAnyFunctionRef();
981-
auto *dc = closure.getAsDeclContext()->getParent();
982-
if (dc->isLocalContext() && !loweredCaptureInfo.hasGenericParamCaptures()) {
983-
// If the lowered function type is not polymorphic but we were given
984-
// substitutions, we have a closure in a generic context which does not
985-
// capture generic parameters. Just drop the substitutions.
986-
subs = { };
987-
} else if (closure.getAbstractClosureExpr()) {
980+
if (constant.getAbstractClosureExpr()) {
988981
// If we have a closure expression in generic context, Sema won't give
989982
// us substitutions, so we just use the forwarding substitutions from
990983
// context.
991-
subs = getForwardingSubstitutionMap();
984+
std::tie(std::ignore, std::ignore, subs)
985+
= SGM.Types.getForwardingSubstitutionsForLowering(constant);
986+
} else {
987+
subs = SGM.Types.getSubstitutionMapWithCapturedEnvironments(
988+
constant, loweredCaptureInfo, subs);
992989
}
993990

994-
bool wasSpecialized = false;
995-
if (!subs.empty()) {
991+
if (subs) {
996992
auto specialized =
997993
pft->substGenericArgs(F.getModule(), subs, getTypeExpansionContext());
998994
functionTy = SILType::getPrimitiveObjectType(specialized);
999-
wasSpecialized = true;
1000995
}
1001996

997+
auto closure = *constant.getAnyFunctionRef();
998+
auto *dc = closure.getAsDeclContext()->getParent();
999+
10021000
// If we're in top-level code, we don't need to physically capture script
10031001
// globals, but we still need to mark them as escaping so that DI can flag
10041002
// uninitialized uses.
@@ -1011,7 +1009,7 @@ SILGenFunction::emitClosureValue(SILLocation loc, SILDeclRef constant,
10111009
typeContext.ExpectedLoweredType->hasErasedIsolation();
10121010

10131011
ManagedValue result;
1014-
if (loweredCaptureInfo.getCaptures().empty() && !wasSpecialized &&
1012+
if (loweredCaptureInfo.getCaptures().empty() && !subs &&
10151013
!hasErasedIsolation) {
10161014
result = ManagedValue::forObjectRValueWithoutOwnership(functionRef);
10171015
} else {

0 commit comments

Comments
 (0)