Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/librustc_parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ impl<'a> StringReader<'a> {
source_file: Lrc<rustc_span::SourceFile>,
override_span: Option<Span>,
) -> 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
.bug(&format!("cannot lex `source_file` without source: {}", source_file.name));
}

let src = (*source_file.src.as_ref().unwrap()).clone();
};

StringReader {
sess,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_span/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ pub enum ExternalSource {
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum ExternalSourceKind {
/// The external source has been loaded already.
Present(String),
Present(Lrc<String>),
/// No attempt has been made to load the external source.
AbsentOk,
/// A failed attempt has been made to load the external source.
Expand All @@ -872,7 +872,7 @@ impl ExternalSource {
}
}

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

if hasher.finish::<u128>() == self.src_hash {
*src_kind = ExternalSourceKind::Present(src);
*src_kind = ExternalSourceKind::Present(Lrc::new(src));
return true;
}
} else {
Expand Down