Skip to content

Commit bca1dd7

Browse files
authored
Merge pull request #71575 from slavapestov/assoc-type-regression-fixes-4
Sema: Associated type inference regressions, round 4
2 parents 808c42b + c34a74c commit bca1dd7

11 files changed

+1009
-228
lines changed

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 672 additions & 218 deletions
Large diffs are not rendered by default.

test/Generics/associated_type_where_clause.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 4
1+
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-experimental-associated-type-inference
2+
// RUN: %target-typecheck-verify-swift -swift-version 4 -disable-experimental-associated-type-inference
23

34
func needsSameType<T>(_: T.Type, _: T.Type) {}
45

test/Sema/where_clause_across_module_boundaries.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -emit-module -o %t/ModuleA.swiftmodule %S/Inputs/where_clause_across_module_boundaries_module.swift
3-
// RUN: %target-typecheck-verify-swift -I %t
3+
// RUN: %target-typecheck-verify-swift -I %t -enable-experimental-associated-type-inference
4+
// RUN: %target-typecheck-verify-swift -I %t -disable-experimental-associated-type-inference
45

56
// https://github.com/apple/swift/issues/58084
67
// Associated Type Inference fails across module boundaries

test/decl/protocol/req/associated_type_inference_abstract.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference
2+
// RUN: not %target-typecheck-verify-swift -disable-experimental-associated-type-inference
23

34
protocol Q {}
45

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,107 @@
1-
// RUN: %target-typecheck-verify-swift -disable-experimental-associated-type-inference
2-
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference
1+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA1
2+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA2
3+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA3
4+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA4
5+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA5
6+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA6
7+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA7
8+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA8
9+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA9
10+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA10
11+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA11
12+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA12
13+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA13
14+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA14
15+
16+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA1
17+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA2
18+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA3
19+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA4
20+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA5
21+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA6
22+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA7
23+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA8
24+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA9
25+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA10
26+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA11
27+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA12
28+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA13
29+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA14
30+
31+
#if A1
332

4-
// The 'for' loop has to come first, to force Sequence.makeIterator().
533
for x in S() { _ = x }
634

