Skip to content

Disallow spaces between @, attribute name and ( in Swift 6 #71237

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

Merged
merged 2 commits into from
Feb 1, 2024

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jan 30, 2024

  • Disallow space between @ and attribute name in Swift 6 mode
    • Not having a space between @ and the attribute name (or even having them on different lines) is weird and shouldn’t be allowed
  • Disallow space between attribute name and '(' in Swift 6 mode
    • This allows us to resolve disambiguaties of whether a parenthesis belong to an argument to the attribute or if it is eg. the start of a tuple.

@ahoppen ahoppen changed the title Disallow spaces between @, attribute name and (. Disallow spaces between @, attribute name and ( in Swift 6 Jan 30, 2024
@ahoppen ahoppen requested a review from hborla January 30, 2024 07:04
@hborla
Copy link
Member

hborla commented Jan 30, 2024

Oh, does this fix the issue with writing @MainActor with an explicit closure parameter list?

let closure = { @MainActor (a, b) in
  print(a, b)
}

@ahoppen
Copy link
Member Author

ahoppen commented Jan 30, 2024

It doesn’t actually fix the issue because there’s no behavior change (for source compatibility reasons). But it will produce a warning in Swift 5 and an error in Swift 6.

let closure = { @MainActor (a, b) in // error: extraneous whitespace between attribute name and '('
}

My thought was that we should stage this as a diagnostic and then we can change the parsing behavior to unambiguously parse (a, b) as the closure’s parameters in a year or so.

@ahoppen ahoppen force-pushed the ahoppen/attribute-parser-changes branch from 8353e68 to eb350cc Compare January 30, 2024 16:50
Comment on lines 3 to 6
@ MainActor // expected-error {{extraneous whitespace between '@' and attribute name}}
class Foo {
func foo(_ x: @ escaping () -> Int) {} // expected-error {{extraneous whitespace between '@' and attribute name}}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth adding tests to cover the warning behavior and the newline case?

Copy link
Member Author

@ahoppen ahoppen Jan 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think adding a dedicated test case for the warning is worth it. See #71238 (comment)

What do you mean by the newline case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by the newline case?

The case where the @ is on the previous line

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case for

let closure = { @MainActor
  (a, b) in
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I should have been clearer, I meant e.g:

@
MainActor

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahoppen ahoppen force-pushed the ahoppen/attribute-parser-changes branch 2 times, most recently from 8533073 to 7731b03 Compare January 31, 2024 23:59
@ahoppen
Copy link
Member Author

ahoppen commented Jan 31, 2024

@swift-ci Please smoke test

@ahoppen ahoppen enabled auto-merge January 31, 2024 23:59
This allows us to resolve disambiguities of whether a parenthesis belong to an argument to the attribute or if it is eg. the start of a tuple.
@ahoppen ahoppen force-pushed the ahoppen/attribute-parser-changes branch from 7731b03 to c6e425a Compare February 1, 2024 02:24
@ahoppen
Copy link
Member Author

ahoppen commented Feb 1, 2024

@swift-ci Please smoke test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants