rustc_span/
source_map.rs

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