Skip to content

Commit 67978d5

Browse files
committed
Fix --pretty=expanded with --remap-path-prefix
Per #80832, using --pretty=expanded and --remap-path-prefix results in an ICE. This is becasue the session source files table is stored in remapped form, whereas --pretty-expanded looks up unremapped files. This remaps the path prefixes before lookup.
1 parent 6184f23 commit 67978d5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

compiler/rustc_driver/src/pretty.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,15 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
363363

364364
fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
365365
let src_name = input.source_name();
366-
let src =
367-
String::clone(&sess.source_map().get_source_file(&src_name).unwrap().src.as_ref().unwrap());
366+
let src = String::clone(
367+
&sess
368+
.source_map()
369+
.get_source_file(&src_name)
370+
.expect("get_source_file")
371+
.src
372+
.as_ref()
373+
.expect("src"),
374+
);
368375
(src, src_name)
369376
}
370377

compiler/rustc_span/src/source_map.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,10 @@ impl SourceMap {
870870
}
871871

872872
pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
873+
// Remap filename before lookup
874+
let filename = self.path_mapping().map_filename_prefix(filename).0;
873875
for sf in self.files.borrow().source_files.iter() {
874-
if *filename == sf.name {
876+
if filename == sf.name {
875877
return Some(sf.clone());
876878
}
877879
}
@@ -1039,4 +1041,15 @@ impl FilePathMapping {
10391041

10401042
(path, false)
10411043
}
1044+
1045+
fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
1046+
match file {
1047+
FileName::Real(realfile) => {
1048+
let path = realfile.local_path();
1049+
let (path, mapped) = self.map_prefix(path.to_path_buf());
1050+
(FileName::Real(RealFileName::Named(path)), mapped)
1051+
}
1052+
other => (other.clone(), false),
1053+
}
1054+
}
10421055
}

0 commit comments

Comments
 (0)