Use correct line offsets for doctests (fixes #45868)
This commit is contained in:
parent
1b193de98a
commit
730679685e
5 changed files with 55 additions and 11 deletions
|
@ -131,6 +131,9 @@ pub struct CodeMap {
|
|||
// -Zremap-path-prefix to all FileMaps allocated within this CodeMap.
|
||||
path_mapping: FilePathMapping,
|
||||
stable_id_to_filemap: RefCell<FxHashMap<StableFilemapId, Rc<FileMap>>>,
|
||||
/// In case we are in a doctest, replace all file names with the PathBuf,
|
||||
/// and add the given offsets to the line info
|
||||
doctest_offset: Option<(FileName, isize)>,
|
||||
}
|
||||
|
||||
impl CodeMap {
|
||||
|
@ -140,9 +143,19 @@ impl CodeMap {
|
|||
file_loader: Box::new(RealFileLoader),
|
||||
path_mapping,
|
||||
stable_id_to_filemap: RefCell::new(FxHashMap()),
|
||||
doctest_offset: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_doctest(path_mapping: FilePathMapping,
|
||||
file: FileName, line: isize) -> CodeMap {
|
||||
CodeMap {
|
||||
doctest_offset: Some((file, line)),
|
||||
..CodeMap::new(path_mapping)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn with_file_loader(file_loader: Box<FileLoader>,
|
||||
path_mapping: FilePathMapping)
|
||||
-> CodeMap {
|
||||
|
@ -151,6 +164,7 @@ impl CodeMap {
|
|||
file_loader,
|
||||
path_mapping,
|
||||
stable_id_to_filemap: RefCell::new(FxHashMap()),
|
||||
doctest_offset: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,7 +178,12 @@ impl CodeMap {
|
|||
|
||||
pub fn load_file(&self, path: &Path) -> io::Result<Rc<FileMap>> {
|
||||
let src = self.file_loader.read_file(path)?;
|
||||
Ok(self.new_filemap(path.to_owned().into(), src))
|
||||
let filename = if let Some((ref name, _)) = self.doctest_offset {
|
||||
name.clone()
|
||||
} else {
|
||||
path.to_owned().into()
|
||||
};
|
||||
Ok(self.new_filemap(filename, src))
|
||||
}
|
||||
|
||||
pub fn files(&self) -> Ref<Vec<Rc<FileMap>>> {
|
||||
|
@ -303,6 +322,18 @@ impl CodeMap {
|
|||
pos.col.to_usize() + 1)).to_string()
|
||||
}
|
||||
|
||||
// If there is a doctest_offset, apply it to the line
|
||||
pub fn doctest_offset_line(&self, mut orig: usize) -> usize {
|
||||
if let Some((_, line)) = self.doctest_offset {
|
||||
if line >= 0 {
|
||||
orig = orig + line as usize;
|
||||
} else {
|
||||
orig = orig - (-line) as usize;
|
||||
}
|
||||
}
|
||||
orig
|
||||
}
|
||||
|
||||
/// Lookup source information about a BytePos
|
||||
pub fn lookup_char_pos(&self, pos: BytePos) -> Loc {
|
||||
let chpos = self.bytepos_to_file_charpos(pos);
|
||||
|
@ -681,6 +712,9 @@ impl CodeMapper for CodeMap {
|
|||
}
|
||||
)
|
||||
}
|
||||
fn doctest_offset_line(&self, line: usize) -> usize {
|
||||
self.doctest_offset_line(line)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue