mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Parser] Support 'any' type in SwiftSyntax
Previously, SwiftSyntax wasn’t able to parse 'any' types. Add support for them now. rdar://90077430
This commit is contained in:
@@ -800,7 +800,8 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) {
|
|||||||
/// type-composition '&' type-simple
|
/// type-composition '&' type-simple
|
||||||
ParserResult<TypeRepr>
|
ParserResult<TypeRepr>
|
||||||
Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
|
Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
|
||||||
SyntaxParsingContext SomeTypeContext(SyntaxContext, SyntaxKind::SomeType);
|
SyntaxParsingContext ConstrainedSugarTypeContext(
|
||||||
|
SyntaxContext, SyntaxKind::ConstrainedSugarType);
|
||||||
// Check for the opaque modifier.
|
// Check for the opaque modifier.
|
||||||
// This is only semantically allowed in certain contexts, but we parse it
|
// This is only semantically allowed in certain contexts, but we parse it
|
||||||
// generally for diagnostics and recovery.
|
// generally for diagnostics and recovery.
|
||||||
@@ -816,7 +817,7 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
|
|||||||
anyLoc = consumeToken();
|
anyLoc = consumeToken();
|
||||||
} else {
|
} else {
|
||||||
// This isn't a some type.
|
// This isn't a some type.
|
||||||
SomeTypeContext.setTransparent();
|
ConstrainedSugarTypeContext.setTransparent();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto applyOpaque = [&](TypeRepr *type) -> TypeRepr * {
|
auto applyOpaque = [&](TypeRepr *type) -> TypeRepr * {
|
||||||
|
|||||||
@@ -580,9 +580,9 @@ func foo<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSigna
|
|||||||
#assert(<BooleanLiteralExpr>false</BooleanLiteralExpr>)</PoundAssertStmt><PoundAssertStmt>
|
#assert(<BooleanLiteralExpr>false</BooleanLiteralExpr>)</PoundAssertStmt><PoundAssertStmt>
|
||||||
#assert(<BooleanLiteralExpr>true</BooleanLiteralExpr>, "hello world")</PoundAssertStmt><FunctionDecl><DeclModifier>
|
#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 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>-> <SomeType>some <CompositionType><CompositionTypeElement><SimpleTypeIdentifier>O </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>O2 </SimpleTypeIdentifier></CompositionTypeElement></CompositionType></SomeType></ReturnClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl>
|
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>-> <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>
|
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>
|
@<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>
|
func foo<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{}</CodeBlock></FunctionDecl><FunctionDecl><CustomAttribute>
|
||||||
|
|||||||
@@ -4,3 +4,9 @@ typealias TwoInts = (Int, Int)
|
|||||||
typealias TwoNamedInts = (a: Int, b: Int)
|
typealias TwoNamedInts = (a: Int, b: Int)
|
||||||
typealias VoidTuple = ()
|
typealias VoidTuple = ()
|
||||||
typealias TupleWithTrivia = ( Int , b :Int )
|
typealias TupleWithTrivia = ( Int , b :Int )
|
||||||
|
|
||||||
|
func testAnyType() {
|
||||||
|
protocol MyProto {}
|
||||||
|
|
||||||
|
let foo: [Int: any MyProto]
|
||||||
|
}
|
||||||
@@ -232,7 +232,7 @@ SYNTAX_NODE_SERIALIZATION_CODES = {
|
|||||||
'NamedAttributeStringArgument': 227,
|
'NamedAttributeStringArgument': 227,
|
||||||
'DeclName': 228,
|
'DeclName': 228,
|
||||||
'PoundAssertStmt': 229,
|
'PoundAssertStmt': 229,
|
||||||
'SomeType': 230,
|
'ConstrainedSugarType': 230,
|
||||||
'CustomAttribute': 231,
|
'CustomAttribute': 231,
|
||||||
'GenericRequirement': 232,
|
'GenericRequirement': 232,
|
||||||
'DifferentiableAttributeArguments': 233,
|
'DifferentiableAttributeArguments': 233,
|
||||||
|
|||||||
@@ -77,12 +77,12 @@ TYPE_NODES = [
|
|||||||
Child('QuestionMark', kind='PostfixQuestionMarkToken'),
|
Child('QuestionMark', kind='PostfixQuestionMarkToken'),
|
||||||
]),
|
]),
|
||||||
|
|
||||||
# some type -> some 'type'
|
# constrained-sugar-type -> ('some'|'any') type
|
||||||
Node('SomeType', kind='Type',
|
Node('ConstrainedSugarType', kind='Type',
|
||||||
children=[
|
children=[
|
||||||
Child('SomeSpecifier', kind='IdentifierToken',
|
Child('SomeOrAnySpecifier', kind='IdentifierToken',
|
||||||
classification='Keyword',
|
classification='Keyword',
|
||||||
text_choices=['some']),
|
text_choices=['some', 'any']),
|
||||||
Child('BaseType', kind='Type'),
|
Child('BaseType', kind='Type'),
|
||||||
]),
|
]),
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user