35+
#elseif A2
36+
37+
func f<T: Sequence>(_: T.Type) -> T.Element.Type { fatalError() }
38+
let x: String.Type = f(S.self)
39+
40+
#elseif A3
41+
42+
func f<T: Sequence>(_: T.Type) -> T.Iterator.Type { fatalError() }
43+
let x: IndexingIterator<S>.Type = f(S.self)
44+
45+
#elseif A4
46+
47+
func f<T: Sequence>(_: T.Type) -> T.Iterator.Element.Type { fatalError() }
48+
let x: String.Type = f(S.self)
49+
50+
#elseif A5
51+
52+
func f<T: Collection>(_: T.Type) -> T.Element.Type { fatalError() }
53+
let x: String.Type = f(S.self)
54+
55+
#elseif A6
56+
57+
func f<T: Collection>(_: T.Type) -> T.Index.Type { fatalError() }
58+
let x: Int.Type = f(S.self)
59+
60+
#elseif A7
61+
62+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Type { fatalError() }
63+
let x: Slice<S>.Type = f(S.self)
64+
65+
#elseif A8
66+
67+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Element.Type { fatalError() }
68+
let x: String.Type = f(S.self)
69+
70+
#elseif A9
71+
72+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Index.Type { fatalError() }
73+
let x: Int.Type = f(S.self)
74+
75+
#elseif A10
76+
77+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Iterator.Type { fatalError() }
78+
let x: IndexingIterator<Slice<S>>.Type = f(S.self)
79+
80+
#elseif A11
81+
82+
func f<T: Collection>(_: T.Type) -> T.Indices.Type { fatalError() }
83+
let x: Range<Int>.Type = f(S.self)
84+
85+
#elseif A12
86+
87+
func f<T: Collection>(_: T.Type) -> T.Indices.Element.Type { fatalError() }
88+
let x: Int.Type = f(S.self)
89+
90+
#elseif A13
91+
92+
func f<T: Collection>(_: T.Type) -> T.Indices.SubSequence.Type { fatalError() }
93+
let x: Range<Int>.Type = f(S.self)
94+
95+
#elseif A14
96+
97+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Indices.Type { fatalError() }
98+
let x: Range<Int>.Type = f(S.self)
99+
100+
#endif
101+
7102
struct S: RandomAccessCollection {
8103
public var startIndex: Int { 0 }
9104
public var endIndex: Int { 0 }
10-
public subscript(position: Int) -> Int { 0 }
105+
public subscript(position: Int) -> String { "" }
11106
}
12107

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA1
2+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA2
3+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA3
4+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA4
5+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA5
6+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA6
7+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA7
8+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA8
9+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA9
10+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA10
11+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA11
12+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA12
13+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA13
14+
// RUN: %target-swift-frontend -emit-silgen %s -disable-experimental-associated-type-inference -DA14
15+
16+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA1
17+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA2
18+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA3
19+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA4
20+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA5
21+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA6
22+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA7
23+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA8
24+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA9
25+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA10
26+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA11
27+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA12
28+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA13
29+
// RUN: %target-swift-frontend -emit-silgen %s -enable-experimental-associated-type-inference -DA14
30+
31+
#if A1
32+
33+
for x in G<String>() { _ = x }
34+
35+
#elseif A2
36+
37+
func f<T: Sequence>(_: T.Type) -> T.Element.Type { fatalError() }
38+
let x: String.Type = f(G<String>.self)
39+
40+
#elseif A3
41+
42+
func f<T: Sequence>(_: T.Type) -> T.Iterator.Type { fatalError() }
43+
let x: IndexingIterator<G<String>>.Type = f(G<String>.self)
44+
45+
#elseif A4
46+
47+
func f<T: Sequence>(_: T.Type) -> T.Iterator.Element.Type { fatalError() }
48+
let x: String.Type = f(G<String>.self)
49+
50+
#elseif A5
51+
52+
func f<T: Collection>(_: T.Type) -> T.Element.Type { fatalError() }
53+
let x: String.Type = f(G<String>.self)
54+
55+
#elseif A6
56+
57+
func f<T: Collection>(_: T.Type) -> T.Index.Type { fatalError() }
58+
let x: Int.Type = f(G<String>.self)
59+
60+
#elseif A7
61+
62+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Type { fatalError() }
63+
let x: Slice<G<String>>.Type = f(G<String>.self)
64+
65+
#elseif A8
66+
67+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Element.Type { fatalError() }
68+
let x: String.Type = f(G<String>.self)
69+
70+
#elseif A9
71+
72+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Index.Type { fatalError() }
73+
let x: Int.Type = f(G<String>.self)
74+
75+
#elseif A10
76+
77+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Iterator.Type { fatalError() }
78+
let x: IndexingIterator<Slice<G<String>>>.Type = f(G<String>.self)
79+
80+
#elseif A11
81+
82+
func f<T: Collection>(_: T.Type) -> T.Indices.Type { fatalError() }
83+
let x: Range<Int>.Type = f(G<String>.self)
84+
85+
#elseif A12
86+
87+
func f<T: Collection>(_: T.Type) -> T.Indices.Element.Type { fatalError() }
88+
let x: Int.Type = f(G<String>.self)
89+
90+
#elseif A13
91+
92+
func f<T: Collection>(_: T.Type) -> T.Indices.SubSequence.Type { fatalError() }
93+
let x: Range<Int>.Type = f(G<String>.self)
94+
95+
#elseif A14
96+
97+
func f<T: Collection>(_: T.Type) -> T.SubSequence.Indices.Type { fatalError() }
98+
let x: Range<Int>.Type = f(G<String>.self)
99+
100+
#endif
101+
102+
struct G<Element>: RandomAccessCollection {
103+
public var startIndex: Int { 0 }
104+
public var endIndex: Int { 0 }
105+
public subscript(position: Int) -> Element { fatalError() }
106+
}

