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

swift-syntax doesn't give negative integer literals special treatment while the Swift compiler does. #2787

Closed
YOCKOW opened this issue Aug 8, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@YOCKOW
Copy link
Member

YOCKOW commented Aug 8, 2024

Description

As discussed in some threads 12, negative numeric literals have special treatment in Swift.
However, swift-syntax seems not to reflect such feature.
For further details, see "Steps to Reproduce" section.

Steps to Reproduce

Swift compiler says -1 is an integer literal with negative flag:

% echo '_ = -1' | swiftc -dump-ast -    
(source_file "<stdin>"
  (top_level_code_decl range=[<stdin>:1:1 - line:1:6]
    (brace_stmt implicit range=[<stdin>:1:1 - line:1:6]
      (assign_expr type='()' location=<stdin>:1:3 range=[<stdin>:1:1 - line:1:6]
        (discard_assignment_expr type='@lvalue Int' location=<stdin>:1:1 range=[<stdin>:1:1 - line:1:1])
        (integer_literal_expr type='Int' location=<stdin>:1:5 range=[<stdin>:1:5 - line:1:6] negative value=1 builtin_initializer=Swift.(file).Int.init(_builtinIntegerLiteral:) initializer=**NULL**)))))

Meanwhile, swift-parser-cli says '-1' is a prefix operator '-' and its operand expression(an integer literal 1):

% echo '_ = -1' | swift-parser-cli print-tree
SourceFileSyntax
├─statements: CodeBlockItemListSyntax
│ ╰─[0]: CodeBlockItemSyntax
│   ╰─item: SequenceExprSyntax
│     ╰─elements: ExprListSyntax
│       ├─[0]: DiscardAssignmentExprSyntax
│       │ ╰─wildcard: wildcard
│       ├─[1]: AssignmentExprSyntax
│       │ ╰─equal: equal
│       ╰─[2]: PrefixOperatorExprSyntax
│         ├─operator: prefixOperator("-")
│         ╰─expression: IntegerLiteralExprSyntax
│           ╰─literal: integerLiteral("1")
╰─endOfFileToken: endOfFile

Footnotes

  1. https://forums.swift.org/t/amendment-se-0368-staticbigint/62992/12

  2. https://forums.swift.org/t/expressiblebyintegerliteral-and-type-inference-and-macro/73698/2

@YOCKOW YOCKOW added the bug Something isn't working label Aug 8, 2024
@ahoppen
Copy link
Contributor

ahoppen commented Aug 8, 2024

Synced to Apple’s issue tracker as rdar://133430491

@ahoppen
Copy link
Contributor

ahoppen commented Aug 8, 2024

This behavior is intentional. In the compiler’s pipeline, ASTGen will handle the unification of the prefix - with the number literal into a single AST node. Clients of swift-syntax will need to apply the prefix - manually.

@ahoppen ahoppen closed this as completed Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants