Skip to content

Commit 0f73f98

Browse files
authored
Merge pull request #12204 from DougGregor/req-sig-short-circuit
2 parents 2545af6 + 45ef8cf commit 0f73f98

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,6 @@ RequirementEnvironment::RequirementEnvironment(
10681068
auto selfType = cast<GenericTypeParamType>(
10691069
proto->getSelfInterfaceType()->getCanonicalType());
10701070

1071-
SmallVector<GenericTypeParamType*, 4> allGenericParams;
1072-
10731071
// Add the generic signature of the context of the conformance. This includes
10741072
// the generic parameters from the conforming type as well as any additional
10751073
// constraints that might occur on the extension that declares the
@@ -1079,9 +1077,7 @@ RequirementEnvironment::RequirementEnvironment(
10791077
if ((conformanceSig = conformanceDC->getGenericSignatureOfContext())) {
10801078
// Use the canonical signature here.
10811079
conformanceSig = conformanceSig->getCanonicalSignature();
1082-
allGenericParams.append(conformanceSig->getGenericParams().begin(),
1083-
conformanceSig->getGenericParams().end());
1084-
depth = allGenericParams.back()->getDepth() + 1;
1080+
depth = conformanceSig->getGenericParams().back()->getDepth() + 1;
10851081
}
10861082

10871083
// Add the generic signature of the requirement, substituting our concrete
@@ -1121,39 +1117,46 @@ RequirementEnvironment::RequirementEnvironment(
11211117
return ProtocolConformanceRef(proto);
11221118
});
11231119

1124-
// First, add the generic parameters from the requirement.
1125-
for (auto genericParam : reqSig->getGenericParams().slice(1)) {
1126-
// The only depth that makes sense is depth == 1, the generic parameters
1127-
// of the requirement itself. Anything else is from invalid code.
1128-
if (genericParam->getDepth() != 1) {
1129-
return;
1120+
// If the requirement itself is non-generic, the synthetic signature
1121+
// is that of the conformance context.
1122+
if (reqSig->getGenericParams().size() == 1 &&
1123+
reqSig->getRequirements().size() == 1) {
1124+
syntheticSignature = conformanceDC->getGenericSignatureOfContext();
1125+
if (syntheticSignature) {
1126+
syntheticSignature = syntheticSignature->getCanonicalSignature();
1127+
syntheticEnvironment =
1128+
syntheticSignature->createGenericEnvironment(
1129+
*conformanceDC->getParentModule());
11301130
}
11311131

1132-
// Create an equivalent generic parameter at the next depth.
1133-
auto substGenericParam =
1134-
GenericTypeParamType::get(depth, genericParam->getIndex(), ctx);
1135-
1136-
allGenericParams.push_back(substGenericParam);
1132+
return;
11371133
}
11381134

1139-
// If there were no generic parameters, we're done.
1140-
if (allGenericParams.empty()) return;
1141-
11421135
// Construct a generic signature builder by collecting the constraints
11431136
// from the requirement and the context of the conformance together,
11441137
// because both define the capabilities of the requirement.
11451138
GenericSignatureBuilder builder(
11461139
ctx,
11471140
TypeChecker::LookUpConformance(tc, conformanceDC));
11481141

1149-
unsigned firstGenericParamToAdd = 0;
11501142
if (conformanceSig) {
11511143
builder.addGenericSignature(conformanceSig);
1152-
firstGenericParamToAdd = conformanceSig->getGenericParams().size();
11531144
}
11541145

1155-
for (unsigned gp : range(firstGenericParamToAdd, allGenericParams.size()))
1156-
builder.addGenericParameter(allGenericParams[gp]);
1146+
// First, add the generic parameters from the requirement.
1147+
for (auto genericParam : reqSig->getGenericParams().slice(1)) {
1148+
// The only depth that makes sense is depth == 1, the generic parameters
1149+
// of the requirement itself. Anything else is from invalid code.
1150+
if (genericParam->getDepth() != 1) {
1151+
return;
1152+
}
1153+
1154+
// Create an equivalent generic parameter at the next depth.
1155+
auto substGenericParam =
1156+
GenericTypeParamType::get(depth, genericParam->getIndex(), ctx);
1157+
1158+
builder.addGenericParameter(substGenericParam);
1159+
}
11571160

11581161
++NumRequirementEnvironments;
11591162

0 commit comments

Comments
 (0)