Skip to content

Commit d98268c

Browse files
authored
Merge pull request #7319 from slavapestov/go-away-moduledecl-bad-bad-bad
Begin groundwork for storing conformances inside generic signatures
2 parents 1e46f13 + bd4f310 commit d98268c

20 files changed

+62
-47
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
195195
}
196196

197197
/// Map an interface type to a contextual type.
198-
static Type mapTypeIntoContext(ModuleDecl *M,
199-
GenericEnvironment *genericEnv,
198+
static Type mapTypeIntoContext(GenericEnvironment *genericEnv,
200199
Type type);
201200

202201
/// Map a contextual type to an interface type.
@@ -207,7 +206,7 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
207206
Type mapTypeOutOfContext(Type type) const;
208207

209208
/// Map an interface type to a contextual type.
210-
Type mapTypeIntoContext(ModuleDecl *M, Type type) const;
209+
Type mapTypeIntoContext(Type type) const;
211210

212211
/// Map an interface type to a contextual type.
213212
Type mapTypeIntoContext(Type type,

include/swift/AST/Type.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ class MakeAbstractConformanceForGenericType {
125125
ProtocolType *conformedProtocol) const;
126126
};
127127

128+
/// Functor class suitable for use as a \c LookupConformanceFn that fetches
129+
/// conformances from a generic signature.
130+
class LookUpConformanceInSignature {
131+
const GenericSignature &Sig;
132+
public:
133+
LookUpConformanceInSignature(const GenericSignature &Sig)
134+
: Sig(Sig) {}
135+
136+
Optional<ProtocolConformanceRef>
137+
operator()(CanType dependentType,
138+
Type conformingReplacementType,
139+
ProtocolType *conformedProtocol) const;
140+
};
141+
128142
/// Flags that can be passed when substituting into a type.
129143
enum class SubstFlags {
130144
/// If a type cannot be produced because some member type is

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,7 @@ static Type computeNominalType(NominalTypeDecl *decl, DeclTypeKind kind) {
20512051

20522052
auto *genericEnv = decl->getGenericEnvironmentOfContext();
20532053
return GenericEnvironment::mapTypeIntoContext(
2054-
decl->getModuleContext(), genericEnv, interfaceType);
2054+
genericEnv, interfaceType);
20552055
}
20562056

20572057
// Get the parent type.

lib/AST/DeclContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ GenericEnvironment *DeclContext::getGenericEnvironmentOfContext() const {
278278

279279
Type DeclContext::mapTypeIntoContext(Type type) const {
280280
return GenericEnvironment::mapTypeIntoContext(
281-
getParentModule(), getGenericEnvironmentOfContext(), type);
281+
getGenericEnvironmentOfContext(), type);
282282
}
283283

284284
Type DeclContext::mapTypeOutOfContext(Type type) const {

lib/AST/GenericEnvironment.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,14 @@ bool GenericEnvironment::containsPrimaryArchetype(
155155
QueryArchetypeToInterfaceSubstitutions(this)(archetype));
156156
}
157157

158-
Type GenericEnvironment::mapTypeIntoContext(ModuleDecl *M,
159-
GenericEnvironment *env,
158+
Type GenericEnvironment::mapTypeIntoContext(GenericEnvironment *env,
160159
Type type) {
161160
assert(!type->hasArchetype() && "already have a contextual type");
162161

163162
if (!env)
164163
return type.substDependentTypesWithErrorTypes();
165164

166-
return env->mapTypeIntoContext(M, type);
165+
return env->mapTypeIntoContext(type);
167166
}
168167

169168
Type
@@ -297,8 +296,9 @@ Type GenericEnvironment::mapTypeIntoContext(
297296

298297
}
299298

300-
Type GenericEnvironment::mapTypeIntoContext(ModuleDecl *M, Type type) const {
301-
return mapTypeIntoContext(type, LookUpConformanceInModule(M));
299+
Type GenericEnvironment::mapTypeIntoContext(Type type) const {
300+
auto *sig = getGenericSignature();
301+
return mapTypeIntoContext(type, LookUpConformanceInSignature(*sig));
302302
}
303303

304304
Type GenericEnvironment::mapTypeIntoContext(GenericTypeParamType *type) const {

lib/AST/Pattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Type Pattern::getType() const {
117117

118118
if (auto genericEnv = dc->getGenericEnvironmentOfContext()) {
119119
ctx.DelayedPatternContexts.erase(this);
120-
Ty = genericEnv->mapTypeIntoContext(dc->getParentModule(), Ty);
120+
Ty = genericEnv->mapTypeIntoContext(Ty);
121121
PatternBits.hasInterfaceType = false;
122122
}
123123
}

lib/AST/Type.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,8 +2961,8 @@ LookUpConformanceInModule::operator()(CanType dependentType,
29612961
return ProtocolConformanceRef(conformedProtocol->getDecl());
29622962

29632963
return M->lookupConformance(conformingReplacementType,
2964-
conformedProtocol->getDecl(),
2965-
conformingReplacementType->getASTContext().getLazyResolver());
2964+
conformedProtocol->getDecl(),
2965+
M->getASTContext().getLazyResolver());
29662966
}
29672967

29682968
Optional<ProtocolConformanceRef>
@@ -2982,6 +2982,21 @@ MakeAbstractConformanceForGenericType::operator()(CanType dependentType,
29822982
return ProtocolConformanceRef(conformedProtocol->getDecl());
29832983
}
29842984

2985+
Optional<ProtocolConformanceRef>
2986+
LookUpConformanceInSignature::operator()(CanType dependentType,
2987+
Type conformingReplacementType,
2988+
ProtocolType *conformedProtocol) const {
2989+
// FIXME: Actually implement this properly.
2990+
auto *M = conformedProtocol->getDecl()->getParentModule();
2991+
2992+
if (conformingReplacementType->isTypeParameter())
2993+
return ProtocolConformanceRef(conformedProtocol->getDecl());
2994+
2995+
return M->lookupConformance(conformingReplacementType,
2996+
conformedProtocol->getDecl(),
2997+
M->getASTContext().getLazyResolver());
2998+
}
2999+
29853000
Type DependentMemberType::substBaseType(ModuleDecl *module,
29863001
Type substBase,
29873002
LazyResolver *resolver) {

lib/IRGen/GenCall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ void CallEmission::emitToMemory(Address addr,
12061206

12071207
if (origResultType->hasTypeParameter())
12081208
origResultType = IGF.IGM.getGenericEnvironment()
1209-
->mapTypeIntoContext(IGF.getSwiftModule(), origResultType)
1209+
->mapTypeIntoContext(origResultType)
12101210
->getCanonicalType();
12111211

12121212
if (origResultType != substResultType) {

lib/IRGen/GenPoly.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ static SILType applyContextArchetypes(IRGenFunction &IGF,
4444

4545
auto substType =
4646
IGF.IGM.getGenericEnvironment()->mapTypeIntoContext(
47-
IGF.getSwiftModule(),
48-
type.getSwiftRValueType())
49-
47+
type.getSwiftRValueType())
5048
->getCanonicalType();
5149
return SILType::getPrimitiveType(substType, type.getCategory());
5250
}

lib/IRGen/GenType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ ArchetypeType *TypeConverter::getExemplarArchetype(ArchetypeType *t) {
960960
// Map the archetype out of its own generic environment and into the
961961
// canonical generic environment.
962962
auto interfaceType = genericEnv->mapTypeOutOfContext(t);
963-
auto exemplar = canGenericEnv->mapTypeIntoContext(module, interfaceType)
963+
auto exemplar = canGenericEnv->mapTypeIntoContext(interfaceType)
964964
->castTo<ArchetypeType>();
965965
assert(isExemplarArchetype(exemplar));
966966
return exemplar;

lib/SIL/SILFunction.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ bool SILFunction::shouldOptimize() const {
160160

161161
Type SILFunction::mapTypeIntoContext(Type type) const {
162162
return GenericEnvironment::mapTypeIntoContext(
163-
getModule().getSwiftModule(),
164-
getGenericEnvironment(),
165-
type);
163+
getGenericEnvironment(), type);
166164
}
167165

168166
namespace {
@@ -259,7 +257,7 @@ SILType GenericEnvironment::mapTypeIntoContext(SILModule &M,
259257
SILType type) const {
260258
return doSubstDependentSILType(M,
261259
[&](CanType t) {
262-
return mapTypeIntoContext(M.getSwiftModule(), t)->getCanonicalType();
260+
return mapTypeIntoContext(t)->getCanonicalType();
263261
},
264262
type);
265263
}

lib/SIL/SILType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ SILBoxType::getFieldLoweredType(SILModule &M, unsigned index) const {
577577
.getGenericEnvironment(*M.getSwiftModule());
578578
auto substMap =
579579
env->getSubstitutionMap(getGenericArgs());
580-
fieldTy = env->mapTypeIntoContext(M.getSwiftModule(), fieldTy)
580+
fieldTy = env->mapTypeIntoContext(fieldTy)
581581
->getCanonicalType();
582582

583583
fieldTy = SILType::getPrimitiveObjectType(fieldTy)

lib/SIL/TypeLowering.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ namespace {
452452
if (Sig && type->hasTypeParameter()) {
453453
type = Sig->getCanonicalSignature()
454454
.getGenericEnvironment(*M.getSwiftModule())
455-
->mapTypeIntoContext(M.getSwiftModule(), type)
455+
->mapTypeIntoContext(type)
456456
->getCanonicalType();
457457
}
458458

@@ -2467,11 +2467,10 @@ TypeConverter::getInterfaceBoxTypeForCapture(ValueDecl *captured,
24672467
auto contextBoxTy = boxTy;
24682468
if (signature) {
24692469
auto env = signature.getGenericEnvironment(*M.getSwiftModule());
2470-
loweredContextType = env->mapTypeIntoContext(M.getSwiftModule(),
2471-
loweredContextType)
2470+
loweredContextType = env->mapTypeIntoContext(loweredContextType)
24722471
->getCanonicalType();
24732472
contextBoxTy = cast<SILBoxType>(
2474-
env->mapTypeIntoContext(M.getSwiftModule(), contextBoxTy)
2473+
env->mapTypeIntoContext(contextBoxTy)
24752474
->getCanonicalType());
24762475
}
24772476
assert(contextBoxTy->getLayout()->getFields().size() == 1
@@ -2502,7 +2501,7 @@ TypeConverter::getContextBoxTypeForCapture(ValueDecl *captured,
25022501
isMutable);
25032502
if (env)
25042503
boxType = cast<SILBoxType>(
2505-
env->mapTypeIntoContext(M.getSwiftModule(), boxType)
2504+
env->mapTypeIntoContext(boxType)
25062505
->getCanonicalType());
25072506

25082507
return boxType;

lib/SILGen/SILGenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,7 @@ SILGenModule::emitProtocolWitness(ProtocolConformance *conformance,
19221922
}
19231923

19241924
selfType = GenericEnvironment::mapTypeIntoContext(
1925-
M.getSwiftModule(), genericEnv, selfInterfaceType);
1925+
genericEnv, selfInterfaceType);
19261926
}
19271927

19281928
SILGenFunction gen(*this, *f);

lib/SILGen/SILGenPoly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2513,7 +2513,7 @@ buildThunkSignature(SILGenFunction &gen,
25132513
contextSubs = calleeGenericEnv->getSubstitutionMap(
25142514
[&](SubstitutableType *type) -> Type {
25152515
auto depTy = calleeGenericEnv->mapTypeOutOfContext(type);
2516-
return genericEnv->mapTypeIntoContext(mod, depTy);
2516+
return genericEnv->mapTypeIntoContext(depTy);
25172517
},
25182518
MakeAbstractConformanceForGenericType());
25192519
}

lib/SILGen/SILGenProlog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ static void emitCaptureArguments(SILGenFunction &gen,
373373
// non-canonical types in that context. We need the original generic
374374
// environment from the AST for that.
375375
auto genericEnv = closure.getGenericEnvironment();
376-
return genericEnv->mapTypeIntoContext(gen.F.getModule().getSwiftModule(),
377-
interfaceType);
376+
return genericEnv->mapTypeIntoContext(interfaceType);
378377
};
379378

380379
switch (gen.SGM.Types.getDeclCaptureKind(capture)) {

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7411,7 +7411,7 @@ checkExtensionGenericParams(TypeChecker &tc, ExtensionDecl *ext, Type type,
74117411
});
74127412

74137413
Type extContextType =
7414-
env->mapTypeIntoContext(ext->getModuleContext(), extInterfaceType);
7414+
env->mapTypeIntoContext(extInterfaceType);
74157415
return { env, extContextType };
74167416
}
74177417

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ Type GenericTypeToArchetypeResolver::resolveSelfAssociatedType(
9999

100100
Type GenericTypeToArchetypeResolver::resolveTypeOfContext(DeclContext *dc) {
101101
return GenericEnvironment::mapTypeIntoContext(
102-
dc->getParentModule(), GenericEnv,
103-
dc->getSelfInterfaceType());
102+
GenericEnv, dc->getSelfInterfaceType());
104103
}
105104

106105
Type GenericTypeToArchetypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
107106
return GenericEnvironment::mapTypeIntoContext(
108-
decl->getDeclContext()->getParentModule(), GenericEnv,
109-
decl->getDeclaredInterfaceType());
107+
GenericEnv, decl->getDeclaredInterfaceType());
110108
}
111109

112110
bool GenericTypeToArchetypeResolver::areSameType(Type type1, Type type2) {

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,8 +1191,7 @@ matchWitness(TypeChecker &tc,
11911191
auto reqGenericEnv = reqEnvironment.getSyntheticEnvironment();
11921192
Type selfTy = proto->getSelfInterfaceType().subst(reqSubs);
11931193
if (reqGenericEnv)
1194-
selfTy = reqGenericEnv->mapTypeIntoContext(dc->getParentModule(),
1195-
selfTy);
1194+
selfTy = reqGenericEnv->mapTypeIntoContext(selfTy);
11961195

11971196
// Open up the type of the requirement.
11981197
reqLocator = cs->getConstraintLocator(
@@ -1220,8 +1219,7 @@ matchWitness(TypeChecker &tc,
12201219
continue;
12211220

12221221
if (reqGenericEnv) {
1223-
replacedInReq = reqGenericEnv->mapTypeIntoContext(dc->getParentModule(),
1224-
replacedInReq);
1222+
replacedInReq = reqGenericEnv->mapTypeIntoContext(replacedInReq);
12251223
}
12261224

12271225
cs->addConstraint(ConstraintKind::Bind, replacement.second, replacedInReq,

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,7 @@ ProtocolConformanceRef ModuleFile::readConformance(
564564
ASTContext &ctx = getContext();
565565
Type conformingType = getType(conformingTypeID);
566566
if (genericEnv) {
567-
conformingType = genericEnv->mapTypeIntoContext(getAssociatedModule(),
568-
conformingType);
567+
conformingType = genericEnv->mapTypeIntoContext(conformingType);
569568
}
570569

571570
PrettyStackTraceType trace(getAssociatedModule()->getASTContext(),
@@ -599,8 +598,7 @@ ProtocolConformanceRef ModuleFile::readConformance(
599598
ASTContext &ctx = getContext();
600599
Type conformingType = getType(conformingTypeID);
601600
if (genericEnv) {
602-
conformingType = genericEnv->mapTypeIntoContext(getAssociatedModule(),
603-
conformingType);
601+
conformingType = genericEnv->mapTypeIntoContext(conformingType);
604602
}
605603

606604
PrettyStackTraceType trace(getAssociatedModule()->getASTContext(),
@@ -761,8 +759,7 @@ ModuleFile::maybeReadSubstitution(llvm::BitstreamCursor &cursor,
761759

762760
auto replacementTy = getType(replacementID);
763761
if (genericEnv) {
764-
replacementTy = genericEnv->mapTypeIntoContext(getAssociatedModule(),
765-
replacementTy);
762+
replacementTy = genericEnv->mapTypeIntoContext(replacementTy);
766763
}
767764

768765
SmallVector<ProtocolConformanceRef, 4> conformanceBuf;

0 commit comments

Comments
 (0)