Skip to content

Commit 3178d6d

Browse files
authored
Merge pull request swiftlang#19760 from gregomni/8902
[Sema]Allow associated type inference for requirement returning dynamic Self...
2 parents e6e841c + 42a24eb commit 3178d6d

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,14 @@ swift::matchWitness(
545545
// Result types must match.
546546
// FIXME: Could allow (trivial?) subtyping here.
547547
if (!ignoreReturnType) {
548+
if (reqResultType->hasDynamicSelfType()) {
549+
auto classDecl = witness->getDeclContext()->getSelfClassDecl();
550+
if (!classDecl || classDecl->isFinal() ||
551+
witnessResultType->hasDynamicSelfType())
552+
reqResultType = reqResultType->eraseDynamicSelfType();
553+
witnessResultType = witnessResultType->eraseDynamicSelfType();
554+
}
555+
548556
auto reqTypeIsIUO =
549557
req->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
550558
auto witnessTypeIsIUO =

test/decl/protocol/conforms/self.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,29 @@ extension Node {
8686
}
8787

8888
class IntNode: Node {}
89+
90+
// SR-8902
91+
protocol P8902 {
92+
associatedtype A // expected-note {{protocol requires nested type 'A'; do you want to add it?}}
93+
func f(_ x: A) -> Self
94+
}
95+
struct S : P8902 {
96+
func f(_ x: Bool) -> S { fatalError() }
97+
}
98+
class C8902 : P8902 { // expected-error {{type 'C8902' does not conform to protocol 'P8902'}}
99+
func f(_ x: Bool) -> C8902 { fatalError() }
100+
}
101+
final class C8902b : P8902 {
102+
func f(_ x: Bool) -> C8902b { fatalError() }
103+
}
104+
class C8902c : P8902 {
105+
func f(_ x: Bool) -> Self { fatalError() }
106+
}
107+
protocol P8902complex {
108+
associatedtype A
109+
func f() -> (A, Self?)
110+
}
111+
final class C8902complex : P8902complex {
112+
func f() -> (Bool, C8902complex?) { fatalError() }
113+
}
114+

validation-test/compiler_crashers_2_fixed/0155-sr7364.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not %target-swift-frontend %s -emit-ir
1+
// RUN: %target-swift-frontend %s -emit-ir
22

33

44
public protocol E {

0 commit comments

Comments
 (0)