parse/lexer: support StringReader::retokenize called on external files.

This commit is contained in:
Eduard-Mihai Burtescu 2020-03-20 08:00:06 +02:00
parent f4c675c476
commit 6f6fe38b19
2 changed files with 15 additions and 7 deletions

View File

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

View File

@ -856,7 +856,7 @@ pub enum ExternalSource {
#[derive(PartialEq, Eq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
pub enum ExternalSourceKind { pub enum ExternalSourceKind {
/// The external source has been loaded already. /// The external source has been loaded already.
Present(String), Present(Lrc<String>),
/// No attempt has been made to load the external source. /// No attempt has been made to load the external source.
AbsentOk, AbsentOk,
/// A failed attempt has been made to load the external source. /// A failed attempt has been made to load the external source.
@ -872,7 +872,7 @@ impl ExternalSource {
} }
} }
pub fn get_source(&self) -> Option<&str> { pub fn get_source(&self) -> Option<&Lrc<String>> {
match self { match self {
ExternalSource::Foreign { kind: ExternalSourceKind::Present(ref src), .. } => Some(src), ExternalSource::Foreign { kind: ExternalSourceKind::Present(ref src), .. } => Some(src),
_ => None, _ => None,
@ -1138,7 +1138,7 @@ impl SourceFile {
hasher.write(src.as_bytes()); hasher.write(src.as_bytes());
if hasher.finish::<u128>() == self.src_hash { if hasher.finish::<u128>() == self.src_hash {
*src_kind = ExternalSourceKind::Present(src); *src_kind = ExternalSourceKind::Present(Lrc::new(src));
return true; return true;
} }
} else { } else {