Skip to content

Commit 9aba844

Browse files
committed
Re-apply "SILGen: Move even more abstraction patterns over to interface types, NFC"
Now with a fix for a crash with rvalue references to local functions that had generic types in their signature. This reverts commit 31b97bc.
1 parent 8d77b94 commit 9aba844

File tree

4 files changed

+52
-38
lines changed

4 files changed

+52
-38
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ class Callee {
159159
};
160160
SILValue SelfValue;
161161
ArrayRef<Substitution> Substitutions;
162-
CanType OrigFormalOldType;
163162
CanType OrigFormalInterfaceType;
164163
CanAnyFunctionType SubstFormalType;
165164
Optional<SILLocation> SpecializeLoc;
@@ -177,18 +176,11 @@ class Callee {
177176
SILLocation L)
178177
: kind(Kind::IndirectValue),
179178
IndirectValue(indirectValue),
180-
OrigFormalOldType(origFormalType),
181179
OrigFormalInterfaceType(origFormalType),
182180
SubstFormalType(substFormalType),
183181
Loc(L)
184182
{}
185183

186-
static CanAnyFunctionType getConstantFormalType(SILGenFunction &gen,
187-
SILDeclRef fn)
188-
SIL_FUNCTION_TYPE_DEPRECATED {
189-
return gen.SGM.Types.getConstantInfo(fn.atUncurryLevel(0)).FormalType;
190-
}
191-
192184
static CanAnyFunctionType getConstantFormalInterfaceType(SILGenFunction &gen,
193185
SILDeclRef fn) {
194186
return gen.SGM.Types.getConstantInfo(fn.atUncurryLevel(0))
@@ -199,7 +191,6 @@ class Callee {
199191
CanAnyFunctionType substFormalType,
200192
SILLocation l)
201193
: kind(Kind::StandaloneFunction), Constant(standaloneFunction),
202-
OrigFormalOldType(getConstantFormalType(gen, standaloneFunction)),
203194
OrigFormalInterfaceType(getConstantFormalInterfaceType(gen,
204195
standaloneFunction)),
205196
SubstFormalType(substFormalType),
@@ -214,7 +205,6 @@ class Callee {
214205
CanAnyFunctionType substFormalType,
215206
SILLocation l)
216207
: kind(methodKind), Constant(methodName), SelfValue(selfValue),
217-
OrigFormalOldType(getConstantFormalType(gen, methodName)),
218208
OrigFormalInterfaceType(getConstantFormalInterfaceType(gen, methodName)),
219209
SubstFormalType(substFormalType),
220210
Loc(l)
@@ -247,11 +237,18 @@ class Callee {
247237
return CanType(TupleType::get(field, ctx));
248238
}
249239

250-
// These first two asserts will crash before they return if
251-
// they're actually wrong.
252-
assert(getArchetypeForSelf(origParamType));
253-
assert(getArchetypeForSelf(selfType));
254240
assert(isa<MetatypeType>(origParamType) == isa<MetatypeType>(selfType));
241+
242+
if (auto mt = dyn_cast<MetatypeType>(origParamType))
243+
assert(mt.getInstanceType()->isTypeParameter());
244+
else
245+
assert(origParamType->isTypeParameter());
246+
247+
if (auto mt = dyn_cast<MetatypeType>(selfType))
248+
assert(isa<ArchetypeType>(mt.getInstanceType()));
249+
else
250+
assert(isa<ArchetypeType>(selfType));
251+
255252
return selfType;
256253
}
257254

@@ -290,13 +287,13 @@ class Callee {
290287

291288
// Add the 'self' parameter back. We want it to look like a
292289
// substitution of the appropriate clause from the original type.
293-
auto polyFormalType = cast<PolymorphicFunctionType>(OrigFormalOldType);
290+
auto origFormalType = cast<AnyFunctionType>(OrigFormalInterfaceType);
294291
auto substSelfType =
295-
buildSubstSelfType(polyFormalType.getInput(), protocolSelfType, ctx);
292+
buildSubstSelfType(origFormalType.getInput(), protocolSelfType, ctx);
296293

297294
auto extInfo = FunctionType::ExtInfo(FunctionType::Representation::Thin,
298295
/*noreturn*/ false,
299-
/*throws*/ polyFormalType->throws());
296+
/*throws*/ origFormalType->throws());
300297

