Skip to content

Commit da4d076

Browse files
committed
AST: Introduce SubstFlags::SubstitutePrimaryArchetypes
1 parent 63028be commit da4d076

14 files changed

+41
-14
lines changed

include/swift/AST/InFlightSubstitution.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ class InFlightSubstitution {
134134
return Options;
135135
}
136136

137+
bool shouldSubstitutePrimaryArchetypes() const {
138+
return Options.contains(SubstFlags::SubstitutePrimaryArchetypes);
139+
}
140+
137141
bool shouldSubstituteOpaqueArchetypes() const {
138142
return Options.contains(SubstFlags::SubstituteOpaqueArchetypes);
139143
}

include/swift/AST/Type.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,20 @@ enum class SubstFlags {
124124
AllowLoweredTypes = 0x01,
125125
/// Map member types to their desugared witness type.
126126
DesugarMemberTypes = 0x02,
127+
/// Allow primary archetypes to themselves be the subject of substitution.
128+
/// Otherwise, we map them out of context first.
129+
SubstitutePrimaryArchetypes = 0x04,
127130
/// Allow opaque archetypes to themselves be the subject of substitution,
128-
/// used when erasing them to their underlying types. otherwise, we
131+
/// used when erasing them to their underlying types. Otherwise, we
129132
/// recursively substitute their substitutions, instead, preserving the
130133
/// opaque archetype.
131-
SubstituteOpaqueArchetypes = 0x04,
134+
SubstituteOpaqueArchetypes = 0x08,
132135
/// Allow local archetypes to themselves be the subject of substitution.
133-
SubstituteLocalArchetypes = 0x08,
136+
SubstituteLocalArchetypes = 0x10,
134137
/// Don't increase pack expansion level for free pack references.
135138
/// Do not introduce new usages of this flag.
136139
/// FIXME: Remove this.
137-
PreservePackExpansionLevel = 0x10,
140+
PreservePackExpansionLevel = 0x20,
138141
};
139142

140143
/// Options for performing substitutions into a type.

include/swift/SIL/SILCloner.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
508508

509509
SILType remapType(SILType Ty) {
510510
if (Functor.SubsMap || Ty.hasLocalArchetype()) {
511-
SubstOptions options(std::nullopt);
511+
SubstOptions options = SubstFlags::SubstitutePrimaryArchetypes;
512512
if (Functor.hasLocalArchetypes())
513513
options |= SubstFlags::SubstituteLocalArchetypes;
514514

@@ -533,7 +533,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
533533

534534
CanType remapASTType(CanType ty) {
535535
if (Functor.SubsMap || ty->hasLocalArchetype()) {
536-
SubstOptions options(std::nullopt);
536+
SubstOptions options = SubstFlags::SubstitutePrimaryArchetypes;
537537
if (Functor.hasLocalArchetypes())
538538
options |= SubstFlags::SubstituteLocalArchetypes;
539539

@@ -557,7 +557,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
557557

558558
ProtocolConformanceRef remapConformance(Type Ty, ProtocolConformanceRef C) {
559559
if (Functor.SubsMap || Ty->hasLocalArchetype()) {
560-
SubstOptions options(std::nullopt);
560+
SubstOptions options = SubstFlags::SubstitutePrimaryArchetypes;
561561
if (Functor.hasLocalArchetypes())
562562
options |= SubstFlags::SubstituteLocalArchetypes;
563563

@@ -582,7 +582,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
582582
SubstitutionMap remapSubstitutionMap(SubstitutionMap Subs) {
583583
// If we have local archetypes to substitute, do so now.
584584
if (Functor.SubsMap || Subs.getRecursiveProperties().hasLocalArchetype()) {
585-
SubstOptions options(std::nullopt);
585+
SubstOptions options = SubstFlags::SubstitutePrimaryArchetypes;
586586
if (Functor.hasLocalArchetypes())
587587
options |= SubstFlags::SubstituteLocalArchetypes;
588588

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3801,6 +3801,7 @@ void ASTMangler::appendClosureComponents(CanType Ty, unsigned discriminator,
38013801
Ty = Ty.subst(MapLocalArchetypesOutOfContext(Sig, capturedEnvs),
38023802
MakeAbstractConformanceForGenericType(),
38033803
SubstFlags::PreservePackExpansionLevel |
3804+
SubstFlags::SubstitutePrimaryArchetypes |
38043805
SubstFlags::SubstituteLocalArchetypes)->getCanonicalType();
38053806

38063807
appendType(Ty, Sig);

lib/AST/GenericEnvironment.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ Type TypeBase::mapTypeOutOfContext() {
378378
return Type(this).subst(MapTypeOutOfContext(),
379379
MakeAbstractConformanceForGenericType(),
380380
SubstFlags::AllowLoweredTypes |
381-
SubstFlags::PreservePackExpansionLevel);
381+
SubstFlags::PreservePackExpansionLevel |
382+
SubstFlags::SubstitutePrimaryArchetypes);
382383
}
383384

384385
class GenericEnvironment::NestedTypeStorage
@@ -718,6 +719,7 @@ GenericEnvironment::mapElementTypeIntoPackContext(Type type) const {
718719
MakeAbstractConformanceForGenericType(),
719720
SubstFlags::AllowLoweredTypes |
720721
SubstFlags::PreservePackExpansionLevel |
722+
SubstFlags::SubstitutePrimaryArchetypes |
721723
SubstFlags::SubstituteLocalArchetypes);
722724

723725
auto shapeClass = elementEnv->getOpenedElementShapeClass();

lib/AST/ProtocolConformanceRef.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ ProtocolConformanceRef ProtocolConformanceRef::mapConformanceOutOfContext() cons
134134
return type;
135135
},
136136
MakeAbstractConformanceForGenericType(),
137-
SubstFlags::PreservePackExpansionLevel);
137+
SubstFlags::PreservePackExpansionLevel |
138+
SubstFlags::SubstitutePrimaryArchetypes);
138139
} else if (isPack()) {
139140
return getPack()->subst(
140141
[](SubstitutableType *type) -> Type {
@@ -143,7 +144,8 @@ ProtocolConformanceRef ProtocolConformanceRef::mapConformanceOutOfContext() cons
143144
return type;
144145
},
145146
MakeAbstractConformanceForGenericType(),
146-
SubstFlags::PreservePackExpansionLevel);
147+
SubstFlags::PreservePackExpansionLevel |
148+
SubstFlags::SubstitutePrimaryArchetypes);
147149
}
148150

149151
return *this;

lib/AST/SubstitutionMap.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
382382
SubstitutionMap SubstitutionMap::mapReplacementTypesOutOfContext() const {
383383
return subst(MapTypeOutOfContext(),
384384
MakeAbstractConformanceForGenericType(),
385-
SubstFlags::PreservePackExpansionLevel);
385+
SubstFlags::PreservePackExpansionLevel |
386+
SubstFlags::SubstitutePrimaryArchetypes);
386387
}
387388

388389
SubstitutionMap SubstitutionMap::subst(SubstitutionMap subMap,

lib/IRGen/GenType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,8 @@ CanType TypeConverter::getExemplarType(CanType contextTy) {
20162016
},
20172017
MakeAbstractConformanceForGenericType(),
20182018
SubstFlags::AllowLoweredTypes |
2019-
SubstFlags::PreservePackExpansionLevel);
2019+
SubstFlags::PreservePackExpansionLevel |
2020+
SubstFlags::SubstitutePrimaryArchetypes);
20202021
return CanType(exemplified);
20212022
}
20222023
}

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,6 +1970,7 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
19701970
t = t.subst(MapLocalArchetypesOutOfContext(origGenericSig, capturedEnvs),
19711971
MakeAbstractConformanceForGenericType(),
19721972
SubstFlags::PreservePackExpansionLevel |
1973+
SubstFlags::SubstitutePrimaryArchetypes |
19731974
SubstFlags::SubstituteLocalArchetypes);
19741975

