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

proc-macro span syntax context and hygiene is underspecified and underdocumented #130995

Open
jieyouxu opened this issue Sep 28, 2024 · 2 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-proc-macros Area: Procedural macros C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jieyouxu
Copy link
Member

jieyouxu commented Sep 28, 2024

zulip thread: https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/proc-macro.20span.20syntax.20context.20docs.20and.20best.20practices

Are there any clear docs or guidance about proc-macro span syntax context and hygiene? I'm asking because I've noticed that rustc lints, diagnostics, also clippy lints, all run into issues where we don't want to lint on or make suggestions on code that are generated by proc-macros, but Span::from_expansion returns false for proc-macro-generated spans that naively forwards user spans without adding distinguishable syntax context.

The best explanation about proc-macro span syntax context and hygiene I can find atm is the The Little Book of Rust Macros by @Veykril. AFAICT The reference on proc-macro span syntax context and hygiene is quite terse: https://doc.rust-lang.org/reference/procedural-macros.html?highlight=hygiene#procedural-macro-hygiene.

This causes issues where crate authors who write proc-macros use

e.g. m!(t) and the span of t is used when generating code via .set_span/quote_spanned!/etc.

Example issue I ran into when trying to implement unit_bindings: #112380 (comment) (I think I now know the fix for Rocket isn't exactly reasoned correctly but happened to add sufficient Syntax Context to suppress the lint)
Example clippy issue: rust-lang/rust-clippy#13458

From the perspective of rustc and clippy, technically it's "not our fault" because the user provided a span that has no distinguishing syntax context, but from the perspective of the user it's very confusing. It does not help rustc/clippy maintainers nor does it help users.

Zulip thread: https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/proc-macro.20span.20hygiene

I don't know who's maintaining proc-macro or who has purview over the proc-macro API, I thought it was T-libs or T-libs-api but apparently it's not T-libs, and I don't know about T-libs-api. I found @petrochenkov in triagebot.toml, maybe you know more about this?

Tagging this as T-lang because it's part of the Rust language that is quite rough when in comes to interaction between compiler/tooling and user experience.

@jieyouxu jieyouxu added A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-proc-macros Area: Procedural macros C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Sep 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 28, 2024
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 28, 2024
@jieyouxu jieyouxu changed the title proc-macro span hygiene is underspecified and underdocumented proc-macro span syntax context and hygiene is underspecified and underdocumented Sep 28, 2024
@traviscross
Copy link
Contributor

cc @eholk @rust-lang/wg-async

@Alexendoo
Copy link
Member

Also from a user perspective, it's confusing that diagnostics for expanded code can point at user written code without the note: this warning originates in the macro annotation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-proc-macros Area: Procedural macros C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants