Skip to content

Commit 6f6fe38

Browse files
committed
parse/lexer: support StringReader::retokenize called on external files.
1 parent f4c675c commit 6f6fe38

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/librustc_parse/lexer/mod.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,20 @@ impl<'a> StringReader<'a> {
4646
source_file: Lrc<rustc_span::SourceFile>,
4747
override_span: Option<Span>,
4848
) -> Self {
49-
if source_file.src.is_none() {
49+
// Make sure external source is loaded first, before accessing it.
50+
// While this can't show up during normal parsing, `retokenize` may
51+
// be called with a source file from an external crate.
52+
sess.source_map().ensure_source_file_source_present(source_file.clone());
53+
54+
// FIXME(eddyb) use `Lrc<str>` or similar to avoid cloning the `String`.
55+
let src = if let Some(src) = &source_file.src {
56+
src.clone()
57+
} else if let Some(src) = source_file.external_src.borrow().get_source() {
58+
src.clone()
59+
} else {
5060
sess.span_diagnostic
5161
.bug(&format!("cannot lex `source_file` without source: {}", source_file.name));
52-
}
53-
54-
let src = (*source_file.src.as_ref().unwrap()).clone();
62+
};
5563

5664
StringReader {
5765
sess,

src/librustc_span/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ pub enum ExternalSource {
856856
#[derive(PartialEq, Eq, Clone, Debug)]
857857
pub enum ExternalSourceKind {
858858
/// The external source has been loaded already.
859-
Present(String),
859+
Present(Lrc<String>),
860860
/// No attempt has been made to load the external source.
861861
AbsentOk,
862862
/// A failed attempt has been made to load the external source.
@@ -872,7 +872,7 @@ impl ExternalSource {
872872
}
873873
}
874874

875-
pub fn get_source(&self) -> Option<&str> {
875+
pub fn get_source(&self) -> Option<&Lrc<String>> {
876876
match self {
877877
ExternalSource::Foreign { kind: ExternalSourceKind::Present(ref src), .. } => Some(src),
878878
_ => None,
@@ -1138,7 +1138,7 @@ impl SourceFile {
11381138
hasher.write(src.as_bytes());
11391139

11401140
if hasher.finish::<u128>() == self.src_hash {
1141-
*src_kind = ExternalSourceKind::Present(src);
1141+
*src_kind = ExternalSourceKind::Present(Lrc::new(src));
11421142
return true;
11431143
}
11441144
} else {

0 commit comments

Comments
 (0)