Skip to content

Commit

Permalink
solution (3) rust-lang#42101
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed May 23, 2017
1 parent 0ed1ec9 commit 7343064
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 44 deletions.
2 changes: 2 additions & 0 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for FileMap {
let FileMap {
ref name,
name_was_remapped,
is_synthetic,
crate_of_origin,
// Do not hash the source as it is not encoded
src: _,
Expand All @@ -339,6 +340,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for FileMap {

name.hash_stable(hcx, hasher);
name_was_remapped.hash_stable(hcx, hasher);
is_synthetic.hash_stable(hcx, hasher);

DefId {
krate: CrateNum::from_u32(crate_of_origin),
Expand Down
35 changes: 19 additions & 16 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
pub use self::code_stats::{CodeStats, DataTypeKind, FieldInfo};
pub use self::code_stats::{SizeKind, TypeSizeInfo, VariantInfo};

use dep_graph::{DepGraph, DepNode};
use hir::def_id::{DefId, CrateNum, DefIndex, CRATE_DEF_INDEX};
// use dep_graph::{DepGraph, DepNode};
use dep_graph::{DepGraph};
// use hir::def_id::{DefId, CrateNum, DefIndex, CRATE_DEF_INDEX};
use hir::def_id::{CrateNum, DefIndex};
use lint;
use middle::cstore::CrateStore;
use middle::dependency_format;
Expand All @@ -32,7 +34,8 @@ use syntax::parse::ParseSess;
use syntax::symbol::Symbol;
use syntax::{ast, codemap};
use syntax::feature_gate::AttributeType;
use syntax_pos::{Span, MultiSpan, FileMap};
// use syntax_pos::{Span, MultiSpan, FileMap};
use syntax_pos::{Span, MultiSpan};

use rustc_back::{LinkerFlavor, PanicStrategy};
use rustc_back::target::Target;
Expand All @@ -46,7 +49,7 @@ use std::io::Write;
use std::rc::Rc;
use std::fmt;
use std::time::Duration;
use std::sync::Arc;
// use std::sync::Arc;

mod code_stats;
pub mod config;
Expand Down Expand Up @@ -628,18 +631,18 @@ pub fn build_session_(sopts: config::Options,

// Hook up the codemap with a callback that allows it to register FileMap
// accesses with the dependency graph.
let cm_depgraph = dep_graph.clone();
let codemap_dep_tracking_callback = Box::new(move |filemap: &FileMap| {
let def_id = DefId {
krate: CrateNum::from_u32(filemap.crate_of_origin),
index: CRATE_DEF_INDEX,
};
let name = Arc::new(filemap.name.clone());
let dep_node = DepNode::FileMap(def_id, name);

cm_depgraph.read(dep_node);
});
codemap.set_dep_tracking_callback(codemap_dep_tracking_callback);
// let cm_depgraph = dep_graph.clone();
// let codemap_dep_tracking_callback = Box::new(move |filemap: &FileMap| {
// let def_id = DefId {
// krate: CrateNum::from_u32(filemap.crate_of_origin),
// index: CRATE_DEF_INDEX,
// };
// let name = Arc::new(filemap.name.clone());
// let dep_node = DepNode::FileMap(def_id, name);

// cm_depgraph.read(dep_node);
// });
// codemap.set_dep_tracking_callback(codemap_dep_tracking_callback);

let p_s = parse::ParseSess::with_span_handler(span_diagnostic, codemap);
let default_sysroot = match sopts.maybe_sysroot {
Expand Down
24 changes: 24 additions & 0 deletions src/librustc_data_structures/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::mem;
use std::rc::Rc;
use std::sync::Arc;
use blake2b::Blake2bHasher;
use rustc_serialize::leb128;

Expand Down Expand Up @@ -330,6 +332,28 @@ impl<'a, T, CTX> HashStable<CTX> for &'a T
}
}

impl<T, CTX> HashStable<CTX> for Rc<T>
where T: HashStable<CTX>
{
#[inline]
fn hash_stable<W: StableHasherResult>(&self,
ctx: &mut CTX,
hasher: &mut StableHasher<W>) {
(**self).hash_stable(ctx, hasher);
}
}

impl<T, CTX> HashStable<CTX> for Arc<T>
where T: HashStable<CTX>
{
#[inline]
fn hash_stable<W: StableHasherResult>(&self,
ctx: &mut CTX,
hasher: &mut StableHasher<W>) {
(**self).hash_stable(ctx, hasher);
}
}

impl<T, CTX> HashStable<CTX> for ::std::mem::Discriminant<T> {
#[inline]
fn hash_stable<W: StableHasherResult>(&self,
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,7 @@ impl<'a, 'tcx> CrateMetadata {
// containing the information we need.
let syntax_pos::FileMap { name,
name_was_remapped,
is_synthetic,
start_pos,
end_pos,
lines,
Expand All @@ -1173,6 +1174,7 @@ impl<'a, 'tcx> CrateMetadata {

let local_version = local_codemap.new_imported_filemap(name,
name_was_remapped,
is_synthetic,
self.cnum.as_u32(),
source_length,
lines,
Expand Down
24 changes: 23 additions & 1 deletion src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// This path of this FileMap has been modified by
// path-remapping, so we use it verbatim (and avoid cloning
// the whole map in the process).
println!("remapped filemap {}", filemap.name);
filemap.clone()
} else if filemap.is_synthetic {
println!("synth filemap {}", filemap.name);
// This filemap does not correspond to a path, leave it as
// it is.
filemap.clone()
} else {
println!("normal filemap {}", filemap.name);

let mut adapted = (**filemap).clone();
let abs_path = Path::new(&working_dir).join(name)
.to_string_lossy()
Expand All @@ -318,12 +326,26 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
});

println!("HASHED FILEMAPS:");

let filemaps: Vec<_> = if self.compute_ich {
adapted.inspect(|filemap| {
let mut hasher = StableHasher::new();
filemap.hash_stable(hcx, &mut hasher);
let fingerprint = hasher.finish();
let dep_node = DepNode::FileMap((), Arc::new(filemap.name.clone()));
let dep_node = if filemap.is_synthetic {
let mut content_hasher = StableHasher::new();
filemap.src.hash_stable(hcx, &mut content_hasher);
let content_hash: Fingerprint = content_hasher.finish();
// Append a hash of the filemap contents to the name. TODO: Docuemnt more
let name = format!("{} ({})", filemap.name, content_hash);
DepNode::FileMap((), Arc::new(name))
} else {
DepNode::FileMap((), Arc::new(filemap.name.clone()))
};
println!(" - {:?}", dep_node);
assert!(self.metadata_hashes.global_hashes.iter().all(|&(ref d, _)| *d != dep_node));

self.metadata_hashes.global_hashes.push((dep_node, fingerprint));
}).collect()
} else {
Expand Down
65 changes: 48 additions & 17 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use std::fs;
use std::io::{self, Read};
use errors::CodeMapper;

use rustc_data_structures::fx::FxHashMap;

/// 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.
Expand Down Expand Up @@ -115,6 +117,8 @@ pub struct CodeMap {
// graph becomes available later during the compilation process, it is
// be replaced with something that notifies the dep-tracking system.
dep_tracking_callback: RefCell<Box<Fn(&FileMap)>>,

files_dedup: RefCell<FxHashMap<(FileName, String), Rc<FileMap>>>,
}

impl CodeMap {
Expand All @@ -124,6 +128,7 @@ impl CodeMap {
file_loader: Box::new(RealFileLoader),
path_mapping: path_mapping,
dep_tracking_callback: RefCell::new(Box::new(|_| {})),
files_dedup: RefCell::new(FxHashMap()),
}
}

Expand All @@ -135,6 +140,7 @@ impl CodeMap {
file_loader: file_loader,
path_mapping: path_mapping,
dep_tracking_callback: RefCell::new(Box::new(|_| {})),
files_dedup: RefCell::new(FxHashMap()),
}
}

Expand Down Expand Up @@ -181,30 +187,53 @@ impl CodeMap {
/// Creates a new filemap without setting its line information. If you don't
/// intend to set the line information yourself, you should use new_filemap_and_lines.
pub fn new_filemap(&self, filename: FileName, mut src: String) -> Rc<FileMap> {
let start_pos = self.next_start_pos();
let mut files = self.files.borrow_mut();

// Remove utf-8 BOM if any.
if src.starts_with("\u{feff}") {
src.drain(..3);
}

let end_pos = start_pos + src.len();

let is_synthetic = filename.starts_with("<") && filename.ends_with(">");
let (filename, was_remapped) = self.path_mapping.map_prefix(filename);

let filemap = Rc::new(FileMap {
name: filename,
name_was_remapped: was_remapped,
crate_of_origin: 0,
src: Some(Rc::new(src)),
start_pos: Pos::from_usize(start_pos),
end_pos: Pos::from_usize(end_pos),
lines: RefCell::new(Vec::new()),
multibyte_chars: RefCell::new(Vec::new()),
});

files.push(filemap.clone());
let mut new_filemap_allocated = false;

let filemap = self
.files_dedup
.borrow_mut()
.entry((filename.clone(), src.clone()))
.or_insert_with(|| {
new_filemap_allocated = true;

let start_pos = self.next_start_pos();
let end_pos = start_pos + src.len();

Rc::new(FileMap {
name: filename,
name_was_remapped: was_remapped,
is_synthetic,
crate_of_origin: 0,
src: Some(Rc::new(src)),
start_pos: Pos::from_usize(start_pos),
end_pos: Pos::from_usize(end_pos),
lines: RefCell::new(Vec::new()),
multibyte_chars: RefCell::new(Vec::new()),
})
})
.clone();
// let filemap = Rc::new(FileMap {
// name: filename,
// name_was_remapped: was_remapped,
// crate_of_origin: 0,
// src: Some(Rc::new(src)),
// start_pos: Pos::from_usize(start_pos),
// end_pos: Pos::from_usize(end_pos),
// lines: RefCell::new(Vec::new()),
// multibyte_chars: RefCell::new(Vec::new()),
// });

if new_filemap_allocated {
self.files.borrow_mut().push(filemap.clone());
}

filemap
}
Expand All @@ -231,6 +260,7 @@ impl CodeMap {
pub fn new_imported_filemap(&self,
filename: FileName,
name_was_remapped: bool,
is_synthetic: bool,
crate_of_origin: u32,
source_len: usize,
mut file_local_lines: Vec<BytePos>,
Expand All @@ -253,6 +283,7 @@ impl CodeMap {
let filemap = Rc::new(FileMap {
name: filename,
name_was_remapped: name_was_remapped,
is_synthetic: is_synthetic,
crate_of_origin: crate_of_origin,
src: None,
start_pos: start_pos,
Expand Down
33 changes: 23 additions & 10 deletions src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ pub struct FileMap {
pub name: FileName,
/// True if the `name` field above has been modified by -Zremap-path-prefix
pub name_was_remapped: bool,
/// TODO:
pub is_synthetic: bool,
/// Indicates which crate this FileMap was imported from.
pub crate_of_origin: u32,
/// The complete source code
Expand All @@ -397,9 +399,10 @@ impl Encodable for FileMap {
s.emit_struct("FileMap", 6, |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("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| {
s.emit_struct_field("is_synthetic", 2, |s| self.name_was_remapped.encode(s))?;
s.emit_struct_field("start_pos", 3, |s| self.start_pos.encode(s))?;
s.emit_struct_field("end_pos", 4, |s| self.end_pos.encode(s))?;
s.emit_struct_field("lines", 5, |s| {
let lines = self.lines.borrow();
// store the length
s.emit_u32(lines.len() as u32)?;
Expand Down Expand Up @@ -445,7 +448,7 @@ impl Encodable for FileMap {

Ok(())
})?;
s.emit_struct_field("multibyte_chars", 5, |s| {
s.emit_struct_field("multibyte_chars", 6, |s| {
(*self.multibyte_chars.borrow()).encode(s)
})
})
Expand All @@ -459,9 +462,11 @@ impl Decodable for FileMap {
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 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 is_synthetic: bool =
d.read_struct_field("is_synthetic", 2, |d| Decodable::decode(d))?;
let start_pos: BytePos = d.read_struct_field("start_pos", 3, |d| Decodable::decode(d))?;
let end_pos: BytePos = d.read_struct_field("end_pos", 4, |d| Decodable::decode(d))?;
let lines: Vec<BytePos> = d.read_struct_field("lines", 5, |d| {
let num_lines: u32 = Decodable::decode(d)?;
let mut lines = Vec::with_capacity(num_lines as usize);

Expand Down Expand Up @@ -490,10 +495,11 @@ impl Decodable for FileMap {
Ok(lines)
})?;
let multibyte_chars: Vec<MultiByteChar> =
d.read_struct_field("multibyte_chars", 5, |d| Decodable::decode(d))?;
d.read_struct_field("multibyte_chars", 6, |d| Decodable::decode(d))?;
Ok(FileMap {
name: name,
name_was_remapped: name_was_remapped,
is_synthetic: is_synthetic,
// `crate_of_origin` has to be set by the importer.
// This value matches up with rustc::hir::def_id::INVALID_CRATE.
// That constant is not available here unfortunately :(
Expand Down Expand Up @@ -528,8 +534,15 @@ impl FileMap {
// the new charpos must be > the last one (or it's the first one).
let mut lines = self.lines.borrow_mut();
let line_len = lines.len();
assert!(line_len == 0 || ((*lines)[line_len - 1] < pos));
lines.push(pos);
// assert!(line_len == 0 || ((*lines)[line_len - 1] < pos));
// lines.push(pos);

if line_len == 0 || ((*lines)[line_len - 1] < pos) {
lines.push(pos);
} else {
// assert!(self.is_synthetic); CHERCKTHIS
assert!(lines.binary_search(&pos).is_ok());
}
}

/// get a line from the list of pre-computed line-beginnings.
Expand Down

0 comments on commit 7343064

Please sign in to comment.