]> git.lizzy.rs Git - rust.git/commitdiff
Added source hashes to FileMap
authorInokentiy Babushkin <twk@twki.de>
Sat, 10 Jun 2017 11:39:39 +0000 (13:39 +0200)
committerInokentiy Babushkin <twk@twki.de>
Sat, 10 Jun 2017 11:39:39 +0000 (13:39 +0200)
We can use these to perform lazy loading of source files belonging to
external crates. That way we will be able to show the source code of
external spans that have been translated.

src/librustc/ich/impls_syntax.rs
src/librustc_metadata/decoder.rs
src/libsyntax/codemap.rs
src/libsyntax_pos/lib.rs

index 995f797df2fbead6a1e2dfcac87eadcc267aa97e..cba5ca148d0e617b6881966997589590e1a005c5 100644 (file)
@@ -336,6 +336,7 @@ fn hash_stable<W: StableHasherResult>(&self,
             crate_of_origin,
             // Do not hash the source as it is not encoded
             src: _,
+            src_hash,
             start_pos,
             end_pos: _,
             ref lines,
@@ -350,6 +351,8 @@ fn hash_stable<W: StableHasherResult>(&self,
             index: CRATE_DEF_INDEX,
         }.hash_stable(hcx, hasher);
 
+        src_hash.hash_stable(hcx, hasher);
+
         // We only hash the relative position within this filemap
         let lines = lines.borrow();
         lines.len().hash_stable(hcx, hasher);
index d4baaa39d5d88f38f6e2f76cdf71259e955ba029..cd50fc4d5276f63836070f4e835b24df2e6b8ee3 100644 (file)
@@ -1148,6 +1148,7 @@ pub fn imported_filemaps(&'a self,
             // containing the information we need.
             let syntax_pos::FileMap { name,
                                       name_was_remapped,
+                                      src_hash,
                                       start_pos,
                                       end_pos,
                                       lines,
@@ -1173,6 +1174,7 @@ pub fn imported_filemaps(&'a self,
             let local_version = local_codemap.new_imported_filemap(name,
                                                                    name_was_remapped,
                                                                    self.cnum.as_u32(),
+                                                                   src_hash,
                                                                    source_length,
                                                                    lines,
                                                                    multibyte_chars);
index 830a457df748eeefaaf19b28f9623317d3798511..0935ec1b01c97c558ec94c674e00c8f23fc24da3 100644 (file)
 
 use std::env;
 use std::fs;
+use std::hash::Hasher;
 use std::io::{self, Read};
 use errors::CodeMapper;
 
+use rustc_data_structures::stable_hasher::StableHasher;
+
 /// Return the span itself if it doesn't come from a macro expansion,
 /// otherwise return the call site span up to the `enclosing_sp` by
 /// following the `expn_info` chain.
@@ -171,11 +174,16 @@ pub fn new_filemap(&self, filename: FileName, mut src: String) -> Rc<FileMap> {
 
         let (filename, was_remapped) = self.path_mapping.map_prefix(filename);
 
+        let mut hasher: StableHasher<u128> = StableHasher::new();
+        hasher.write(src.as_bytes());
+        let src_hash = hasher.finish();
+
         let filemap = Rc::new(FileMap {
             name: filename,
             name_was_remapped: was_remapped,
             crate_of_origin: 0,
             src: Some(Rc::new(src)),
+            src_hash: src_hash,
             start_pos: Pos::from_usize(start_pos),
             end_pos: Pos::from_usize(end_pos),
             lines: RefCell::new(Vec::new()),
@@ -210,6 +218,7 @@ pub fn new_imported_filemap(&self,
                                 filename: FileName,
                                 name_was_remapped: bool,
                                 crate_of_origin: u32,
+                                src_hash: u128,
                                 source_len: usize,
                                 mut file_local_lines: Vec<BytePos>,
                                 mut file_local_multibyte_chars: Vec<MultiByteChar>)
@@ -233,6 +242,7 @@ pub fn new_imported_filemap(&self,
             name_was_remapped: name_was_remapped,
             crate_of_origin: crate_of_origin,
             src: None,
+            src_hash: src_hash,
             start_pos: start_pos,
             end_pos: end_pos,
             lines: RefCell::new(file_local_lines),
index 25f74aeecf4046eed1ce357fb435ed246a002203..caea14971816241e47be616266498233416b489f 100644 (file)
@@ -24,6 +24,7 @@
 
 #![feature(const_fn)]
 #![feature(custom_attribute)]
+#![feature(i128_type)]
 #![feature(optin_builtin_traits)]
 #![allow(unused_attributes)]
 #![feature(specialization)]
@@ -36,7 +37,6 @@
 use std::ops::{Add, Sub};
 use std::rc::Rc;
 use std::cmp;
-
 use std::fmt;
 
 use serialize::{Encodable, Decodable, Encoder, Decoder};
@@ -382,6 +382,8 @@ pub struct FileMap {
     pub crate_of_origin: u32,
     /// The complete source code
     pub src: Option<Rc<String>>,
+    /// The source code's hash
+    pub src_hash: u128,
     /// The start position of this source in the CodeMap
     pub start_pos: BytePos,
     /// The end position of this source in the CodeMap
@@ -394,9 +396,10 @@ pub struct FileMap {
 
 impl Encodable for FileMap {
     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
-        s.emit_struct("FileMap", 6, |s| {
+        s.emit_struct("FileMap", 7, |s| {
             s.emit_struct_field("name", 0, |s| self.name.encode(s))?;
             s.emit_struct_field("name_was_remapped", 1, |s| self.name_was_remapped.encode(s))?;
+            s.emit_struct_field("src_hash", 6, |s| self.src_hash.encode(s))?;
             s.emit_struct_field("start_pos", 2, |s| self.start_pos.encode(s))?;
             s.emit_struct_field("end_pos", 3, |s| self.end_pos.encode(s))?;
             s.emit_struct_field("lines", 4, |s| {
@@ -459,7 +462,10 @@ fn decode<D: Decoder>(d: &mut D) -> Result<FileMap, D::Error> {
             let name: String = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
             let name_was_remapped: bool =
                 d.read_struct_field("name_was_remapped", 1, |d| Decodable::decode(d))?;
-            let start_pos: BytePos = d.read_struct_field("start_pos", 2, |d| Decodable::decode(d))?;
+            let src_hash: u128 =
+                d.read_struct_field("src_hash", 6, |d| Decodable::decode(d))?;
+            let start_pos: BytePos =
+                d.read_struct_field("start_pos", 2, |d| Decodable::decode(d))?;
             let end_pos: BytePos = d.read_struct_field("end_pos", 3, |d| Decodable::decode(d))?;
             let lines: Vec<BytePos> = d.read_struct_field("lines", 4, |d| {
                 let num_lines: u32 = Decodable::decode(d)?;
@@ -501,6 +507,7 @@ fn decode<D: Decoder>(d: &mut D) -> Result<FileMap, D::Error> {
                 start_pos: start_pos,
                 end_pos: end_pos,
                 src: None,
+                src_hash: src_hash,
                 lines: RefCell::new(lines),
                 multibyte_chars: RefCell::new(multibyte_chars)
             })