Skip to content

Commit b1e0e77

Browse files
committed
AST: Simplify SubstitutionMap::lookupConformance()
1 parent 7e4e79c commit b1e0e77

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ struct SubstitutionMapWithLocalArchetypes {
8585
ProtocolDecl *proto) {
8686
if (isa<LocalArchetypeType>(origType))
8787
return swift::lookupConformance(substType, proto);
88+
89+
if (isa<PrimaryArchetypeType>(origType) ||
90+
isa<PackArchetypeType>(origType))
91+
origType = origType->mapTypeOutOfContext()->getCanonicalType();
92+
8893
if (SubsMap)
8994
return SubsMap->lookupConformance(origType, proto);
9095

lib/AST/SubstitutionMap.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111
//===----------------------------------------------------------------------===//
1212
//
1313
// This file defines the SubstitutionMap class. A SubstitutionMap packages
14-
// together a set of replacement types and protocol conformances for
15-
// specializing generic types.
14+
// together a set of replacement types and protocol conformances, given by
15+
// the generic parameters and conformance requirements of the substitution map's
16+
// input generic signature.
1617
//
17-
// SubstitutionMaps either have type parameters or archetypes as keys,
18-
// based on whether they were built from a GenericSignature or a
19-
// GenericEnvironment.
20-
//
21-
// To specialize a type, call Type::subst() with the right SubstitutionMap.
18+
// To substitute a type, call Type::subst() with the right SubstitutionMap.
2219
//
2320
//===----------------------------------------------------------------------===//
2421

@@ -238,20 +235,9 @@ Type SubstitutionMap::lookupSubstitution(GenericTypeParamType *genericParam) con
238235

239236
ProtocolConformanceRef
240237
SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
241-
if (empty())
242-
return ProtocolConformanceRef::forInvalid();
243-
244-
// If we have an archetype, map out of the context so we can compute a
245-
// conformance access path.
246-
if (auto archetype = dyn_cast<ArchetypeType>(type)) {
247-
if (!isa<OpaqueTypeArchetypeType>(archetype)) {
248-
type = archetype->getInterfaceType()->getCanonicalType();
249-
}
250-
}
238+
ASSERT(type->isTypeParameter());
251239

252-
// Error path: if we don't have a type parameter, there is no conformance.
253-
// FIXME: Query concrete conformances in the generic signature?
254-
if (!type->isTypeParameter())
240+
if (empty())
255241
return ProtocolConformanceRef::forInvalid();
256242

257243
auto genericSig = getGenericSignature();

lib/AST/TypeSubstitution.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ operator()(CanType dependentType, Type conformingReplacementType,
206206
/*allowMissing=*/true);
207207
}
208208

209+
if (isa<PrimaryArchetypeType>(dependentType) ||
210+
isa<PackArchetypeType>(dependentType))
211+
dependentType = dependentType->mapTypeOutOfContext()->getCanonicalType();
212+
209213
auto result = Subs.lookupConformance(dependentType, conformedProtocol);
210214
if (!result.isInvalid())
211215
return result;

lib/SILOptimizer/Utils/ConstExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ SymbolicValue ConstExprFunctionState::computeConstantValue(SILValue value) {
426426
// Try to resolve a witness method against our known conformances.
427427
if (auto *wmi = dyn_cast<WitnessMethodInst>(value)) {
428428
auto conf = substitutionMap.lookupConformance(
429-
wmi->getLookupType(), wmi->getConformance().getRequirement());
429+
wmi->getLookupType()->mapTypeOutOfContext()->getCanonicalType(),
430+
wmi->getConformance().getRequirement());
430431
if (conf.isInvalid())
431432
return getUnknown(evaluator, value,
432433
UnknownReason::UnknownWitnessMethodConformance);

0 commit comments

Comments
 (0)