• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Types for tracking pieces of source code within a crate.
2 //!
3 //! The [`SourceMap`] tracks all the source code used within a single crate, mapping
4 //! from integer byte positions to the original source code location. Each bit
5 //! of source parsed during crate parsing (typically files, in-memory strings,
6 //! or various bits of macro expansion) cover a continuous range of bytes in the
7 //! `SourceMap` and are represented by [`SourceFile`]s. Byte positions are stored in
8 //! [`Span`] and used pervasively in the compiler. They are absolute positions
9 //! within the `SourceMap`, which upon request can be converted to line and column
10 //! information, source code snippets, etc.
11 
12 pub use crate::hygiene::{ExpnData, ExpnKind};
13 pub use crate::*;
14 
15 use rustc_data_structures::fx::FxHashMap;
16 use rustc_data_structures::stable_hasher::{Hash128, Hash64, StableHasher};
17 use rustc_data_structures::sync::{
18     AtomicU32, IntoDynSyncSend, Lrc, MappedReadGuard, ReadGuard, RwLock,
19 };
20 use std::cmp;
21 use std::hash::Hash;
22 use std::path::{self, Path, PathBuf};
23 use std::sync::atomic::Ordering;
24 
25 use std::fs;
26 use std::io;
27 
28 #[cfg(test)]
29 mod tests;
30 
31 /// Returns the span itself if it doesn't come from a macro expansion,
32 /// otherwise return the call site span up to the `enclosing_sp` by
33 /// following the `expn_data` chain.
original_sp(sp: Span, enclosing_sp: Span) -> Span34 pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span {
35     let expn_data1 = sp.ctxt().outer_expn_data();
36     let expn_data2 = enclosing_sp.ctxt().outer_expn_data();
37     if expn_data1.is_root() || !expn_data2.is_root() && expn_data1.call_site == expn_data2.call_site
38     {
39         sp
40     } else {
41         original_sp(expn_data1.call_site, enclosing_sp)
42     }
43 }
44 
45 pub mod monotonic {
46     use std::ops::{Deref, DerefMut};
47 
48     /// A `MonotonicVec` is a `Vec` which can only be grown.
49     /// Once inserted, an element can never be removed or swapped,
50     /// guaranteeing that any indices into a `MonotonicVec` are stable
51     // This is declared in its own module to ensure that the private
52     // field is inaccessible
53     pub struct MonotonicVec<T>(Vec<T>);
54     impl<T> MonotonicVec<T> {
new(val: Vec<T>) -> MonotonicVec<T>55         pub fn new(val: Vec<T>) -> MonotonicVec<T> {
56             MonotonicVec(val)
57         }
58 
push(&mut self, val: T)59         pub fn push(&mut self, val: T) {
60             self.0.push(val);
61         }
62     }
63 
64     impl<T> Default for MonotonicVec<T> {
default() -> Self65         fn default() -> Self {
66             MonotonicVec::new(vec![])
67         }
68     }
69 
70     impl<T> Deref for MonotonicVec<T> {
71         type Target = Vec<T>;
deref(&self) -> &Self::Target72         fn deref(&self) -> &Self::Target {
73             &self.0
74         }
75     }
76 
77     impl<T> !DerefMut for MonotonicVec<T> {}
78 }
79 
80 #[derive(Clone, Encodable, Decodable, Debug, Copy, HashStable_Generic)]
81 pub struct Spanned<T> {
82     pub node: T,
83     pub span: Span,
84 }
85 
respan<T>(sp: Span, t: T) -> Spanned<T>86 pub fn respan<T>(sp: Span, t: T) -> Spanned<T> {
87     Spanned { node: t, span: sp }
88 }
89 
dummy_spanned<T>(t: T) -> Spanned<T>90 pub fn dummy_spanned<T>(t: T) -> Spanned<T> {
91     respan(DUMMY_SP, t)
92 }
93 
94 // _____________________________________________________________________________
95 // SourceFile, MultiByteChar, FileName, FileLines
96 //
97 
98 /// An abstraction over the fs operations used by the Parser.
99 pub trait FileLoader {
100     /// Query the existence of a file.
file_exists(&self, path: &Path) -> bool101     fn file_exists(&self, path: &Path) -> bool;
102 
103     /// Read the contents of a UTF-8 file into memory.
read_file(&self, path: &Path) -> io::Result<String>104     fn read_file(&self, path: &Path) -> io::Result<String>;
105 
106     /// Read the contents of a potentially non-UTF-8 file into memory.
read_binary_file(&self, path: &Path) -> io::Result<Vec<u8>>107     fn read_binary_file(&self, path: &Path) -> io::Result<Vec<u8>>;
108 }
109 
110 /// A FileLoader that uses std::fs to load real files.
111 pub struct RealFileLoader;
112 
113 impl FileLoader for RealFileLoader {
file_exists(&self, path: &Path) -> bool114     fn file_exists(&self, path: &Path) -> bool {
115         path.exists()
116     }
117 
read_file(&self, path: &Path) -> io::Result<String>118     fn read_file(&self, path: &Path) -> io::Result<String> {
119         fs::read_to_string(path)
120     }
121 
read_binary_file(&self, path: &Path) -> io::Result<Vec<u8>>122     fn read_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
123         fs::read(path)
124     }
125 }
126 
127 /// This is a [SourceFile] identifier that is used to correlate source files between
128 /// subsequent compilation sessions (which is something we need to do during
129 /// incremental compilation).
130 ///
131 /// The [StableSourceFileId] also contains the CrateNum of the crate the source
132 /// file was originally parsed for. This way we get two separate entries in
133 /// the [SourceMap] if the same file is part of both the local and an upstream
134 /// crate. Trying to only have one entry for both cases is problematic because
135 /// at the point where we discover that there's a local use of the file in
136 /// addition to the upstream one, we might already have made decisions based on
137 /// the assumption that it's an upstream file. Treating the two files as
138 /// different has no real downsides.
139 #[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
140 pub struct StableSourceFileId {
141     /// A hash of the source file's [`FileName`]. This is hash so that it's size
142     /// is more predictable than if we included the actual [`FileName`] value.
143     pub file_name_hash: Hash64,
144 
145     /// The [`CrateNum`] of the crate this source file was originally parsed for.
146     /// We cannot include this information in the hash because at the time
147     /// of hashing we don't have the context to map from the [`CrateNum`]'s numeric
148     /// value to a `StableCrateId`.
149     pub cnum: CrateNum,
150 }
151 
152 // FIXME: we need a more globally consistent approach to the problem solved by
153 // StableSourceFileId, perhaps built atop source_file.name_hash.
154 impl StableSourceFileId {
new(source_file: &SourceFile) -> StableSourceFileId155     pub fn new(source_file: &SourceFile) -> StableSourceFileId {
156         StableSourceFileId::new_from_name(&source_file.name, source_file.cnum)
157     }
158 
new_from_name(name: &FileName, cnum: CrateNum) -> StableSourceFileId159     fn new_from_name(name: &FileName, cnum: CrateNum) -> StableSourceFileId {
160         let mut hasher = StableHasher::new();
161         name.hash(&mut hasher);
162         StableSourceFileId { file_name_hash: hasher.finish(), cnum }
163     }
164 }
165 
166 // _____________________________________________________________________________
167 // SourceMap
168 //
169 
170 #[derive(Default)]
171 pub(super) struct SourceMapFiles {
172     source_files: monotonic::MonotonicVec<Lrc<SourceFile>>,
173     stable_id_to_source_file: FxHashMap<StableSourceFileId, Lrc<SourceFile>>,
174 }
175 
176 pub struct SourceMap {
177     /// The address space below this value is currently used by the files in the source map.
178     used_address_space: AtomicU32,
179 
180     files: RwLock<SourceMapFiles>,
181     file_loader: IntoDynSyncSend<Box<dyn FileLoader + Sync + Send>>,
182     // This is used to apply the file path remapping as specified via
183     // `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`.
184     path_mapping: FilePathMapping,
185 
186     /// The algorithm used for hashing the contents of each source file.
187     hash_kind: SourceFileHashAlgorithm,
188 }
189 
190 impl SourceMap {
new(path_mapping: FilePathMapping) -> SourceMap191     pub fn new(path_mapping: FilePathMapping) -> SourceMap {
192         Self::with_file_loader_and_hash_kind(
193             Box::new(RealFileLoader),
194             path_mapping,
195             SourceFileHashAlgorithm::Md5,
196         )
197     }
198 
with_file_loader_and_hash_kind( file_loader: Box<dyn FileLoader + Sync + Send>, path_mapping: FilePathMapping, hash_kind: SourceFileHashAlgorithm, ) -> SourceMap199     pub fn with_file_loader_and_hash_kind(
200         file_loader: Box<dyn FileLoader + Sync + Send>,
201         path_mapping: FilePathMapping,
202         hash_kind: SourceFileHashAlgorithm,
203     ) -> SourceMap {
204         SourceMap {
205             used_address_space: AtomicU32::new(0),
206             files: Default::default(),
207             file_loader: IntoDynSyncSend(file_loader),
208             path_mapping,
209             hash_kind,
210         }
211     }
212 
path_mapping(&self) -> &FilePathMapping213     pub fn path_mapping(&self) -> &FilePathMapping {
214         &self.path_mapping
215     }
216 
file_exists(&self, path: &Path) -> bool217     pub fn file_exists(&self, path: &Path) -> bool {
218         self.file_loader.file_exists(path)
219     }
220 
load_file(&self, path: &Path) -> io::Result<Lrc<SourceFile>>221     pub fn load_file(&self, path: &Path) -> io::Result<Lrc<SourceFile>> {
222         let src = self.file_loader.read_file(path)?;
223         let filename = path.to_owned().into();
224         Ok(self.new_source_file(filename, src))
225     }
226 
227     /// Loads source file as a binary blob.
228     ///
229     /// Unlike `load_file`, guarantees that no normalization like BOM-removal
230     /// takes place.
load_binary_file(&self, path: &Path) -> io::Result<Vec<u8>>231     pub fn load_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
232         let bytes = self.file_loader.read_binary_file(path)?;
233 
234         // We need to add file to the `SourceMap`, so that it is present
235         // in dep-info. There's also an edge case that file might be both
236         // loaded as a binary via `include_bytes!` and as proper `SourceFile`
237         // via `mod`, so we try to use real file contents and not just an
238         // empty string.
239         let text = std::str::from_utf8(&bytes).unwrap_or("").to_string();
240         self.new_source_file(path.to_owned().into(), text);
241         Ok(bytes)
242     }
243 
244     // By returning a `MonotonicVec`, we ensure that consumers cannot invalidate
245     // any existing indices pointing into `files`.
files(&self) -> MappedReadGuard<'_, monotonic::MonotonicVec<Lrc<SourceFile>>>246     pub fn files(&self) -> MappedReadGuard<'_, monotonic::MonotonicVec<Lrc<SourceFile>>> {
247         ReadGuard::map(self.files.borrow(), |files| &files.source_files)
248     }
249 
source_file_by_stable_id( &self, stable_id: StableSourceFileId, ) -> Option<Lrc<SourceFile>>250     pub fn source_file_by_stable_id(
251         &self,
252         stable_id: StableSourceFileId,
253     ) -> Option<Lrc<SourceFile>> {
254         self.files.borrow().stable_id_to_source_file.get(&stable_id).cloned()
255     }
256 
allocate_address_space(&self, size: usize) -> Result<usize, OffsetOverflowError>257     fn allocate_address_space(&self, size: usize) -> Result<usize, OffsetOverflowError> {
258         let size = u32::try_from(size).map_err(|_| OffsetOverflowError)?;
259 
260         loop {
261             let current = self.used_address_space.load(Ordering::Relaxed);
262             let next = current
263                 .checked_add(size)
264                 // Add one so there is some space between files. This lets us distinguish
265                 // positions in the `SourceMap`, even in the presence of zero-length files.
266                 .and_then(|next| next.checked_add(1))
267                 .ok_or(OffsetOverflowError)?;
268 
269             if self
270                 .used_address_space
271                 .compare_exchange(current, next, Ordering::Relaxed, Ordering::Relaxed)
272                 .is_ok()
273             {
274                 return Ok(usize::try_from(current).unwrap());
275             }
276         }
277     }
278 
279     /// Creates a new `SourceFile`.
280     /// If a file already exists in the `SourceMap` with the same ID, that file is returned
281     /// unmodified.
new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile>282     pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
283         self.try_new_source_file(filename, src).unwrap_or_else(|OffsetOverflowError| {
284             eprintln!("fatal error: rustc does not support files larger than 4GB");
285             crate::fatal_error::FatalError.raise()
286         })
287     }
288 
try_new_source_file( &self, filename: FileName, src: String, ) -> Result<Lrc<SourceFile>, OffsetOverflowError>289     fn try_new_source_file(
290         &self,
291         filename: FileName,
292         src: String,
293     ) -> Result<Lrc<SourceFile>, OffsetOverflowError> {
294         // Note that filename may not be a valid path, eg it may be `<anon>` etc,
295         // but this is okay because the directory determined by `path.pop()` will
296         // be empty, so the working directory will be used.
297         let (filename, _) = self.path_mapping.map_filename_prefix(&filename);
298 
299         let file_id = StableSourceFileId::new_from_name(&filename, LOCAL_CRATE);
300 
301         let lrc_sf = match self.source_file_by_stable_id(file_id) {
302             Some(lrc_sf) => lrc_sf,
303             None => {
304                 let start_pos = self.allocate_address_space(src.len())?;
305 
306                 let source_file = Lrc::new(SourceFile::new(
307                     filename,
308                     src,
309                     Pos::from_usize(start_pos),
310                     self.hash_kind,
311                 ));
312 
313                 // Let's make sure the file_id we generated above actually matches
314                 // the ID we generate for the SourceFile we just created.
315                 debug_assert_eq!(StableSourceFileId::new(&source_file), file_id);
316 
317                 let mut files = self.files.borrow_mut();
318 
319                 files.source_files.push(source_file.clone());
320                 files.stable_id_to_source_file.insert(file_id, source_file.clone());
321 
322                 source_file
323             }
324         };
325         Ok(lrc_sf)
326     }
327 
328     /// Allocates a new `SourceFile` representing a source file from an external
329     /// crate. The source code of such an "imported `SourceFile`" is not available,
330     /// but we still know enough to generate accurate debuginfo location
331     /// information for things inlined from other crates.
new_imported_source_file( &self, filename: FileName, src_hash: SourceFileHash, name_hash: Hash128, source_len: usize, cnum: CrateNum, file_local_lines: Lock<SourceFileLines>, mut file_local_multibyte_chars: Vec<MultiByteChar>, mut file_local_non_narrow_chars: Vec<NonNarrowChar>, mut file_local_normalized_pos: Vec<NormalizedPos>, original_start_pos: BytePos, metadata_index: u32, ) -> Lrc<SourceFile>332     pub fn new_imported_source_file(
333         &self,
334         filename: FileName,
335         src_hash: SourceFileHash,
336         name_hash: Hash128,
337         source_len: usize,
338         cnum: CrateNum,
339         file_local_lines: Lock<SourceFileLines>,
340         mut file_local_multibyte_chars: Vec<MultiByteChar>,
341         mut file_local_non_narrow_chars: Vec<NonNarrowChar>,
342         mut file_local_normalized_pos: Vec<NormalizedPos>,
343         original_start_pos: BytePos,
344         metadata_index: u32,
345     ) -> Lrc<SourceFile> {
346         let start_pos = self
347             .allocate_address_space(source_len)
348             .expect("not enough address space for imported source file");
349 
350         let end_pos = Pos::from_usize(start_pos + source_len);
351         let start_pos = Pos::from_usize(start_pos);
352 
353         // Translate these positions into the new global frame of reference,
354         // now that the offset of the SourceFile is known.
355         //
356         // These are all unsigned values. `original_start_pos` may be larger or
357         // smaller than `start_pos`, but `pos` is always larger than both.
358         // Therefore, `(pos - original_start_pos) + start_pos` won't overflow
359         // but `start_pos - original_start_pos` might. So we use the former
360         // form rather than pre-computing the offset into a local variable. The
361         // compiler backend can optimize away the repeated computations in a
362         // way that won't trigger overflow checks.
363         match &mut *file_local_lines.borrow_mut() {
364             SourceFileLines::Lines(lines) => {
365                 for pos in lines {
366                     *pos = (*pos - original_start_pos) + start_pos;
367                 }
368             }
369             SourceFileLines::Diffs(SourceFileDiffs { line_start, .. }) => {
370                 *line_start = (*line_start - original_start_pos) + start_pos;
371             }
372         }
373         for mbc in &mut file_local_multibyte_chars {
374             mbc.pos = (mbc.pos - original_start_pos) + start_pos;
375         }
376         for swc in &mut file_local_non_narrow_chars {
377             *swc = (*swc - original_start_pos) + start_pos;
378         }
379         for nc in &mut file_local_normalized_pos {
380             nc.pos = (nc.pos - original_start_pos) + start_pos;
381         }
382 
383         let source_file = Lrc::new(SourceFile {
384             name: filename,
385             src: None,
386             src_hash,
387             external_src: Lock::new(ExternalSource::Foreign {
388                 kind: ExternalSourceKind::AbsentOk,
389                 metadata_index,
390             }),
391             start_pos,
392             end_pos,
393             lines: file_local_lines,
394             multibyte_chars: file_local_multibyte_chars,
395             non_narrow_chars: file_local_non_narrow_chars,
396             normalized_pos: file_local_normalized_pos,
397             name_hash,
398             cnum,
399         });
400 
401         let mut files = self.files.borrow_mut();
402 
403         files.source_files.push(source_file.clone());
404         files
405             .stable_id_to_source_file
406             .insert(StableSourceFileId::new(&source_file), source_file.clone());
407 
408         source_file
409     }
410 
411     /// If there is a doctest offset, applies it to the line.
doctest_offset_line(&self, file: &FileName, orig: usize) -> usize412     pub fn doctest_offset_line(&self, file: &FileName, orig: usize) -> usize {
413         match file {
414             FileName::DocTest(_, offset) => {
415                 if *offset < 0 {
416                     orig - (-(*offset)) as usize
417                 } else {
418                     orig + *offset as usize
419                 }
420             }
421             _ => orig,
422         }
423     }
424 
425     /// Return the SourceFile that contains the given `BytePos`
lookup_source_file(&self, pos: BytePos) -> Lrc<SourceFile>426     pub fn lookup_source_file(&self, pos: BytePos) -> Lrc<SourceFile> {
427         let idx = self.lookup_source_file_idx(pos);
428         (*self.files.borrow().source_files)[idx].clone()
429     }
430 
431     /// Looks up source information about a `BytePos`.
lookup_char_pos(&self, pos: BytePos) -> Loc432     pub fn lookup_char_pos(&self, pos: BytePos) -> Loc {
433         let sf = self.lookup_source_file(pos);
434         let (line, col, col_display) = sf.lookup_file_pos_with_col_display(pos);
435         Loc { file: sf, line, col, col_display }
436     }
437 
438     /// If the corresponding `SourceFile` is empty, does not return a line number.
lookup_line(&self, pos: BytePos) -> Result<SourceFileAndLine, Lrc<SourceFile>>439     pub fn lookup_line(&self, pos: BytePos) -> Result<SourceFileAndLine, Lrc<SourceFile>> {
440         let f = self.lookup_source_file(pos);
441 
442         match f.lookup_line(pos) {
443             Some(line) => Ok(SourceFileAndLine { sf: f, line }),
444             None => Err(f),
445         }
446     }
447 
span_to_string( &self, sp: Span, filename_display_pref: FileNameDisplayPreference, ) -> String448     pub fn span_to_string(
449         &self,
450         sp: Span,
451         filename_display_pref: FileNameDisplayPreference,
452     ) -> String {
453         let (source_file, lo_line, lo_col, hi_line, hi_col) = self.span_to_location_info(sp);
454 
455         let file_name = match source_file {
456             Some(sf) => sf.name.display(filename_display_pref).to_string(),
457             None => return "no-location".to_string(),
458         };
459 
460         format!(
461             "{file_name}:{lo_line}:{lo_col}{}",
462             if let FileNameDisplayPreference::Short = filename_display_pref {
463                 String::new()
464             } else {
465                 format!(": {hi_line}:{hi_col}")
466             }
467         )
468     }
469 
span_to_location_info( &self, sp: Span, ) -> (Option<Lrc<SourceFile>>, usize, usize, usize, usize)470     pub fn span_to_location_info(
471         &self,
472         sp: Span,
473     ) -> (Option<Lrc<SourceFile>>, usize, usize, usize, usize) {
474         if self.files.borrow().source_files.is_empty() || sp.is_dummy() {
475             return (None, 0, 0, 0, 0);
476         }
477 
478         let lo = self.lookup_char_pos(sp.lo());
479         let hi = self.lookup_char_pos(sp.hi());
480         (Some(lo.file), lo.line, lo.col.to_usize() + 1, hi.line, hi.col.to_usize() + 1)
481     }
482 
483     /// Format the span location suitable for embedding in build artifacts
span_to_embeddable_string(&self, sp: Span) -> String484     pub fn span_to_embeddable_string(&self, sp: Span) -> String {
485         self.span_to_string(sp, FileNameDisplayPreference::Remapped)
486     }
487 
488     /// Format the span location suitable for pretty printing annotations with relative line numbers
span_to_relative_line_string(&self, sp: Span, relative_to: Span) -> String489     pub fn span_to_relative_line_string(&self, sp: Span, relative_to: Span) -> String {
490         if self.files.borrow().source_files.is_empty() || sp.is_dummy() || relative_to.is_dummy() {
491             return "no-location".to_string();
492         }
493 
494         let lo = self.lookup_char_pos(sp.lo());
495         let hi = self.lookup_char_pos(sp.hi());
496         let offset = self.lookup_char_pos(relative_to.lo());
497 
498         if lo.file.name != offset.file.name || !relative_to.contains(sp) {
499             return self.span_to_embeddable_string(sp);
500         }
501 
502         let lo_line = lo.line.saturating_sub(offset.line);
503         let hi_line = hi.line.saturating_sub(offset.line);
504 
505         format!(
506             "{}:+{}:{}: +{}:{}",
507             lo.file.name.display(FileNameDisplayPreference::Remapped),
508             lo_line,
509             lo.col.to_usize() + 1,
510             hi_line,
511             hi.col.to_usize() + 1,
512         )
513     }
514 
515     /// Format the span location to be printed in diagnostics. Must not be emitted
516     /// to build artifacts as this may leak local file paths. Use span_to_embeddable_string
517     /// for string suitable for embedding.
span_to_diagnostic_string(&self, sp: Span) -> String518     pub fn span_to_diagnostic_string(&self, sp: Span) -> String {
519         self.span_to_string(sp, self.path_mapping.filename_display_for_diagnostics)
520     }
521 
span_to_filename(&self, sp: Span) -> FileName522     pub fn span_to_filename(&self, sp: Span) -> FileName {
523         self.lookup_char_pos(sp.lo()).file.name.clone()
524     }
525 
filename_for_diagnostics<'a>(&self, filename: &'a FileName) -> FileNameDisplay<'a>526     pub fn filename_for_diagnostics<'a>(&self, filename: &'a FileName) -> FileNameDisplay<'a> {
527         filename.display(self.path_mapping.filename_display_for_diagnostics)
528     }
529 
is_multiline(&self, sp: Span) -> bool530     pub fn is_multiline(&self, sp: Span) -> bool {
531         let lo = self.lookup_source_file_idx(sp.lo());
532         let hi = self.lookup_source_file_idx(sp.hi());
533         if lo != hi {
534             return true;
535         }
536         let f = (*self.files.borrow().source_files)[lo].clone();
537         f.lookup_line(sp.lo()) != f.lookup_line(sp.hi())
538     }
539 
540     #[instrument(skip(self), level = "trace")]
is_valid_span(&self, sp: Span) -> Result<(Loc, Loc), SpanLinesError>541     pub fn is_valid_span(&self, sp: Span) -> Result<(Loc, Loc), SpanLinesError> {
542         let lo = self.lookup_char_pos(sp.lo());
543         trace!(?lo);
544         let hi = self.lookup_char_pos(sp.hi());
545         trace!(?hi);
546         if lo.file.start_pos != hi.file.start_pos {
547             return Err(SpanLinesError::DistinctSources(Box::new(DistinctSources {
548                 begin: (lo.file.name.clone(), lo.file.start_pos),
549                 end: (hi.file.name.clone(), hi.file.start_pos),
550             })));
551         }
552         Ok((lo, hi))
553     }
554 
is_line_before_span_empty(&self, sp: Span) -> bool555     pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
556         match self.span_to_prev_source(sp) {
557             Ok(s) => s.rsplit_once('\n').unwrap_or(("", &s)).1.trim_start().is_empty(),
558             Err(_) => false,
559         }
560     }
561 
span_to_lines(&self, sp: Span) -> FileLinesResult562     pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
563         debug!("span_to_lines(sp={:?})", sp);
564         let (lo, hi) = self.is_valid_span(sp)?;
565         assert!(hi.line >= lo.line);
566 
567         if sp.is_dummy() {
568             return Ok(FileLines { file: lo.file, lines: Vec::new() });
569         }
570 
571         let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
572 
573         // The span starts partway through the first line,
574         // but after that it starts from offset 0.
575         let mut start_col = lo.col;
576 
577         // For every line but the last, it extends from `start_col`
578         // and to the end of the line. Be careful because the line
579         // numbers in Loc are 1-based, so we subtract 1 to get 0-based
580         // lines.
581         //
582         // FIXME: now that we handle DUMMY_SP up above, we should consider
583         // asserting that the line numbers here are all indeed 1-based.
584         let hi_line = hi.line.saturating_sub(1);
585         for line_index in lo.line.saturating_sub(1)..hi_line {
586             let line_len = lo.file.get_line(line_index).map_or(0, |s| s.chars().count());
587             lines.push(LineInfo { line_index, start_col, end_col: CharPos::from_usize(line_len) });
588             start_col = CharPos::from_usize(0);
589         }
590 
591         // For the last line, it extends from `start_col` to `hi.col`:
592         lines.push(LineInfo { line_index: hi_line, start_col, end_col: hi.col });
593 
594         Ok(FileLines { file: lo.file, lines })
595     }
596 
597     /// Extracts the source surrounding the given `Span` using the `extract_source` function. The
598     /// extract function takes three arguments: a string slice containing the source, an index in
599     /// the slice for the beginning of the span and an index in the slice for the end of the span.
span_to_source<F, T>(&self, sp: Span, extract_source: F) -> Result<T, SpanSnippetError> where F: Fn(&str, usize, usize) -> Result<T, SpanSnippetError>,600     fn span_to_source<F, T>(&self, sp: Span, extract_source: F) -> Result<T, SpanSnippetError>
601     where
602         F: Fn(&str, usize, usize) -> Result<T, SpanSnippetError>,
603     {
604         let local_begin = self.lookup_byte_offset(sp.lo());
605         let local_end = self.lookup_byte_offset(sp.hi());
606 
607         if local_begin.sf.start_pos != local_end.sf.start_pos {
608             Err(SpanSnippetError::DistinctSources(Box::new(DistinctSources {
609                 begin: (local_begin.sf.name.clone(), local_begin.sf.start_pos),
610                 end: (local_end.sf.name.clone(), local_end.sf.start_pos),
611             })))
612         } else {
613             self.ensure_source_file_source_present(local_begin.sf.clone());
614 
615             let start_index = local_begin.pos.to_usize();
616             let end_index = local_end.pos.to_usize();
617             let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
618 
619             if start_index > end_index || end_index > source_len {
620                 return Err(SpanSnippetError::MalformedForSourcemap(MalformedSourceMapPositions {
621                     name: local_begin.sf.name.clone(),
622                     source_len,
623                     begin_pos: local_begin.pos,
624                     end_pos: local_end.pos,
625                 }));
626             }
627 
628             if let Some(ref src) = local_begin.sf.src {
629                 extract_source(src, start_index, end_index)
630             } else if let Some(src) = local_begin.sf.external_src.borrow().get_source() {
631                 extract_source(src, start_index, end_index)
632             } else {
633                 Err(SpanSnippetError::SourceNotAvailable { filename: local_begin.sf.name.clone() })
634             }
635         }
636     }
637 
is_span_accessible(&self, sp: Span) -> bool638     pub fn is_span_accessible(&self, sp: Span) -> bool {
639         self.span_to_source(sp, |src, start_index, end_index| {
640             Ok(src.get(start_index..end_index).is_some())
641         })
642         .is_ok_and(|is_accessible| is_accessible)
643     }
644 
645     /// Returns the source snippet as `String` corresponding to the given `Span`.
span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError>646     pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
647         self.span_to_source(sp, |src, start_index, end_index| {
648             src.get(start_index..end_index)
649                 .map(|s| s.to_string())
650                 .ok_or(SpanSnippetError::IllFormedSpan(sp))
651         })
652     }
653 
span_to_margin(&self, sp: Span) -> Option<usize>654     pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
655         Some(self.indentation_before(sp)?.len())
656     }
657 
indentation_before(&self, sp: Span) -> Option<String>658     pub fn indentation_before(&self, sp: Span) -> Option<String> {
659         self.span_to_source(sp, |src, start_index, _| {
660             let before = &src[..start_index];
661             let last_line = before.rsplit_once('\n').map_or(before, |(_, last)| last);
662             Ok(last_line
663                 .split_once(|c: char| !c.is_whitespace())
664                 .map_or(last_line, |(indent, _)| indent)
665                 .to_string())
666         })
667         .ok()
668     }
669 
670     /// Returns the source snippet as `String` before the given `Span`.
span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError>671     pub fn span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
672         self.span_to_source(sp, |src, start_index, _| {
673             src.get(..start_index).map(|s| s.to_string()).ok_or(SpanSnippetError::IllFormedSpan(sp))
674         })
675     }
676 
677     /// Extends the given `Span` to just after the previous occurrence of `c`. Return the same span
678     /// if no character could be found or if an error occurred while retrieving the code snippet.
span_extend_to_prev_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span679     pub fn span_extend_to_prev_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
680         if let Ok(prev_source) = self.span_to_prev_source(sp) {
681             let prev_source = prev_source.rsplit(c).next().unwrap_or("");
682             if !prev_source.is_empty() && (accept_newlines || !prev_source.contains('\n')) {
683                 return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
684             }
685         }
686 
687         sp
688     }
689 
690     /// Extends the given `Span` to just after the previous occurrence of `pat` when surrounded by
691     /// whitespace. Returns None if the pattern could not be found or if an error occurred while
692     /// retrieving the code snippet.
span_extend_to_prev_str( &self, sp: Span, pat: &str, accept_newlines: bool, include_whitespace: bool, ) -> Option<Span>693     pub fn span_extend_to_prev_str(
694         &self,
695         sp: Span,
696         pat: &str,
697         accept_newlines: bool,
698         include_whitespace: bool,
699     ) -> Option<Span> {
700         // assure that the pattern is delimited, to avoid the following
701         //     fn my_fn()
702         //           ^^^^ returned span without the check
703         //     ---------- correct span
704         let prev_source = self.span_to_prev_source(sp).ok()?;
705         for ws in &[" ", "\t", "\n"] {
706             let pat = pat.to_owned() + ws;
707             if let Some(pat_pos) = prev_source.rfind(&pat) {
708                 let just_after_pat_pos = pat_pos + pat.len() - 1;
709                 let just_after_pat_plus_ws = if include_whitespace {
710                     just_after_pat_pos
711                         + prev_source[just_after_pat_pos..]
712                             .find(|c: char| !c.is_whitespace())
713                             .unwrap_or(0)
714                 } else {
715                     just_after_pat_pos
716                 };
717                 let len = prev_source.len() - just_after_pat_plus_ws;
718                 let prev_source = &prev_source[just_after_pat_plus_ws..];
719                 if accept_newlines || !prev_source.trim_start().contains('\n') {
720                     return Some(sp.with_lo(BytePos(sp.lo().0 - len as u32)));
721                 }
722             }
723         }
724 
725         None
726     }
727 
728     /// Returns the source snippet as `String` after the given `Span`.
span_to_next_source(&self, sp: Span) -> Result<String, SpanSnippetError>729     pub fn span_to_next_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
730         self.span_to_source(sp, |src, _, end_index| {
731             src.get(end_index..).map(|s| s.to_string()).ok_or(SpanSnippetError::IllFormedSpan(sp))
732         })
733     }
734 
735     /// Extends the given `Span` while the next character matches the predicate
span_extend_while( &self, span: Span, f: impl Fn(char) -> bool, ) -> Result<Span, SpanSnippetError>736     pub fn span_extend_while(
737         &self,
738         span: Span,
739         f: impl Fn(char) -> bool,
740     ) -> Result<Span, SpanSnippetError> {
741         self.span_to_source(span, |s, _start, end| {
742             let n = s[end..].char_indices().find(|&(_, c)| !f(c)).map_or(s.len() - end, |(i, _)| i);
743             Ok(span.with_hi(span.hi() + BytePos(n as u32)))
744         })
745     }
746 
747     /// Extends the given `Span` to previous character while the previous character matches the predicate
span_extend_prev_while( &self, span: Span, f: impl Fn(char) -> bool, ) -> Result<Span, SpanSnippetError>748     pub fn span_extend_prev_while(
749         &self,
750         span: Span,
751         f: impl Fn(char) -> bool,
752     ) -> Result<Span, SpanSnippetError> {
753         self.span_to_source(span, |s, start, _end| {
754             let n = s[..start]
755                 .char_indices()
756                 .rfind(|&(_, c)| !f(c))
757                 .map_or(start, |(i, _)| start - i - 1);
758             Ok(span.with_lo(span.lo() - BytePos(n as u32)))
759         })
760     }
761 
762     /// Extends the given `Span` to just before the next occurrence of `c`.
span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span763     pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
764         if let Ok(next_source) = self.span_to_next_source(sp) {
765             let next_source = next_source.split(c).next().unwrap_or("");
766             if !next_source.is_empty() && (accept_newlines || !next_source.contains('\n')) {
767                 return sp.with_hi(BytePos(sp.hi().0 + next_source.len() as u32));
768             }
769         }
770 
771         sp
772     }
773 
774     /// Extends the given `Span` to contain the entire line it is on.
span_extend_to_line(&self, sp: Span) -> Span775     pub fn span_extend_to_line(&self, sp: Span) -> Span {
776         self.span_extend_to_prev_char(self.span_extend_to_next_char(sp, '\n', true), '\n', true)
777     }
778 
779     /// Given a `Span`, tries to get a shorter span ending before the first occurrence of `char`
780     /// `c`.
span_until_char(&self, sp: Span, c: char) -> Span781     pub fn span_until_char(&self, sp: Span, c: char) -> Span {
782         match self.span_to_snippet(sp) {
783             Ok(snippet) => {
784                 let snippet = snippet.split(c).next().unwrap_or("").trim_end();
785                 if !snippet.is_empty() && !snippet.contains('\n') {
786                     sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32))
787                 } else {
788                     sp
789                 }
790             }
791             _ => sp,
792         }
793     }
794 
795     /// Given a 'Span', tries to tell if it's wrapped by "<>" or "()"
796     /// the algorithm searches if the next character is '>' or ')' after skipping white space
797     /// then searches the previous character to match '<' or '(' after skipping white space
798     /// return true if wrapped by '<>' or '()'
span_wrapped_by_angle_or_parentheses(&self, span: Span) -> bool799     pub fn span_wrapped_by_angle_or_parentheses(&self, span: Span) -> bool {
800         self.span_to_source(span, |src, start_index, end_index| {
801             if src.get(start_index..end_index).is_none() {
802                 return Ok(false);
803             }
804             // test the right side to match '>' after skipping white space
805             let end_src = &src[end_index..];
806             let mut i = 0;
807             let mut found_right_parentheses = false;
808             let mut found_right_angle = false;
809             while let Some(cc) = end_src.chars().nth(i) {
810                 if cc == ' ' {
811                     i = i + 1;
812                 } else if cc == '>' {
813                     // found > in the right;
814                     found_right_angle = true;
815                     break;
816                 } else if cc == ')' {
817                     found_right_parentheses = true;
818                     break;
819                 } else {
820                     // failed to find '>' return false immediately
821                     return Ok(false);
822                 }
823             }
824             // test the left side to match '<' after skipping white space
825             i = start_index;
826             let start_src = &src[0..start_index];
827             while let Some(cc) = start_src.chars().nth(i) {
828                 if cc == ' ' {
829                     if i == 0 {
830                         return Ok(false);
831                     }
832                     i = i - 1;
833                 } else if cc == '<' {
834                     // found < in the left
835                     if !found_right_angle {
836                         // skip something like "(< )>"
837                         return Ok(false);
838                     }
839                     break;
840                 } else if cc == '(' {
841                     if !found_right_parentheses {
842                         // skip something like "<(>)"
843                         return Ok(false);
844                     }
845                     break;
846                 } else {
847                     // failed to find '<' return false immediately
848                     return Ok(false);
849                 }
850             }
851             return Ok(true);
852         })
853         .is_ok_and(|is_accessible| is_accessible)
854     }
855 
856     /// Given a `Span`, tries to get a shorter span ending just after the first occurrence of `char`
857     /// `c`.
span_through_char(&self, sp: Span, c: char) -> Span858     pub fn span_through_char(&self, sp: Span, c: char) -> Span {
859         if let Ok(snippet) = self.span_to_snippet(sp) {
860             if let Some(offset) = snippet.find(c) {
861                 return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32));
862             }
863         }
864         sp
865     }
866 
867     /// Given a `Span`, gets a new `Span` covering the first token and all its trailing whitespace
868     /// or the original `Span`.
869     ///
870     /// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned.
span_until_non_whitespace(&self, sp: Span) -> Span871     pub fn span_until_non_whitespace(&self, sp: Span) -> Span {
872         let mut whitespace_found = false;
873 
874         self.span_take_while(sp, |c| {
875             if !whitespace_found && c.is_whitespace() {
876                 whitespace_found = true;
877             }
878 
879             !whitespace_found || c.is_whitespace()
880         })
881     }
882 
883     /// Given a `Span`, gets a new `Span` covering the first token without its trailing whitespace
884     /// or the original `Span` in case of error.
885     ///
886     /// If `sp` points to `"let mut x"`, then a span pointing at `"let"` will be returned.
span_until_whitespace(&self, sp: Span) -> Span887     pub fn span_until_whitespace(&self, sp: Span) -> Span {
888         self.span_take_while(sp, |c| !c.is_whitespace())
889     }
890 
891     /// Given a `Span`, gets a shorter one until `predicate` yields `false`.
span_take_while<P>(&self, sp: Span, predicate: P) -> Span where P: for<'r> FnMut(&'r char) -> bool,892     pub fn span_take_while<P>(&self, sp: Span, predicate: P) -> Span
893     where
894         P: for<'r> FnMut(&'r char) -> bool,
895     {
896         if let Ok(snippet) = self.span_to_snippet(sp) {
897             let offset = snippet.chars().take_while(predicate).map(|c| c.len_utf8()).sum::<usize>();
898 
899             sp.with_hi(BytePos(sp.lo().0 + (offset as u32)))
900         } else {
901             sp
902         }
903     }
904 
905     /// Given a `Span`, return a span ending in the closest `{`. This is useful when you have a
906     /// `Span` enclosing a whole item but we need to point at only the head (usually the first
907     /// line) of that item.
908     ///
909     /// *Only suitable for diagnostics.*
guess_head_span(&self, sp: Span) -> Span910     pub fn guess_head_span(&self, sp: Span) -> Span {
911         // FIXME: extend the AST items to have a head span, or replace callers with pointing at
912         // the item's ident when appropriate.
913         self.span_until_char(sp, '{')
914     }
915 
916     /// Returns a new span representing just the first character of the given span.
start_point(&self, sp: Span) -> Span917     pub fn start_point(&self, sp: Span) -> Span {
918         let width = {
919             let sp = sp.data();
920             let local_begin = self.lookup_byte_offset(sp.lo);
921             let start_index = local_begin.pos.to_usize();
922             let src = local_begin.sf.external_src.borrow();
923 
924             let snippet = if let Some(ref src) = local_begin.sf.src {
925                 Some(&src[start_index..])
926             } else {
927                 src.get_source().map(|src| &src[start_index..])
928             };
929 
930             match snippet {
931                 None => 1,
932                 Some(snippet) => match snippet.chars().next() {
933                     None => 1,
934                     Some(c) => c.len_utf8(),
935                 },
936             }
937         };
938 
939         sp.with_hi(BytePos(sp.lo().0 + width as u32))
940     }
941 
942     /// Returns a new span representing just the last character of this span.
end_point(&self, sp: Span) -> Span943     pub fn end_point(&self, sp: Span) -> Span {
944         let pos = sp.hi().0;
945 
946         let width = self.find_width_of_character_at_span(sp, false);
947         let corrected_end_position = pos.checked_sub(width).unwrap_or(pos);
948 
949         let end_point = BytePos(cmp::max(corrected_end_position, sp.lo().0));
950         sp.with_lo(end_point)
951     }
952 
953     /// Returns a new span representing the next character after the end-point of this span.
954     /// Special cases:
955     /// - if span is a dummy one, returns the same span
956     /// - if next_point reached the end of source, return a span exceeding the end of source,
957     ///   which means sm.span_to_snippet(next_point) will get `Err`
958     /// - respect multi-byte characters
next_point(&self, sp: Span) -> Span959     pub fn next_point(&self, sp: Span) -> Span {
960         if sp.is_dummy() {
961             return sp;
962         }
963         let start_of_next_point = sp.hi().0;
964 
965         let width = self.find_width_of_character_at_span(sp, true);
966         // If the width is 1, then the next span should only contain the next char besides current ending.
967         // However, in the case of a multibyte character, where the width != 1, the next span should
968         // span multiple bytes to include the whole character.
969         let end_of_next_point =
970             start_of_next_point.checked_add(width).unwrap_or(start_of_next_point);
971 
972         let end_of_next_point = BytePos(cmp::max(start_of_next_point + 1, end_of_next_point));
973         Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None)
974     }
975 
976     /// Returns a new span to check next none-whitespace character or some specified expected character
977     /// If `expect` is none, the first span of non-whitespace character is returned.
978     /// If `expect` presented, the first span of the character `expect` is returned
979     /// Otherwise, the span reached to limit is returned.
span_look_ahead(&self, span: Span, expect: Option<&str>, limit: Option<usize>) -> Span980     pub fn span_look_ahead(&self, span: Span, expect: Option<&str>, limit: Option<usize>) -> Span {
981         let mut sp = span;
982         for _ in 0..limit.unwrap_or(100_usize) {
983             sp = self.next_point(sp);
984             if let Ok(ref snippet) = self.span_to_snippet(sp) {
985                 if expect.is_some_and(|es| snippet == es) {
986                     break;
987                 }
988                 if expect.is_none() && snippet.chars().any(|c| !c.is_whitespace()) {
989                     break;
990                 }
991             }
992         }
993         sp
994     }
995 
996     /// Finds the width of the character, either before or after the end of provided span,
997     /// depending on the `forwards` parameter.
998     #[instrument(skip(self, sp))]
find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32999     fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
1000         let sp = sp.data();
1001 
1002         if sp.lo == sp.hi && !forwards {
1003             debug!("early return empty span");
1004             return 1;
1005         }
1006 
1007         let local_begin = self.lookup_byte_offset(sp.lo);
1008         let local_end = self.lookup_byte_offset(sp.hi);
1009         debug!("local_begin=`{:?}`, local_end=`{:?}`", local_begin, local_end);
1010 
1011         if local_begin.sf.start_pos != local_end.sf.start_pos {
1012             debug!("begin and end are in different files");
1013             return 1;
1014         }
1015 
1016         let start_index = local_begin.pos.to_usize();
1017         let end_index = local_end.pos.to_usize();
1018         debug!("start_index=`{:?}`, end_index=`{:?}`", start_index, end_index);
1019 
1020         // Disregard indexes that are at the start or end of their spans, they can't fit bigger
1021         // characters.
1022         if (!forwards && end_index == usize::MIN) || (forwards && start_index == usize::MAX) {
1023             debug!("start or end of span, cannot be multibyte");
1024             return 1;
1025         }
1026 
1027         let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
1028         debug!("source_len=`{:?}`", source_len);
1029         // Ensure indexes are also not malformed.
1030         if start_index > end_index || end_index > source_len - 1 {
1031             debug!("source indexes are malformed");
1032             return 1;
1033         }
1034 
1035         let src = local_begin.sf.external_src.borrow();
1036 
1037         let snippet = if let Some(src) = &local_begin.sf.src {
1038             src
1039         } else if let Some(src) = src.get_source() {
1040             src
1041         } else {
1042             return 1;
1043         };
1044 
1045         if forwards {
1046             (snippet.ceil_char_boundary(end_index + 1) - end_index) as u32
1047         } else {
1048             (end_index - snippet.floor_char_boundary(end_index - 1)) as u32
1049         }
1050     }
1051 
get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>>1052     pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
1053         // Remap filename before lookup
1054         let filename = self.path_mapping().map_filename_prefix(filename).0;
1055         for sf in self.files.borrow().source_files.iter() {
1056             if filename == sf.name {
1057                 return Some(sf.clone());
1058             }
1059         }
1060         None
1061     }
1062 
1063     /// For a global `BytePos`, computes the local offset within the containing `SourceFile`.
lookup_byte_offset(&self, bpos: BytePos) -> SourceFileAndBytePos1064     pub fn lookup_byte_offset(&self, bpos: BytePos) -> SourceFileAndBytePos {
1065         let idx = self.lookup_source_file_idx(bpos);
1066         let sf = (*self.files.borrow().source_files)[idx].clone();
1067         let offset = bpos - sf.start_pos;
1068         SourceFileAndBytePos { sf, pos: offset }
1069     }
1070 
1071     /// Returns the index of the [`SourceFile`] (in `self.files`) that contains `pos`.
1072     /// This index is guaranteed to be valid for the lifetime of this `SourceMap`,
1073     /// since `source_files` is a `MonotonicVec`
lookup_source_file_idx(&self, pos: BytePos) -> usize1074     pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
1075         self.files.borrow().source_files.partition_point(|x| x.start_pos <= pos) - 1
1076     }
1077 
count_lines(&self) -> usize1078     pub fn count_lines(&self) -> usize {
1079         self.files().iter().fold(0, |a, f| a + f.count_lines())
1080     }
1081 
ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool1082     pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool {
1083         source_file.add_external_src(|| {
1084             let FileName::Real(ref name) = source_file.name else {
1085                 return None;
1086             };
1087 
1088             let local_path: Cow<'_, Path> = match name {
1089                 RealFileName::LocalPath(local_path) => local_path.into(),
1090                 RealFileName::Remapped { local_path: Some(local_path), .. } => local_path.into(),
1091                 RealFileName::Remapped { local_path: None, virtual_name } => {
1092                     // The compiler produces better error messages if the sources of dependencies
1093                     // are available. Attempt to undo any path mapping so we can find remapped
1094                     // dependencies.
1095                     // We can only use the heuristic because `add_external_src` checks the file
1096                     // content hash.
1097                     self.path_mapping.reverse_map_prefix_heuristically(virtual_name)?.into()
1098                 }
1099             };
1100 
1101             self.file_loader.read_file(&local_path).ok()
1102         })
1103     }
1104 
is_imported(&self, sp: Span) -> bool1105     pub fn is_imported(&self, sp: Span) -> bool {
1106         let source_file_index = self.lookup_source_file_idx(sp.lo());
1107         let source_file = &self.files()[source_file_index];
1108         source_file.is_imported()
1109     }
1110 
1111     /// Gets the span of a statement. If the statement is a macro expansion, the
1112     /// span in the context of the block span is found. The trailing semicolon is included
1113     /// on a best-effort basis.
stmt_span(&self, stmt_span: Span, block_span: Span) -> Span1114     pub fn stmt_span(&self, stmt_span: Span, block_span: Span) -> Span {
1115         if !stmt_span.from_expansion() {
1116             return stmt_span;
1117         }
1118         let mac_call = original_sp(stmt_span, block_span);
1119         self.mac_call_stmt_semi_span(mac_call).map_or(mac_call, |s| mac_call.with_hi(s.hi()))
1120     }
1121 
1122     /// Tries to find the span of the semicolon of a macro call statement.
1123     /// The input must be the *call site* span of a statement from macro expansion.
1124     /// ```ignore (illustrative)
1125     /// //       v output
1126     ///    mac!();
1127     /// // ^^^^^^ input
1128     /// ```
mac_call_stmt_semi_span(&self, mac_call: Span) -> Option<Span>1129     pub fn mac_call_stmt_semi_span(&self, mac_call: Span) -> Option<Span> {
1130         let span = self.span_extend_while(mac_call, char::is_whitespace).ok()?;
1131         let span = span.shrink_to_hi().with_hi(BytePos(span.hi().0.checked_add(1)?));
1132         if self.span_to_snippet(span).as_deref() != Ok(";") {
1133             return None;
1134         }
1135         Some(span)
1136     }
1137 }
1138 
1139 #[derive(Clone)]
1140 pub struct FilePathMapping {
1141     mapping: Vec<(PathBuf, PathBuf)>,
1142     filename_display_for_diagnostics: FileNameDisplayPreference,
1143 }
1144 
1145 impl FilePathMapping {
empty() -> FilePathMapping1146     pub fn empty() -> FilePathMapping {
1147         FilePathMapping::new(Vec::new())
1148     }
1149 
new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping1150     pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping {
1151         let filename_display_for_diagnostics = if mapping.is_empty() {
1152             FileNameDisplayPreference::Local
1153         } else {
1154             FileNameDisplayPreference::Remapped
1155         };
1156 
1157         FilePathMapping { mapping, filename_display_for_diagnostics }
1158     }
1159 
1160     /// Applies any path prefix substitution as defined by the mapping.
1161     /// The return value is the remapped path and a boolean indicating whether
1162     /// the path was affected by the mapping.
map_prefix<'a>(&'a self, path: impl Into<Cow<'a, Path>>) -> (Cow<'a, Path>, bool)1163     pub fn map_prefix<'a>(&'a self, path: impl Into<Cow<'a, Path>>) -> (Cow<'a, Path>, bool) {
1164         let path = path.into();
1165         if path.as_os_str().is_empty() {
1166             // Exit early if the path is empty and therefore there's nothing to remap.
1167             // This is mostly to reduce spam for `RUSTC_LOG=[remap_path_prefix]`.
1168             return (path, false);
1169         }
1170 
1171         return remap_path_prefix(&self.mapping, path);
1172 
1173         #[instrument(level = "debug", skip(mapping), ret)]
1174         fn remap_path_prefix<'a>(
1175             mapping: &'a [(PathBuf, PathBuf)],
1176             path: Cow<'a, Path>,
1177         ) -> (Cow<'a, Path>, bool) {
1178             // NOTE: We are iterating over the mapping entries from last to first
1179             //       because entries specified later on the command line should
1180             //       take precedence.
1181             for (from, to) in mapping.iter().rev() {
1182                 debug!("Trying to apply {from:?} => {to:?}");
1183 
1184                 if let Ok(rest) = path.strip_prefix(from) {
1185                     let remapped = if rest.as_os_str().is_empty() {
1186                         // This is subtle, joining an empty path onto e.g. `foo/bar` will
1187                         // result in `foo/bar/`, that is, there'll be an additional directory
1188                         // separator at the end. This can lead to duplicated directory separators
1189                         // in remapped paths down the line.
1190                         // So, if we have an exact match, we just return that without a call
1191                         // to `Path::join()`.
1192                         to.into()
1193                     } else {
1194                         to.join(rest).into()
1195                     };
1196                     debug!("Match - remapped");
1197 
1198                     return (remapped, true);
1199                 } else {
1200                     debug!("No match - prefix {from:?} does not match");
1201                 }
1202             }
1203 
1204             debug!("not remapped");
1205             (path, false)
1206         }
1207     }
1208 
map_filename_prefix(&self, file: &FileName) -> (FileName, bool)1209     fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
1210         match file {
1211             FileName::Real(realfile) if let RealFileName::LocalPath(local_path) = realfile => {
1212                 let (mapped_path, mapped) = self.map_prefix(local_path);
1213                 let realfile = if mapped {
1214                     RealFileName::Remapped {
1215                         local_path: Some(local_path.clone()),
1216                         virtual_name: mapped_path.into_owned(),
1217                     }
1218                 } else {
1219                     realfile.clone()
1220                 };
1221                 (FileName::Real(realfile), mapped)
1222             }
1223             FileName::Real(_) => unreachable!("attempted to remap an already remapped filename"),
1224             other => (other.clone(), false),
1225         }
1226     }
1227 
1228     /// Expand a relative path to an absolute path with remapping taken into account.
1229     /// Use this when absolute paths are required (e.g. debuginfo or crate metadata).
1230     ///
1231     /// The resulting `RealFileName` will have its `local_path` portion erased if
1232     /// possible (i.e. if there's also a remapped path).
to_embeddable_absolute_path( &self, file_path: RealFileName, working_directory: &RealFileName, ) -> RealFileName1233     pub fn to_embeddable_absolute_path(
1234         &self,
1235         file_path: RealFileName,
1236         working_directory: &RealFileName,
1237     ) -> RealFileName {
1238         match file_path {
1239             // Anything that's already remapped we don't modify, except for erasing
1240             // the `local_path` portion.
1241             RealFileName::Remapped { local_path: _, virtual_name } => {
1242                 RealFileName::Remapped {
1243                     // We do not want any local path to be exported into metadata
1244                     local_path: None,
1245                     // We use the remapped name verbatim, even if it looks like a relative
1246                     // path. The assumption is that the user doesn't want us to further
1247                     // process paths that have gone through remapping.
1248                     virtual_name,
1249                 }
1250             }
1251 
1252             RealFileName::LocalPath(unmapped_file_path) => {
1253                 // If no remapping has been applied yet, try to do so
1254                 let (new_path, was_remapped) = self.map_prefix(unmapped_file_path);
1255                 if was_remapped {
1256                     // It was remapped, so don't modify further
1257                     return RealFileName::Remapped {
1258                         local_path: None,
1259                         virtual_name: new_path.into_owned(),
1260                     };
1261                 }
1262 
1263                 if new_path.is_absolute() {
1264                     // No remapping has applied to this path and it is absolute,
1265                     // so the working directory cannot influence it either, so
1266                     // we are done.
1267                     return RealFileName::LocalPath(new_path.into_owned());
1268                 }
1269 
1270                 debug_assert!(new_path.is_relative());
1271                 let unmapped_file_path_rel = new_path;
1272 
1273                 match working_directory {
1274                     RealFileName::LocalPath(unmapped_working_dir_abs) => {
1275                         let file_path_abs = unmapped_working_dir_abs.join(unmapped_file_path_rel);
1276 
1277                         // Although neither `working_directory` nor the file name were subject
1278                         // to path remapping, the concatenation between the two may be. Hence
1279                         // we need to do a remapping here.
1280                         let (file_path_abs, was_remapped) = self.map_prefix(file_path_abs);
1281                         if was_remapped {
1282                             RealFileName::Remapped {
1283                                 // Erase the actual path
1284                                 local_path: None,
1285                                 virtual_name: file_path_abs.into_owned(),
1286                             }
1287                         } else {
1288                             // No kind of remapping applied to this path, so
1289                             // we leave it as it is.
1290                             RealFileName::LocalPath(file_path_abs.into_owned())
1291                         }
1292                     }
1293                     RealFileName::Remapped {
1294                         local_path: _,
1295                         virtual_name: remapped_working_dir_abs,
1296                     } => {
1297                         // If working_directory has been remapped, then we emit
1298                         // Remapped variant as the expanded path won't be valid
1299                         RealFileName::Remapped {
1300                             local_path: None,
1301                             virtual_name: Path::new(remapped_working_dir_abs)
1302                                 .join(unmapped_file_path_rel),
1303                         }
1304                     }
1305                 }
1306             }
1307         }
1308     }
1309 
1310     /// Attempts to (heuristically) reverse a prefix mapping.
1311     ///
1312     /// Returns [`Some`] if there is exactly one mapping where the "to" part is
1313     /// a prefix of `path` and has at least one non-empty
1314     /// [`Normal`](path::Component::Normal) component. The component
1315     /// restriction exists to avoid reverse mapping overly generic paths like
1316     /// `/` or `.`).
1317     ///
1318     /// This is a heuristic and not guaranteed to return the actual original
1319     /// path! Do not rely on the result unless you have other means to verify
1320     /// that the mapping is correct (e.g. by checking the file content hash).
1321     #[instrument(level = "debug", skip(self), ret)]
reverse_map_prefix_heuristically(&self, path: &Path) -> Option<PathBuf>1322     fn reverse_map_prefix_heuristically(&self, path: &Path) -> Option<PathBuf> {
1323         let mut found = None;
1324 
1325         for (from, to) in self.mapping.iter() {
1326             let has_normal_component = to.components().any(|c| match c {
1327                 path::Component::Normal(s) => !s.is_empty(),
1328                 _ => false,
1329             });
1330 
1331             if !has_normal_component {
1332                 continue;
1333             }
1334 
1335             let Ok(rest) = path.strip_prefix(to) else {
1336                 continue;
1337             };
1338 
1339             if found.is_some() {
1340                 return None;
1341             }
1342 
1343             found = Some(from.join(rest));
1344         }
1345 
1346         found
1347     }
1348 }
1349