Skip to content

Commit b32694e

Browse files
committed
GSB: Try to avoid calling Type::subst() from expandConformanceRequirement()
1 parent 9e44aee commit b32694e

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,8 +3998,16 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
39983998
ProtocolDecl *proto,
39993999
const RequirementSource *source,
40004000
bool onlySameTypeConstraints) {
4001-
auto protocolSubMap = SubstitutionMap::getProtocolSubstitutions(
4002-
proto, selfType.getDependentType(*this), ProtocolConformanceRef(proto));
4001+
auto selfTy = selfType.getDependentType(*this);
4002+
4003+
auto subst = [&](Requirement req) -> Optional<Requirement> {
4004+
return req.subst(
4005+
[&](SubstitutableType *t) -> Type {
4006+
assert(isa<GenericTypeParamType>(t));
4007+
return selfTy;
4008+
},
4009+
MakeAbstractConformanceForGenericType());
4010+
};
40034011

40044012
auto result = ConstraintResult::Resolved;
40054013

@@ -4015,7 +4023,7 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
40154023
if (onlySameTypeConstraints && req.getKind() != RequirementKind::SameType)
40164024
continue;
40174025

4018-
auto substReq = req.subst(protocolSubMap);
4026+
auto substReq = subst(req);
40194027
auto reqResult = substReq
40204028
? addRequirement(*substReq, innerSource, nullptr)
40214029
: ConstraintResult::Conflicting;
@@ -4045,8 +4053,9 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
40454053

40464054
auto innerSource = FloatingRequirementSource::viaProtocolRequirement(
40474055
source, proto, reqRepr->getSeparatorLoc(), /*inferred=*/false);
4048-
addRequirement(req, reqRepr, innerSource,
4049-
&protocolSubMap, nullptr);
4056+
4057+
if (auto substReq = subst(req))
4058+
addRequirement(*substReq, reqRepr, innerSource, nullptr);
40504059
return false;
40514060
});
40524061

@@ -4159,7 +4168,7 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
41594168
source, proto, SourceLoc(), /*inferred=*/true);
41604169

41614170
auto rawReq = Requirement(RequirementKind::SameType, firstType, secondType);
4162-
if (auto req = rawReq.subst(protocolSubMap))
4171+
if (auto req = subst(rawReq))
41634172
addRequirement(*req, inferredSameTypeSource, proto->getParentModule());
41644173
};
41654174

@@ -4189,8 +4198,10 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
41894198

41904199
auto innerSource = FloatingRequirementSource::viaProtocolRequirement(
41914200
source, proto, reqRepr->getSeparatorLoc(), /*inferred=*/false);
4192-
addRequirement(req, reqRepr, innerSource, &protocolSubMap,
4193-
/*inferForModule=*/nullptr);
4201+
if (auto substReq = subst(req)) {
4202+
addRequirement(*substReq, reqRepr, innerSource,
4203+
/*inferForModule=*/nullptr);
4204+
}
41944205
return false;
41954206
});
41964207

@@ -5171,28 +5182,19 @@ ConstraintResult
51715182
GenericSignatureBuilder::addRequirement(const Requirement &req,
51725183
FloatingRequirementSource source,
51735184
ModuleDecl *inferForModule) {
5174-
return addRequirement(req, nullptr, source, nullptr, inferForModule);
5185+
return addRequirement(req, nullptr, source, inferForModule);
51755186
}
51765187

51775188
ConstraintResult
51785189
GenericSignatureBuilder::addRequirement(const Requirement &req,
51795190
const RequirementRepr *reqRepr,
51805191
FloatingRequirementSource source,
5181-
const SubstitutionMap *subMap,
51825192
ModuleDecl *inferForModule) {
5183-
// Local substitution for types in the requirement.
5184-
auto subst = [&](Type t) {
5185-
if (subMap)
5186-
return t.subst(*subMap);
5187-
5188-
return t;
5189-
};
5190-
5191-
auto firstType = subst(req.getFirstType());
5193+
auto firstType = req.getFirstType();
51925194
switch (req.getKind()) {
51935195
case RequirementKind::Superclass:
51945196
case RequirementKind::Conformance: {
5195-
auto secondType = subst(req.getSecondType());
5197+
auto secondType = req.getSecondType();
51965198

51975199
if (inferForModule) {
51985200
inferRequirements(*inferForModule, firstType,
@@ -5220,7 +5222,7 @@ GenericSignatureBuilder::addRequirement(const Requirement &req,
52205222
}
52215223

52225224
case RequirementKind::SameType: {
5223-
auto secondType = subst(req.getSecondType());
5225+
auto secondType = req.getSecondType();
52245226

52255227
if (inferForModule) {
52265228
inferRequirements(*inferForModule, firstType,
@@ -8567,8 +8569,8 @@ InferredGenericSignatureRequest::evaluate(
85678569
}
85688570
}
85698571

8570-
builder.addRequirement(req, reqRepr, source, nullptr,
8571-
lookupDC->getParentModule());
8572+
builder.addRequirement(req, reqRepr, source,
8573+
lookupDC->getParentModule());
85728574
return false;
85738575
};
85748576

lib/AST/GenericSignatureBuilder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ class GenericSignatureBuilder {
571571
ConstraintResult addRequirement(const Requirement &req,
572572
const RequirementRepr *reqRepr,
573573
FloatingRequirementSource source,
574-
const SubstitutionMap *subMap,
575574
ModuleDecl *inferForModule);
576575

577576
/// Add all of a generic signature's parameters and requirements.

validation-test/compiler_crashers_2_fixed/0159-rdar40009245.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
protocol P {
44
associatedtype A : P where A.X == Self
5-
// expected-error@-1{{'X' is not a member type of type 'Self.A'}}
65
associatedtype X : P where P.A == Self
76
// expected-error@-1{{associated type 'A' can only be used with a concrete type or generic parameter base}}
87
}

0 commit comments

Comments
 (0)