Skip to content

Commit 7a1792a

Browse files
committed
[Parser] Support 'any' type in SwiftSyntax
Previously, SwiftSyntax wasn’t able to parse 'any' types. Add support for them now. rdar://90077430
1 parent f807ef9 commit 7a1792a

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

lib/Parse/ParseType.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,8 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) {
800800
/// type-composition '&' type-simple
801801
ParserResult<TypeRepr>
802802
Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
803-
SyntaxParsingContext SomeTypeContext(SyntaxContext, SyntaxKind::SomeType);
803+
SyntaxParsingContext ConstrainedSugarTypeContext(
804+
SyntaxContext, SyntaxKind::ConstrainedSugarType);
804805
// Check for the opaque modifier.
805806
// This is only semantically allowed in certain contexts, but we parse it
806807
// generally for diagnostics and recovery.
@@ -816,7 +817,7 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
816817
anyLoc = consumeToken();
817818
} else {
818819
// This isn't a some type.
819-
SomeTypeContext.setTransparent();
820+
ConstrainedSugarTypeContext.setTransparent();
820821
}
821822

822823
auto applyOpaque = [&](TypeRepr *type) -> TypeRepr * {

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,9 @@ func foo<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSigna
580580
#assert(<BooleanLiteralExpr>false</BooleanLiteralExpr>)</PoundAssertStmt><PoundAssertStmt>
581581
#assert(<BooleanLiteralExpr>true</BooleanLiteralExpr>, "hello world")</PoundAssertStmt><FunctionDecl><DeclModifier>
582582

583-
public </DeclModifier>func anyFoo<FunctionSignature><ParameterClause>() </ParameterClause><ReturnClause>-> <SomeType>some <SimpleTypeIdentifier>Foo </SimpleTypeIdentifier></SomeType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl><DeclModifier>
584-
public </DeclModifier>func qoo<FunctionSignature><ParameterClause>() </ParameterClause><ReturnClause>-> <SomeType>some <CompositionType><CompositionTypeElement><SimpleTypeIdentifier>O </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>O2 </SimpleTypeIdentifier></CompositionTypeElement></CompositionType></SomeType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl>
585-
func zlop<FunctionSignature><ParameterClause>() </ParameterClause><ReturnClause>-> <SomeType>some <CompositionType><CompositionTypeElement><SimpleTypeIdentifier>C </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>AnyObject </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>P </SimpleTypeIdentifier></CompositionTypeElement></CompositionType></SomeType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl><CustomAttribute>
583+
public </DeclModifier>func anyFoo<FunctionSignature><ParameterClause>() </ParameterClause><ReturnClause>-> <ConstrainedSugarType>some <SimpleTypeIdentifier>Foo </SimpleTypeIdentifier></ConstrainedSugarType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl><DeclModifier>
584+
public </DeclModifier>func qoo<FunctionSignature><ParameterClause>() </ParameterClause><ReturnClause>-> <ConstrainedSugarType>some <CompositionType><CompositionTypeElement><SimpleTypeIdentifier>O </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>O2 </SimpleTypeIdentifier></CompositionTypeElement></CompositionType></ConstrainedSugarType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl>
585+
func zlop<FunctionSignature><ParameterClause>() </ParameterClause><ReturnClause>-> <ConstrainedSugarType>some <CompositionType><CompositionTypeElement><SimpleTypeIdentifier>C </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>AnyObject </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>P </SimpleTypeIdentifier></CompositionTypeElement></CompositionType></ConstrainedSugarType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl><CustomAttribute>
586586

587587
@<SimpleTypeIdentifier>custom</SimpleTypeIdentifier>(<TupleExprElement><IdentifierExpr>a</IdentifierExpr>, </TupleExprElement><TupleExprElement><IdentifierExpr>b</IdentifierExpr>,</TupleExprElement><TupleExprElement><IdentifierExpr>c</IdentifierExpr></TupleExprElement>)</CustomAttribute>
588588
func foo<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl><CustomAttribute>

test/Syntax/round_trip_tuple.swift renamed to test/Syntax/round_trip_types.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ typealias TwoInts = (Int, Int)
44
typealias TwoNamedInts = (a: Int, b: Int)
55
typealias VoidTuple = ()
66
typealias TupleWithTrivia = ( Int , b :Int )
7+
8+
func testAnyType() {
9+
protocol MyProto {}
10+
11+
let foo: [Int: any MyProto]
12+
}

utils/gyb_syntax_support/NodeSerializationCodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
'NamedAttributeStringArgument': 227,
233233
'DeclName': 228,
234234
'PoundAssertStmt': 229,
235-
'SomeType': 230,
235+
'ConstrainedSugarType': 230,
236236
'CustomAttribute': 231,
237237
'GenericRequirement': 232,
238238
'DifferentiableAttributeArguments': 233,

utils/gyb_syntax_support/TypeNodes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@
7777
Child('QuestionMark', kind='PostfixQuestionMarkToken'),
7878
]),
7979

80-
# some type -> some 'type'
81-
Node('SomeType', kind='Type',
80+
# constrained-sugar-type -> ('some'|'any') type
81+
Node('ConstrainedSugarType', kind='Type',
8282
children=[
83-
Child('SomeSpecifier', kind='IdentifierToken',
83+
Child('SomeOrAnySpecifier', kind='IdentifierToken',
8484
classification='Keyword',
85-
text_choices=['some']),
85+
text_choices=['some', 'any']),
8686
Child('BaseType', kind='Type'),
8787
]),
8888

0 commit comments

Comments
 (0)