Skip to content

Commit c2c31b2

Browse files
committed
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.
1 parent dd8f7cd commit c2c31b2

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for FileMa
337337
// Do not hash the source as it is not encoded
338338
src: _,
339339
src_hash,
340+
external_src: _,
340341
start_pos,
341342
end_pos: _,
342343
ref lines,

src/librustc_errors/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub trait CodeMapper {
103103
fn span_to_filename(&self, sp: Span) -> FileName;
104104
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
105105
fn call_span_if_macro(&self, sp: Span) -> Span;
106+
fn load_source_for_filemap(&mut self, file: FileName) -> bool;
106107
}
107108

108109
impl CodeSuggestion {

src/libsyntax/codemap.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ impl CodeMap {
219219
crate_of_origin: crate_of_origin,
220220
src: None,
221221
src_hash: src_hash,
222+
external_src: RefCell::new(ExternalSource::AbsentOk),
222223
start_pos: start_pos,
223224
end_pos: end_pos,
224225
lines: RefCell::new(file_local_lines),
@@ -558,6 +559,25 @@ impl CodeMapper for CodeMap {
558559
}
559560
sp
560561
}
562+
fn load_source_for_filemap(&mut self, filename: FileName) -> bool {
563+
let file_map = if let Some(fm) = self.get_filemap(&filename) {
564+
fm
565+
} else {
566+
return false;
567+
};
568+
569+
if *file_map.external_src.borrow() == ExternalSource::AbsentOk {
570+
let mut external_src = file_map.external_src.borrow_mut();
571+
if let Ok(src) = self.file_loader.read_file(Path::new(&filename)) {
572+
*external_src = ExternalSource::Present(src);
573+
return true;
574+
} else {
575+
*external_src = ExternalSource::AbsentErr;
576+
}
577+
}
578+
579+
false
580+
}
561581
}
562582

563583
#[derive(Clone)]

src/libsyntax_pos/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,14 @@ pub struct MultiByteChar {
374374
pub bytes: usize,
375375
}
376376

377+
#[derive(PartialEq, Eq, Clone)]
378+
pub enum ExternalSource {
379+
Present(String),
380+
AbsentOk,
381+
AbsentErr,
382+
Unneeded,
383+
}
384+
377385
/// A single source in the CodeMap.
378386
#[derive(Clone)]
379387
pub struct FileMap {
@@ -389,6 +397,9 @@ pub struct FileMap {
389397
pub src: Option<Rc<String>>,
390398
/// The source code's hash
391399
pub src_hash: u128,
400+
/// The external source code (used for external crates, which will have a `None`
401+
/// value as `self.src`.
402+
pub external_src: RefCell<ExternalSource>,
392403
/// The start position of this source in the CodeMap
393404
pub start_pos: BytePos,
394405
/// The end position of this source in the CodeMap
@@ -513,6 +524,7 @@ impl Decodable for FileMap {
513524
end_pos: end_pos,
514525
src: None,
515526
src_hash: src_hash,
527+
external_src: RefCell::new(ExternalSource::AbsentOk),
516528
lines: RefCell::new(lines),
517529
multibyte_chars: RefCell::new(multibyte_chars)
518530
})
@@ -545,6 +557,7 @@ impl FileMap {
545557
crate_of_origin: 0,
546558
src: Some(Rc::new(src)),
547559
src_hash: src_hash,
560+
external_src: RefCell::new(ExternalSource::Unneeded),
548561
start_pos: start_pos,
549562
end_pos: Pos::from_usize(end_pos),
550563
lines: RefCell::new(Vec::new()),

0 commit comments

Comments
 (0)