Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1d96771

Browse files
committedMar 16, 2025
Fix ICE: attempted to remap an already remapped filename
This commit fixes an internal compiler error (ICE) that occurs when rustdoc attempts to process macros with a remapped filename. The issue arose during macro expansion when the `--remap-path-prefix` option was used. Instead of passing remapped filenames through, which would trigger the "attempted to remap an already remapped filename" panic, we now extract the original local path from remapped filenames before processing them. A test case has been added to verify this behavior. Fixes #138520 Signed-off-by: Charalampos Mitrodimas <charmitro@posteo.net>
1 parent 5f3b84a commit 1d96771

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed
 

‎src/librustdoc/clean/render_macro_matchers.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rustc_ast_pretty::pprust::PrintState;
44
use rustc_ast_pretty::pprust::state::State as Printer;
55
use rustc_middle::ty::TyCtxt;
66
use rustc_session::parse::ParseSess;
7-
use rustc_span::Span;
87
use rustc_span::symbol::{Ident, Symbol, kw};
8+
use rustc_span::{FileName, RealFileName, Span};
99

1010
/// Render a macro matcher in a format suitable for displaying to the user
1111
/// as part of an item declaration.
@@ -63,7 +63,21 @@ fn snippet_equal_to_token(tcx: TyCtxt<'_>, matcher: &TokenTree) -> Option<String
6363

6464
// Create a Parser.
6565
let psess = ParseSess::new(rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec());
66-
let file_name = source_map.span_to_filename(span);
66+
67+
// Get the original filename and ensure we're using a fresh, unmapped filename
68+
// to avoid trying to remap an already remapped filename
69+
let file_name = match source_map.span_to_filename(span) {
70+
FileName::Real(real_filename) => {
71+
if let Some(local_path) = real_filename.clone().into_local_path() {
72+
FileName::Real(RealFileName::LocalPath(local_path))
73+
} else {
74+
// Fallback to original if no local path is available
75+
FileName::Real(real_filename.clone())
76+
}
77+
}
78+
other => other,
79+
};
80+
6781
let mut parser =
6882
match rustc_parse::new_parser_from_source_str(&psess, file_name, snippet.clone()) {
6983
Ok(parser) => parser,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Regression test for "attempted to remap an already remapped filename" ICE in rustdoc
2+
// when using --remap-path-prefix with macro rendering.
3+
// <https://github.com/rust-lang/rust/issues/138520>
4+
5+
//@ compile-flags:-Z unstable-options --remap-path-prefix={{src-base}}=remapped_path
6+
//@ rustc-env:RUST_BACKTRACE=0
7+
//@ build-pass
8+
9+
macro_rules! f(() => {});

0 commit comments

Comments
 (0)
Failed to load comments.