301298
SubstFormalType = CanFunctionType::get(substSelfType, SubstFormalType,
302299
extInfo);
@@ -308,17 +305,18 @@ class Callee {
308305
assert(kind == Kind::DynamicMethod);
309306

310307
// Drop the original self clause.
311-
CanType methodType = OrigFormalOldType;
308+
CanType methodType = OrigFormalInterfaceType;
312309
methodType = cast<AnyFunctionType>(methodType).getResult();
313310

314311
// Replace it with the dynamic self type.
315-
OrigFormalOldType = OrigFormalInterfaceType
312+
OrigFormalInterfaceType
316313
= getDynamicMethodFormalType(SGM, SelfValue,
317314
Constant.getDecl(),
318315
Constant, methodType);
316+
assert(!OrigFormalInterfaceType->hasTypeParameter());
319317

320318
// Add a self clause to the substituted type.
321-
auto origFormalType = cast<AnyFunctionType>(OrigFormalOldType);
319+
auto origFormalType = cast<AnyFunctionType>(OrigFormalInterfaceType);
322320
auto selfType = origFormalType.getInput();
323321
SubstFormalType
324322
= CanFunctionType::get(selfType, SubstFormalType,
@@ -417,7 +415,7 @@ class Callee {
417415
}
418416

419417
CanType getOrigFormalType() const {
420-
return OrigFormalOldType;
418+
return OrigFormalInterfaceType;
421419
}
422420

423421
CanAnyFunctionType getSubstFormalType() const {
@@ -3648,11 +3646,11 @@ SILGenFunction::emitApplyOfLibraryIntrinsic(SILLocation loc,
36483646
ArrayRef<ManagedValue> args,
36493647
SGFContext ctx) {
36503648
auto origFormalType =
3651-
cast<AnyFunctionType>(fn->getType()->getCanonicalType());
3649+
cast<AnyFunctionType>(fn->getInterfaceType()->getCanonicalType());
36523650
auto substFormalType = origFormalType;
36533651
if (!subs.empty()) {
3654-
auto polyFnType = cast<PolymorphicFunctionType>(substFormalType);
3655-
auto applied = polyFnType->substGenericArgs(SGM.SwiftModule, subs);
3652+
auto genericFnType = cast<GenericFunctionType>(substFormalType);
3653+
auto applied = genericFnType->substGenericArgs(SGM.SwiftModule, subs);
36563654
substFormalType = cast<FunctionType>(applied->getCanonicalType());
36573655
}
36583656

@@ -3671,7 +3669,7 @@ SILGenFunction::emitApplyOfLibraryIntrinsic(SILLocation loc,
36713669
== SILFunctionLanguage::Swift);
36723670

36733671
return emitApply(loc, mv, subs, args, substFnType,
3674-
AbstractionPattern(origFormalType.getResult()),
3672+
AbstractionPattern(origFormalType).getFunctionResultType(),
36753673
substFormalType.getResult(),
36763674
options, None, None, ctx);
36773675
}
@@ -3807,10 +3805,10 @@ emitSpecializedAccessorFunctionRef(SILGenFunction &gen,
38073805
SILConstantInfo constantInfo = gen.getConstantInfo(constant);
38083806

38093807
// Apply substitutions to the callee type.
3810-
CanAnyFunctionType substAccessorType = constantInfo.FormalType;
3808+
CanAnyFunctionType substAccessorType = constantInfo.FormalInterfaceType;
38113809
if (!substitutions.empty()) {
3812-
auto polyFn = cast<PolymorphicFunctionType>(substAccessorType);
3813-
auto substFn = polyFn->substGenericArgs(gen.SGM.SwiftModule, substitutions);
3810+
auto genericFn = cast<GenericFunctionType>(substAccessorType);
3811+
auto substFn = genericFn->substGenericArgs(gen.SGM.SwiftModule, substitutions);
38143812
substAccessorType = cast<FunctionType>(substFn->getCanonicalType());
38153813
}
38163814

lib/SILGen/SILGenConvert.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,18 @@ getOptionalSomeValue(SILLocation loc, ManagedValue value,
149149
return emitManagedRValueWithCleanup(result, optTL);
150150
}
151151

152-
static Substitution getSimpleSubstitution(GenericParamList &generics,
152+
static Substitution getSimpleSubstitution(GenericSignature *genericSig,
153153
CanType typeArg) {
154-
assert(generics.getParams().size() == 1);
154+
assert(genericSig->getGenericParams().size() == 1);
155155
return Substitution{typeArg, {}};
156156
}
157157

158158
/// Create the correct substitution for calling the given function at
159159
/// the given type.
160160
static Substitution getSimpleSubstitution(FuncDecl *fn, CanType typeArg) {
161-
auto polyFnType =
162-
cast<PolymorphicFunctionType>(fn->getType()->getCanonicalType());
163-
return getSimpleSubstitution(polyFnType->getGenericParams(), typeArg);
161+
auto genericFnType =
162+
cast<GenericFunctionType>(fn->getInterfaceType()->getCanonicalType());
163+
return getSimpleSubstitution(genericFnType->getGenericSignature(), typeArg);
164164
}
165165

166166
static CanType getOptionalValueType(SILType optType,

lib/SILGen/SILGenExpr.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,22 @@ emitRValueForDecl(SILLocation loc, ConcreteDeclRef declRef, Type ncRefType,
442442

443443
// Get the lowered AST types:
444444
// - the original type
445-
auto origLoweredFormalType = AbstractionPattern(constantInfo.LoweredType);
445+
auto origLoweredFormalType =
446+
AbstractionPattern(constantInfo.LoweredInterfaceType);
446447
if (hasLocalCaptures) {
447-
auto formalTypeWithoutCaptures =
448-
cast<AnyFunctionType>(constantInfo.FormalType.getResult());
448+
// Get the unlowered formal type of the constant, stripping off
449+
// the first level of function application, which applies captures.
450+
origLoweredFormalType =
451+
AbstractionPattern(constantInfo.FormalInterfaceType)
452+
.getFunctionResultType();
453+
454+
// Lower it, being careful to use the right generic signature.
449455
origLoweredFormalType =
450456
AbstractionPattern(
451-
SGM.Types.getLoweredASTFunctionType(formalTypeWithoutCaptures,0,
452-
silDeclRef));
457+
origLoweredFormalType.getGenericSignature(),
458+
SGM.Types.getLoweredASTFunctionType(
459+
cast<FunctionType>(origLoweredFormalType.getType()),
460+
0, silDeclRef));
453461
}
454462

455463
// - the substituted type

test/SILGen/closures.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ func return_local_generic_function_without_captures<A, R>() -> A -> R {
1616
return f
1717
}
1818

19+
func return_local_generic_function_with_captures<A, R>(a: A) -> A -> R {
20+
func f(_: A) -> R {
21+
_ = a
22+
}
23+
24+
return f
25+
}
26+
1927
// CHECK-LABEL: sil hidden @_TF8closures17read_only_capture
2028
func read_only_capture(x: Int) -> Int {
2129
var x = x

0 commit comments

Comments
 (0)