]> git.lizzy.rs Git - rust.git/commitdiff
External spans: address review.
authorInokentiy Babushkin <twk@twki.de>
Mon, 12 Jun 2017 13:37:26 +0000 (15:37 +0200)
committerInokentiy Babushkin <twk@twki.de>
Mon, 12 Jun 2017 13:37:26 +0000 (15:37 +0200)
* 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.

src/librustc_errors/emitter.rs
src/librustc_errors/lib.rs
src/libsyntax/codemap.rs
src/libsyntax_pos/lib.rs

index 2099725c48aa481af342ec852b732e954cba7471..b4b14328b3db70c6104214bb64adccc9d95480c8 100644 (file)
@@ -177,8 +177,6 @@ fn add_annotation_to_file(file_vec: &mut Vec<FileWithAnnotatedLines>,
                     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 @@ fn emit_message_default(&mut self,
         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 @@ fn emit_message_default(&mut self,
         // 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;
             }
 
index 26ecbe724f8d36c381e4177093c530b431690bc0..975b720276e82b8d58c80becad14bc8e396bd147 100644 (file)
@@ -50,7 +50,7 @@
 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 {
index 7267f510a490f3a150ad31c902675d1a48708ac1..5b10139cd1968c5ed2800df96d9ecbc69e3e77aa 100644 (file)
@@ -559,19 +559,9 @@ fn call_span_if_macro(&self, sp: Span) -> Span {
         }
         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)
     }
 }
 
index 719c25e2410b9102cdda0e3f2263f291352b7201..94656b3aea79e0f5f24e77b94619f2e5fbe47ed6 100644 (file)
@@ -604,28 +604,33 @@ pub fn next_line(&self, pos: BytePos) {
         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 hasher.finish() == self.src_hash {
-                *external_src = ExternalSource::Present(src);
-                return true;
+        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;
+                }
+            } else {
+                *external_src = ExternalSource::AbsentErr;
             }
+
+            false
         } else {
-            *external_src = ExternalSource::AbsentErr;
+            self.src.is_some() || self.external_src.borrow().get_source().is_some()
         }
-
-        false
     }
 
-    /// 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