Skip to content

Commit 271133b

Browse files
committed
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.
1 parent afe8415 commit 271133b

File tree

4 files changed

+28
-36
lines changed

4 files changed

+28
-36
lines changed

src/librustc_errors/emitter.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ impl EmitterWriter {
177177
continue;
178178
}
179179

180-
cm.load_source_for_filemap(cm.span_to_filename(span_label.span));
181-
182180
let lo = cm.lookup_char_pos(span_label.span.lo);
183181
let mut hi = cm.lookup_char_pos(span_label.span.hi);
184182

@@ -891,10 +889,10 @@ impl EmitterWriter {
891889
let mut annotated_files = self.preprocess_annotations(msp);
892890

893891
// Make sure our primary file comes first
894-
let primary_lo = if let (Some(ref cm), Some(ref primary_span)) =
892+
let (primary_lo, cm) = if let (Some(cm), Some(ref primary_span)) =
895893
(self.cm.as_ref(), msp.primary_span().as_ref()) {
896894
if primary_span != &&DUMMY_SP {
897-
cm.lookup_char_pos(primary_span.lo)
895+
(cm.lookup_char_pos(primary_span.lo), cm)
898896
} else {
899897
emit_to_destination(&buffer.render(), level, &mut self.dst)?;
900898
return Ok(());
@@ -912,8 +910,7 @@ impl EmitterWriter {
912910
// Print out the annotate source lines that correspond with the error
913911
for annotated_file in annotated_files {
914912
// we can't annotate anything if the source is unavailable.
915-
if annotated_file.file.src.is_none()
916-
&& annotated_file.file.external_src.borrow().is_absent() {
913+
if !cm.ensure_filemap_source_present(annotated_file.file.clone()) {
917914
continue;
918915
}
919916

src/librustc_errors/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub mod registry;
5050
pub mod styled_buffer;
5151
mod lock;
5252

53-
use syntax_pos::{BytePos, Loc, FileLinesResult, FileName, MultiSpan, Span, NO_EXPANSION};
53+
use syntax_pos::{BytePos, Loc, FileLinesResult, FileMap, FileName, MultiSpan, Span, NO_EXPANSION};
5454

5555
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
5656
pub enum RenderSpan {
@@ -104,7 +104,7 @@ pub trait CodeMapper {
104104
fn span_to_filename(&self, sp: Span) -> FileName;
105105
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
106106
fn call_span_if_macro(&self, sp: Span) -> Span;
107-
fn load_source_for_filemap(&self, file: FileName) -> bool;
107+
fn ensure_filemap_source_present(&self, file_map: Rc<FileMap>) -> bool;
108108
}
109109

110110
impl CodeSuggestion {

src/libsyntax/codemap.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -559,19 +559,9 @@ impl CodeMapper for CodeMap {
559559
}
560560
sp
561561
}
562-
fn load_source_for_filemap(&self, filename: FileName) -> bool {
563-
let file_map = if let Some(fm) = self.get_filemap(&filename) {
564-
fm
565-
} else {
566-
return false;
567-
};
568-
569-
if *file_map.external_src.borrow() == ExternalSource::AbsentOk {
570-
let src = self.file_loader.read_file(Path::new(&filename)).ok();
571-
return file_map.add_external_src(src);
572-
}
573-
574-
false
562+
fn ensure_filemap_source_present(&self, file_map: Rc<FileMap>) -> bool {
563+
let src = self.file_loader.read_file(Path::new(&file_map.name)).ok();
564+
return file_map.add_external_src(src)
575565
}
576566
}
577567

src/libsyntax_pos/lib.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -604,28 +604,33 @@ impl FileMap {
604604
lines.push(pos);
605605
}
606606

607-
/// add externally loaded source.
608-
/// if the hash of the input doesn't match or no input is supplied via None,
607+
/// Add externally loaded source.
608+
/// If the hash of the input doesn't match or no input is supplied via None,
609609
/// it is interpreted as an error and the corresponding enum variant is set.
610+
/// The return value signifies whether some kind of source is present.
610611
pub fn add_external_src(&self, src: Option<String>) -> bool {
611-
let mut external_src = self.external_src.borrow_mut();
612-
if let Some(src) = src {
613-
let mut hasher: StableHasher<u128> = StableHasher::new();
614-
hasher.write(src.as_bytes());
615-
616-
if hasher.finish() == self.src_hash {
617-
*external_src = ExternalSource::Present(src);
618-
return true;
612+
if *self.external_src.borrow() == ExternalSource::AbsentOk {
613+
let mut external_src = self.external_src.borrow_mut();
614+
if let Some(src) = src {
615+
let mut hasher: StableHasher<u128> = StableHasher::new();
616+
hasher.write(src.as_bytes());
617+
618+
if hasher.finish() == self.src_hash {
619+
*external_src = ExternalSource::Present(src);
620+
return true;
621+
}
622+
} else {
623+
*external_src = ExternalSource::AbsentErr;
619624
}
625+
626+
false
620627
} else {
621-
*external_src = ExternalSource::AbsentErr;
628+
self.src.is_some() || self.external_src.borrow().get_source().is_some()
622629
}
623-
624-
false
625630
}
626631

627-
/// get a line from the list of pre-computed line-beginnings.
628-
/// line-number here is 0-based.
632+
/// Get a line from the list of pre-computed line-beginnings.
633+
/// The line number here is 0-based.
629634
pub fn get_line(&self, line_number: usize) -> Option<Cow<str>> {
630635
fn get_until_newline(src: &str, begin: usize) -> &str {
631636
// We can't use `lines.get(line_number+1)` because we might

0 commit comments

Comments
 (0)