adds DocTest filename variant, refactors doctest_offset out of source_map, fixes remaining test failures

This commit is contained in:
Matthew Russo 2018-12-04 15:18:03 -05:00
parent 88130f1796
commit f0f8aa9e05
12 changed files with 52 additions and 54 deletions

View file

@ -204,6 +204,7 @@ fn res_rel_file(cx: &mut ExtCtxt, sp: syntax_pos::Span, arg: String) -> PathBuf
let callsite = sp.source_callsite();
let mut path = match cx.source_map().span_to_unmapped_path(callsite) {
FileName::Real(path) => path,
FileName::DocTest(path, _) => path,
other => panic!("cannot resolve relative path in non-file source `{}`", other),
};
path.pop();

View file

@ -1898,7 +1898,7 @@ mod tests {
sess: &'a ParseSess,
teststr: String)
-> StringReader<'a> {
let sf = sm.new_source_file(PathBuf::from("zebra.rs").into(), teststr);
let sf = sm.new_source_file(PathBuf::from(teststr.clone()).into(), teststr);
StringReader::new(sess, sf, None)
}

View file

@ -977,23 +977,25 @@ mod tests {
with_globals(|| {
let sess = ParseSess::new(FilePathMapping::empty());
let name = FileName::Custom("source".to_string());
let name_1 = FileName::Custom("crlf_source_1".to_string());
let source = "/// doc comment\r\nfn foo() {}".to_string();
let item = parse_item_from_source_str(name.clone(), source, &sess)
let item = parse_item_from_source_str(name_1, source, &sess)
.unwrap().unwrap();
let doc = first_attr_value_str_by_name(&item.attrs, "doc").unwrap();
assert_eq!(doc, "/// doc comment");
let name_2 = FileName::Custom("crlf_source_2".to_string());
let source = "/// doc comment\r\n/// line 2\r\nfn foo() {}".to_string();
let item = parse_item_from_source_str(name.clone(), source, &sess)
let item = parse_item_from_source_str(name_2, source, &sess)
.unwrap().unwrap();
let docs = item.attrs.iter().filter(|a| a.path == "doc")
.map(|a| a.value_str().unwrap().to_string()).collect::<Vec<_>>();
let b: &[_] = &["/// doc comment".to_string(), "/// line 2".to_string()];
assert_eq!(&docs[..], b);
let name_3 = FileName::Custom("clrf_source_3".to_string());
let source = "/** doc comment\r\n * with CRLF */\r\nfn foo() {}".to_string();
let item = parse_item_from_source_str(name, source, &sess).unwrap().unwrap();
let item = parse_item_from_source_str(name_3, source, &sess).unwrap().unwrap();
let doc = first_attr_value_str_by_name(&item.attrs, "doc").unwrap();
assert_eq!(doc, "/** doc comment\n * with CRLF */");
});

View file

@ -144,9 +144,6 @@ pub struct SourceMap {
// This is used to apply the file path remapping as specified via
// --remap-path-prefix to all SourceFiles allocated within this SourceMap.
path_mapping: FilePathMapping,
/// 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 SourceMap {
@ -155,19 +152,9 @@ impl SourceMap {
files: Default::default(),
file_loader: Box::new(RealFileLoader),
path_mapping,
doctest_offset: None,
}
}
pub fn new_doctest(path_mapping: FilePathMapping,
file: FileName, line: isize) -> SourceMap {
SourceMap {
doctest_offset: Some((file, line)),
..SourceMap::new(path_mapping)
}
}
pub fn with_file_loader(file_loader: Box<dyn FileLoader + Sync + Send>,
path_mapping: FilePathMapping)
-> SourceMap {
@ -175,7 +162,6 @@ impl SourceMap {
files: Default::default(),
file_loader: file_loader,
path_mapping,
doctest_offset: None,
}
}
@ -189,11 +175,7 @@ impl SourceMap {
pub fn load_file(&self, path: &Path) -> io::Result<Lrc<SourceFile>> {
let src = self.file_loader.read_file(path)?;
let filename = if let Some((ref name, _)) = self.doctest_offset {
name.clone()
} else {
path.to_owned().into()
};
let filename = path.to_owned().into();
Ok(self.new_source_file(filename, src))
}
@ -328,15 +310,17 @@ impl SourceMap {
}
// 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;
}
pub fn doctest_offset_line(&self, file: &FileName, orig: usize) -> usize {
return match file {
FileName::DocTest(_, offset) => {
return if *offset >= 0 {
orig + *offset as usize
} else {
orig - (-(*offset)) as usize
}
},
_ => orig
}
orig
}
/// Lookup source information about a BytePos
@ -1001,8 +985,8 @@ impl SourceMapper for SourceMap {
}
)
}
fn doctest_offset_line(&self, line: usize) -> usize {
self.doctest_offset_line(line)
fn doctest_offset_line(&self, file: &FileName, line: usize) -> usize {
self.doctest_offset_line(file, line)
}
}