diff options
| author | Inokentiy Babushkin <twk@twki.de> | 2017-06-10 21:08:32 +0200 |
|---|---|---|
| committer | Inokentiy Babushkin <twk@twki.de> | 2017-06-10 21:08:32 +0200 |
| commit | c2c31b2db33e0d0b5356a0c9e032269034cdc70a (patch) | |
| tree | 4a2ddeafa8e6ff5a359c2bd954831adfbb60663f | |
| parent | dd8f7cd126403955295c8b0cdbccc5ca5cbef763 (diff) | |
| download | rust-c2c31b2db33e0d0b5356a0c9e032269034cdc70a.tar.gz rust-c2c31b2db33e0d0b5356a0c9e032269034cdc70a.zip | |
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.
| -rw-r--r-- | src/librustc/ich/impls_syntax.rs | 1 | ||||
| -rw-r--r-- | src/librustc_errors/lib.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/codemap.rs | 20 | ||||
| -rw-r--r-- | src/libsyntax_pos/lib.rs | 13 |
4 files changed, 35 insertions, 0 deletions
diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index cba5ca148d0..b9cc3b5fb93 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -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, diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 8d5e9e776ed..545a485732e 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -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 { diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 442b92be1cb..9779a678845 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -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)] diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index 75ce8b675f3..d6adf45e68a 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -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()), |
