Skip to content

Commit 1664865

Browse files
committed
SILGen: Use TypeConverter::getSubstitutionMapWithCapturedEnvironments()
1 parent 4d45588 commit 1664865

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
@@ -1225,9 +1225,8 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
12251225
auto captureInfo = SGF.SGM.Types.getLoweredLocalCaptures(constant);
12261226
SGF.SGM.Types.setCaptureTypeExpansionContext(constant, SGF.SGM.M);
12271227

1228-
if (afd->getDeclContext()->isLocalContext() &&
1229-
!captureInfo.hasGenericParamCaptures())
1230-
subs = SubstitutionMap();
1228+
subs = SGF.SGM.Types.getSubstitutionMapWithCapturedEnvironments(
1229+
constant, captureInfo, subs);
12311230

12321231
// Check whether we have to dispatch to the original implementation of a
12331232
// dynamically_replaceable inside of a dynamic_replacement(for:) function.
@@ -1304,17 +1303,14 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
13041303

13051304
// A directly-called closure can be emitted as a direct call instead of
13061305
// really producing a closure object.
1307-
1308-
auto captureInfo = SGF.SGM.M.Types.getLoweredLocalCaptures(constant);
1309-
13101306
SubstitutionMap subs;
1311-
if (captureInfo.hasGenericParamCaptures())
1312-
subs = SGF.getForwardingSubstitutionMap();
1307+
std::tie(std::ignore, std::ignore, subs)
1308+
= SGF.SGM.Types.getForwardingSubstitutionsForLowering(constant);
13131309

13141310
setCallee(Callee::forDirect(SGF, constant, subs, e));
13151311

13161312
// If the closure requires captures, emit them.
1317-
if (!captureInfo.getCaptures().empty()) {
1313+
if (SGF.SGM.Types.hasLoweredLocalCaptures(constant)) {
13181314
SmallVector<ManagedValue, 4> captures;
13191315
SGF.emitCaptures(e, constant, CaptureEmission::ImmediateApplication,
13201316
captures);
@@ -6712,14 +6708,9 @@ static Callee getBaseAccessorFunctionRef(SILGenFunction &SGF,
67126708
subs, loc, true);
67136709
}
67146710

6715-
// The accessor might be a local function that does not capture any
6716-
// generic parameters, in which case we don't want to pass in any
6717-
// substitutions.
67186711
auto captureInfo = SGF.SGM.Types.getLoweredLocalCaptures(constant);
6719-
if (decl->getDeclContext()->isLocalContext() &&
6720-
!captureInfo.hasGenericParamCaptures()) {
6721-
subs = SubstitutionMap();
6722-
}
6712+
subs = SGF.SGM.Types.getSubstitutionMapWithCapturedEnvironments(
6713+
constant, captureInfo, subs);
67236714

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

lib/SILGen/SILGenExpr.cpp

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

3031-
SubstitutionMap subs;
3032-
if (e->getCaptureInfo().hasGenericParamCaptures())
3033-
subs = SGF.getForwardingSubstitutionMap();
3034-
30353031
// Generate the closure value (if any) for the closure expr's function
30363032
// reference.
30373033
SILLocation loc = e;
3038-
return SGF.emitClosureValue(loc, SILDeclRef(e), contextInfo, subs);
3034+
return SGF.emitClosureValue(loc, SILDeclRef(e), contextInfo,
3035+
SubstitutionMap());
30393036
}
30403037

30413038
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)