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

[macros] prepare generic arguments for replacement in macros #2450

Merged
merged 12 commits into from
Feb 6, 2024

Conversation

ktoso
Copy link
Contributor

@ktoso ktoso commented Jan 31, 2024

Swift-syntax changes necessary of allowing the following code:

@freestanding(expression)
public macro checkGeneric_root<DAS>() = #externalMacro(module: "MacroDefinition", type: "GenericToVoidMacro")

@freestanding(expression)
public macro checkGeneric<DAS>() = #checkGeneric_root<DAS>()

I didn't quite implement it entirely right and need to add a test here as well.

Resolves rdar://122004157

@ktoso
Copy link
Contributor Author

ktoso commented Jan 31, 2024

@swift-ci please smoke test

@ktoso ktoso requested review from DougGregor and hborla January 31, 2024 12:17
@ktoso
Copy link
Contributor Author

ktoso commented Feb 2, 2024

Thanks folks! I added the notes, and a test covering the new functionality as well.

Please have a look 👀

@ktoso
Copy link
Contributor Author

ktoso commented Feb 2, 2024

@swift-ci please smoke test

@ktoso
Copy link
Contributor Author

ktoso commented Feb 2, 2024

@swift-ci Please test

@ktoso
Copy link
Contributor Author

ktoso commented Feb 3, 2024

Thanks a lot for the review! I'll follow up shortly

@ktoso ktoso force-pushed the wip-macros-generics branch 3 times, most recently from b696a64 to d449bac Compare February 5, 2024 04:41
@ktoso
Copy link
Contributor Author

ktoso commented Feb 5, 2024

@swift-ci Please test

@ktoso
Copy link
Contributor Author

ktoso commented Feb 6, 2024

swiftlang/swift#71271
@swift-ci Please test


override func visit(_ node: GenericArgumentSyntax) -> SyntaxVisitorContinueKind {
guard let baseName = node.argument.as(IdentifierTypeSyntax.self)?.name else {
// Handle error
Copy link
Member

Choose a reason for hiding this comment

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

Is this a leftover todo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wasn't sure what to do here, but actually best might be to just .skipChildren and call it a day

Sources/SwiftSyntaxMacroExpansion/MacroReplacement.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntaxMacroExpansion/MacroReplacement.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntaxMacroExpansion/MacroReplacement.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntaxMacroExpansion/MacroReplacement.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntaxMacroExpansion/MacroReplacement.swift Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Just an open question: Would it make sense to specialize macros with types that aren’t part of the second-level macro? Ie. would the following make sense?

macro gen<T>(a: T) = #externalMacro 
macro genString(a: String) = #gen<String>(a: a)

And then #genString(a: "x") would have the intermediate expansion step #gen<String>(a: "x")


Similarly, would it make sense to allow generic nesting in a second-level macro definition? Ie. would something like the following make sense?

macro gen<T>(a: T) = #externalMacro 
macro genArray<T>(a: [T]) = #gen<Array<T>>(a: a)

And then #genArray<Int>(a: [1]) would have the intermediate expansion step #gen<Array<Int>>(a: [1])

AFAICT neither of those are currently supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah these may be worth supporting as well -- I made a ticket for it #2476

Thanks for the thorough review Alex. I think we're good now, if CI gods are willing 😇

@ktoso ktoso force-pushed the wip-macros-generics branch from 5af31e5 to 9d22af9 Compare February 6, 2024 06:05
@ktoso
Copy link
Contributor Author

ktoso commented Feb 6, 2024

swiftlang/swift#71271
@swift-ci Please smoke test

@ktoso ktoso enabled auto-merge February 6, 2024 06:05
@ktoso ktoso disabled auto-merge February 6, 2024 06:05
@ktoso ktoso enabled auto-merge February 6, 2024 06:05
@ktoso
Copy link
Contributor Author

ktoso commented Feb 6, 2024

swiftlang/swift#71271
@swift-ci Please test

@ktoso ktoso merged commit e77b169 into swiftlang:main Feb 6, 2024
3 checks passed
@ktoso ktoso deleted the wip-macros-generics branch February 6, 2024 09:13
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