rustdoc: Correctly handle when a macro generates multiple items in --generate-macro-expansion - #161876
Merged
rust-bors[bot] merged 2 commits intoAug 28, 2026
Merged
Conversation
Urgau
reviewed
Aug 27, 2026
GuillaumeGomez
force-pushed
the
expand-multiple-items
branch
from
August 27, 2026 18:56
514a0d6 to
c9cb963
Compare
Urgau
approved these changes
Aug 27, 2026
Member
Author
|
@bors r=Urgau rollup |
Contributor
GuillaumeGomez
added a commit
to GuillaumeGomez/rust
that referenced
this pull request
Aug 27, 2026
…ms, r=Urgau rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion` Fixes rust-lang#157508. When a macro expands multiple items, they all have the exact same `Span`. So to prevent them from overwrite the previous one(s) sharing the same `Span`, we check this case too. r? @Urgau
rust-bors Bot
pushed a commit
that referenced
this pull request
Aug 28, 2026
Rollup of 14 pull requests Successful merges: - #150075 (Implement clamp_to) - #159103 (fix(reborrow): recursive implementation) - #160848 (std: avoid aliasing violations when wrapping opaque C types) - #161421 (Include startup crt objects on WASI for more outputs) - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order) - #161862 (Put data segment in specified section with link_section on wasm) - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations) - #161456 (reduce perf impact of scalar size checks) - #161666 (Print vendor instructions in `x vendor`) - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers) - #161828 (Never type after-stabilization cleanup) - #161860 (atomicptr.rs test: remove unused import) - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods) - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`)
JonathanBrouwer
added a commit
to JonathanBrouwer/rust
that referenced
this pull request
Aug 28, 2026
…ms, r=Urgau rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion` Fixes rust-lang#157508. When a macro expands multiple items, they all have the exact same `Span`. So to prevent them from overwrite the previous one(s) sharing the same `Span`, we check this case too. r? @Urgau
JonathanBrouwer
added a commit
to JonathanBrouwer/rust
that referenced
this pull request
Aug 28, 2026
…ms, r=Urgau rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion` Fixes rust-lang#157508. When a macro expands multiple items, they all have the exact same `Span`. So to prevent them from overwrite the previous one(s) sharing the same `Span`, we check this case too. r? @Urgau
rust-bors Bot
pushed a commit
that referenced
this pull request
Aug 28, 2026
…uwer Rollup of 21 pull requests Successful merges: - #158609 (Update sccache to 0.16.0) - #150075 (Implement clamp_to) - #159103 (fix(reborrow): recursive implementation) - #160562 (add target feature ABI checks for SPARC) - #160848 (std: avoid aliasing violations when wrapping opaque C types) - #161421 (Include startup crt objects on WASI for more outputs) - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order) - #161862 (Put data segment in specified section with link_section on wasm) - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations) - #161456 (reduce perf impact of scalar size checks) - #161528 (Add regression test to ensure optimal compilation) - #161666 (Print vendor instructions in `x vendor`) - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers) - #161828 (Never type after-stabilization cleanup) - #161859 (Do not optimize MIR for comptime ConstFns) - #161860 (atomicptr.rs test: remove unused import) - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods) - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`) - #161889 (Add link to ownership section in ptr::read docs) - #161890 (rustdoc: some clarifying comments) - #161891 (Mark `extern_item_impls` feature as incomplete) Failed merges: - #161702 (Use `drop_guard` in some places in {core,alloc,std})
rust-bors Bot
pushed a commit
that referenced
this pull request
Aug 28, 2026
…uwer Rollup of 21 pull requests Successful merges: - #150075 (Implement clamp_to) - #159103 (fix(reborrow): recursive implementation) - #160562 (add target feature ABI checks for SPARC) - #160848 (std: avoid aliasing violations when wrapping opaque C types) - #161421 (Include startup crt objects on WASI for more outputs) - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order) - #161862 (Put data segment in specified section with link_section on wasm) - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations) - #157218 (Track items behind `cfg_select` in the same way we do for `cfg`) - #161456 (reduce perf impact of scalar size checks) - #161528 (Add regression test to ensure optimal compilation) - #161666 (Print vendor instructions in `x vendor`) - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers) - #161828 (Never type after-stabilization cleanup) - #161859 (Do not optimize MIR for comptime ConstFns) - #161860 (atomicptr.rs test: remove unused import) - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods) - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`) - #161889 (Add link to ownership section in ptr::read docs) - #161890 (rustdoc: some clarifying comments) - #161891 (Mark `extern_item_impls` feature as incomplete) Failed merges: - #161702 (Use `drop_guard` in some places in {core,alloc,std})
rust-bors Bot
pushed a commit
that referenced
this pull request
Aug 28, 2026
Rollup merge of #161876 - GuillaumeGomez:expand-multiple-items, r=Urgau rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion` Fixes #157508. When a macro expands multiple items, they all have the exact same `Span`. So to prevent them from overwrite the previous one(s) sharing the same `Span`, we check this case too. r? @Urgau
2 tasks
fmease
reviewed
Aug 29, 2026
| @@ -73,7 +74,12 @@ impl<'ast> ExpandedCodeVisitor<'ast> { | |||
| self.expanded_codes.iter().position(|info| info.span.overlaps(callsite_span)) | |||
Member
There was a problem hiding this comment.
Completely unrelated to this PR since it's preexisting but the position + manually indexing can easily be replaced by a find:
diff --git a/src/librustdoc/html/macro_expansion.rs b/src/librustdoc/html/macro_expansion.rs
index 55ca93e601b..27741ea485a 100644
--- a/src/librustdoc/html/macro_expansion.rs
+++ b/src/librustdoc/html/macro_expansion.rs
@@ -70,10 +70,9 @@ fn handle_new_span<F: Fn() -> String>(&mut self, new_span: Span, f: F) {
return;
}
let callsite_span = new_span.source_callsite();
- if let Some(index) =
- self.expanded_codes.iter().position(|info| info.span.overlaps(callsite_span))
+ if let Some(info) =
+ self.expanded_codes.iter_mut().find(|info| info.span.overlaps(callsite_span))
{
- let info = &mut self.expanded_codes[index];
// If the new span we got has the exact same span information as a span already in the
// list, it means it's generated from the same macro but is a different item, so we need
// to add it as well.
@@ -86,12 +85,11 @@ fn handle_new_span<F: Fn() -> String>(&mut self, new_span: Span, f: F) {
info.code = f();
} else {
// We push the new item after the existing one.
- let expanded_code = &mut self.expanded_codes[index];
- expanded_code.code.push('\n');
- expanded_code.code.push_str(&f());
- let lo = BytePos(expanded_code.expanded_span.lo().0.min(new_span.lo().0));
- let hi = BytePos(expanded_code.expanded_span.hi().0.max(new_span.hi().0));
- expanded_code.expanded_span = expanded_code.expanded_span.with_lo(lo).with_hi(hi);
+ info.code.push('\n');
+ info.code.push_str(&f());
+ let lo = BytePos(info.expanded_span.lo().0.min(new_span.lo().0));
+ let hi = BytePos(info.expanded_span.hi().0.max(new_span.hi().0));
+ info.expanded_span = info.expanded_span.with_lo(lo).with_hi(hi);
}
} else {
// We add a new item.Noticed while reviewing #161944 and figuring out what original_span meant. Not worth a PR, so it's just a comment. idk. Edit: Added to my TODO list.
JonathanBrouwer
added a commit
to JonathanBrouwer/rust
that referenced
this pull request
Aug 29, 2026
…sion-code, r=GuillaumeGomez Improve rustdoc macro expansion code As commented [here](rust-lang#161876 (comment)). I also set you as commit author @fmease. :p r? ghost
rust-bors Bot
pushed a commit
that referenced
this pull request
Aug 29, 2026
Rollup merge of #161976 - GuillaumeGomez:improve-macro-expansion-code, r=GuillaumeGomez Improve rustdoc macro expansion code As commented [here](#161876 (comment)). I also set you as commit author @fmease. :p r? ghost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #157508.
When a macro expands multiple items, they all have the exact same
Span. So to prevent them from overwrite the previous one(s) sharing the sameSpan, we check this case too.r? @Urgau