Skip to content

Commit 35a0a93

Browse files
authored
Merge pull request #86080 from kavon/suppressed-assoc-types-with-defaults-3
SuppressedAssociatedTypesWithDefaults: make scoping pay attention to root
2 parents 5c4ce2f + fbd51c0 commit 35a0a93

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

lib/AST/RequirementMachine/ApplyInverses.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ void swift::rewriting::applyInverses(
204204
// func f() where Self: ~Copyable
205205
// }
206206
//
207-
if (representativeGPs.find(canSubject) == representativeGPs.end()) {
207+
auto subjectRoot = canSubject->getDependentMemberRoot()->getCanonicalType();
208+
if (representativeGPs.find(subjectRoot) == representativeGPs.end()) {
208209
errors.push_back(
209210
RequirementError::forInvalidInverseOuterSubject(inverse));
210211
continue;

test/Generics/inverse_assoc_types.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protocol Iterable<Element>: ~Copyable {
5353

5454
struct ReqC<T: Copyable> {}
5555

56-
func reqC<T: Copyable>(_ t: T) {} // expected-note 8{{'where T: Copyable' is implicit here}}
56+
func reqC<T: Copyable>(_ t: T) {} // expected-note 14{{'where T: Copyable' is implicit here}}
5757

5858

5959
protocol ProvideA<A>: ~Copyable {
@@ -130,25 +130,45 @@ func checkExistential(_ s: any Gen) {
130130
reqC(s.i().e().i().e())
131131
}
132132

133+
func checkExistential2<R: ~Copyable>(_ s: any Gen<R>) {
134+
reqC(s.e()) // expected-error {{global function 'reqC' requires that 'R' conform to 'Copyable'}}
135+
reqC(s.e().e()) // expected-error {{value of type 'R' has no member 'e'}}
136+
137+
reqC(s.i()) // expected-error {{requires that 'T' conform to 'Copyable'}}
138+
reqC(s.i().e())
139+
reqC(s.i().e().i()) // expected-error {{requires that 'T' conform to 'Copyable'}}
140+
reqC(s.i().e().i().e())
141+
}
142+
143+
// Suppression on the constrained existential only goes far down
144+
// as possible to suppress on the generic type parameter itself.
145+
func checkExistential3<R>(_ s: any Gen<R>) where R: Gen, R: ~Copyable, R.E: ~Copyable {
146+
reqC(s.e()) // expected-error {{global function 'reqC' requires that 'R' conform to 'Copyable'}}
147+
reqC(s.e().e()) // expected-error {{global function 'reqC' requires that 'R.E' conform to 'Copyable'}}
148+
reqC(s.e().e().e())
149+
}
150+
133151
protocol Veggie<A> {
134152
associatedtype A: ~Copyable
135153
associatedtype NeedsCopyable
154+
155+
func a() -> A
136156
}
137157
protocol Carrot: Veggie
138158
where Self.NeedsCopyable: ~Copyable {} // expected-error {{'Self.NeedsCopyable' required to be 'Copyable' but is marked with '~Copyable'}}
139-
// expected-error @-1{{cannot suppress '~Copyable' on generic parameter 'Self.NeedsCopyable' defined in outer scope}}
140159

141160
protocol CarrotCake: Carrot where Self.A: ~Copyable {} // expected-error {{'Self.A' required to be 'Copyable' but is marked with '~Copyable'}}
142-
// expected-error @-1{{cannot suppress '~Copyable' on generic parameter 'Self.A' defined in outer scope}}
143161

162+
func ex1<Cucumber: ~Copyable, Potato>(_ nc: any Veggie<Cucumber>, c: any Veggie<Potato>) {
163+
reqC(nc.a()) // expected-error {{global function 'reqC' requires that 'Cucumber' conform to 'Copyable'}}
164+
reqC(c.a())
165+
}
144166

145167
protocol Bird {
146168
associatedtype Song
147169
}
148170

149171
protocol Eagle: Bird where Self.Song: ~Copyable {}// expected-error {{'Self.Song' required to be 'Copyable' but is marked with '~Copyable'}}
150-
// expected-error @-1{{cannot suppress '~Copyable' on generic parameter 'Self.Song' defined in outer scope}}
151-
152172

153173
protocol Pushable<Element> {
154174
associatedtype Element: ~Copyable

test/Generics/inverse_extensions.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ protocol HasAssoc {
1616
associatedtype A
1717
}
1818
extension HasAssoc where Self.A: ~Copyable {}
19-
// expected-error@-1 {{cannot suppress '~Copyable' on generic parameter 'Self.A' defined in outer scope}}
20-
// expected-error@-2 {{'Self.A' required to be 'Copyable' but is marked with '~Copyable'}}
19+
// expected-error@-1 {{'Self.A' required to be 'Copyable' but is marked with '~Copyable'}}
2120

2221
class Box<T: ~Copyable> {}
2322
extension Box where T: ~Copyable {}

0 commit comments

Comments
 (0)