Skip to content

Commit eefa409

Browse files
committed
AST: Change return type of ProtocolConformance::subst() to ProtocolConformanceRef
1 parent bc7139b commit eefa409

File tree

4 files changed

+50
-37
lines changed

4 files changed

+50
-37
lines changed

include/swift/AST/ProtocolConformance.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,23 +299,23 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance
299299
/// be satisfied.
300300
ArrayRef<Requirement> getConditionalRequirements() const;
301301

302-
/// Substitute the conforming type and produce a ProtocolConformance that
302+
/// Substitute the conforming type and produce a ProtocolConformanceRef that
303303
/// applies to the substituted type.
304-
ProtocolConformance *subst(SubstitutionMap subMap,
305-
SubstOptions options = llvm::None) const;
304+
ProtocolConformanceRef subst(SubstitutionMap subMap,
305+
SubstOptions options = llvm::None) const;
306306

307-
/// Substitute the conforming type and produce a ProtocolConformance that
307+
/// Substitute the conforming type and produce a ProtocolConformanceRef that
308308
/// applies to the substituted type.
309-
ProtocolConformance *subst(TypeSubstitutionFn subs,
310-
LookupConformanceFn conformances,
311-
SubstOptions options = llvm::None) const;
309+
ProtocolConformanceRef subst(TypeSubstitutionFn subs,
310+
LookupConformanceFn conformances,
311+
SubstOptions options = llvm::None) const;
312312

313-
/// Substitute the conforming type and produce a ProtocolConformance that
313+
/// Substitute the conforming type and produce a ProtocolConformanceRef that
314314
/// applies to the substituted type.
315315
///
316316
/// This function should generally not be used outside of the substitution
317317
/// subsystem.
318-
ProtocolConformance *subst(InFlightSubstitution &IFS) const;
318+
ProtocolConformanceRef subst(InFlightSubstitution &IFS) const;
319319

320320
SWIFT_DEBUG_DUMP;
321321
void dump(llvm::raw_ostream &out, unsigned indent = 0) const;

lib/AST/ProtocolConformance.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -918,63 +918,69 @@ bool ProtocolConformance::isVisibleFrom(const DeclContext *dc) const {
918918
return true;
919919
}
920920

921-
ProtocolConformance *
921+
ProtocolConformanceRef
922922
ProtocolConformance::subst(SubstitutionMap subMap,
923923
SubstOptions options) const {
924924
InFlightSubstitutionViaSubMap IFS(subMap, options);
925925
return subst(IFS);
926926
}
927927

928-
ProtocolConformance *
928+
ProtocolConformanceRef
929929
ProtocolConformance::subst(TypeSubstitutionFn subs,
930930
LookupConformanceFn conformances,
931931
SubstOptions options) const {
932932
InFlightSubstitution IFS(subs, conformances, options);
933933
return subst(IFS);
934934
}
935935

