Skip to content

[GSB] Merge layout constraints when merging equivalence classes. #15308

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
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
18 changes: 16 additions & 2 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4106,8 +4106,6 @@ static ConstraintResult visitInherited(
ConstraintResult result = ConstraintResult::Resolved;
std::function<void(Type, const TypeRepr *)> visitInherited;

// FIXME: Should this whole thing use getExistentialLayout() instead?

visitInherited = [&](Type inheritedType, const TypeRepr *typeRepr) {
// Decompose explicitly-written protocol compositions.
if (auto composition = dyn_cast_or_null<CompositionTypeRepr>(typeRepr)) {
Expand Down Expand Up @@ -4938,6 +4936,22 @@ GenericSignatureBuilder::addSameTypeRequirementBetweenTypeParameters(
// Make T1 the representative of T2, merging the equivalence classes.
T2->representativeOrEquivClass = T1;

// Layout requirements.
if (equivClass2 && equivClass2->layout) {
if (!equivClass->layout) {
equivClass->layout = equivClass2->layout;
equivClass->layoutConstraints = std::move(equivClass2->layoutConstraints);
} else {
const RequirementSource *source2
= equivClass2->layoutConstraints.front().source;
addLayoutRequirementDirect(T1, equivClass2->layout, source2);
equivClass->layoutConstraints.insert(
equivClass->layoutConstraints.end(),
equivClass2->layoutConstraints.begin() + 1,
equivClass2->layoutConstraints.end());
}
}

// Superclass requirements.
if (equivClass2 && equivClass2->superclass) {
const RequirementSource *source2;
Expand Down
20 changes: 20 additions & 0 deletions test/Generics/class_constraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ struct Z<U> {
let bad3: X<U> // expected-error{{'X' requires that 'U' be a class type}}
}

// SR-7168: layout constraints weren't getting merged.
protocol P1 {
associatedtype A
var a: A { get }
}

protocol P2 {
associatedtype B: P1
var b: B { get }
}

func requiresAnyObject<T: AnyObject>(_: T) { }

func anyObjectConstraint<T: P2, U: P2>(_ t: T, _ u: U)
where T.B.A: AnyObject, U.B: AnyObject, T.B == T.B.A, U.B.A == U.B {
requiresAnyObject(t.b)
requiresAnyObject(u.b)
requiresAnyObject(t.b.a)
requiresAnyObject(u.b.a)
}