Skip to content

Commit 4ea6dba

Browse files
authored
Merge pull request #4655 from slavapestov/same-type-capture-canonical-representative
SILGen: Use the canonical representative when lowering capture types
2 parents 8358d3c + 58f9a8f commit 4ea6dba

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/SIL/SILFunctionType.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,12 @@ static CanSILFunctionType getSILFunctionType(SILModule &M,
737737
// from the function to which the argument is attached.
738738
if (constant && !constant->isDefaultArgGenerator())
739739
if (auto function = constant->getAnyFunctionRef()) {
740+
auto getCanonicalType = [&](Type t) -> CanType {
741+
if (genericSig)
742+
return genericSig->getCanonicalTypeInContext(t, *M.getSwiftModule());
743+
return t->getCanonicalType();
744+
};
745+
740746
auto &Types = M.Types;
741747
auto loweredCaptures = Types.getLoweredLocalCaptures(*function);
742748

@@ -745,22 +751,21 @@ static CanSILFunctionType getSILFunctionType(SILModule &M,
745751
ParameterConvention convention = ParameterConvention::Direct_Unowned;
746752
auto selfMetatype = MetatypeType::get(
747753
loweredCaptures.getDynamicSelfType()->getSelfType(),
748-
MetatypeRepresentation::Thick)
749-
->getCanonicalType();
750-
SILParameterInfo param(selfMetatype, convention);
754+
MetatypeRepresentation::Thick);
755+
auto canSelfMetatype = getCanonicalType(selfMetatype);
756+
SILParameterInfo param(canSelfMetatype, convention);
751757
inputs.push_back(param);
752758

753759
continue;
754760
}
755761

756762
auto *VD = capture.getDecl();
757-
auto type = VD->getType()->getCanonicalType();
758-
759-
type = ArchetypeBuilder::mapTypeOutOfContext(
760-
function->getAsDeclContext(), type)->getCanonicalType();
761-
763+
auto type = ArchetypeBuilder::mapTypeOutOfContext(
764+
function->getAsDeclContext(), VD->getType());
765+
auto canType = getCanonicalType(type);
766+
762767
auto &loweredTL = Types.getTypeLowering(
763-
AbstractionPattern(genericSig, type), type);
768+
AbstractionPattern(genericSig, canType), canType);
764769
auto loweredTy = loweredTL.getLoweredType();
765770
switch (Types.getDeclCaptureKind(capture)) {
766771
case CaptureKind::None:

test/SILGen/generic_closures.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,21 @@ func mixed_generic_nongeneric_nesting<T>(t: T) {
294294
// CHECK-LABEL: sil shared @_TFF16generic_closures32mixed_generic_nongeneric_nestingurFT1tx_T_L_5outerurFT_T_ : $@convention(thin) <T> () -> ()
295295
// CHECK-LABEL: sil shared @_TFFF16generic_closures32mixed_generic_nongeneric_nestingurFT1tx_T_L_5outerurFT_T_L_6middleu__rFT1uqd___T_ : $@convention(thin) <T><U> (@in U) -> ()
296296
// CHECK-LABEL: sil shared @_TFFFF16generic_closures32mixed_generic_nongeneric_nestingurFT1tx_T_L_5outerurFT_T_L_6middleu__rFT1uqd___T_L_5inneru__rfT_qd__ : $@convention(thin) <T><U> (@owned @box U) -> @out U
297+
298+
protocol Doge {
299+
associatedtype Nose : NoseProtocol
300+
}
301+
302+
protocol NoseProtocol {
303+
associatedtype Squeegee
304+
}
305+
306+
protocol Doggo {}
307+
308+
struct DogSnacks<A : Doggo> {}
309+
310+
func capture_same_type_representative<Daisy: Doge, Roo: Doggo>(slobber: Roo)
311+
where Roo == Daisy.Nose.Squeegee {
312+
var s = DogSnacks<Daisy.Nose.Squeegee>()
313+
_ = { _ = s }
314+
}

0 commit comments

Comments
 (0)