936-
ProtocolConformance *
936+
ProtocolConformanceRef
937937
ProtocolConformance::subst(InFlightSubstitution &IFS) const {
938+
auto *mutableThis = const_cast<ProtocolConformance *>(this);
939+
938940
switch (getKind()) {
939941
case ProtocolConformanceKind::Normal: {
940942
auto origType = getType();
941943
if (!origType->hasTypeParameter() &&
942944
!origType->hasArchetype())
943-
return const_cast<ProtocolConformance *>(this);
945+
return ProtocolConformanceRef(mutableThis);
944946

945947
auto substType = origType.subst(IFS);
946948
if (substType->isEqual(origType))
947-
return const_cast<ProtocolConformance *>(this);
949+
return ProtocolConformanceRef(mutableThis);
948950

951+
auto *generic = cast<NormalProtocolConformance>(mutableThis);
949952
auto subMap = SubstitutionMap::get(getGenericSignature(), IFS);
950953

951-
auto *mutableThis = const_cast<ProtocolConformance *>(this);
952-
return substType->getASTContext()
953-
.getSpecializedConformance(substType,
954-
cast<NormalProtocolConformance>(mutableThis),
955-
subMap);
954+
auto &ctx = substType->getASTContext();
955+
auto *concrete = ctx.getSpecializedConformance(substType, generic, subMap);
956+
957+
return ProtocolConformanceRef(concrete);
956958
}
959+
957960
case ProtocolConformanceKind::Builtin: {
958961
auto origType = getType();
959962
if (!origType->hasTypeParameter() &&
960963
!origType->hasArchetype())
961-
return const_cast<ProtocolConformance *>(this);
964+
return ProtocolConformanceRef(mutableThis);
962965

963966
auto substType = origType.subst(IFS);
964967

965968
// We do an exact pointer equality check because subst() can
966969
// change sugar.
967970
if (substType.getPointer() == origType.getPointer())
968-
return const_cast<ProtocolConformance *>(this);
971+
return ProtocolConformanceRef(mutableThis);
969972

970973
auto kind = cast<BuiltinProtocolConformance>(this)
971974
->getBuiltinConformanceKind();
972975

973-
return substType->getASTContext()
976+
auto *concrete = substType->getASTContext()
974977
.getBuiltinConformance(substType, getProtocol(), kind);
978+
return ProtocolConformanceRef(concrete);
975979
}
980+
976981
case ProtocolConformanceKind::Self:
977-
return const_cast<ProtocolConformance*>(this);
982+
return ProtocolConformanceRef(mutableThis);
983+
978984
case ProtocolConformanceKind::Inherited: {
979985
// Substitute the base.
980986
auto inheritedConformance
@@ -983,31 +989,39 @@ ProtocolConformance::subst(InFlightSubstitution &IFS) const {
983989
auto origType = getType();
984990
if (!origType->hasTypeParameter() &&
985991
!origType->hasArchetype()) {
986-
return const_cast<ProtocolConformance *>(this);
992+
return ProtocolConformanceRef(mutableThis);
987993
}
988994

989995
auto origBaseType = inheritedConformance->getType();
990996
if (origBaseType->hasTypeParameter() ||
991997
origBaseType->hasArchetype()) {
992998
// Substitute into the superclass.
993-
inheritedConformance = inheritedConformance->subst(IFS);
999+
auto substConformance = inheritedConformance->subst(IFS);
1000+
if (!substConformance.isConcrete())
1001+
return substConformance;
1002+
1003+
inheritedConformance = substConformance.getConcrete();
9941004
}
9951005

9961006
auto substType = origType.subst(IFS);
997-
return substType->getASTContext()
1007+
auto *concrete = substType->getASTContext()
9981008
.getInheritedConformance(substType, inheritedConformance);
1009+
return ProtocolConformanceRef(concrete);
9991010
}
1011+
10001012
case ProtocolConformanceKind::Specialized: {
10011013
// Substitute the substitutions in the specialized conformance.
10021014
auto spec = cast<SpecializedProtocolConformance>(this);
1003-
auto genericConformance = spec->getGenericConformance();
1004-
auto subMap = spec->getSubstitutionMap();
10051015

1006-
auto origType = getType();
1007-
auto substType = origType.subst(IFS);
1008-
return substType->getASTContext()
1009-
.getSpecializedConformance(substType, genericConformance,
1010-
subMap.subst(IFS));
1016+
auto *generic = spec->getGenericConformance();
1017+
auto subMap = spec->getSubstitutionMap().subst(IFS);
1018+
1019+
auto substType = spec->getType().subst(IFS);
1020+
1021+
auto &ctx = substType->getASTContext();
1022+
auto *concrete = ctx.getSpecializedConformance(substType, generic, subMap);
1023+
1024+
return ProtocolConformanceRef(concrete);
10111025
}
10121026
}
10131027
llvm_unreachable("bad ProtocolConformanceKind");

lib/AST/ProtocolConformanceRef.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ProtocolConformanceRef::subst(Type origType, InFlightSubstitution &IFS) const {
8787
return *this;
8888

8989
if (isConcrete())
90-
return ProtocolConformanceRef(getConcrete()->subst(IFS));
90+
return getConcrete()->subst(IFS);
9191
if (isPack())
9292
return getPack()->subst(IFS);
9393

@@ -126,15 +126,14 @@ ProtocolConformanceRef::subst(Type origType, InFlightSubstitution &IFS) const {
126126

127127
ProtocolConformanceRef ProtocolConformanceRef::mapConformanceOutOfContext() const {
128128
if (isConcrete()) {
129-
auto *concrete = getConcrete()->subst(
129+
return getConcrete()->subst(
130130
[](SubstitutableType *type) -> Type {
131131
if (auto *archetypeType = type->getAs<ArchetypeType>())
132132
return archetypeType->getInterfaceType();
133133
return type;
134134
},
135135
MakeAbstractConformanceForGenericType(),
136136
SubstFlags::PreservePackExpansionLevel);
137-
return ProtocolConformanceRef(concrete);
138137
} else if (isPack()) {
139138
return getPack()->subst(
140139
[](SubstitutableType *type) -> Type {

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ getWitnessTableLazyAccessFunction(IRGenModule &IGM,
11901190
static const ProtocolConformance *
11911191
mapConformanceIntoContext(const RootProtocolConformance *conf) {
11921192
if (auto *genericEnv = conf->getDeclContext()->getGenericEnvironmentOfContext())
1193-
return conf->subst(genericEnv->getForwardingSubstitutionMap());
1193+
return conf->subst(genericEnv->getForwardingSubstitutionMap()).getConcrete();
11941194
return conf;
11951195
}
11961196

0 commit comments

Comments
 (0)