19751976
LLVM_DEBUG(llvm::dbgs() << "-- maps to " << t->getCanonicalType() << "\n");
@@ -3010,6 +3011,7 @@ CanSILFunctionType swift::buildSILFunctionThunkType(
30103011
t.subst(mapOutOfContext,
30113012
MakeAbstractConformanceForGenericType(),
30123013
SubstFlags::PreservePackExpansionLevel |
3014+
SubstFlags::SubstitutePrimaryArchetypes |
30133015
SubstFlags::SubstituteLocalArchetypes))
30143016
->getCanonicalType();
30153017
};
@@ -3020,6 +3022,7 @@ CanSILFunctionType swift::buildSILFunctionThunkType(
30203022
Type(t).subst(mapOutOfContext,
30213023
MakeAbstractConformanceForGenericType(),
30223024
SubstFlags::PreservePackExpansionLevel |
3025+
SubstFlags::SubstitutePrimaryArchetypes |
30233026
SubstFlags::SubstituteLocalArchetypes |
30243027
SubstFlags::AllowLoweredTypes))
30253028
->getCanonicalType());

lib/SIL/IR/TypeLowering.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,6 +3910,7 @@ getAnyFunctionRefInterfaceType(TypeConverter &TC,
39103910
MapLocalArchetypesOutOfContext(sig.baseGenericSig, sig.capturedEnvs),
39113911
MakeAbstractConformanceForGenericType(),
39123912
SubstFlags::PreservePackExpansionLevel |
3913+
SubstFlags::SubstitutePrimaryArchetypes |
39133914
SubstFlags::SubstituteLocalArchetypes);
39143915
funcType = cast<FunctionType>(substType->getCanonicalType());
39153916
}
@@ -4993,6 +4994,7 @@ TypeConverter::getInterfaceBoxTypeForCapture(ValueDecl *captured,
49934994
MakeAbstractConformanceForGenericType(),
49944995
SubstFlags::PreservePackExpansionLevel |
49954996
SubstFlags::AllowLoweredTypes |
4997+
SubstFlags::SubstitutePrimaryArchetypes |
49964998
SubstFlags::SubstituteLocalArchetypes)->getCanonicalType();
49974999

49985000
// If the type is not dependent at all, we can form a concrete box layout.

0 commit comments

Comments
 (0)