Added external crates' sources to FileMap.

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.
This commit is contained in:
Inokentiy Babushkin 2017-06-10 21:08:32 +02:00
parent dd8f7cd126
commit c2c31b2db3
No known key found for this signature in database
GPG Key ID: 7EFC8EC5224DE8EC
4 changed files with 35 additions and 0 deletions

View File

@ -337,6 +337,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for FileMa
// Do not hash the source as it is not encoded
src: _,
src_hash,
external_src: _,
start_pos,
end_pos: _,
ref lines,

View 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 {

View File

@ -219,6 +219,7 @@ impl CodeMap {
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 @@ impl CodeMapper for CodeMap {
}
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)]

View 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 @@ impl Decodable for FileMap {
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 @@ impl FileMap {
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()),