diff options
| author | bors <bors@rust-lang.org> | 2018-01-14 11:28:27 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-01-14 11:28:27 +0000 |
| commit | 5d6f6e65ffb61a7acec28f1a08491de321e32be5 (patch) | |
| tree | d0cd97e077026036e5c8eb4121c6fbb83a0e40dd /src/libsyntax | |
| parent | 48ab4cde5460f9f7c57249e4e62ad569dc843dd8 (diff) | |
| parent | 44b659ac2d7563198ce3ae39a4bfcecc6954fe21 (diff) | |
| download | rust-5d6f6e65ffb61a7acec28f1a08491de321e32be5.tar.gz rust-5d6f6e65ffb61a7acec28f1a08491de321e32be5.zip | |
Auto merge of #47274 - Manishearth:rustdoc-span, r=QuietMisdreavus
Use correct line offsets for doctests Not yet tested. This doesn't handle char positions. It could if I collected a map of char offsets and lines, but this is a bit more work and requires hooking into the parser much more (unsure if it's possible). r? @QuietMisdreavus (fixes #45868)
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/codemap.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 129defd2093..a58a61c3636 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -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)] |
