1
Fork 0

External spans: address review.

* The lazy loading mechanism has been moved to a more appropriate place.
* Return values from the functions invoked there are properly used.
* Documentation has gotten some minor improvements.
* Possibly some larger restructuring will need to take place still.
This commit is contained in:
Inokentiy Babushkin 2017-06-12 15:37:26 +02:00
parent afe841587d
commit 271133b03e
No known key found for this signature in database
GPG key ID: 7EFC8EC5224DE8EC
4 changed files with 28 additions and 36 deletions

View file

@ -177,8 +177,6 @@ impl EmitterWriter {
continue;
}
cm.load_source_for_filemap(cm.span_to_filename(span_label.span));
let lo = cm.lookup_char_pos(span_label.span.lo);
let mut hi = cm.lookup_char_pos(span_label.span.hi);
@ -891,10 +889,10 @@ impl EmitterWriter {
let mut annotated_files = self.preprocess_annotations(msp);
// Make sure our primary file comes first
let primary_lo = if let (Some(ref cm), Some(ref primary_span)) =
let (primary_lo, cm) = if let (Some(cm), Some(ref primary_span)) =
(self.cm.as_ref(), msp.primary_span().as_ref()) {
if primary_span != &&DUMMY_SP {
cm.lookup_char_pos(primary_span.lo)
(cm.lookup_char_pos(primary_span.lo), cm)
} else {
emit_to_destination(&buffer.render(), level, &mut self.dst)?;
return Ok(());
@ -912,8 +910,7 @@ impl EmitterWriter {
// Print out the annotate source lines that correspond with the error
for annotated_file in annotated_files {
// we can't annotate anything if the source is unavailable.
if annotated_file.file.src.is_none()
&& annotated_file.file.external_src.borrow().is_absent() {
if !cm.ensure_filemap_source_present(annotated_file.file.clone()) {
continue;
}

View file

@ -50,7 +50,7 @@ pub mod registry;
pub mod styled_buffer;
mod lock;
use syntax_pos::{BytePos, Loc, FileLinesResult, FileName, MultiSpan, Span, NO_EXPANSION};
use syntax_pos::{BytePos, Loc, FileLinesResult, FileMap, FileName, MultiSpan, Span, NO_EXPANSION};
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
pub enum RenderSpan {
@ -104,7 +104,7 @@ pub trait CodeMapper {
fn span_to_filename(&self, sp: Span) -> FileName;
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
fn call_span_if_macro(&self, sp: Span) -> Span;
fn load_source_for_filemap(&self, file: FileName) -> bool;
fn ensure_filemap_source_present(&self, file_map: Rc<FileMap>) -> bool;
}
impl CodeSuggestion {

View file

@ -559,19 +559,9 @@ impl CodeMapper for CodeMap {
}
sp
}
fn load_source_for_filemap(&self, filename: FileName) -> bool {
let file_map = if let Some(fm) = self.get_filemap(&filename) {
fm
} else {
return false;
};
if *file_map.external_src.borrow() == ExternalSource::AbsentOk {
let src = self.file_loader.read_file(Path::new(&filename)).ok();
return file_map.add_external_src(src);
}
false
fn ensure_filemap_source_present(&self, file_map: Rc<FileMap>) -> bool {
let src = self.file_loader.read_file(Path::new(&file_map.name)).ok();
return file_map.add_external_src(src)
}
}

View file

@ -604,28 +604,33 @@ impl FileMap {
lines.push(pos);
}
/// add externally loaded source.
/// if the hash of the input doesn't match or no input is supplied via None,
/// Add externally loaded source.
/// If the hash of the input doesn't match or no input is supplied via None,
/// it is interpreted as an error and the corresponding enum variant is set.
/// The return value signifies whether some kind of source is present.
pub fn add_external_src(&self, src: Option<String>) -> bool {
let mut external_src = self.external_src.borrow_mut();
if let Some(src) = src {
let mut hasher: StableHasher<u128> = StableHasher::new();
hasher.write(src.as_bytes());
if *self.external_src.borrow() == ExternalSource::AbsentOk {
let mut external_src = self.external_src.borrow_mut();
if let Some(src) = src {
let mut hasher: StableHasher<u128> = StableHasher::new();
hasher.write(src.as_bytes());
if hasher.finish() == self.src_hash {
*external_src = ExternalSource::Present(src);
return true;
if hasher.finish() == self.src_hash {
*external_src = ExternalSource::Present(src);
return true;
}
} else {
*external_src = ExternalSource::AbsentErr;
}
} else {
*external_src = ExternalSource::AbsentErr;
}
false
false
} else {
self.src.is_some() || self.external_src.borrow().get_source().is_some()
}
}
/// get a line from the list of pre-computed line-beginnings.
/// line-number here is 0-based.
/// Get a line from the list of pre-computed line-beginnings.
/// The line number here is 0-based.
pub fn get_line(&self, line_number: usize) -> Option<Cow<str>> {
fn get_until_newline(src: &str, begin: usize) -> &str {
// We can't use `lines.get(line_number+1)` because we might