Skip to content

OpenedExistentials: Do not attempt to erase existential metatypes wit… #78593

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
Jan 19, 2025
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
9 changes: 4 additions & 5 deletions lib/Sema/OpenedExistentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ findGenericParameterReferencesRec(CanGenericSignature genericSig,
}

// Metatypes preserve variance.
if (auto metaTy = type->getAs<MetatypeType>()) {
if (auto metaTy = type->getAs<AnyMetatypeType>()) {
return findGenericParameterReferencesRec(genericSig, origParam, openedParam,
metaTy->getInstanceType(),
position, canBeCovariantResult);
Expand Down Expand Up @@ -272,10 +272,9 @@ findGenericParameterReferencesRec(CanGenericSignature genericSig,
TypePosition::Invariant, /*canBeCovariantResult=*/false);
}

// Specifically ignore parameterized protocols and existential
// metatypes because we can erase them to the upper bound.
if (type->is<ParameterizedProtocolType>() ||
type->is<ExistentialMetatypeType>()) {
// Specifically ignore parameterized protocols because we can erase them to
// the upper bound.
if (type->is<ParameterizedProtocolType>()) {
return GenericParameterReferenceInfo();
}

Expand Down
14 changes: 14 additions & 0 deletions test/Constraints/opened_existentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,20 @@ do {
nestedMetatypeCallee(t)
}

do {
class C<T> {}
protocol P {}

func f<T: P>(_: T, _: (() -> any (P & C<T>).Type)? = nil) {}
// expected-note@-1 {{required by local function 'f' where 'T' = 'any P'}}

let p: any P
// CHECK-NOT: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
f(p)
// expected-error@-1 {{type 'any P' cannot conform to 'P'}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
}

do {
protocol P {}

Expand Down
6 changes: 6 additions & 0 deletions test/decl/protocol/existential_member_access/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ do {
// expected-error@+1 {{non-protocol, non-class type 'Sequence<${type}>' cannot be used within a protocol-constrained type}}
func invariant13() -> any P & Sequence<${type}>
func invariant14() -> (any Sequence<${type}>).Type
func invariant15() -> any (P & Class<${type}>).Type


var invariantProp1: (inout ${type}) -> Void { get }
Expand All @@ -523,6 +524,7 @@ do {
var invariantProp9: (Struct<() -> ${type}>) -> Void { get }
var invariantProp10: (any P & Class<${type}>) -> Void { get }
var invariantProp11: Struct<${type}>.InnerGeneric<Void> { get }
var invariantProp15: any (P & Class<${type}>).Type { get }

subscript(invariantSubscript1 _: Struct<${type}>) -> Void { get }
subscript(invariantSubscript2 _: Void) -> Struct<${type}> { get }
Expand All @@ -532,6 +534,7 @@ do {
subscript(invariantSubscript6 _: Struct<() -> ${type}>) -> Void { get }
subscript(invariantSubscript7 _: any P & Class<${type}>) -> Void { get }
subscript(invariantSubscript8 _: Void) -> Struct<${type}>.InnerGeneric<Void> { get }
subscript(invariantSubscript15 _: Void) -> any (P & Class<${type}>).Type { get }
}

let exist: any P
Expand Down Expand Up @@ -561,6 +564,7 @@ do {
var types = SwiftTypePair(typeOf: exist.invariant14(), type2: SwiftType<(any Sequence).Type>.self)
types.assertTypesAreEqual()
}
exist.invariant15() // expected-error {{member 'invariant15' cannot be used on value of type 'any P'; consider using a generic constraint instead}}

exist.invariantProp1 // expected-error {{member 'invariantProp1' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
exist.invariantProp2 // expected-error {{member 'invariantProp2' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
Expand All @@ -573,6 +577,7 @@ do {
exist.invariantProp9 // expected-error {{member 'invariantProp9' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
exist.invariantProp10 // expected-error {{member 'invariantProp10' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
exist.invariantProp11 // expected-error {{member 'invariantProp11' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
exist.invariantProp15 // expected-error {{member 'invariantProp15' cannot be used on value of type 'any P'; consider using a generic constraint instead}}

exist[invariantSubscript1: 0] // expected-error {{member 'subscript' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
exist[invariantSubscript2: ()] // expected-error {{member 'subscript' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
Expand All @@ -582,4 +587,5 @@ do {
exist[invariantSubscript6: 0] // expected-error {{member 'subscript' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
exist[invariantSubscript7: 0] // expected-error {{member 'subscript' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
exist[invariantSubscript8: ()] // expected-error {{member 'subscript' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
exist[invariantSubscript15: ()] // expected-error {{member 'subscript' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
}