]> git.lizzy.rs Git - rust.git/commitdiff
Added external crates' sources to FileMap.
authorInokentiy Babushkin <twk@twki.de>
Sat, 10 Jun 2017 19:08:32 +0000 (21:08 +0200)
committerInokentiy Babushkin <twk@twki.de>
Sat, 10 Jun 2017 19:08:32 +0000 (21:08 +0200)
They are now handled in their own member to prevent mutating access to
the `src` member. This way, we can safely load external sources, while
keeping the mutation of local source strings off-limits.

src/librustc/ich/impls_syntax.rs
src/librustc_errors/lib.rs
src/libsyntax/codemap.rs
src/libsyntax_pos/lib.rs

index cba5ca148d0e617b6881966997589590e1a005c5..b9cc3b5fb937fe1927ecba066a1e2d5f4940b3dd 100644 (file)
@@ -337,6 +337,7 @@ fn hash_stable<W: StableHasherResult>(&self,
             // Do not hash the source as it is not encoded
             src: _,
             src_hash,
+            external_src: _,
             start_pos,
             end_pos: _,
             ref lines,
index 8d5e9e776ed23ec05d8e7e6f77b27dcc7b9a6961..545a485732e075698b0b19ac19fe0becb59039be 100644 (file)
@@ -103,6 +103,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(&mut self, file: FileName) -> bool;
 }
 
 impl CodeSuggestion {
index 442b92be1cb852a6e2dca36f830443e61fd99092..9779a6788453d1c95876862c5bb89f7e9a2506f0 100644 (file)
@@ -219,6 +219,7 @@ pub fn new_imported_filemap(&self,
             crate_of_origin: crate_of_origin,
             src: None,
             src_hash: src_hash,
+            external_src: RefCell::new(ExternalSource::AbsentOk),
             start_pos: start_pos,
             end_pos: end_pos,
             lines: RefCell::new(file_local_lines),
@@ -558,6 +559,25 @@ fn call_span_if_macro(&self, sp: Span) -> Span {
         }
         sp
     }
+    fn load_source_for_filemap(&mut 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 mut external_src = file_map.external_src.borrow_mut();
+            if let Ok(src) = self.file_loader.read_file(Path::new(&filename)) {
+                *external_src = ExternalSource::Present(src);
+                return true;
+            } else {
+                *external_src = ExternalSource::AbsentErr;
+            }
+        }
+
+        false
+    }
 }
 
 #[derive(Clone)]
index 75ce8b675f3d5b9bf8fb5147e00c8fec1e692ea6..d6adf45e68a2f8465ef112d19f2c359e5c6acfc8 100644 (file)
@@ -374,6 +374,14 @@ pub struct MultiByteChar {
     pub bytes: usize,
 }
 
+#[derive(PartialEq, Eq, Clone)]
+pub enum ExternalSource {
+    Present(String),
+    AbsentOk,
+    AbsentErr,
+    Unneeded,
+}
+
 /// A single source in the CodeMap.
 #[derive(Clone)]
 pub struct FileMap {
@@ -389,6 +397,9 @@ pub struct FileMap {
     pub src: Option<Rc<String>>,
     /// The source code's hash
     pub src_hash: u128,
+    /// The external source code (used for external crates, which will have a `None`
+    /// value as `self.src`.
+    pub external_src: RefCell<ExternalSource>,
     /// The start position of this source in the CodeMap
     pub start_pos: BytePos,
     /// The end position of this source in the CodeMap
@@ -513,6 +524,7 @@ fn decode<D: Decoder>(d: &mut D) -> Result<FileMap, D::Error> {
                 end_pos: end_pos,
                 src: None,
                 src_hash: src_hash,
+                external_src: RefCell::new(ExternalSource::AbsentOk),
                 lines: RefCell::new(lines),
                 multibyte_chars: RefCell::new(multibyte_chars)
             })
@@ -545,6 +557,7 @@ pub fn new(name: FileName,
             crate_of_origin: 0,
             src: Some(Rc::new(src)),
             src_hash: src_hash,
+            external_src: RefCell::new(ExternalSource::Unneeded),
             start_pos: start_pos,
             end_pos: Pos::from_usize(end_pos),
             lines: RefCell::new(Vec::new()),