Skip to content
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

Certain IdentifierPatterns in the old parser now parsed as IdentifierExpr #831

Closed
allevato opened this issue Sep 22, 2022 · 1 comment · Fixed by #837
Closed

Certain IdentifierPatterns in the old parser now parsed as IdentifierExpr #831

allevato opened this issue Sep 22, 2022 · 1 comment · Fixed by #837
Labels
bug Something isn't working SwiftParser Bugs in the (new) Parser written in Swift

Comments

@allevato
Copy link
Collaborator

Issue Kind

Parse of Valid Source Produced Invalid Syntax Tree

Source Code

switch x { case let .y(z): break }

Description

This code snippet produces the following parse tree:

SourceFileSyntax children=2
  0: CodeBlockItemListSyntax children=1
    0: CodeBlockItemSyntax children=1
      0: SwitchStmtSyntax children=5
        0: switchKeyword
        1: IdentifierExprSyntax children=1
          0: identifier("x")
        2: leftBrace
        3: SwitchCaseListSyntax children=1
          0: SwitchCaseSyntax children=2
            0: SwitchCaseLabelSyntax children=3
              0: caseKeyword
              1: CaseItemListSyntax children=1
                0: CaseItemSyntax children=1
                  0: ValueBindingPatternSyntax children=2
                    0: letKeyword
                    1: ExpressionPatternSyntax children=1
                      0: FunctionCallExprSyntax children=4
                        0: MemberAccessExprSyntax children=2
                          0: prefixPeriod
                          1: identifier("y")
                        1: leftParen
                        2: TupleExprElementListSyntax children=1
                          0: TupleExprElementSyntax children=1
                            0: IdentifierExprSyntax children=1
                              0: identifier("z")
                        3: rightParen
              2: colon
            1: CodeBlockItemListSyntax children=1
              0: CodeBlockItemSyntax children=1
                0: BreakStmtSyntax children=1
                  0: breakKeyword
        4: rightBrace
  1: eof

For z, the C++ parser instead produced an UnresolvedPatternExprSyntax with an IdentifierPatternSyntax as its child.

The C++ tree distinguishes identifiers that are bound in the pattern from those which aren't or can't be. For example,

  • if the pattern was let .y(z, someFunction(5)), z would be an IdentifierPattern while someFunction would be an IdentifierExprs.
  • if the pattern was let .y(z, someFunction(w)), z and w would be IdentifierPatterns while someFunction would be an IdentifierExpr (and the compiler would later emit an error that pattern variable binding cannot appear in an expression).
@allevato allevato added bug Something isn't working SwiftParser Bugs in the (new) Parser written in Swift labels Sep 22, 2022
@ahoppen
Copy link
Collaborator

ahoppen commented Sep 23, 2022

rdar://100301666

ahoppen added a commit to ahoppen/swift-syntax that referenced this issue Sep 23, 2022
Fixes apple#831
Resolves rdar://100301666
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working SwiftParser Bugs in the (new) Parser written in Swift
Projects
None yet
2 participants