Skip to content

[clang/AST] Make it possible to use SwiftAttr in type context #7918

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

Conversation

xedin
Copy link

@xedin xedin commented Dec 21, 2023

SwiftAttr is switched from InheritableAttr which is a declaration attribute to DeclOrTypeAttr.

To support this attribute in type context we need access to its "Attribute" argument which requires
AttributedType to be extended to include Attr * when available instead of just attr::Kind.

@xedin xedin requested a review from rjmccall December 21, 2023 23:05
@xedin
Copy link
Author

xedin commented Dec 21, 2023

Cannot merge this without changes on the Swift side because it introduces new {read, write}Attr functionality to serialization.

@xedin
Copy link
Author

xedin commented Dec 22, 2023

@swift-ci please test

Expands allowed use of SwiftAttr to type context from declarations
only, this enables use of the attribute on generic parameters,
in Objective-C blocks, typedefs etc.
@xedin xedin force-pushed the attributedtype-with-optional-attr branch from 617dbef to 32e5bdd Compare December 23, 2023 04:08
…ion attribute

Adjust `SwiftAttr` handling to determine when attribute should be
handled as a type or declaration attribute i.e. if attribute is
attached to a declarator or declaration name, it should be handled
as a declaration attribute.
// it should be handled as a declaration attribute,
// unless it's associated with a type or a function
// prototype (i.e. appears on a parameter or result type).
if (State.isProcessingDeclSpec()) {
Copy link

Choose a reason for hiding this comment

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

This is a change in the current interpretation, right? Isn't swift_attr currently allowed in the decl-specifiers and always applied to the declaration, even on function declarations?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I expected that switching from InheritedAttr to DeclOrTypeAttr would preserve the behavior and attributes that appear on the outer decl-spec of a parameter would be handled by SemaDeclAttr but that is not the case. I couldn't figure out a robust way to determine the position because there appears to be no way to walk up declarators so I opted for a simpler approach where anything that appears in "prototype" context is handled as a type attribute and adjusted that on the Swift side too.

Copy link

Choose a reason for hiding this comment

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

Okay, Pavel explained to me offline that the Swift importer already has to walk into the type-as-written to look for AttributedTypes that might have been written at the top level of a declarator (where they syntactically apply to the outermost declarator; this is common with e.g. function declarators, where people often write attributes after the prototype). In that case, making the rule always prefer to apply attributes to the type, and then making the importer also look for "declaration" attributes in nested positions seems reasonable. (As ever, the real problem here is that the C grammar is poorly designed for interpreting this sort of thing with universal rules.)

@xedin
Copy link
Author

xedin commented Jan 3, 2024

swiftlang/swift#70693
@swift-ci please test

@xedin
Copy link
Author

xedin commented Jan 5, 2024

swiftlang/swift#70693
@swift-ci please test Windows platform

@xedin
Copy link
Author

xedin commented Jan 11, 2024

swiftlang/swift#70693
@swift-ci please smoke test

@xedin
Copy link
Author

xedin commented Jan 11, 2024

swiftlang/swift#70693
@swift-ci please test Windows platform

3 similar comments
@xedin
Copy link
Author

xedin commented Jan 12, 2024

swiftlang/swift#70693
@swift-ci please test Windows platform

@xedin
Copy link
Author

xedin commented Jan 12, 2024

swiftlang/swift#70693
@swift-ci please test Windows platform

@xedin
Copy link
Author

xedin commented Jan 15, 2024

swiftlang/swift#70693
@swift-ci please test Windows platform

@xedin xedin merged commit 71521cf into swiftlang:stable/20230725 Jan 16, 2024
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