Skip to content

Commit

Permalink
Rollup merge of rust-lang#80859 - jsgf:fix-pretty-remap, r=davidtwco
Browse files Browse the repository at this point in the history
Fix --pretty=expanded with --remap-path-prefix

Per rust-lang#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.

~~There don't appear to be any existing tests for --pretty=expanded; I'll look into
adding some.~~ Never mind, found the pretty tests.

Fixes rust-lang#80832
  • Loading branch information
Dylan-DPC authored Jan 13, 2021
2 parents 330e196 + d7ce9d5 commit 7ce8246
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
11 changes: 9 additions & 2 deletions compiler/rustc_driver/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,15 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {

fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
let src_name = input.source_name();
let src =
String::clone(&sess.source_map().get_source_file(&src_name).unwrap().src.as_ref().unwrap());
let src = String::clone(
&sess
.source_map()
.get_source_file(&src_name)
.expect("get_source_file")
.src
.as_ref()
.expect("src"),
);
(src, src_name)
}

Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,10 @@ impl SourceMap {
}

pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
// Remap filename before lookup
let filename = self.path_mapping().map_filename_prefix(filename).0;
for sf in self.files.borrow().source_files.iter() {
if *filename == sf.name {
if filename == sf.name {
return Some(sf.clone());
}
}
Expand Down Expand Up @@ -1041,4 +1043,15 @@ impl FilePathMapping {

(path, false)
}

fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
match file {
FileName::Real(realfile) => {
let path = realfile.local_path();
let (path, mapped) = self.map_prefix(path.to_path_buf());
(FileName::Real(RealFileName::Named(path)), mapped)
}
other => (other.clone(), false),
}
}
}
13 changes: 13 additions & 0 deletions src/test/pretty/expanded-and-path-remap-80832.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::v1::*;
#[macro_use]
extern crate std;
// Test for issue 80832
//
// pretty-mode:expanded
// pp-exact:expanded-and-path-remap-80832.pp
// compile-flags: --remap-path-prefix {{src-base}}=the/src

fn main() { }
7 changes: 7 additions & 0 deletions src/test/pretty/expanded-and-path-remap-80832.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Test for issue 80832
//
// pretty-mode:expanded
// pp-exact:expanded-and-path-remap-80832.pp
// compile-flags: --remap-path-prefix {{src-base}}=the/src

fn main() {}

0 comments on commit 7ce8246

Please sign in to comment.