Skip to content

Commit 2e44f51

Browse files
authored
Merge pull request #12637 from DougGregor/gsb-associated-type-redecl
2 parents 76d1551 + 3452dda commit 2e44f51

11 files changed

+71
-26
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3497,7 +3497,9 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
34973497

34983498
bool shouldWarnAboutRedeclaration =
34993499
source->kind == RequirementSource::RequirementSignatureSelf &&
3500-
assocTypeDecl->getDefaultDefinitionLoc().isNull();
3500+
assocTypeDecl->getDefaultDefinitionLoc().isNull() &&
3501+
(!assocTypeDecl->getInherited().empty() ||
3502+
assocTypeDecl->getTrailingWhereClause());
35013503
for (auto inheritedType : knownInherited->second) {
35023504
// If we have inherited associated type...
35033505
if (auto inheritedAssocTypeDecl =

test/Constraints/generic_overload.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
protocol P1 { associatedtype Assoc } // expected-note{{declared here}}
4-
protocol P2 : P1 { associatedtype Assoc } // expected-warning{{redeclaration of associated type}}
3+
protocol P1 { associatedtype Assoc }
4+
protocol P2 : P1 { associatedtype Assoc }
55
protocol P3 { }
66

77
struct X1 : P1 { typealias Assoc = X3 }

test/Generics/associated_type_where_clause.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,16 @@ protocol P {
138138

139139
// Lookup of same-named associated types aren't ambiguous in this context.
140140
protocol P1 {
141-
associatedtype A // expected-note 2{{declared here}}
141+
associatedtype A
142142
}
143143

144144
protocol P2: P1 {
145-
associatedtype A // expected-warning{{redeclaration of associated type}}
145+
associatedtype A
146146
associatedtype B where A == B
147147
}
148148

149149
protocol P3: P1 {
150-
associatedtype A // expected-warning{{redeclaration of associated type}}
150+
associatedtype A
151151
}
152152

153153
protocol P4 {

test/Generics/canonicalization.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
protocol P0 { }
55

66
protocol P {
7-
associatedtype A // expected-note{{declared here}}
7+
associatedtype A
88
}
99

1010
protocol Q : P {
11-
associatedtype A // expected-warning{{redeclaration of associated type 'A' from protocol 'P' is better expressed as a 'where' clause on the protocol}}
11+
associatedtype A
1212
}
1313

1414
func f<T>(t: T) where T : P, T : Q, T.A : P0 { } // expected-note{{'f(t:)' previously declared here}}

test/Generics/protocol_requirement_signatures.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protocol P3 {}
2222
// CHECK-NEXT: Requirement signature: <Self where Self.X : P1>
2323
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.X : P1>
2424
protocol Q1 {
25-
associatedtype X: P1 // expected-note 2{{declared here}}
25+
associatedtype X: P1 // expected-note {{declared here}}
2626
}
2727

2828
// inheritance
@@ -36,7 +36,7 @@ protocol Q2: Q1 {}
3636
// CHECK-NEXT: Requirement signature: <Self where Self : Q1>
3737
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0 : Q1>
3838
protocol Q3: Q1 {
39-
associatedtype X // expected-warning{{redeclaration of associated type 'X'}}
39+
associatedtype X
4040
}
4141

4242
// inheritance adding a new conformance

test/Generics/requirement_inference.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ extension P7 where AssocP6.Element : P6, // expected-note{{conformance constrain
139139
}
140140

141141
protocol P8 {
142-
associatedtype A // expected-note{{'A' declared here}}
143-
associatedtype B // expected-note{{'B' declared here}}
142+
associatedtype A
143+
associatedtype B
144144
}
145145

146146
protocol P9 : P8 {
147-
associatedtype A // expected-warning{{redeclaration of associated type 'A' from protocol 'P8' is better expressed as a 'where' clause on the protocol}}
148-
associatedtype B // expected-warning{{redeclaration of associated type 'B' from protocol 'P8' is better expressed as a 'where' clause on the protocol}}
147+
associatedtype A
148+
associatedtype B
149149
}
150150

151151
protocol P10 {

test/IDE/complete_associated_types.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ protocol FooBaseProtocolWithAssociatedTypes {
3232
associatedtype FooBaseDefaultedTypeB = Int
3333
associatedtype FooBaseDefaultedTypeC = Int
3434

35-
associatedtype DeducedTypeCommonA // expected-note{{declared here}}
36-
associatedtype DeducedTypeCommonB // expected-note{{declared here}}
35+
associatedtype DeducedTypeCommonA
36+
associatedtype DeducedTypeCommonB
3737
associatedtype DeducedTypeCommonC
3838
associatedtype DeducedTypeCommonD
3939
func deduceCommonA() -> DeducedTypeCommonA
@@ -57,8 +57,8 @@ protocol FooProtocolWithAssociatedTypes : FooBaseProtocolWithAssociatedTypes {
5757

5858
associatedtype FooBaseDefaultedTypeB = Double
5959

60-
associatedtype DeducedTypeCommonA // expected-warning{{redeclaration of associated type}}
61-
associatedtype DeducedTypeCommonB // expected-warning{{redeclaration of associated type}}
60+
associatedtype DeducedTypeCommonA
61+
associatedtype DeducedTypeCommonB
6262
func deduceCommonA() -> DeducedTypeCommonA
6363
func deduceCommonB() -> DeducedTypeCommonB
6464

@@ -79,7 +79,7 @@ protocol BarBaseProtocolWithAssociatedTypes {
7979
associatedtype DefaultedTypeCommonA = Int
8080
associatedtype DefaultedTypeCommonC = Int
8181

82-
associatedtype DeducedTypeCommonA // expected-note{{'DeducedTypeCommonA' declared here}}
82+
associatedtype DeducedTypeCommonA
8383
associatedtype DeducedTypeCommonC
8484
func deduceCommonA() -> DeducedTypeCommonA
8585
func deduceCommonC() -> DeducedTypeCommonC
@@ -102,7 +102,7 @@ protocol BarProtocolWithAssociatedTypes : BarBaseProtocolWithAssociatedTypes {
102102
associatedtype DefaultedTypeCommonA = Int
103103
associatedtype DefaultedTypeCommonD = Int
104104

105-
associatedtype DeducedTypeCommonA // expected-warning{{redeclaration of associated type}}
105+
associatedtype DeducedTypeCommonA
106106
associatedtype DeducedTypeCommonD
107107
func deduceCommonA() -> DeducedTypeCommonA
108108
func deduceCommonD() -> DeducedTypeCommonD

test/attr/attr_autoclosure.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class DerivedClass {
4242
}
4343

4444
protocol P1 {
45-
associatedtype Element // expected-note{{declared here}}
45+
associatedtype Element
4646
}
4747
protocol P2 : P1 {
48-
associatedtype Element // expected-warning{{redeclaration of associated type 'Element'}}
48+
associatedtype Element
4949
}
5050

5151
func overloadedEach<O: P1>(_ source: O, _ closure: @escaping () -> ()) {

test/attr/attr_noescape.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ func redundant(_ fn : @noescape // expected-error @+1 {{@noescape is implied by
183183

184184

185185
protocol P1 {
186-
associatedtype Element // expected-note{{declared here}}
186+
associatedtype Element
187187
}
188188
protocol P2 : P1 {
189-
associatedtype Element // expected-warning{{redeclaration of associated type 'Element'}}
189+
associatedtype Element
190190
}
191191

192192
func overloadedEach<O: P1, T>(_ source: O, _ transform: @escaping (O.Element) -> (), _: T) {}

test/decl/protocol/indirectly_recursive_requirement.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ protocol Incrementable {
55
}
66

77
protocol _ForwardIndex {
8-
associatedtype Distance = MyInt // expected-note{{declared here}}
8+
associatedtype Distance = MyInt
99
}
1010

1111
protocol ForwardIndex : _ForwardIndex {
@@ -19,7 +19,7 @@ protocol BidirectionalIndex : ForwardIndex, _BidirectionalIndex {
1919
}
2020

2121
protocol _RandomAccessIndex : _BidirectionalIndex {
22-
associatedtype Distance // expected-warning{{redeclaration of associated type 'Distance}}
22+
associatedtype Distance
2323
}
2424

2525
protocol RandomAccessIndex
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: not %target-swift-frontend %s -typecheck
2+
3+
enum SortOrder {
4+
case ascending
5+
case equal
6+
case descending
7+
}
8+
9+
struct SortedArray<Element> {
10+
var contents = [Element]()
11+
let compare: (Element, Element) -> SortOrder
12+
13+
init(_ comparator: @escaping (Element, Element) -> SortOrder) {
14+
compare = comparator
15+
}
16+
17+
mutating func add(_ element: Element) {
18+
19+
}
20+
}
21+
22+
extension SortedArray where Element: Comparable {
23+
init() {
24+
compare = { a, b in
25+
if a < b { return .ascending }
26+
else if a > b { return .descending }
27+
else { return .equal }
28+
}
29+
}
30+
31+
init<S: Sequence>(_ sequence: S) where S.Iterator.Element == Element {
32+
self.init()
33+
34+
for element in sequence {
35+
add(element)
36+
}
37+
}
38+
}
39+
40+
extension SortedArray: Sequence {
41+
typealias Iterator = IndexingIterator
42+
43+
}

0 commit comments

Comments
 (0)