Skip to content

[Macros] Disallow expression macro as default argument #68782

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

ApolloZhu
Copy link
Contributor

@ApolloZhu ApolloZhu commented Sep 26, 2023

Add error diagnostic when an expression macro is used as the default argument per SE-0382. Using a macro as a sub expression in default argument position is still allowed as expression macros behave the same as built-in magic literals.

rdar://115674594

@ApolloZhu
Copy link
Contributor Author

@swift-ci please test

@tevelee
Copy link
Contributor

tevelee commented Oct 9, 2023

Can't we fix the behavior (by propagating source location information to the callsite) instead of turning it off? It would be absolutely great to have this working.

Holly solved the expansion of magic literals to accurately represent values in the outermost source file. Can we take this a step further and process default arguments as well?

Using as a sub expression in default argument still allowed as expression macros behave the same as built-in magic literals
@ApolloZhu ApolloZhu force-pushed the ApolloZhu/macro/diagnose-default-argument branch from 92afc89 to 35d5b68 Compare October 27, 2023 12:41
@ApolloZhu ApolloZhu changed the title Disallow expression macro as default argument [Macros] Disallow expression macro as default argument Oct 27, 2023
@ApolloZhu
Copy link
Contributor Author

Hi @tevelee, I agree that allowing non-built-in macro as default argument could be an interesting future direction. But as reasoned in the original proposal, we should disable it right now as specified, and go through Swift Evolution process to make sure that developers won't be surprised by when the macros expand:

With the exception of the built-in macro declarations for source locations (e.g., #fileID, #line), a macro cannot be used as the default argument of a parameter. The existing features for source locations have special behavior when they appear as a default argument, wherein they are expanded by the caller using the source-location information at the call site rather than in the function declaration where they appear. This is useful, existing behavior that we cannot change, but it might not make sense for all macros, and could be surprising. Therefore, we prohibit such default argument that are (non-built-in) macros to avoid confusion, and are open to revisiting this restriction in the future.

@ApolloZhu
Copy link
Contributor Author

@swift-ci please smoke test

@tevelee
Copy link
Contributor

tevelee commented Oct 28, 2023

Thanks for the context, @ApolloZhu! I missed this during the review of the proposal, your PR makes perfect sense now.

@ApolloZhu ApolloZhu merged commit 6b88070 into swiftlang:main Nov 3, 2023
ApolloZhu added a commit that referenced this pull request Nov 7, 2023
…ault-argument

[Macros] Disallow expression macro as default argument
@ApolloZhu ApolloZhu deleted the ApolloZhu/macro/diagnose-default-argument branch December 15, 2023 19:28
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