From 6eda714718c8ab290a050f979e7503520ea27ea8 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 26 Mar 2023 09:26:42 -0700 Subject: [PATCH] Lazily construct PathBuf for sourcemap entries --- src/fallback.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/fallback.rs b/src/fallback.rs index b0ed7b06..e7224145 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -13,8 +13,6 @@ use core::mem::ManuallyDrop; use core::ops::RangeBounds; use core::ptr; use core::str::FromStr; -#[cfg(procmacro2_semver_exempt)] -use std::path::Path; use std::path::PathBuf; /// Force use of proc-macro2's fallback implementation of the API for now, even @@ -165,8 +163,7 @@ fn get_cursor(src: &str) -> Cursor { // Create a dummy file & add it to the source map SOURCE_MAP.with(|cm| { let mut cm = cm.borrow_mut(); - let name = format!("", cm.files.len()); - let span = cm.add_file(&name, src); + let span = cm.add_file(src); Cursor { rest: src, off: span.lo, @@ -340,8 +337,6 @@ thread_local! { // NOTE: We start with a single dummy file which all call_site() and // def_site() spans reference. files: vec![FileInfo { - #[cfg(procmacro2_semver_exempt)] - name: "".to_owned(), source_text: String::new(), span: Span { lo: 0, hi: 0 }, lines: vec![0], @@ -351,8 +346,6 @@ thread_local! { #[cfg(span_locations)] struct FileInfo { - #[cfg(procmacro2_semver_exempt)] - name: String, source_text: String, span: Span, lines: Vec, @@ -421,7 +414,7 @@ impl SourceMap { self.files.last().unwrap().span.hi + 1 } - fn add_file(&mut self, name: &str, src: &str) -> Span { + fn add_file(&mut self, src: &str) -> Span { let (len, lines) = lines_offsets(src); let lo = self.next_start_pos(); // XXX(nika): Should we bother doing a checked cast or checked add here? @@ -431,26 +424,35 @@ impl SourceMap { }; self.files.push(FileInfo { - #[cfg(procmacro2_semver_exempt)] - name: name.to_owned(), source_text: src.to_owned(), span, lines, }); - #[cfg(not(procmacro2_semver_exempt))] - let _ = name; - span } + #[cfg(procmacro2_semver_exempt)] + fn filepath(&self, span: Span) -> PathBuf { + for (i, file) in self.files.iter().enumerate() { + if file.span_within(span) { + return PathBuf::from(if i == 0 { + "".to_owned() + } else { + format!("", i) + }); + } + } + unreachable!("Invalid span with no related FileInfo!"); + } + fn fileinfo(&self, span: Span) -> &FileInfo { for file in &self.files { if file.span_within(span) { return file; } } - panic!("Invalid span with no related FileInfo!"); + unreachable!("Invalid span with no related FileInfo!"); } } @@ -498,10 +500,8 @@ impl Span { pub fn source_file(&self) -> SourceFile { SOURCE_MAP.with(|cm| { let cm = cm.borrow(); - let fi = cm.fileinfo(*self); - SourceFile { - path: Path::new(&fi.name).to_owned(), - } + let path = cm.filepath(*self); + SourceFile { path } }) }