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-186] Comments not followed by whitespace can confuse compiler #42808

Closed
swift-ci opened this issue Dec 10, 2015 · 8 comments
Closed

[SR-186] Comments not followed by whitespace can confuse compiler #42808

swift-ci opened this issue Dec 10, 2015 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself good first issue Good for newcomers

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-186
Radar None
Original Reporter somegeekintn (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Swift 2.1.1, Xcode 7.2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, StarterBug
Assignee jder (JIRA)
Priority Medium

md5: 7122c4cd48dd86437f4b98b0c6e18b52

relates to:

  • SR-960 Implement SE-0037: Clarify interaction between comments & operators

Issue Description:

Attempting to compile the following:

let even = false

if /* some comment */!even {
    print("odd")
}

Fails with the following errors:

error: expected expression, var, or let in 'if' condition
                if /* some comment */!even {
                                     ^
error: braced block of statements is an unused closure
                if /* some comment */!even {
                                           ^
error: expression resolves to an unused function
                if /* some comment */!even {

Adding whitespace between the */ and ! compiles fine:

if /* some comment */ !even {

As does removing the NOT without added whitespace:

if /* some comment */even {

Expectation: I would expect the compiler to treat everything between and including the /* */ delimiters to be treated as invisible such that:

if /* some comment */!even {

would appear to the compiler as

if !even {
@gribozavr
Copy link
Collaborator

Sounds like a simple Lexer/Parser bug.

@swift-ci
Copy link
Collaborator Author

Comment by Jesse Rusak (JIRA)

The issue appears to be that the ! is lexed as a postfix operator because the character to the left of it (/) is not whitespace, parens, etc. I have a patch that I believe fixes this; will put up a PR shortly.

@swift-ci
Copy link
Collaborator Author

Comment by Jesse Rusak (JIRA)

After looking over the language reference, it seems like comments should be treated as whitespace, rather than as "invisible". At the moment, the code which decides between postfix/prefix/binary operators treats comments as non-whitespace.

Changing this code to treat comments as whitespace could break existing code. For example, the code foo/* */! is currently parsed as a postfix operator. Fixing this bug would break that code.

Worth discussing on the mailing list before making a PR?

@gribozavr
Copy link
Collaborator

I think it is worth a quick email.

@swift-ci
Copy link
Collaborator Author

Comment by Jesse Rusak (JIRA)

I've started a thread on swift-evolution to discuss the questions which were raised by this.

@swift-ci
Copy link
Collaborator Author

Comment by Jesse Rusak (JIRA)

I've started a draft patch for this, pending further discussion on swift-evolution.

master...jder:comment-operator-fixes

@swift-ci
Copy link
Collaborator Author

Comment by Jesse Rusak (JIRA)

The review for the related swift-evolution proposal concludes today: http://thread.gmane.org/gmane.comp.lang.swift.evolution/9218

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 3, 2016

Comment by Jesse Rusak (JIRA)

This has been fixed with this PR: #1732

@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 good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants