Skip to content

Commit 5f79319

Browse files
authored
Merge pull request #78559 from slavapestov/fix-rdar141968103
Sema: Relax primary associated type matching in matchExistentialTypes()
2 parents 84c7188 + c257cdb commit 5f79319

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
@@ -4222,7 +4222,7 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
42224222

42234223
// Finally, check parameterized protocol requirements.
42244224
if (!layout.getParameterizedProtocols().empty()) {
4225-
SmallVector<std::pair<AssociatedTypeDecl *, Type>, 4> fromReqs;
4225+
SmallVector<std::pair<Identifier, Type>, 4> fromReqs;
42264226

42274227
if (type1->isExistentialType()) {
42284228
auto fromLayout = type1->getExistentialLayout();
@@ -4233,8 +4233,7 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
42334233

42344234
for (unsigned i : indices(argTypes)) {
42354235
auto argType = argTypes[i];
4236-
auto *assocType = assocTypes[i]->getAssociatedTypeAnchor();
4237-
fromReqs.push_back(std::make_pair(assocType, argType));
4236+
fromReqs.push_back(std::make_pair(assocTypes[i]->getName(), argType));
42384237
}
42394238
}
42404239
}
@@ -4249,10 +4248,9 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
42494248

42504249
for (unsigned i : indices(argTypes)) {
42514250
auto argType = argTypes[i];
4252-
auto *assocType = assocTypes[i]->getAssociatedTypeAnchor();
42534251
bool found = false;
42544252
for (auto fromReq : fromReqs) {
4255-
if (fromReq.first == assocType) {
4253+
if (fromReq.first == assocTypes[i]->getName()) {
42564254
// FIXME: Extend the locator path to point to the argument
42574255
// inducing the requirement.
42584256
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)