Skip to content

Existential Types Cannot Satisfy Superclass Bounds #58624

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 3, 2022
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
7 changes: 5 additions & 2 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3465,8 +3465,11 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
if (!req)
return getTypeMatchFailure(locator);

if (type1->isPlaceholder() ||
req->getRequirementKind() == RequirementKind::Superclass)
// Superclass constraints are never satisfied by existentials,
// even those that contain the superclass a la `any C & P`.
if (!type1->isExistentialType() &&
(type1->isPlaceholder() ||
req->getRequirementKind() == RequirementKind::Superclass))
return getTypeMatchSuccess();

auto *fix = fixRequirementFailure(*this, type1, type2, locator);
Expand Down
22 changes: 22 additions & 0 deletions test/Constraints/members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,25 @@ func fSR14533(_ s: SR14533) {
// expected-error@-1{{value of type 'SR14533' has no member 'ys'}}
}
}

// rdar://92358570
class SomeClassBound {}
protocol ClassBoundProtocol: SomeClassBound {
}

struct RDAR92358570<Element> {}

extension RDAR92358570 where Element : SomeClassBound {
// expected-note@-1 2 {{where 'Element' = 'any ClassBoundProtocol', 'SomeClassBound' = 'AnyObject'}}
// expected-note@-2 2 {{where 'Element' = 'any SomeClassBound & ClassBoundProtocol', 'SomeClassBound' = 'AnyObject'}}
func doSomething() {}
static func doSomethingStatically() {}
}

func rdar92358570(_ x: RDAR92358570<ClassBoundProtocol>, _ y: RDAR92358570<SomeClassBound & ClassBoundProtocol>) {
x.doSomething() // expected-error {{referencing instance method 'doSomething()' on 'RDAR92358570' requires that 'any ClassBoundProtocol' inherit from 'AnyObject'}}
RDAR92358570<ClassBoundProtocol>.doSomethingStatically() // expected-error {{referencing static method 'doSomethingStatically()' on 'RDAR92358570' requires that 'any ClassBoundProtocol' inherit from 'AnyObject'}}

y.doSomething() // expected-error {{referencing instance method 'doSomething()' on 'RDAR92358570' requires that 'any SomeClassBound & ClassBoundProtocol' inherit from 'AnyObject'}}
RDAR92358570<SomeClassBound & ClassBoundProtocol>.doSomethingStatically() // expected-error {{referencing static method 'doSomethingStatically()' on 'RDAR92358570' requires that 'any SomeClassBound & ClassBoundProtocol' inherit from 'AnyObject'}}
}