test/decl/protocol/req/issue-10831.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference
2+
// RUN: not %target-typecheck-verify-swift -disable-experimental-associated-type-inference
23

34
struct G<T> {}
45

test/decl/protocol/req/rdar122587920.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference
2-
// RUN: %target-typecheck-verify-swift -disable-experimental-associated-type-inference
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference -DNEW
2+
// RUN: %target-typecheck-verify-swift -disable-experimental-associated-type-inference -DOLD
33

44
struct S<Element> {}
55

@@ -27,4 +27,10 @@ extension S: Collection {
2727
}
2828
}
2929

30+
// The old behavior didn't make much sense.
31+
32+
#if NEW
3033
let x: S<Int>.Type = S<Int>.Iterator.self
34+
#elseif OLD
35+
let x: IndexingIterator<S<Int>>.Type = S<Int>.Iterator.self
36+
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference
2+
// RUN: %target-typecheck-verify-swift -disable-experimental-associated-type-inference
3+
4+
protocol P1 {
5+
associatedtype A
6+
7+
func f1(_: C) -> A
8+
func f2(_: A, _: C)
9+
10+
typealias C = S1<Self>
11+
}
12+
13+
struct S1<T> {}
14+
15+
protocol P2: P1 where A == B {
16+
associatedtype B
17+
18+
func g1(_: C) -> B
19+
func g2(_: B, _: C)
20+
}
21+
22+
extension P2 {
23+
func f1(_: C) -> B { fatalError() }
24+
func f2(_: B, _: C) { fatalError() }
25+
}
26+
27+
extension P2 {
28+
func g2(_: B, _: C) {}
29+
}
30+
31+
struct S2: P2 {
32+
func g1(_: C) -> Int {
33+
fatalError()
34+
}
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %target-typecheck-verify-swift -disable-experimental-associated-type-inference
2+
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference
3+
4+
public protocol P1 {
5+
associatedtype A = Void
6+
7+
func makeA() -> A
8+
func consumeA(a: inout A)
9+
}
10+
11+
extension P1 where A == Void {
12+
// Don't consider this witness in the 'S2: P1' conformance below.
13+
public func makeA() -> A { fatalError() }
14+
}
15+
16+
public struct S1: P1 {}
17+
18+
public protocol P2: P1 where A == B.A {
19+
associatedtype B: P1
20+
var base: B { get }
21+
}
22+
23+
extension P2 {
24+
public func makeA() -> B.A { fatalError() }
25+
public func consumeA(a: inout B.A) {}
26+
}
27+
28+
extension S1: P2 {
29+
public var base: S2 { fatalError() }
30+
}
31+
32+
public struct S2 {}
33+
34+
public struct S3 {}
35+
36+
extension S2: P1 {
37+
public typealias A = S3
38+
}
39+
40+
public protocol P3: P1 where A == S3 {}
41+
42+
extension P3 {
43+
public func makeA() -> A { fatalError() }
44+
public func consumeA(a: inout A) {}
45+
}
46+
47+
extension S2: P3 {}
48+
49+
let x: S3.Type = S2.A.self
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %target-typecheck-verify-swift -disable-experimental-associated-type-inference -disable-availability-checking
2+
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference -disable-availability-checking
3+
4+
public protocol P<A> {
5+
associatedtype A
6+
associatedtype B: P
7+
8+
func makeA() -> A
9+
var b: B { get }
10+
}
11+
12+
extension P where A == B.A {
13+
public func makeA() -> B.A {
14+
fatalError()
15+
}
16+
}
17+
18+
public struct S: P {
19+
public var b: some P<Int> {
20+
return G<Int>()
21+
}
22+
}
23+
24+
public struct G<A>: P {
25+
public func makeA() -> A { fatalError() }
26+
public var b: Never { fatalError() }
27+
}
28+
29+
extension Never: P {
30+
public func makeA() -> Never { fatalError() }
31+
public var b: Never { fatalError() }
32+
}

0 commit comments

Comments
 (0)