mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[SR-511][Parse] Add 'associatedtype' keyword and fixit
Adds an associatedtype keyword to the parser tokens, and accepts either typealias or associatedtype to create an AssociatedTypeDecl, warning that the former is deprecated. The ASTPrinter now emits associatedtype for AssociatedTypeDecls. Separated AssociatedType from TypeAlias as two different kinds of CodeCompletionDeclKinds. This part probably doesn’t turn out to be absolutely necessary currently, but it is nice cleanup from formerly specifically glomming the two together. And then many, many changes to tests. The actual new tests for the fixits is at the end of Generics/associated_types.swift.
This commit is contained in:
@@ -1704,6 +1704,7 @@ static bool isKeywordPossibleDeclStart(const Token &Tok) {
|
||||
case tok::kw_struct:
|
||||
case tok::kw_subscript:
|
||||
case tok::kw_typealias:
|
||||
case tok::kw_associatedtype:
|
||||
case tok::kw_var:
|
||||
case tok::pound_if:
|
||||
case tok::pound_line:
|
||||
@@ -2025,6 +2026,7 @@ ParserStatus Parser::parseDecl(SmallVectorImpl<Decl*> &Entries,
|
||||
StaticLoc = SourceLoc(); // we handled static if present.
|
||||
break;
|
||||
case tok::kw_typealias:
|
||||
case tok::kw_associatedtype:
|
||||
DeclResult = parseDeclTypeAlias(!(Flags & PD_DisallowTypeAliasDef),
|
||||
Flags.contains(PD_InProtocol),
|
||||
Attributes);
|
||||
@@ -2671,7 +2673,22 @@ ParserResult<IfConfigDecl> Parser::parseDeclIfConfig(ParseDeclOptions Flags) {
|
||||
ParserResult<TypeDecl> Parser::parseDeclTypeAlias(bool WantDefinition,
|
||||
bool isAssociatedType,
|
||||
DeclAttributes &Attributes) {
|
||||
SourceLoc TypeAliasLoc = consumeToken(tok::kw_typealias);
|
||||
SourceLoc TypeAliasLoc;
|
||||
|
||||
if (isAssociatedType) {
|
||||
if (consumeIf(tok::kw_typealias, TypeAliasLoc)) {
|
||||
diagnose(TypeAliasLoc, diag::typealias_inside_protocol)
|
||||
.fixItReplace(TypeAliasLoc, "associatedtype");
|
||||
} else {
|
||||
TypeAliasLoc = consumeToken(tok::kw_associatedtype);
|
||||
}
|
||||
} else {
|
||||
if (consumeIf(tok::kw_associatedtype, TypeAliasLoc)) {
|
||||
diagnose(TypeAliasLoc, diag::associatedtype_outside_protocol);
|
||||
return makeParserErrorResult<TypeDecl>();
|
||||
}
|
||||
TypeAliasLoc = consumeToken(tok::kw_typealias);
|
||||
}
|
||||
|
||||
Identifier Id;
|
||||
SourceLoc IdLoc;
|
||||
@@ -2679,7 +2696,7 @@ ParserResult<TypeDecl> Parser::parseDeclTypeAlias(bool WantDefinition,
|
||||
|
||||
Status |=
|
||||
parseIdentifierDeclName(*this, Id, IdLoc, tok::colon, tok::equal,
|
||||
diag::expected_identifier_in_decl, "typealias");
|
||||
diag::expected_identifier_in_decl, isAssociatedType ? "associatedtype" : "typealias");
|
||||
if (Status.isError())
|
||||
return nullptr;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user