Skip to content

Commit 7eeb563

Browse files
committed
RequirementMachine: Small concrete contraction cleanup
1 parent 9c4972f commit 7eeb563

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

lib/AST/RequirementMachine/ConcreteContraction.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ using namespace rewriting;
154154
/// Strip associated types from types used as keys to erase differences between
155155
/// resolved types coming from the parent generic signature and unresolved types
156156
/// coming from user-written requirements.
157-
static CanType stripBoundDependentMemberTypes(Type t) {
157+
static Type stripBoundDependentMemberTypes(Type t) {
158158
if (auto *depMemTy = t->getAs<DependentMemberType>()) {
159-
return CanType(DependentMemberType::get(
159+
return DependentMemberType::get(
160160
stripBoundDependentMemberTypes(depMemTy->getBase()),
161-
depMemTy->getName()));
161+
depMemTy->getName());
162162
}
163163

164-
return t->getCanonicalType();
164+
return t;
165165
}
166166

167167
namespace {
@@ -232,17 +232,18 @@ Optional<Type> ConcreteContraction::substTypeParameterRec(
232232
// losing the requirement.
233233
if (position == Position::BaseType ||
234234
position == Position::ConformanceRequirement) {
235+
auto key = stripBoundDependentMemberTypes(type)->getCanonicalType();
235236

236237
Type concreteType;
237238
{
238-
auto found = ConcreteTypes.find(stripBoundDependentMemberTypes(type));
239+
auto found = ConcreteTypes.find(key);
239240
if (found != ConcreteTypes.end() && found->second.size() == 1)
240241
concreteType = *found->second.begin();
241242
}
242243

243244
Type superclass;
244245
{
245-
auto found = Superclasses.find(stripBoundDependentMemberTypes(type));
246+
auto found = Superclasses.find(key);
246247
if (found != Superclasses.end() && found->second.size() == 1)
247248
superclass = *found->second.begin();
248249
}
@@ -392,7 +393,8 @@ ConcreteContraction::substRequirement(const Requirement &req) const {
392393
// 'T : Sendable' would be incorrect; we want to ensure that we only admit
393394
// subclasses of 'C' which are 'Sendable'.
394395
bool allowMissing = false;
395-
if (ConcreteTypes.count(stripBoundDependentMemberTypes(firstType)) > 0)
396+
auto key = stripBoundDependentMemberTypes(firstType)->getCanonicalType();
397+
if (ConcreteTypes.count(key) > 0)
396398
allowMissing = true;
397399

398400
if (!substFirstType->isTypeParameter()) {
@@ -449,17 +451,18 @@ hasResolvedMemberTypeOfInterestingParameter(Type type) const {
449451
if (memberTy->getAssocType() == nullptr)
450452
return false;
451453

452-
auto baseTy = memberTy->getBase();
454+
auto key = stripBoundDependentMemberTypes(memberTy->getBase())
455+
->getCanonicalType();
453456
Type concreteType;
454457
{
455-
auto found = ConcreteTypes.find(stripBoundDependentMemberTypes(baseTy));
458+
auto found = ConcreteTypes.find(key);
456459
if (found != ConcreteTypes.end() && found->second.size() == 1)
457460
return true;
458461
}
459462

460463
Type superclass;
461464
{
462-
auto found = Superclasses.find(stripBoundDependentMemberTypes(baseTy));
465+
auto found = Superclasses.find(key);
463466
if (found != Superclasses.end() && found->second.size() == 1)
464467
return true;
465468
}
@@ -496,14 +499,14 @@ bool ConcreteContraction::preserveSameTypeRequirement(
496499

497500
// One of the parent types of this type parameter should be subject
498501
// to a superclass requirement.
499-
auto type = req.getFirstType();
502+
auto type = stripBoundDependentMemberTypes(req.getFirstType())
503+
->getCanonicalType();
500504
while (true) {
501-
if (Superclasses.find(stripBoundDependentMemberTypes(type))
502-
!= Superclasses.end())
505+
if (Superclasses.find(type) != Superclasses.end())
503506
break;
504507

505-
if (auto *memberType = type->getAs<DependentMemberType>()) {
506-
type = memberType->getBase();
508+
if (auto memberType = dyn_cast<DependentMemberType>(type)) {
509+
type = memberType.getBase();
507510
continue;
508511
}
509512

@@ -546,23 +549,23 @@ bool ConcreteContraction::performConcreteContraction(
546549
if (constraintType->isTypeParameter())
547550
break;
548551

549-
ConcreteTypes[stripBoundDependentMemberTypes(subjectType)]
550-
.insert(constraintType);
552+
subjectType = stripBoundDependentMemberTypes(subjectType);
553+
ConcreteTypes[subjectType->getCanonicalType()].insert(constraintType);
551554
break;
552555
}
553556
case RequirementKind::Superclass: {
554557
auto constraintType = req.req.getSecondType();
555558
assert(!constraintType->isTypeParameter() &&
556559
"You forgot to call desugarRequirement()");
557560

558-
Superclasses[stripBoundDependentMemberTypes(subjectType)]
559-
.insert(constraintType);
561+
subjectType = stripBoundDependentMemberTypes(subjectType);
562+
Superclasses[subjectType->getCanonicalType()].insert(constraintType);
560563
break;
561564
}
562565
case RequirementKind::Conformance: {
563566
auto *protoDecl = req.req.getProtocolDecl();
564-
Conformances[stripBoundDependentMemberTypes(subjectType)]
565-
.push_back(protoDecl);
567+
subjectType = stripBoundDependentMemberTypes(subjectType);
568+
Conformances[subjectType->getCanonicalType()].push_back(protoDecl);
566569

567570
break;
568571
}
@@ -588,7 +591,7 @@ bool ConcreteContraction::performConcreteContraction(
588591
if (auto otherSuperclassTy = proto->getSuperclass()) {
589592
if (Debug) {
590593
llvm::dbgs() << "@ Subject type of superclass requirement "
591-
<< "τ_" << subjectType << " : " << superclassTy
594+
<< subjectType << " : " << superclassTy
592595
<< " conforms to "<< proto->getName()
593596
<< " which has a superclass bound "
594597
<< otherSuperclassTy << "\n";

0 commit comments

Comments
 (0)