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

[SR-9580] Cannot create array literal of one floating-point literal and reference a member of that array #52027

Closed
swift-ci opened this issue Dec 27, 2018 · 4 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself parser Area → compiler: The legacy C++ parser

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-9580
Radar None
Original Reporter BenLeggiero (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment
  • Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)
    Target: x86_64-apple-darwin17.7.0

  • Xcode Version 10.1 (10B61)

  • macOS Version 10.13.6 (17G4015)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Parser
Assignee @rintaro
Priority Medium

md5: 531753d303555d7338e2b36a3bb85c86

Issue Description:

I cannot call any member on an array literal containing only one floating point literal.

Here's my SSCCE:

print([1.2].count)

Expected result:

A new array containing only a Double with a value of 1.2 is created, and its count of 1 is printed

Actual result:

Compiler error is emitted:

main.swift:1:10: error: expected named member of numeric literal
print([1.2].count)
       ~~^

Notes:

I have confirmed that this issue does not occur with any of these following permutations, which lead me to the conclusion that this is a compiler bug and not an intended syntax disambiguation feature:

  • Array's sole element is an integer, string, nil, array, or dictionary literal

  • Array's sole element is an identifier instead of a literal

  • Array has more than one element

  • Array has less than one element

  • No member of the array is referenced

  • Float literal is the key andor value of a single-element dictionary literal

@belkadan
Copy link
Contributor

belkadan commented Jan 7, 2019

@rintaro, any idea why we'd parse this as an integer literal instead of a floating-point literal?

@rintaro
Copy link
Mannequin

rintaro mannequin commented Jan 8, 2019

This particular issue is fixed in #21487 .
The reason why we parsed this as an integer_literal was a combination of backtracking and Lexer problem.

swift/lib/Parse/Lexer.cpp

Lines 1136 to 1138 in 75033ed

// NextToken is the soon to be previous token
// Therefore: x.0.1 is sub-tuple access, not x.float_literal
if (!isDigit(CurPtr[1]) || NextToken.is(tok::period))

    // NextToken is the soon to be previous token
    // Therefore: x.0.1 is sub-tuple access, not x.float_literal
    if (!isDigit(CurPtr[1]) || NextToken.is(tok::period))
      return formToken(tok::integer_literal, TokStart);

In parsing [1.2].count , after the backtracking, the Lexer is at 1, but unfortunately, NextToken is actually tok::period (which is the dot after ]) because backtracking doesn't restore NextToken state.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 5, 2019

Comment by Ky (JIRA)

As far as I can tell, this is fixed as of:

Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.6.0

@swift-ci
Copy link
Collaborator Author

Comment by Ky (JIRA)

Seems fixed

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself parser Area → compiler: The legacy C++ parser
Projects
None yet
Development

No branches or pull requests

2 participants