about summary refs log tree commit diff
path: root/src/libsyntax/codemap.rs
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2018-01-08 20:17:23 +0530
committerManish Goregaokar <manishsmail@gmail.com>2018-01-10 10:10:34 +0530
commit730679685ebad64e94b50961bb6c8c2e385529e4 (patch)
tree8e0f4529205efb84263d8b6fd7f0e1f5ce0ddf02 /src/libsyntax/codemap.rs
parent1b193de98af5b8c6e2b50908ea64323b9095a1a4 (diff)
downloadrust-730679685ebad64e94b50961bb6c8c2e385529e4.tar.gz
rust-730679685ebad64e94b50961bb6c8c2e385529e4.zip
Use correct line offsets for doctests (fixes #45868)
Diffstat (limited to 'src/libsyntax/codemap.rs')
-rw-r--r--src/libsyntax/codemap.rs36
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)]