Skip to content

[Parser] Support 'any' type in SwiftSyntax #41804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,8 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) {
/// type-composition '&' type-simple
ParserResult<TypeRepr>
Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
SyntaxParsingContext SomeTypeContext(SyntaxContext, SyntaxKind::SomeType);
SyntaxParsingContext ConstrainedSugarTypeContext(
SyntaxContext, SyntaxKind::ConstrainedSugarType);
// Check for the opaque modifier.
// This is only semantically allowed in certain contexts, but we parse it
// generally for diagnostics and recovery.
Expand All @@ -816,7 +817,7 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
anyLoc = consumeToken();
} else {
// This isn't a some type.
SomeTypeContext.setTransparent();
ConstrainedSugarTypeContext.setTransparent();
}

auto applyOpaque = [&](TypeRepr *type) -> TypeRepr * {
Expand Down
6 changes: 3 additions & 3 deletions test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,9 @@ func foo<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSigna
#assert(<BooleanLiteralExpr>false</BooleanLiteralExpr>)</PoundAssertStmt><PoundAssertStmt>
#assert(<BooleanLiteralExpr>true</BooleanLiteralExpr>, "hello world")</PoundAssertStmt><FunctionDecl><DeclModifier>

public </DeclModifier>func anyFoo<FunctionSignature><ParameterClause>() </ParameterClause><ReturnClause>-> <SomeType>some <SimpleTypeIdentifier>Foo </SimpleTypeIdentifier></SomeType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl><DeclModifier>
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>
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>
public </DeclModifier>func anyFoo<FunctionSignature><ParameterClause>() </ParameterClause><ReturnClause>-> <ConstrainedSugarType>some <SimpleTypeIdentifier>Foo </SimpleTypeIdentifier></ConstrainedSugarType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl><DeclModifier>
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>
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>

@<SimpleTypeIdentifier>custom</SimpleTypeIdentifier>(<TupleExprElement><IdentifierExpr>a</IdentifierExpr>, </TupleExprElement><TupleExprElement><IdentifierExpr>b</IdentifierExpr>,</TupleExprElement><TupleExprElement><IdentifierExpr>c</IdentifierExpr></TupleExprElement>)</CustomAttribute>
func foo<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl><CustomAttribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ typealias TwoInts = (Int, Int)
typealias TwoNamedInts = (a: Int, b: Int)
typealias VoidTuple = ()
typealias TupleWithTrivia = ( Int , b :Int )

func testAnyType() {
protocol MyProto {}

let foo: [Int: any MyProto]
}
2 changes: 1 addition & 1 deletion utils/gyb_syntax_support/NodeSerializationCodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
'NamedAttributeStringArgument': 227,
'DeclName': 228,
'PoundAssertStmt': 229,
'SomeType': 230,
'ConstrainedSugarType': 230,
'CustomAttribute': 231,
'GenericRequirement': 232,
'DifferentiableAttributeArguments': 233,
Expand Down
8 changes: 4 additions & 4 deletions utils/gyb_syntax_support/TypeNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@
Child('QuestionMark', kind='PostfixQuestionMarkToken'),
]),

# some type -> some 'type'
Node('SomeType', kind='Type',
# constrained-sugar-type -> ('some'|'any') type
Node('ConstrainedSugarType', kind='Type',
children=[
Child('SomeSpecifier', kind='IdentifierToken',
Child('SomeOrAnySpecifier', kind='IdentifierToken',
classification='Keyword',
text_choices=['some']),
text_choices=['some', 'any']),
Child('BaseType', kind='Type'),
]),

Expand Down