Skip to content

Requirement environment: adopt the conformance's generic signature when we can. #12204

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
Oct 1, 2017
Merged
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
49 changes: 26 additions & 23 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,6 @@ RequirementEnvironment::RequirementEnvironment(
auto selfType = cast<GenericTypeParamType>(
proto->getSelfInterfaceType()->getCanonicalType());

SmallVector<GenericTypeParamType*, 4> allGenericParams;

// Add the generic signature of the context of the conformance. This includes
// the generic parameters from the conforming type as well as any additional
// constraints that might occur on the extension that declares the
Expand All @@ -1079,9 +1077,7 @@ RequirementEnvironment::RequirementEnvironment(
if ((conformanceSig = conformanceDC->getGenericSignatureOfContext())) {
// Use the canonical signature here.
conformanceSig = conformanceSig->getCanonicalSignature();
allGenericParams.append(conformanceSig->getGenericParams().begin(),
conformanceSig->getGenericParams().end());
depth = allGenericParams.back()->getDepth() + 1;
depth = conformanceSig->getGenericParams().back()->getDepth() + 1;
}

// Add the generic signature of the requirement, substituting our concrete
Expand Down Expand Up @@ -1121,39 +1117,46 @@ RequirementEnvironment::RequirementEnvironment(
return ProtocolConformanceRef(proto);
});

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

// Create an equivalent generic parameter at the next depth.
auto substGenericParam =
GenericTypeParamType::get(depth, genericParam->getIndex(), ctx);

allGenericParams.push_back(substGenericParam);
return;
}

// If there were no generic parameters, we're done.
if (allGenericParams.empty()) return;

// Construct a generic signature builder by collecting the constraints
// from the requirement and the context of the conformance together,
// because both define the capabilities of the requirement.
GenericSignatureBuilder builder(
ctx,
TypeChecker::LookUpConformance(tc, conformanceDC));

unsigned firstGenericParamToAdd = 0;
if (conformanceSig) {
builder.addGenericSignature(conformanceSig);
firstGenericParamToAdd = conformanceSig->getGenericParams().size();
}

for (unsigned gp : range(firstGenericParamToAdd, allGenericParams.size()))
builder.addGenericParameter(allGenericParams[gp]);
// First, add the generic parameters from the requirement.
for (auto genericParam : reqSig->getGenericParams().slice(1)) {
// The only depth that makes sense is depth == 1, the generic parameters
// of the requirement itself. Anything else is from invalid code.
if (genericParam->getDepth() != 1) {
return;
}

// Create an equivalent generic parameter at the next depth.
auto substGenericParam =
GenericTypeParamType::get(depth, genericParam->getIndex(), ctx);

builder.addGenericParameter(substGenericParam);
}

++NumRequirementEnvironments;

Expand Down