Skip to content

[509] Fix a formatting issue if a member attribute macro is applied to properties that already have an attribute #2105

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

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Aug 24, 2023

If a variable already had a macro already had an attribute and we add a new attribute to it via a member macro, the produced code was invalid. For example,

@wrapStoredProperties
struct Foo {
  @available(*, deprecated) var x: Int
}

produced

struct Foo {
  @available(*, deprecated)
  @Wrappervar x: Int
}

which is invalid because there is no space between Wrapper and var.

To fix this, transfer the trailing trivia from the last attribute to the last newly inserted attribute.

This way, we essentially insert the new attributes right after the last attribute in source, but before its trailing trivia, keeping the trivia that separates the attribute block from the variable itself.

struct Foo {
  @available(*, deprecated)
  @Wrapper var x: Int
}

rdar://114275860

…erties that already have an attribute

If a variable already had a macro already had an attribute and we add a new attribute to it via a member macro, the produced code was invalid. For example,

```swift
@wrapStoredProperties
struct Foo {
  @available(*, deprecated) var x: Int
}
```

produced

```swift
struct Foo {
  @available(*, deprecated)
  @Wrappervar x: Int
}
```

which is invalid because there is no space between `Wrapper` and `var`.

To fix this, transfer the trailing trivia from the last attribute to the last newly inserted attribute.

This way, we essentially insert the new attributes right after the last attribute in source, but before its trailing trivia, keeping the trivia that separates the attribute block from the variable itself.

```swift
struct Foo {
  @available(*, deprecated)
  @wrapper var x: Int
}
```

rdar://114275860
@ahoppen ahoppen requested a review from bnbarham August 24, 2023 22:32
@ahoppen
Copy link
Member Author

ahoppen commented Aug 24, 2023

Local testing passed ✅

@ahoppen ahoppen merged commit 057bdfb into swiftlang:package-release/509 Aug 25, 2023
@ahoppen ahoppen deleted the ahoppen/509/member-attribute-formatting branch August 25, 2023 05:08
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.

2 participants