Skip to content

Commit bda3983

Browse files
committed
Turn off typealiases in protocols.
Since there still are some holes in this feature, and I haven't had time to fill them lately: Go back to the 2.2 behavior of treating 'typealias' keyword in protocols as an associated type, and emit a deprecation warning. Commented out tests specifically for typealiases in protocols for now, and random validation tests that crash or not based on whether keyword is interpreted as associatedtype or typealias updated.
1 parent ea8ab58 commit bda3983

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ ERROR(expected_close_after_else_directive,none,
771771
"further conditions after #else are unreachable", ())
772772

773773
/// Associatedtype Statement
774+
WARNING(typealias_in_protocol_deprecated,none,
775+
"use of 'typealias' to declare associated types is deprecated; use 'associatedtype' instead", ())
774776
ERROR(typealias_inside_protocol_without_type,none,
775777
"typealias is missing an assigned type; use 'associatedtype' to define an associated type requirement", ())
776778
ERROR(associatedtype_outside_protocol,none,

lib/Parse/ParseDecl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,11 @@ ParserStatus Parser::parseDecl(ParseDeclOptions Flags,
21752175
break;
21762176
}
21772177
case tok::kw_typealias:
2178-
DeclResult = parseDeclTypeAlias(Flags, Attributes);
2178+
if (Flags.contains(PD_InProtocol)) {
2179+
DeclResult = parseDeclAssociatedType(Flags, Attributes);
2180+
} else {
2181+
DeclResult = parseDeclTypeAlias(Flags, Attributes);
2182+
}
21792183
Status = DeclResult;
21802184
break;
21812185
case tok::kw_associatedtype:
@@ -3021,7 +3025,7 @@ ParserResult<TypeDecl> Parser::parseDeclAssociatedType(Parser::ParseDeclOptions
30213025
// ask us to fix up leftover Swift 2 code intending to be an associatedtype.
30223026
if (Tok.is(tok::kw_typealias)) {
30233027
AssociatedTypeLoc = consumeToken(tok::kw_typealias);
3024-
diagnose(AssociatedTypeLoc, diag::typealias_inside_protocol_without_type)
3028+
diagnose(AssociatedTypeLoc, diag::typealias_in_protocol_deprecated)
30253029
.fixItReplace(AssociatedTypeLoc, "associatedtype");
30263030
} else {
30273031
AssociatedTypeLoc = consumeToken(tok::kw_associatedtype);

test/Generics/associated_types.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct C<a : B> : B { // expected-error {{type 'C<a>' does not conform to protoc
173173

174174
// SR-511
175175
protocol sr511 {
176-
typealias Foo // expected-error {{typealias is missing an assigned type; use 'associatedtype' to define an associated type requirement}}
176+
typealias Foo // expected-warning {{use of 'typealias' to declare associated types is deprecated; use 'associatedtype' instead}}
177177
}
178178

179179
associatedtype Foo = Int // expected-error {{associated types can only be defined in a protocol; define a type or introduce a 'typealias' to satisfy an associated type requirement}}

test/decl/typealias/typealias.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ struct MyType<TyA, TyB> {
2929

3030
protocol P {
3131
associatedtype X<T> // expected-error {{associated types may not have a generic parameter list}}
32-
33-
typealias Y<T> // expected-error {{expected '=' in typealias declaration}}
32+
// FIXME Disabled until typealiases in protocols are entirely fixed
33+
// typealias Y<T> // fixme-error {{expected '=' in typealias declaration}}
3434
}
3535

3636
typealias basicTypealias = Int
@@ -148,6 +148,8 @@ extension C<Int> {} // expected-error {{constrained extension must be declared
148148

149149

150150
// Allow typealias inside protocol, but don't allow it in where clauses (at least not yet)
151+
// FIXME Disabled until typealiases in protocols are entirely fixed
152+
/*
151153
protocol Col {
152154
associatedtype Elem
153155
var elem: Elem { get }
@@ -227,23 +229,23 @@ protocol P2 {
227229
associatedtype B
228230
}
229231

230-
func go3<T : P1, U : P2 where T.F == U.B>(_ x: T) -> U { // expected-error {{typealias 'F' is too complex to be used as a generic constraint; use an associatedtype instead}} expected-error {{'F' is not a member type of 'T'}}
232+
func go3<T : P1, U : P2 where T.F == U.B>(_ x: T) -> U { // fixme-error {{typealias 'F' is too complex to be used as a generic constraint; use an associatedtype instead}} fixme-error {{'F' is not a member type of 'T'}}
231233
}
232234

233235
// Specific diagnosis for things that look like Swift 2.x typealiases
234236
protocol P3 {
235-
typealias T // expected-error {{typealias is missing an assigned type; use 'associatedtype' to define an associated type requirement}}
236-
typealias U : P2 // expected-error {{typealias is missing an assigned type; use 'associatedtype' to define an associated type requirement}}
237+
typealias T // fixme-error {{typealias is missing an assigned type; use 'associatedtype' to define an associated type requirement}}
238+
typealias U : P2 // fixme-error {{typealias is missing an assigned type; use 'associatedtype' to define an associated type requirement}}
237239

238-
associatedtype V : P2 = // expected-error {{expected type in associatedtype declaration}}
240+
associatedtype V : P2 = // fixme-error {{expected type in associatedtype declaration}}
239241
}
240242

241243
// Test for not crashing on self and recursive aliases
242244
protocol P4 {
243245
typealias X = Self
244-
typealias Y = Self.Y // expected-error {{type alias 'Y' circularly references itself}}
246+
typealias Y = Self.Y // fixme-error {{type alias 'Y' circularly references itself}}
245247

246248
func getSelf() -> X
247249
}
248-
250+
*/
249251

validation-test/compiler_crashers_fixed/28268-swift-type-transform.swift renamed to validation-test/compiler_crashers/28268-swift-type-transform.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See http://swift.org/LICENSE.txt for license information
66
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not %target-swift-frontend %s -parse
8+
// RUN: not --crash %target-swift-frontend %s -parse
99
var:{protocol a{struct A:a
1010
typealias e=A.e

validation-test/compiler_crashers/28288-swift-genericparamlist-getsubstitutionmap.swift renamed to validation-test/compiler_crashers_fixed/28288-swift-genericparamlist-getsubstitutionmap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// See http://swift.org/LICENSE.txt for license information
66
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -parse
8+
// RUN: not %target-swift-frontend %s -parse
99
// REQUIRES: asserts
1010
protocol A{
1111
typealias B<a>:A

0 commit comments

Comments
 (0)