Skip to content

[Macros] Only freestanding expression macros can have a non-Void result type #66504

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 1 commit into from
Jun 9, 2023

Conversation

DougGregor
Copy link
Member

Fixes rdar://108871352.

@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

// If the macro has a (non-Void) result type, it must have the freestanding
// expression role. Other roles cannot have result types.
if (auto resultTypeRepr = MD->getResultTypeRepr()) {
if (!MD->getMacroRoles().contains(MacroRole::Expression) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

If a macro had multiple roles (including freestanding(expression)), should we reject non-void types in that case too?

@attached(accessor) @freestanding(expression)
macro Foo() -> Int

Because in this case the return type -> Int still doesn't make sense for the accessor role.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, I don't think we should. The result type only applies to the expression role, and it's fine for it to be ignored for any attached roles.

Note that we don't currently diagnose when a macro has multiple freestanding roles. I'm handling that (and related cleanup) via a separate pull request

@swift-ci swift-ci merged commit 4f8f262 into swiftlang:main Jun 9, 2023
@DougGregor DougGregor deleted the bad-macros-with-result-types branch June 9, 2023 21:05
// expected-note@-3{{make this macro a freestanding expression macro}}{{1-1=@freestanding(expression)\n}}
// expected-note@-4{{remove the result type if the macro does not produce a value}}{{67-74=}}

@freestanding(declaration) macro nonExpressionReturnsVoid<T>(_: T) -> Void = #externalMacro(module: "A", type: "B")
Copy link
Member

Choose a reason for hiding this comment

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

I don’t think we should be allowing the declaration of any return type for non-freestanding-expression macros. Specifying Void here just doesn’t make any sense to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, I agree with that. #66526

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.

4 participants