Skip to content

Sema: Use GSB to build the signature for opaque result type decls. #24485

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
May 4, 2019
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
51 changes: 27 additions & 24 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,49 +194,52 @@ Type TypeChecker::getOrCreateOpaqueResultType(TypeResolution resolution,
// Create a generic signature for the opaque environment. This is the outer
// generic signature with an added generic parameter representing the opaque
// type and its interface constraints.
SmallVector<GenericTypeParamType *, 4> interfaceGenericParams;
SmallVector<Requirement, 4> interfaceRequirements;
GenericSignatureBuilder builder(Context);

auto originatingDC = originatingDecl->getInnermostDeclContext();
unsigned returnTypeDepth = 0;
auto outerGenericSignature = originatingDC->getGenericSignatureOfContext();

if (outerGenericSignature) {
std::copy(outerGenericSignature->getGenericParams().begin(),
outerGenericSignature->getGenericParams().end(),
std::back_inserter(interfaceGenericParams));
std::copy(outerGenericSignature->getRequirements().begin(),
outerGenericSignature->getRequirements().end(),
std::back_inserter(interfaceRequirements));
builder.addGenericSignature(outerGenericSignature);
returnTypeDepth =
outerGenericSignature->getGenericParams().back()->getDepth() + 1;
}

unsigned returnTypeDepth = 0;
if (!interfaceGenericParams.empty())
returnTypeDepth = interfaceGenericParams.back()->getDepth() + 1;

auto returnTypeParam = GenericTypeParamType::get(returnTypeDepth, 0,
Context);
interfaceGenericParams.push_back(returnTypeParam);

builder.addGenericParameter(returnTypeParam);

if (constraintType->getClassOrBoundGenericClass()) {
// Use the type as a superclass constraint.
interfaceRequirements.push_back(Requirement(RequirementKind::Superclass,
returnTypeParam, constraintType));
builder.addRequirement(Requirement(RequirementKind::Superclass,
returnTypeParam, constraintType),
GenericSignatureBuilder::FloatingRequirementSource::forAbstract(),
originatingDC->getParentModule());
} else {
auto constraints = constraintType->getExistentialLayout();
if (auto superclass = constraints.getSuperclass()) {
interfaceRequirements.push_back(Requirement(RequirementKind::Superclass,
returnTypeParam, superclass));
builder.addRequirement(Requirement(RequirementKind::Superclass,
returnTypeParam, superclass),
GenericSignatureBuilder::FloatingRequirementSource::forAbstract(),
originatingDC->getParentModule());
}
for (auto protocol : constraints.getProtocols()) {
interfaceRequirements.push_back(Requirement(RequirementKind::Conformance,
returnTypeParam, protocol));
builder.addRequirement(Requirement(RequirementKind::Conformance,
returnTypeParam, protocol),
GenericSignatureBuilder::FloatingRequirementSource::forAbstract(),
originatingDC->getParentModule());
}
if (auto layout = constraints.getLayoutConstraint()) {
interfaceRequirements.push_back(Requirement(RequirementKind::Layout,
returnTypeParam, layout));
builder.addRequirement(Requirement(RequirementKind::Layout,
returnTypeParam, layout),
GenericSignatureBuilder::FloatingRequirementSource::forAbstract(),
originatingDC->getParentModule());
}
}

auto interfaceSignature = GenericSignature::get(interfaceGenericParams,
interfaceRequirements);
auto interfaceSignature = std::move(builder)
.computeGenericSignature(SourceLoc());

// Create the OpaqueTypeDecl for the result type.
// It has the same parent context and generic environment as the originating
Expand Down
17 changes: 17 additions & 0 deletions test/type/opaque_constraint_order.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend -emit-ir -verify %s

// rdar://problem/50309983, make sure that the generic signature built for
// an opaque return type is correct when there are associated type constraints
// on the enclosing generic context

protocol Bort {}

extension Int: Bort {}

struct Butt<T: RandomAccessCollection>
where T.Element: Bort, T.Index: Hashable
{
func foo() -> some Bort {
return 0
}
}