Skip to content

proc_macro: preserve file module spans for inner attrs#158046

Open
Dnreikronos wants to merge 1 commit into
rust-lang:mainfrom
Dnreikronos:proc_macro/file_mod_inner_attr_spans
Open

proc_macro: preserve file module spans for inner attrs#158046
Dnreikronos wants to merge 1 commit into
rust-lang:mainfrom
Dnreikronos:proc_macro/file_mod_inner_attr_spans

Conversation

@Dnreikronos

@Dnreikronos Dnreikronos commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

View all comments

fixes #157881

out-of-line mods with custom inner attrs were sending proc macros a pretty-printed module wrapper. the awkward bit: tokens inside the module picked up the span from the parent mod foo;, so later diags pointed at the decl instead of the file where the code actually came from.

this keeps the wrapper synthetic, but puts the loaded file body back into the token stream with its own spans. it also skips the attr currently being expanded, so we don't replay it by accident. added a small ui repro too, plus the older span-debug output changed because those spans are now real.

imo this is the least annoying fix for now. fwiw, it keeps the old fake-wrapper path instead of trying to make full token capture for file mods happen in this PR.

@rustbot

rustbot commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 17, 2026
@rustbot

rustbot commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

r? @mu001999

rustbot has assigned @mu001999.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, parser
  • compiler, parser expanded to 73 candidates
  • Random selection from 20 candidates

@rust-log-analyzer

This comment has been minimized.

@mu001999

Copy link
Copy Markdown
Member

@rustbot reroll

@zjp-CN

zjp-CN commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Could someone review the PR? The fix seems sensible, and works for me.

@mati865

mati865 commented Jun 19, 2026

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned nikomatsakis and unassigned mati865 Jun 19, 2026
Comment thread compiler/rustc_parse/src/lib.rs Outdated
@rust-bors

This comment has been minimized.

@Dnreikronos
Dnreikronos force-pushed the proc_macro/file_mod_inner_attr_spans branch from 9633953 to 8a1f71e Compare July 6, 2026 13:44
@rustbot

rustbot commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Dnreikronos

Copy link
Copy Markdown
Contributor Author

Hi, @nikomatsakis!
Do you need something from my side to finish the work on this?

Best wishes,
Joao Soares

@GuillaumeGomez

Copy link
Copy Markdown
Member

r? @Urgau

@rustbot rustbot assigned Urgau and unassigned nikomatsakis Jul 15, 2026
@nnethercote

Copy link
Copy Markdown
Contributor

cc @petrochenkov

@petrochenkov petrochenkov self-assigned this Jul 16, 2026
Comment thread tests/ui/proc-macro/inner-attr-file-mod-diagnostics.rs Outdated
Comment thread compiler/rustc_expand/src/expand.rs
Comment thread compiler/rustc_parse/src/lib.rs Outdated
Comment thread compiler/rustc_parse/src/lib.rs Outdated
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 16, 2026
@Urgau Urgau removed their assignment Jul 16, 2026
@petrochenkov petrochenkov removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 17, 2026
@petrochenkov petrochenkov added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 17, 2026
Comment thread compiler/rustc_parse/src/lib.rs Outdated
Comment thread compiler/rustc_parse/src/lib.rs Outdated
Comment thread compiler/rustc_parse/src/lib.rs Outdated
Comment thread compiler/rustc_parse/src/lib.rs Outdated
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 17, 2026
@Dnreikronos

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 17, 2026
Comment thread compiler/rustc_parse/src/lib.rs Outdated
}

fn lex_token_trees_for_span(psess: &ParseSess, span: Span) -> Option<TokenStream> {
if span.is_dummy() {

@petrochenkov petrochenkov Jul 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd rather remove this to simplify code, I don't think we need optimizations here.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, makes sense. I removed the explicit is_dummy() check and let span_to_snippet handle unavailable spans.

Comment thread compiler/rustc_parse/src/lib.rs Outdated

let mut body_tts = Vec::new();
body_tts.extend(
lex_token_trees_for_span(psess, spans.inner_span.until(attr.span))?.iter().cloned(),

@petrochenkov petrochenkov Jul 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: the .iter().cloned() part can be hidden into lex_token_trees_for_span, which can return impl Iterator.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. lex_token_trees_for_span returns an iterator now, so callers don't need .iter().cloned() anymore.

@petrochenkov

Copy link
Copy Markdown
Contributor

r=me after addressing the remaining nits and squashing commits.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 17, 2026
@rustbot

rustbot commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Dnreikronos
Dnreikronos force-pushed the proc_macro/file_mod_inner_attr_spans branch from 46fdb65 to daff591 Compare July 18, 2026 12:04
@Dnreikronos

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom inner attribute on file modules has wrong span