Skip to content

Commit 3d2cff0

Browse files
committed
Added source hashes to FileMap
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.
1 parent 70fa1fb commit 3d2cff0

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for FileMa
336336
crate_of_origin,
337337
// Do not hash the source as it is not encoded
338338
src: _,
339+
src_hash,
339340
start_pos,
340341
end_pos: _,
341342
ref lines,
@@ -350,6 +351,8 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for FileMa
350351
index: CRATE_DEF_INDEX,
351352
}.hash_stable(hcx, hasher);
352353

354+
src_hash.hash_stable(hcx, hasher);
355+
353356
// We only hash the relative position within this filemap
354357
let lines = lines.borrow();
355358
lines.len().hash_stable(hcx, hasher);

src/librustc_metadata/decoder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,7 @@ impl<'a, 'tcx> CrateMetadata {
11481148
// containing the information we need.
11491149
let syntax_pos::FileMap { name,
11501150
name_was_remapped,
1151+
src_hash,
11511152
start_pos,
11521153
end_pos,
11531154
lines,
@@ -1173,6 +1174,7 @@ impl<'a, 'tcx> CrateMetadata {
11731174
let local_version = local_codemap.new_imported_filemap(name,
11741175
name_was_remapped,
11751176
self.cnum.as_u32(),
1177+
src_hash,
11761178
source_length,
11771179
lines,
11781180
multibyte_chars);

src/libsyntax/codemap.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ use std::rc::Rc;
2727

2828
use std::env;
2929
use std::fs;
30+
use std::hash::Hasher;
3031
use std::io::{self, Read};
3132
use errors::CodeMapper;
3233

34+
use rustc_data_structures::stable_hasher::StableHasher;
35+
3336
/// Return the span itself if it doesn't come from a macro expansion,
3437
/// otherwise return the call site span up to the `enclosing_sp` by
3538
/// following the `expn_info` chain.
@@ -171,11 +174,16 @@ impl CodeMap {
171174

172175
let (filename, was_remapped) = self.path_mapping.map_prefix(filename);
173176

177+
let mut hasher: StableHasher<u128> = StableHasher::new();
178+
hasher.write(src.as_bytes());
179+
let src_hash = hasher.finish();
180+
174181
let filemap = Rc::new(FileMap {
175182
name: filename,
176183
name_was_remapped: was_remapped,
177184
crate_of_origin: 0,
178185
src: Some(Rc::new(src)),
186+
src_hash: src_hash,
179187
start_pos: Pos::from_usize(start_pos),
180188
end_pos: Pos::from_usize(end_pos),
181189
lines: RefCell::new(Vec::new()),
@@ -210,6 +218,7 @@ impl CodeMap {
210218
filename: FileName,
211219
name_was_remapped: bool,
212220
crate_of_origin: u32,
221+
src_hash: u128,
213222
source_len: usize,
214223
mut file_local_lines: Vec<BytePos>,
215224
mut file_local_multibyte_chars: Vec<MultiByteChar>)
@@ -233,6 +242,7 @@ impl CodeMap {
233242
name_was_remapped: name_was_remapped,
234243
crate_of_origin: crate_of_origin,
235244
src: None,
245+
src_hash: src_hash,
236246
start_pos: start_pos,
237247
end_pos: end_pos,
238248
lines: RefCell::new(file_local_lines),

src/libsyntax_pos/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#![feature(const_fn)]
2626
#![feature(custom_attribute)]
27+
#![feature(i128_type)]
2728
#![feature(optin_builtin_traits)]
2829
#![allow(unused_attributes)]
2930
#![feature(specialization)]
@@ -36,7 +37,6 @@ use std::cell::{Cell, RefCell};
3637
use std::ops::{Add, Sub};
3738
use std::rc::Rc;
3839
use std::cmp;
39-
4040
use std::fmt;
4141

4242
use serialize::{Encodable, Decodable, Encoder, Decoder};
@@ -382,6 +382,8 @@ pub struct FileMap {
382382
pub crate_of_origin: u32,
383383
/// The complete source code
384384
pub src: Option<Rc<String>>,
385+
/// The source code's hash
386+
pub src_hash: u128,
385387
/// The start position of this source in the CodeMap
386388
pub start_pos: BytePos,
387389
/// The end position of this source in the CodeMap
@@ -394,9 +396,10 @@ pub struct FileMap {
394396

395397
impl Encodable for FileMap {
396398
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
397-
s.emit_struct("FileMap", 6, |s| {
399+
s.emit_struct("FileMap", 7, |s| {
398400
s.emit_struct_field("name", 0, |s| self.name.encode(s))?;
399401
s.emit_struct_field("name_was_remapped", 1, |s| self.name_was_remapped.encode(s))?;
402+
s.emit_struct_field("src_hash", 6, |s| self.src_hash.encode(s))?;
400403
s.emit_struct_field("start_pos", 2, |s| self.start_pos.encode(s))?;
401404
s.emit_struct_field("end_pos", 3, |s| self.end_pos.encode(s))?;
402405
s.emit_struct_field("lines", 4, |s| {
@@ -459,7 +462,10 @@ impl Decodable for FileMap {
459462
let name: String = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
460463
let name_was_remapped: bool =
461464
d.read_struct_field("name_was_remapped", 1, |d| Decodable::decode(d))?;
462-
let start_pos: BytePos = d.read_struct_field("start_pos", 2, |d| Decodable::decode(d))?;
465+
let src_hash: u128 =
466+
d.read_struct_field("src_hash", 6, |d| Decodable::decode(d))?;
467+
let start_pos: BytePos =
468+
d.read_struct_field("start_pos", 2, |d| Decodable::decode(d))?;
463469
let end_pos: BytePos = d.read_struct_field("end_pos", 3, |d| Decodable::decode(d))?;
464470
let lines: Vec<BytePos> = d.read_struct_field("lines", 4, |d| {
465471
let num_lines: u32 = Decodable::decode(d)?;
@@ -501,6 +507,7 @@ impl Decodable for FileMap {
501507
start_pos: start_pos,
502508
end_pos: end_pos,
503509
src: None,
510+
src_hash: src_hash,
504511
lines: RefCell::new(lines),
505512
multibyte_chars: RefCell::new(multibyte_chars)
506513
})

0 commit comments

Comments
 (0)