Skip to content

Commit 7b29c84

Browse files
authored
Merge pull request #78631 from slavapestov/fix-rdar141968103-6.1
Sema: Relax primary associated type matching in matchExistentialTypes() [6.1]
2 parents ac10f1e + b50c38f commit 7b29c84

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4207,7 +4207,7 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
42074207

42084208
// Finally, check parameterized protocol requirements.
42094209
if (!layout.getParameterizedProtocols().empty()) {
4210-
SmallVector<std::pair<AssociatedTypeDecl *, Type>, 4> fromReqs;
4210+
SmallVector<std::pair<Identifier, Type>, 4> fromReqs;
42114211

42124212
if (type1->isExistentialType()) {
42134213
auto fromLayout = type1->getExistentialLayout();
@@ -4218,8 +4218,7 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
42184218

42194219
for (unsigned i : indices(argTypes)) {
42204220
auto argType = argTypes[i];
4221-
auto *assocType = assocTypes[i]->getAssociatedTypeAnchor();
4222-
fromReqs.push_back(std::make_pair(assocType, argType));
4221+
fromReqs.push_back(std::make_pair(assocTypes[i]->getName(), argType));
42234222
}
42244223
}
42254224
}
@@ -4234,10 +4233,9 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
42344233

42354234
for (unsigned i : indices(argTypes)) {
42364235
auto argType = argTypes[i];
4237-
auto *assocType = assocTypes[i]->getAssociatedTypeAnchor();
42384236
bool found = false;
42394237
for (auto fromReq : fromReqs) {
4240-
if (fromReq.first == assocType) {
4238+
if (fromReq.first == assocTypes[i]->getName()) {
42414239
// FIXME: Extend the locator path to point to the argument
42424240
// inducing the requirement.
42434241
auto result = matchTypes(fromReq.second, argType,

test/Constraints/parameterized_existential_unrelated_args.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,25 @@ var q: any Q<String> = p // expected-error {{cannot convert value of type 'any P
3131
// Previously we accepted the above conversion, and then getB()
3232
// would return something that was dynamically Array<String>
3333
// and not String as expected.
34-
print(q.getB())
34+
// print(q.getB())
35+
36+
37+
// However, this is OK -- the two A's have the same name, so by the
38+
// semantics of the generics system they must be equivalent as type
39+
// parameters.
40+
41+
protocol P1<A> {
42+
associatedtype A
43+
}
44+
45+
protocol P2<A> {
46+
associatedtype A
47+
}
48+
49+
protocol P3<A>: P1, P2 {
50+
associatedtype A
51+
}
52+
53+
func f<T>(_ value: any P3<T>) -> (any P1<T>, any P2<T>) {
54+
return (value, value)
55+
}

0 commit comments

Comments
 (0)