Skip to content

Commit 3f723e7

Browse files
committed
Sema: Allow nested typealiases to reference protocols with associated type or Self requirements
Previously we allowed this: protocol HasSelf { func foo(_: Self) } typealias Alias = HasSelf But not this: struct Outer { typealias Alias = HasSelf } Lift this restriction since the new String implementation wants to make use of it.
1 parent cbc35f0 commit 3f723e7

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4115,13 +4115,8 @@ void TypeChecker::checkUnsupportedProtocolType(Decl *decl) {
41154115
if (!decl || decl->isInvalid())
41164116
return;
41174117

4118-
// Global type aliases are okay.
4119-
if (isa<TypeAliasDecl>(decl) &&
4120-
decl->getDeclContext()->isModuleScopeContext())
4121-
return;
4122-
4123-
// Non-typealias type declarations are okay.
4124-
if (isa<TypeDecl>(decl) && !isa<TypeAliasDecl>(decl))
4118+
// Type declarations are okay.
4119+
if (isa<TypeDecl>(decl))
41254120
return;
41264121

41274122
// Extensions are okay.

test/type/protocol_types.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,32 @@ func refinementErasure(_ p: Pub) {
2323
typealias Compo = HasSelfRequirements & Bar
2424

2525
struct CompoAssocType {
26-
typealias Compo = HasSelfRequirements & Bar // expected-error{{protocol 'HasSelfRequirements' can only be used as a generic constraint}}
26+
typealias Compo = HasSelfRequirements & Bar
2727
}
2828

2929
func useAsRequirement<T: HasSelfRequirements>(_ x: T) { }
3030
func useCompoAsRequirement<T: HasSelfRequirements & Bar>(_ x: T) { }
3131
func useCompoAliasAsRequirement<T: Compo>(_ x: T) { }
32+
func useNestedCompoAliasAsRequirement<T: CompoAssocType.Compo>(_ x: T) { }
3233

3334
func useAsWhereRequirement<T>(_ x: T) where T: HasSelfRequirements {}
3435
func useCompoAsWhereRequirement<T>(_ x: T) where T: HasSelfRequirements & Bar {}
3536
func useCompoAliasAsWhereRequirement<T>(_ x: T) where T: Compo {}
37+
func useNestedCompoAliasAsWhereRequirement<T>(_ x: T) where T: CompoAssocType.Compo {}
3638

3739
func useAsType(_ x: HasSelfRequirements) { } // expected-error{{protocol 'HasSelfRequirements' can only be used as a generic constraint}}
3840
func useCompoAsType(_ x: HasSelfRequirements & Bar) { } // expected-error{{protocol 'HasSelfRequirements' can only be used as a generic constraint}}
3941
func useCompoAliasAsType(_ x: Compo) { } // expected-error{{protocol 'HasSelfRequirements' can only be used as a generic constraint}}
42+
func useNestedCompoAliasAsType(_ x: CompoAssocType.Compo) { } // expected-error{{protocol 'HasSelfRequirements' can only be used as a generic constraint}}
4043

4144
struct TypeRequirement<T: HasSelfRequirements> {}
4245
struct CompoTypeRequirement<T: HasSelfRequirements & Bar> {}
4346
struct CompoAliasTypeRequirement<T: Compo> {}
47+
struct NestedCompoAliasTypeRequirement<T: CompoAssocType.Compo> {}
4448

4549
struct CompoTypeWhereRequirement<T> where T: HasSelfRequirements & Bar {}
4650
struct CompoAliasTypeWhereRequirement<T> where T: Compo {}
51+
struct NestedCompoAliasTypeWhereRequirement<T> where T: CompoAssocType.Compo {}
4752

4853
struct Struct1<T> { }
4954
struct Struct2<T : Pub & Bar> { }

0 commit comments

Comments
 (0)