]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_span/src/source_map.rs
fix most compiler/ doctests
[rust.git] / compiler / rustc_span / src / 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
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::StableHasher;
17 use rustc_data_structures::sync::{AtomicU32, Lrc, MappedReadGuard, ReadGuard, RwLock};
18 use std::hash::Hash;
19 use std::path::{Path, PathBuf};
20 use std::sync::atomic::Ordering;
21 use std::{clone::Clone, cmp};
22 use std::{convert::TryFrom, unreachable};
23
24 use std::fs;
25 use std::io;
26 use tracing::debug;
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.
34 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> {
55         pub fn new(val: Vec<T>) -> MonotonicVec<T> {
56             MonotonicVec(val)
57         }
58
59         pub fn push(&mut self, val: T) {
60             self.0.push(val);
61         }
62     }
63
64     impl<T> Default for MonotonicVec<T> {
65         fn default() -> Self {
66             MonotonicVec::new(vec![])
67         }
68     }
69
70     impl<T> Deref for MonotonicVec<T> {
71         type Target = Vec<T>;
72         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
86 pub fn respan<T>(sp: Span, t: T) -> Spanned<T> {
87     Spanned { node: t, span: sp }
88 }
89
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.
101     fn file_exists(&self, path: &Path) -> bool;
102
103     /// Read the contents of a UTF-8 file into memory.
104     fn read_file(&self, path: &Path) -> io::Result<String>;
105 }
106
107 /// A FileLoader that uses std::fs to load real files.
108 pub struct RealFileLoader;
109
110 impl FileLoader for RealFileLoader {
111     fn file_exists(&self, path: &Path) -> bool {
112         path.exists()
113     }
114
115     fn read_file(&self, path: &Path) -> io::Result<String> {
116         fs::read_to_string(path)
117     }
118 }
119
120 /// This is a [SourceFile] identifier that is used to correlate source files between
121 /// subsequent compilation sessions (which is something we need to do during
122 /// incremental compilation).
123 ///
124 /// The [StableSourceFileId] also contains the CrateNum of the crate the source
125 /// file was originally parsed for. This way we get two separate entries in
126 /// the [SourceMap] if the same file is part of both the local and an upstream
127 /// crate. Trying to only have one entry for both cases is problematic because
128 /// at the point where we discover that there's a local use of the file in
129 /// addition to the upstream one, we might already have made decisions based on
130 /// the assumption that it's an upstream file. Treating the two files as
131 /// different has no real downsides.
132 #[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
133 pub struct StableSourceFileId {
134     // A hash of the source file's FileName. This is hash so that it's size
135     // is more predictable than if we included the actual FileName value.
136     pub file_name_hash: u64,
137
138     // The CrateNum of the crate this source file was originally parsed for.
139     // We cannot include this information in the hash because at the time
140     // of hashing we don't have the context to map from the CrateNum's numeric
141     // value to a StableCrateId.
142     pub cnum: CrateNum,
143 }
144
145 // FIXME: we need a more globally consistent approach to the problem solved by
146 // StableSourceFileId, perhaps built atop source_file.name_hash.
147 impl StableSourceFileId {
148     pub fn new(source_file: &SourceFile) -> StableSourceFileId {
149         StableSourceFileId::new_from_name(&source_file.name, source_file.cnum)
150     }
151
152     fn new_from_name(name: &FileName, cnum: CrateNum) -> StableSourceFileId {
153         let mut hasher = StableHasher::new();
154         name.hash(&mut hasher);
155         StableSourceFileId { file_name_hash: hasher.finish(), cnum }
156     }
157 }
158
159 // _____________________________________________________________________________
160 // SourceMap
161 //
162
163 #[derive(Default)]
164 pub(super) struct SourceMapFiles {
165     source_files: monotonic::MonotonicVec<Lrc<SourceFile>>,
166     stable_id_to_source_file: FxHashMap<StableSourceFileId, Lrc<SourceFile>>,
167 }
168
169 pub struct SourceMap {
170     /// The address space below this value is currently used by the files in the source map.
171     used_address_space: AtomicU32,
172
173     files: RwLock<SourceMapFiles>,
174     file_loader: Box<dyn FileLoader + Sync + Send>,
175     // This is used to apply the file path remapping as specified via
176     // `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`.
177     path_mapping: FilePathMapping,
178
179     /// The algorithm used for hashing the contents of each source file.
180     hash_kind: SourceFileHashAlgorithm,
181 }
182
183 impl SourceMap {
184     pub fn new(path_mapping: FilePathMapping) -> SourceMap {
185         Self::with_file_loader_and_hash_kind(
186             Box::new(RealFileLoader),
187             path_mapping,
188             SourceFileHashAlgorithm::Md5,
189         )
190     }
191
192     pub fn with_file_loader_and_hash_kind(
193         file_loader: Box<dyn FileLoader + Sync + Send>,
194         path_mapping: FilePathMapping,
195         hash_kind: SourceFileHashAlgorithm,
196     ) -> SourceMap {
197         SourceMap {
198             used_address_space: AtomicU32::new(0),
199             files: Default::default(),
200             file_loader,
201             path_mapping,
202             hash_kind,
203         }
204     }
205
206     pub fn path_mapping(&self) -> &FilePathMapping {
207         &self.path_mapping
208     }
209
210     pub fn file_exists(&self, path: &Path) -> bool {
211         self.file_loader.file_exists(path)
212     }
213
214     pub fn load_file(&self, path: &Path) -> io::Result<Lrc<SourceFile>> {
215         let src = self.file_loader.read_file(path)?;
216         let filename = path.to_owned().into();
217         Ok(self.new_source_file(filename, src))
218     }
219
220     /// Loads source file as a binary blob.
221     ///
222     /// Unlike `load_file`, guarantees that no normalization like BOM-removal
223     /// takes place.
224     pub fn load_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
225         // Ideally, this should use `self.file_loader`, but it can't
226         // deal with binary files yet.
227         let bytes = fs::read(path)?;
228
229         // We need to add file to the `SourceMap`, so that it is present
230         // in dep-info. There's also an edge case that file might be both
231         // loaded as a binary via `include_bytes!` and as proper `SourceFile`
232         // via `mod`, so we try to use real file contents and not just an
233         // empty string.
234         let text = std::str::from_utf8(&bytes).unwrap_or("").to_string();
235         self.new_source_file(path.to_owned().into(), text);
236         Ok(bytes)
237     }
238
239     // By returning a `MonotonicVec`, we ensure that consumers cannot invalidate
240     // any existing indices pointing into `files`.
241     pub fn files(&self) -> MappedReadGuard<'_, monotonic::MonotonicVec<Lrc<SourceFile>>> {
242         ReadGuard::map(self.files.borrow(), |files| &files.source_files)
243     }
244
245     pub fn source_file_by_stable_id(
246         &self,
247         stable_id: StableSourceFileId,
248     ) -> Option<Lrc<SourceFile>> {
249         self.files.borrow().stable_id_to_source_file.get(&stable_id).cloned()
250     }
251
252     fn allocate_address_space(&self, size: usize) -> Result<usize, OffsetOverflowError> {
253         let size = u32::try_from(size).map_err(|_| OffsetOverflowError)?;
254
255         loop {
256             let current = self.used_address_space.load(Ordering::Relaxed);
257             let next = current
258                 .checked_add(size)
259                 // Add one so there is some space between files. This lets us distinguish
260                 // positions in the `SourceMap`, even in the presence of zero-length files.
261                 .and_then(|next| next.checked_add(1))
262                 .ok_or(OffsetOverflowError)?;
263
264             if self
265                 .used_address_space
266                 .compare_exchange(current, next, Ordering::Relaxed, Ordering::Relaxed)
267                 .is_ok()
268             {
269                 return Ok(usize::try_from(current).unwrap());
270             }
271         }
272     }
273
274     /// Creates a new `SourceFile`.
275     /// If a file already exists in the `SourceMap` with the same ID, that file is returned
276     /// unmodified.
277     pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
278         self.try_new_source_file(filename, src).unwrap_or_else(|OffsetOverflowError| {
279             eprintln!("fatal error: rustc does not support files larger than 4GB");
280             crate::fatal_error::FatalError.raise()
281         })
282     }
283
284     fn try_new_source_file(
285         &self,
286         filename: FileName,
287         src: String,
288     ) -> Result<Lrc<SourceFile>, OffsetOverflowError> {
289         // Note that filename may not be a valid path, eg it may be `<anon>` etc,
290         // but this is okay because the directory determined by `path.pop()` will
291         // be empty, so the working directory will be used.
292         let (filename, _) = self.path_mapping.map_filename_prefix(&filename);
293
294         let file_id = StableSourceFileId::new_from_name(&filename, LOCAL_CRATE);
295
296         let lrc_sf = match self.source_file_by_stable_id(file_id) {
297             Some(lrc_sf) => lrc_sf,
298             None => {
299                 let start_pos = self.allocate_address_space(src.len())?;
300
301                 let source_file = Lrc::new(SourceFile::new(
302                     filename,
303                     src,
304                     Pos::from_usize(start_pos),
305                     self.hash_kind,
306                 ));
307
308                 // Let's make sure the file_id we generated above actually matches
309                 // the ID we generate for the SourceFile we just created.
310                 debug_assert_eq!(StableSourceFileId::new(&source_file), file_id);
311
312                 let mut files = self.files.borrow_mut();
313
314                 files.source_files.push(source_file.clone());
315                 files.stable_id_to_source_file.insert(file_id, source_file.clone());
316
317                 source_file
318             }
319         };
320         Ok(lrc_sf)
321     }
322
323     /// Allocates a new `SourceFile` representing a source file from an external
324     /// crate. The source code of such an "imported `SourceFile`" is not available,
325     /// but we still know enough to generate accurate debuginfo location
326     /// information for things inlined from other crates.
327     pub fn new_imported_source_file(
328         &self,
329         filename: FileName,
330         src_hash: SourceFileHash,
331         name_hash: u128,
332         source_len: usize,
333         cnum: CrateNum,
334         mut file_local_lines: Vec<BytePos>,
335         mut file_local_multibyte_chars: Vec<MultiByteChar>,
336         mut file_local_non_narrow_chars: Vec<NonNarrowChar>,
337         mut file_local_normalized_pos: Vec<NormalizedPos>,
338         original_start_pos: BytePos,
339         original_end_pos: BytePos,
340     ) -> Lrc<SourceFile> {
341         let start_pos = self
342             .allocate_address_space(source_len)
343             .expect("not enough address space for imported source file");
344
345         let end_pos = Pos::from_usize(start_pos + source_len);
346         let start_pos = Pos::from_usize(start_pos);
347
348         for pos in &mut file_local_lines {
349             *pos = *pos + start_pos;
350         }
351
352         for mbc in &mut file_local_multibyte_chars {
353             mbc.pos = mbc.pos + start_pos;
354         }
355
356         for swc in &mut file_local_non_narrow_chars {
357             *swc = *swc + start_pos;
358         }
359
360         for nc in &mut file_local_normalized_pos {
361             nc.pos = nc.pos + start_pos;
362         }
363
364         let source_file = Lrc::new(SourceFile {
365             name: filename,
366             src: None,
367             src_hash,
368             external_src: Lock::new(ExternalSource::Foreign {
369                 kind: ExternalSourceKind::AbsentOk,
370                 original_start_pos,
371                 original_end_pos,
372             }),
373             start_pos,
374             end_pos,
375             lines: file_local_lines,
376             multibyte_chars: file_local_multibyte_chars,
377             non_narrow_chars: file_local_non_narrow_chars,
378             normalized_pos: file_local_normalized_pos,
379             name_hash,
380             cnum,
381         });
382
383         let mut files = self.files.borrow_mut();
384
385         files.source_files.push(source_file.clone());
386         files
387             .stable_id_to_source_file
388             .insert(StableSourceFileId::new(&source_file), source_file.clone());
389
390         source_file
391     }
392
393     // If there is a doctest offset, applies it to the line.
394     pub fn doctest_offset_line(&self, file: &FileName, orig: usize) -> usize {
395         match file {
396             FileName::DocTest(_, offset) => {
397                 if *offset < 0 {
398                     orig - (-(*offset)) as usize
399                 } else {
400                     orig + *offset as usize
401                 }
402             }
403             _ => orig,
404         }
405     }
406
407     /// Return the SourceFile that contains the given `BytePos`
408     pub fn lookup_source_file(&self, pos: BytePos) -> Lrc<SourceFile> {
409         let idx = self.lookup_source_file_idx(pos);
410         (*self.files.borrow().source_files)[idx].clone()
411     }
412
413     /// Looks up source information about a `BytePos`.
414     pub fn lookup_char_pos(&self, pos: BytePos) -> Loc {
415         let sf = self.lookup_source_file(pos);
416         let (line, col, col_display) = sf.lookup_file_pos_with_col_display(pos);
417         Loc { file: sf, line, col, col_display }
418     }
419
420     // If the corresponding `SourceFile` is empty, does not return a line number.
421     pub fn lookup_line(&self, pos: BytePos) -> Result<SourceFileAndLine, Lrc<SourceFile>> {
422         let f = self.lookup_source_file(pos);
423
424         match f.lookup_line(pos) {
425             Some(line) => Ok(SourceFileAndLine { sf: f, line }),
426             None => Err(f),
427         }
428     }
429
430     fn span_to_string(&self, sp: Span, filename_display_pref: FileNameDisplayPreference) -> String {
431         if self.files.borrow().source_files.is_empty() || sp.is_dummy() {
432             return "no-location".to_string();
433         }
434
435         let lo = self.lookup_char_pos(sp.lo());
436         let hi = self.lookup_char_pos(sp.hi());
437         format!(
438             "{}:{}:{}: {}:{}",
439             lo.file.name.display(filename_display_pref),
440             lo.line,
441             lo.col.to_usize() + 1,
442             hi.line,
443             hi.col.to_usize() + 1,
444         )
445     }
446
447     /// Format the span location suitable for embedding in build artifacts
448     pub fn span_to_embeddable_string(&self, sp: Span) -> String {
449         self.span_to_string(sp, FileNameDisplayPreference::Remapped)
450     }
451
452     /// Format the span location to be printed in diagnostics. Must not be emitted
453     /// to build artifacts as this may leak local file paths. Use span_to_embeddable_string
454     /// for string suitable for embedding.
455     pub fn span_to_diagnostic_string(&self, sp: Span) -> String {
456         self.span_to_string(sp, self.path_mapping.filename_display_for_diagnostics)
457     }
458
459     pub fn span_to_filename(&self, sp: Span) -> FileName {
460         self.lookup_char_pos(sp.lo()).file.name.clone()
461     }
462
463     pub fn filename_for_diagnostics<'a>(&self, filename: &'a FileName) -> FileNameDisplay<'a> {
464         filename.display(self.path_mapping.filename_display_for_diagnostics)
465     }
466
467     pub fn is_multiline(&self, sp: Span) -> bool {
468         let lo = self.lookup_source_file_idx(sp.lo());
469         let hi = self.lookup_source_file_idx(sp.hi());
470         if lo != hi {
471             return true;
472         }
473         let f = (*self.files.borrow().source_files)[lo].clone();
474         f.lookup_line(sp.lo()) != f.lookup_line(sp.hi())
475     }
476
477     #[instrument(skip(self), level = "trace")]
478     pub fn is_valid_span(&self, sp: Span) -> Result<(Loc, Loc), SpanLinesError> {
479         let lo = self.lookup_char_pos(sp.lo());
480         trace!(?lo);
481         let hi = self.lookup_char_pos(sp.hi());
482         trace!(?hi);
483         if lo.file.start_pos != hi.file.start_pos {
484             return Err(SpanLinesError::DistinctSources(DistinctSources {
485                 begin: (lo.file.name.clone(), lo.file.start_pos),
486                 end: (hi.file.name.clone(), hi.file.start_pos),
487             }));
488         }
489         Ok((lo, hi))
490     }
491
492     pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
493         match self.span_to_prev_source(sp) {
494             Ok(s) => s.rsplit_once('\n').unwrap_or(("", &s)).1.trim_start().is_empty(),
495             Err(_) => false,
496         }
497     }
498
499     pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
500         debug!("span_to_lines(sp={:?})", sp);
501         let (lo, hi) = self.is_valid_span(sp)?;
502         assert!(hi.line >= lo.line);
503
504         if sp.is_dummy() {
505             return Ok(FileLines { file: lo.file, lines: Vec::new() });
506         }
507
508         let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
509
510         // The span starts partway through the first line,
511         // but after that it starts from offset 0.
512         let mut start_col = lo.col;
513
514         // For every line but the last, it extends from `start_col`
515         // and to the end of the line. Be careful because the line
516         // numbers in Loc are 1-based, so we subtract 1 to get 0-based
517         // lines.
518         //
519         // FIXME: now that we handle DUMMY_SP up above, we should consider
520         // asserting that the line numbers here are all indeed 1-based.
521         let hi_line = hi.line.saturating_sub(1);
522         for line_index in lo.line.saturating_sub(1)..hi_line {
523             let line_len = lo.file.get_line(line_index).map_or(0, |s| s.chars().count());
524             lines.push(LineInfo { line_index, start_col, end_col: CharPos::from_usize(line_len) });
525             start_col = CharPos::from_usize(0);
526         }
527
528         // For the last line, it extends from `start_col` to `hi.col`:
529         lines.push(LineInfo { line_index: hi_line, start_col, end_col: hi.col });
530
531         Ok(FileLines { file: lo.file, lines })
532     }
533
534     /// Extracts the source surrounding the given `Span` using the `extract_source` function. The
535     /// extract function takes three arguments: a string slice containing the source, an index in
536     /// the slice for the beginning of the span and an index in the slice for the end of the span.
537     fn span_to_source<F, T>(&self, sp: Span, extract_source: F) -> Result<T, SpanSnippetError>
538     where
539         F: Fn(&str, usize, usize) -> Result<T, SpanSnippetError>,
540     {
541         let local_begin = self.lookup_byte_offset(sp.lo());
542         let local_end = self.lookup_byte_offset(sp.hi());
543
544         if local_begin.sf.start_pos != local_end.sf.start_pos {
545             Err(SpanSnippetError::DistinctSources(DistinctSources {
546                 begin: (local_begin.sf.name.clone(), local_begin.sf.start_pos),
547                 end: (local_end.sf.name.clone(), local_end.sf.start_pos),
548             }))
549         } else {
550             self.ensure_source_file_source_present(local_begin.sf.clone());
551
552             let start_index = local_begin.pos.to_usize();
553             let end_index = local_end.pos.to_usize();
554             let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
555
556             if start_index > end_index || end_index > source_len {
557                 return Err(SpanSnippetError::MalformedForSourcemap(MalformedSourceMapPositions {
558                     name: local_begin.sf.name.clone(),
559                     source_len,
560                     begin_pos: local_begin.pos,
561                     end_pos: local_end.pos,
562                 }));
563             }
564
565             if let Some(ref src) = local_begin.sf.src {
566                 extract_source(src, start_index, end_index)
567             } else if let Some(src) = local_begin.sf.external_src.borrow().get_source() {
568                 extract_source(src, start_index, end_index)
569             } else {
570                 Err(SpanSnippetError::SourceNotAvailable { filename: local_begin.sf.name.clone() })
571             }
572         }
573     }
574
575     /// Returns whether or not this span points into a file
576     /// in the current crate. This may be `false` for spans
577     /// produced by a macro expansion, or for spans associated
578     /// with the definition of an item in a foreign crate
579     pub fn is_local_span(&self, sp: Span) -> bool {
580         let local_begin = self.lookup_byte_offset(sp.lo());
581         let local_end = self.lookup_byte_offset(sp.hi());
582         // This might be a weird span that covers multiple files
583         local_begin.sf.src.is_some() && local_end.sf.src.is_some()
584     }
585
586     /// Returns the source snippet as `String` corresponding to the given `Span`.
587     pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
588         self.span_to_source(sp, |src, start_index, end_index| {
589             src.get(start_index..end_index)
590                 .map(|s| s.to_string())
591                 .ok_or(SpanSnippetError::IllFormedSpan(sp))
592         })
593     }
594
595     pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
596         Some(self.indentation_before(sp)?.len())
597     }
598
599     pub fn indentation_before(&self, sp: Span) -> Option<String> {
600         self.span_to_source(sp, |src, start_index, _| {
601             let before = &src[..start_index];
602             let last_line = before.rsplit_once('\n').map_or(before, |(_, last)| last);
603             Ok(last_line
604                 .split_once(|c: char| !c.is_whitespace())
605                 .map_or(last_line, |(indent, _)| indent)
606                 .to_string())
607         })
608         .ok()
609     }
610
611     /// Returns the source snippet as `String` before the given `Span`.
612     pub fn span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
613         self.span_to_source(sp, |src, start_index, _| {
614             src.get(..start_index).map(|s| s.to_string()).ok_or(SpanSnippetError::IllFormedSpan(sp))
615         })
616     }
617
618     /// Extends the given `Span` to just after the previous occurrence of `c`. Return the same span
619     /// if no character could be found or if an error occurred while retrieving the code snippet.
620     pub fn span_extend_to_prev_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
621         if let Ok(prev_source) = self.span_to_prev_source(sp) {
622             let prev_source = prev_source.rsplit(c).next().unwrap_or("");
623             if !prev_source.is_empty() && (accept_newlines || !prev_source.contains('\n')) {
624                 return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
625             }
626         }
627
628         sp
629     }
630
631     /// Extends the given `Span` to just after the previous occurrence of `pat` when surrounded by
632     /// whitespace. Returns None if the pattern could not be found or if an error occurred while
633     /// retrieving the code snippet.
634     pub fn span_extend_to_prev_str(
635         &self,
636         sp: Span,
637         pat: &str,
638         accept_newlines: bool,
639         include_whitespace: bool,
640     ) -> Option<Span> {
641         // assure that the pattern is delimited, to avoid the following
642         //     fn my_fn()
643         //           ^^^^ returned span without the check
644         //     ---------- correct span
645         let prev_source = self.span_to_prev_source(sp).ok()?;
646         for ws in &[" ", "\t", "\n"] {
647             let pat = pat.to_owned() + ws;
648             if let Some(pat_pos) = prev_source.rfind(&pat) {
649                 let just_after_pat_pos = pat_pos + pat.len() - 1;
650                 let just_after_pat_plus_ws = if include_whitespace {
651                     just_after_pat_pos
652                         + prev_source[just_after_pat_pos..]
653                             .find(|c: char| !c.is_whitespace())
654                             .unwrap_or(0)
655                 } else {
656                     just_after_pat_pos
657                 };
658                 let len = prev_source.len() - just_after_pat_plus_ws;
659                 let prev_source = &prev_source[just_after_pat_plus_ws..];
660                 if accept_newlines || !prev_source.trim_start().contains('\n') {
661                     return Some(sp.with_lo(BytePos(sp.lo().0 - len as u32)));
662                 }
663             }
664         }
665
666         None
667     }
668
669     /// Returns the source snippet as `String` after the given `Span`.
670     pub fn span_to_next_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
671         self.span_to_source(sp, |src, _, end_index| {
672             src.get(end_index..).map(|s| s.to_string()).ok_or(SpanSnippetError::IllFormedSpan(sp))
673         })
674     }
675
676     /// Extends the given `Span` while the next character matches the predicate
677     pub fn span_extend_while(
678         &self,
679         span: Span,
680         f: impl Fn(char) -> bool,
681     ) -> Result<Span, SpanSnippetError> {
682         self.span_to_source(span, |s, _start, end| {
683             let n = s[end..].char_indices().find(|&(_, c)| !f(c)).map_or(s.len() - end, |(i, _)| i);
684             Ok(span.with_hi(span.hi() + BytePos(n as u32)))
685         })
686     }
687
688     /// Extends the given `Span` to just after the next occurrence of `c`.
689     pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
690         if let Ok(next_source) = self.span_to_next_source(sp) {
691             let next_source = next_source.split(c).next().unwrap_or("");
692             if !next_source.is_empty() && (accept_newlines || !next_source.contains('\n')) {
693                 return sp.with_hi(BytePos(sp.hi().0 + next_source.len() as u32));
694             }
695         }
696
697         sp
698     }
699
700     /// Given a `Span`, tries to get a shorter span ending before the first occurrence of `char`
701     /// `c`.
702     pub fn span_until_char(&self, sp: Span, c: char) -> Span {
703         match self.span_to_snippet(sp) {
704             Ok(snippet) => {
705                 let snippet = snippet.split(c).next().unwrap_or("").trim_end();
706                 if !snippet.is_empty() && !snippet.contains('\n') {
707                     sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32))
708                 } else {
709                     sp
710                 }
711             }
712             _ => sp,
713         }
714     }
715
716     /// Given a `Span`, tries to get a shorter span ending just after the first occurrence of `char`
717     /// `c`.
718     pub fn span_through_char(&self, sp: Span, c: char) -> Span {
719         if let Ok(snippet) = self.span_to_snippet(sp) {
720             if let Some(offset) = snippet.find(c) {
721                 return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32));
722             }
723         }
724         sp
725     }
726
727     /// Given a `Span`, gets a new `Span` covering the first token and all its trailing whitespace
728     /// or the original `Span`.
729     ///
730     /// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned.
731     pub fn span_until_non_whitespace(&self, sp: Span) -> Span {
732         let mut whitespace_found = false;
733
734         self.span_take_while(sp, |c| {
735             if !whitespace_found && c.is_whitespace() {
736                 whitespace_found = true;
737             }
738
739             !whitespace_found || c.is_whitespace()
740         })
741     }
742
743     /// Given a `Span`, gets a new `Span` covering the first token without its trailing whitespace
744     /// or the original `Span` in case of error.
745     ///
746     /// If `sp` points to `"let mut x"`, then a span pointing at `"let"` will be returned.
747     pub fn span_until_whitespace(&self, sp: Span) -> Span {
748         self.span_take_while(sp, |c| !c.is_whitespace())
749     }
750
751     /// Given a `Span`, gets a shorter one until `predicate` yields `false`.
752     pub fn span_take_while<P>(&self, sp: Span, predicate: P) -> Span
753     where
754         P: for<'r> FnMut(&'r char) -> bool,
755     {
756         if let Ok(snippet) = self.span_to_snippet(sp) {
757             let offset = snippet.chars().take_while(predicate).map(|c| c.len_utf8()).sum::<usize>();
758
759             sp.with_hi(BytePos(sp.lo().0 + (offset as u32)))
760         } else {
761             sp
762         }
763     }
764
765     /// Given a `Span`, return a span ending in the closest `{`. This is useful when you have a
766     /// `Span` enclosing a whole item but we need to point at only the head (usually the first
767     /// line) of that item.
768     ///
769     /// *Only suitable for diagnostics.*
770     pub fn guess_head_span(&self, sp: Span) -> Span {
771         // FIXME: extend the AST items to have a head span, or replace callers with pointing at
772         // the item's ident when appropriate.
773         self.span_until_char(sp, '{')
774     }
775
776     /// Returns a new span representing just the first character of the given span.
777     pub fn start_point(&self, sp: Span) -> Span {
778         let width = {
779             let sp = sp.data();
780             let local_begin = self.lookup_byte_offset(sp.lo);
781             let start_index = local_begin.pos.to_usize();
782             let src = local_begin.sf.external_src.borrow();
783
784             let snippet = if let Some(ref src) = local_begin.sf.src {
785                 Some(&src[start_index..])
786             } else if let Some(src) = src.get_source() {
787                 Some(&src[start_index..])
788             } else {
789                 None
790             };
791
792             match snippet {
793                 None => 1,
794                 Some(snippet) => match snippet.chars().next() {
795                     None => 1,
796                     Some(c) => c.len_utf8(),
797                 },
798             }
799         };
800
801         sp.with_hi(BytePos(sp.lo().0 + width as u32))
802     }
803
804     /// Returns a new span representing just the last character of this span.
805     pub fn end_point(&self, sp: Span) -> Span {
806         let pos = sp.hi().0;
807
808         let width = self.find_width_of_character_at_span(sp, false);
809         let corrected_end_position = pos.checked_sub(width).unwrap_or(pos);
810
811         let end_point = BytePos(cmp::max(corrected_end_position, sp.lo().0));
812         sp.with_lo(end_point)
813     }
814
815     /// Returns a new span representing the next character after the end-point of this span.
816     pub fn next_point(&self, sp: Span) -> Span {
817         if sp.is_dummy() {
818             return sp;
819         }
820         let start_of_next_point = sp.hi().0;
821
822         let width = self.find_width_of_character_at_span(sp.shrink_to_hi(), true);
823         // If the width is 1, then the next span should point to the same `lo` and `hi`. However,
824         // in the case of a multibyte character, where the width != 1, the next span should
825         // span multiple bytes to include the whole character.
826         let end_of_next_point =
827             start_of_next_point.checked_add(width - 1).unwrap_or(start_of_next_point);
828
829         let end_of_next_point = BytePos(cmp::max(sp.lo().0 + 1, end_of_next_point));
830         Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None)
831     }
832
833     /// Finds the width of the character, either before or after the end of provided span,
834     /// depending on the `forwards` parameter.
835     fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
836         let sp = sp.data();
837         if sp.lo == sp.hi {
838             debug!("find_width_of_character_at_span: early return empty span");
839             return 1;
840         }
841
842         let local_begin = self.lookup_byte_offset(sp.lo);
843         let local_end = self.lookup_byte_offset(sp.hi);
844         debug!(
845             "find_width_of_character_at_span: local_begin=`{:?}`, local_end=`{:?}`",
846             local_begin, local_end
847         );
848
849         if local_begin.sf.start_pos != local_end.sf.start_pos {
850             debug!("find_width_of_character_at_span: begin and end are in different files");
851             return 1;
852         }
853
854         let start_index = local_begin.pos.to_usize();
855         let end_index = local_end.pos.to_usize();
856         debug!(
857             "find_width_of_character_at_span: start_index=`{:?}`, end_index=`{:?}`",
858             start_index, end_index
859         );
860
861         // Disregard indexes that are at the start or end of their spans, they can't fit bigger
862         // characters.
863         if (!forwards && end_index == usize::MIN) || (forwards && start_index == usize::MAX) {
864             debug!("find_width_of_character_at_span: start or end of span, cannot be multibyte");
865             return 1;
866         }
867
868         let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
869         debug!("find_width_of_character_at_span: source_len=`{:?}`", source_len);
870         // Ensure indexes are also not malformed.
871         if start_index > end_index || end_index > source_len {
872             debug!("find_width_of_character_at_span: source indexes are malformed");
873             return 1;
874         }
875
876         let src = local_begin.sf.external_src.borrow();
877
878         // We need to extend the snippet to the end of the src rather than to end_index so when
879         // searching forwards for boundaries we've got somewhere to search.
880         let snippet = if let Some(ref src) = local_begin.sf.src {
881             &src[start_index..]
882         } else if let Some(src) = src.get_source() {
883             &src[start_index..]
884         } else {
885             return 1;
886         };
887         debug!("find_width_of_character_at_span: snippet=`{:?}`", snippet);
888
889         let mut target = if forwards { end_index + 1 } else { end_index - 1 };
890         debug!("find_width_of_character_at_span: initial target=`{:?}`", target);
891
892         while !snippet.is_char_boundary(target - start_index) && target < source_len {
893             target = if forwards {
894                 target + 1
895             } else {
896                 match target.checked_sub(1) {
897                     Some(target) => target,
898                     None => {
899                         break;
900                     }
901                 }
902             };
903             debug!("find_width_of_character_at_span: target=`{:?}`", target);
904         }
905         debug!("find_width_of_character_at_span: final target=`{:?}`", target);
906
907         if forwards { (target - end_index) as u32 } else { (end_index - target) as u32 }
908     }
909
910     pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
911         // Remap filename before lookup
912         let filename = self.path_mapping().map_filename_prefix(filename).0;
913         for sf in self.files.borrow().source_files.iter() {
914             if filename == sf.name {
915                 return Some(sf.clone());
916             }
917         }
918         None
919     }
920
921     /// For a global `BytePos`, computes the local offset within the containing `SourceFile`.
922     pub fn lookup_byte_offset(&self, bpos: BytePos) -> SourceFileAndBytePos {
923         let idx = self.lookup_source_file_idx(bpos);
924         let sf = (*self.files.borrow().source_files)[idx].clone();
925         let offset = bpos - sf.start_pos;
926         SourceFileAndBytePos { sf, pos: offset }
927     }
928
929     // Returns the index of the `SourceFile` (in `self.files`) that contains `pos`.
930     // This index is guaranteed to be valid for the lifetime of this `SourceMap`,
931     // since `source_files` is a `MonotonicVec`
932     pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
933         self.files
934             .borrow()
935             .source_files
936             .binary_search_by_key(&pos, |key| key.start_pos)
937             .unwrap_or_else(|p| p - 1)
938     }
939
940     pub fn count_lines(&self) -> usize {
941         self.files().iter().fold(0, |a, f| a + f.count_lines())
942     }
943
944     pub fn generate_fn_name_span(&self, span: Span) -> Option<Span> {
945         let prev_span = self.span_extend_to_prev_str(span, "fn", true, true).unwrap_or(span);
946         if let Ok(snippet) = self.span_to_snippet(prev_span) {
947             debug!(
948                 "generate_fn_name_span: span={:?}, prev_span={:?}, snippet={:?}",
949                 span, prev_span, snippet
950             );
951
952             if snippet.is_empty() {
953                 return None;
954             };
955
956             let len = snippet
957                 .find(|c: char| !c.is_alphanumeric() && c != '_')
958                 .expect("no label after fn");
959             Some(prev_span.with_hi(BytePos(prev_span.lo().0 + len as u32)))
960         } else {
961             None
962         }
963     }
964
965     /// Takes the span of a type parameter in a function signature and try to generate a span for
966     /// the function name (with generics) and a new snippet for this span with the pointed type
967     /// parameter as a new local type parameter.
968     ///
969     /// For instance:
970     /// ```rust,ignore (pseudo-Rust)
971     /// // Given span
972     /// fn my_function(param: T)
973     /// //                    ^ Original span
974     ///
975     /// // Result
976     /// fn my_function(param: T)
977     /// // ^^^^^^^^^^^ Generated span with snippet `my_function<T>`
978     /// ```
979     ///
980     /// Attention: The method used is very fragile since it essentially duplicates the work of the
981     /// parser. If you need to use this function or something similar, please consider updating the
982     /// `SourceMap` functions and this function to something more robust.
983     pub fn generate_local_type_param_snippet(&self, span: Span) -> Option<(Span, String)> {
984         // Try to extend the span to the previous "fn" keyword to retrieve the function
985         // signature.
986         if let Some(sugg_span) = self.span_extend_to_prev_str(span, "fn", false, true) {
987             if let Ok(snippet) = self.span_to_snippet(sugg_span) {
988                 // Consume the function name.
989                 let mut offset = snippet
990                     .find(|c: char| !c.is_alphanumeric() && c != '_')
991                     .expect("no label after fn");
992
993                 // Consume the generics part of the function signature.
994                 let mut bracket_counter = 0;
995                 let mut last_char = None;
996                 for c in snippet[offset..].chars() {
997                     match c {
998                         '<' => bracket_counter += 1,
999                         '>' => bracket_counter -= 1,
1000                         '(' => {
1001                             if bracket_counter == 0 {
1002                                 break;
1003                             }
1004                         }
1005                         _ => {}
1006                     }
1007                     offset += c.len_utf8();
1008                     last_char = Some(c);
1009                 }
1010
1011                 // Adjust the suggestion span to encompass the function name with its generics.
1012                 let sugg_span = sugg_span.with_hi(BytePos(sugg_span.lo().0 + offset as u32));
1013
1014                 // Prepare the new suggested snippet to append the type parameter that triggered
1015                 // the error in the generics of the function signature.
1016                 let mut new_snippet = if last_char == Some('>') {
1017                     format!("{}, ", &snippet[..(offset - '>'.len_utf8())])
1018                 } else {
1019                     format!("{}<", &snippet[..offset])
1020                 };
1021                 new_snippet
1022                     .push_str(&self.span_to_snippet(span).unwrap_or_else(|_| "T".to_string()));
1023                 new_snippet.push('>');
1024
1025                 return Some((sugg_span, new_snippet));
1026             }
1027         }
1028
1029         None
1030     }
1031     pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool {
1032         source_file.add_external_src(|| {
1033             match source_file.name {
1034                 FileName::Real(ref name) if let Some(local_path) = name.local_path() => {
1035                     self.file_loader.read_file(local_path).ok()
1036                 }
1037                 _ => None,
1038             }
1039         })
1040     }
1041
1042     pub fn is_imported(&self, sp: Span) -> bool {
1043         let source_file_index = self.lookup_source_file_idx(sp.lo());
1044         let source_file = &self.files()[source_file_index];
1045         source_file.is_imported()
1046     }
1047
1048     /// Gets the span of a statement. If the statement is a macro expansion, the
1049     /// span in the context of the block span is found. The trailing semicolon is included
1050     /// on a best-effort basis.
1051     pub fn stmt_span(&self, stmt_span: Span, block_span: Span) -> Span {
1052         if !stmt_span.from_expansion() {
1053             return stmt_span;
1054         }
1055         let mac_call = original_sp(stmt_span, block_span);
1056         self.mac_call_stmt_semi_span(mac_call).map_or(mac_call, |s| mac_call.with_hi(s.hi()))
1057     }
1058
1059     /// Tries to find the span of the semicolon of a macro call statement.
1060     /// The input must be the *call site* span of a statement from macro expansion.
1061     /// ```ignore (illustrative)
1062     /// //       v output
1063     ///    mac!();
1064     /// // ^^^^^^ input
1065     /// ```
1066     pub fn mac_call_stmt_semi_span(&self, mac_call: Span) -> Option<Span> {
1067         let span = self.span_extend_while(mac_call, char::is_whitespace).ok()?;
1068         let span = span.shrink_to_hi().with_hi(BytePos(span.hi().0.checked_add(1)?));
1069         if self.span_to_snippet(span).as_deref() != Ok(";") {
1070             return None;
1071         }
1072         Some(span)
1073     }
1074 }
1075
1076 #[derive(Clone)]
1077 pub struct FilePathMapping {
1078     mapping: Vec<(PathBuf, PathBuf)>,
1079     filename_display_for_diagnostics: FileNameDisplayPreference,
1080 }
1081
1082 impl FilePathMapping {
1083     pub fn empty() -> FilePathMapping {
1084         FilePathMapping::new(Vec::new())
1085     }
1086
1087     pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping {
1088         let filename_display_for_diagnostics = if mapping.is_empty() {
1089             FileNameDisplayPreference::Local
1090         } else {
1091             FileNameDisplayPreference::Remapped
1092         };
1093
1094         FilePathMapping { mapping, filename_display_for_diagnostics }
1095     }
1096
1097     /// Applies any path prefix substitution as defined by the mapping.
1098     /// The return value is the remapped path and a boolean indicating whether
1099     /// the path was affected by the mapping.
1100     pub fn map_prefix(&self, path: PathBuf) -> (PathBuf, bool) {
1101         // NOTE: We are iterating over the mapping entries from last to first
1102         //       because entries specified later on the command line should
1103         //       take precedence.
1104         for &(ref from, ref to) in self.mapping.iter().rev() {
1105             if let Ok(rest) = path.strip_prefix(from) {
1106                 let remapped = if rest.as_os_str().is_empty() {
1107                     // This is subtle, joining an empty path onto e.g. `foo/bar` will
1108                     // result in `foo/bar/`, that is, there'll be an additional directory
1109                     // separator at the end. This can lead to duplicated directory separators
1110                     // in remapped paths down the line.
1111                     // So, if we have an exact match, we just return that without a call
1112                     // to `Path::join()`.
1113                     to.clone()
1114                 } else {
1115                     to.join(rest)
1116                 };
1117
1118                 return (remapped, true);
1119             }
1120         }
1121
1122         (path, false)
1123     }
1124
1125     fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
1126         match file {
1127             FileName::Real(realfile) if let RealFileName::LocalPath(local_path) = realfile => {
1128                 let (mapped_path, mapped) = self.map_prefix(local_path.to_path_buf());
1129                 let realfile = if mapped {
1130                     RealFileName::Remapped {
1131                         local_path: Some(local_path.clone()),
1132                         virtual_name: mapped_path,
1133                     }
1134                 } else {
1135                     realfile.clone()
1136                 };
1137                 (FileName::Real(realfile), mapped)
1138             }
1139             FileName::Real(_) => unreachable!("attempted to remap an already remapped filename"),
1140             other => (other.clone(), false),
1141         }
1142     }
1143 }