Skip to content

[AST] Strengthen signature of SubstututionMap::addSubstitution(). #6606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/swift/AST/SubstitutionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ namespace swift {

class SubstitutableType;

template<class Type> class CanTypeWrapper;
typedef CanTypeWrapper<SubstitutableType> CanSubstitutableType;

class SubstitutionMap {
using ParentType = std::pair<CanType, AssociatedTypeDecl *>;

Expand All @@ -61,7 +64,7 @@ class SubstitutionMap {
/// Retrieve the conformances for the given type.
ArrayRef<ProtocolConformanceRef> getConformances(CanType type) const;

void addSubstitution(CanType type, Type replacement);
void addSubstitution(CanSubstitutableType type, Type replacement);

void addConformances(CanType type, ArrayRef<ProtocolConformanceRef> conformances);

Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class SILCloner : protected SILVisitor<ImplClass> {

// Register a re-mapping for opened existentials.
void registerOpenedExistentialRemapping(ArchetypeType *From, ArchetypeType *To) {
OpenedExistentialSubs.addSubstitution(CanType(From), CanType(To));
OpenedExistentialSubs.addSubstitution(CanArchetypeType(From), CanType(To));
}

protected:
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/GenericEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ getSubstitutionMap(ModuleDecl *mod,

// Record the replacement type and its conformances.
if (auto *archetype = contextTy->getAs<ArchetypeType>()) {
result.addSubstitution(CanType(archetype), sub.getReplacement());
result.addSubstitution(CanArchetypeType(archetype), sub.getReplacement());
result.addConformances(CanType(archetype), sub.getConformances());
continue;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/AST/GenericSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ GenericSignature::getSubstitutionMap(ArrayRef<Substitution> subs,
subs = subs.slice(1);

auto canTy = depTy->getCanonicalType();
if (isa<GenericTypeParamType>(canTy))
result.addSubstitution(canTy, sub.getReplacement());
if (isa<SubstitutableType>(canTy)) {
result.addSubstitution(cast<SubstitutableType>(canTy),
sub.getReplacement());
}
result.addConformances(canTy, sub.getConformances());
}

Expand Down
11 changes: 4 additions & 7 deletions lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ SubstitutionMap::lookupConformance(CanType type,
}

void SubstitutionMap::
addSubstitution(CanType type, Type replacement) {
auto result = subMap.insert(std::make_pair(type->castTo<SubstitutableType>(),
replacement));
addSubstitution(CanSubstitutableType type, Type replacement) {
auto result = subMap.insert(std::make_pair(type, replacement));
assert(result.second);
(void) result;
}
Expand Down Expand Up @@ -182,14 +181,12 @@ SubstitutionMap::getOverrideSubstitutions(const ClassDecl *baseClass,
assert(baseParams.size() == derivedParams.size());

for (unsigned i = 0, e = derivedParams.size(); i < e; i++) {
auto paramTy = baseParams[i]->getCanonicalType()
->castTo<GenericTypeParamType>();
auto paramTy = cast<GenericTypeParamType>(baseParams[i]->getCanonicalType());
assert(paramTy->getDepth() >= minDepth);
Type replacementTy = derivedParams[i];
if (derivedSubs)
replacementTy = replacementTy.subst(*derivedSubs);
subMap.addSubstitution(paramTy->getCanonicalType(),
replacementTy);
subMap.addSubstitution(paramTy, replacementTy);
}

auto isRootedInInnermostParameter = [&](Type t) -> bool {
Expand Down
5 changes: 3 additions & 2 deletions lib/SILGen/SILGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1758,8 +1758,9 @@ SILGenModule::emitProtocolWitness(ProtocolConformance *conformance,
genericEnv = witnessRef.getDecl()->getInnermostDeclContext()
->getGenericEnvironmentOfContext();

auto selfTy = conformance->getProtocol()->getSelfInterfaceType()
->getCanonicalType();
auto selfTy = cast<GenericTypeParamType>(
conformance->getProtocol()->getSelfInterfaceType()
->getCanonicalType());

Type concreteTy = conformance->getInterfaceType();

Expand Down
8 changes: 6 additions & 2 deletions lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2484,8 +2484,12 @@ buildThunkSignature(SILGenFunction &gen,
auto newArchetype = genericEnv->mapTypeIntoContext(mod, depTy)
->castTo<ArchetypeType>();

contextSubs.addSubstitution(CanType(oldArchetype), newArchetype);
interfaceSubs.addSubstitution(canTy, oldArchetype);
contextSubs.addSubstitution(CanArchetypeType(oldArchetype), newArchetype);

if (isa<SubstitutableType>(canTy)) {
interfaceSubs.addSubstitution(cast<SubstitutableType>(canTy),
oldArchetype);
}

contextSubs.addConformances(CanType(oldArchetype), conformances);
interfaceSubs.addConformances(canTy, conformances);
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ SILCombiner::createApplyWithConcreteType(FullApplySite AI,
NewSubstCalleeType = SILType::getPrimitiveObjectType(SFT);
} else {
SubstitutionMap Subs;
Subs.addSubstitution(CanType(OpenedArchetype), ConcreteType);
Subs.addSubstitution(CanArchetypeType(OpenedArchetype), ConcreteType);
Subs.addConformances(CanType(OpenedArchetype), Conformance);
NewSubstCalleeType = SubstCalleeType.subst(AI.getModule(), Subs);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/Transforms/CSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ bool CSE::processOpenExistentialRef(SILInstruction *Inst, ValueBase *V,
auto OldOpenedArchetype = getOpenedArchetypeOf(Inst);
auto NewOpenedArchetype = getOpenedArchetypeOf(dyn_cast<SILInstruction>(V));
SubstitutionMap TypeSubstMap;
TypeSubstMap.addSubstitution(CanType(OldOpenedArchetype), NewOpenedArchetype);
TypeSubstMap.addSubstitution(CanArchetypeType(OldOpenedArchetype),
NewOpenedArchetype);
// Collect all candidates that may contain opened archetypes
// that need to be replaced.
for (auto Use : Inst->getUses()) {
Expand Down
15 changes: 10 additions & 5 deletions lib/SILOptimizer/Utils/Devirtualize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,10 @@ getSubstitutionsForCallee(SILModule &M,

// Otherwise, record the replacement and conformances for the mapped
// type.
if (isa<GenericTypeParamType>(canTy))
subMap.addSubstitution(canTy, sub.getReplacement());
if (isa<SubstitutableType>(canTy)) {
subMap.addSubstitution(cast<SubstitutableType>(canTy),
sub.getReplacement());
}
subMap.addConformances(canTy, sub.getConformances());
}
assert(origSubs.empty());
Expand Down Expand Up @@ -834,7 +836,8 @@ static void getWitnessMethodSubstitutions(
unsigned depth = 0;
if (isDefaultWitness) {
// For default witnesses, we substitute all of Self.
auto gp = witnessThunkSig->getGenericParams().front()->getCanonicalType();
auto gp = cast<GenericTypeParamType>(witnessThunkSig->getGenericParams()
.front()->getCanonicalType());
subMap.addSubstitution(gp, origSubs.front().getReplacement());
subMap.addConformances(gp, origSubs.front().getConformances());

Expand Down Expand Up @@ -899,8 +902,10 @@ static void getWitnessMethodSubstitutions(
// Otherwise, record the replacement and conformances for the mapped
// type.
auto canTy = mappedDepTy->getCanonicalType();
if (isa<GenericTypeParamType>(canTy))
subMap.addSubstitution(canTy, sub.getReplacement());
if (isa<SubstitutableType>(canTy)) {
subMap.addSubstitution(cast<SubstitutableType>(canTy),
sub.getReplacement());
}
subMap.addConformances(canTy, sub.getConformances());
}
assert(subs.empty() && "Did not consume all substitutions");
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,8 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
}

tl.setType(ty, /*validated=*/true);
subMap.addSubstitution(genericTypeParamTy->getCanonicalType(), ty);
subMap.addSubstitution(
cast<GenericTypeParamType>(genericTypeParamTy->getCanonicalType()), ty);
}

// Capture the conformances needed for the substitution map.
Expand Down
14 changes: 8 additions & 6 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,13 +983,14 @@ RequirementEnvironment::RequirementEnvironment(
// type parameters of the conformance context and the parameters of the
// requirement.
Type concreteType = conformanceDC->getSelfInterfaceType();
auto selfType = proto->getSelfInterfaceType()->getCanonicalType();
auto selfType = cast<GenericTypeParamType>(
proto->getSelfInterfaceType()->getCanonicalType());


reqToSyntheticEnvMap.addSubstitution(selfType, concreteType);

// 'Self' is always at depth 0, index 0. Anything else is invalid code.
auto selfGenericParam = selfType->castTo<GenericTypeParamType>();
if (selfGenericParam->getDepth() != 0 ||
selfGenericParam->getIndex() != 0) {
if (selfType->getDepth() != 0 || selfType->getIndex() != 0) {
return;
}

Expand Down Expand Up @@ -1058,8 +1059,9 @@ RequirementEnvironment::RequirementEnvironment(

// Create a substitution from the requirement's generic parameter to the
// generic parameter known to the builder.
reqToSyntheticEnvMap.addSubstitution(genericParam->getCanonicalType(),
substGenericParam);
reqToSyntheticEnvMap.addSubstitution(
cast<GenericTypeParamType>(genericParam->getCanonicalType()),
substGenericParam);
if (auto archetypeType
= reqEnv->mapTypeIntoContext(genericParam)->getAs<ArchetypeType>()) {
// Add substitutions.
Expand Down