]> git.lizzy.rs Git - rust.git/blob - src/librustc_span/lib.rs
Rollup merge of #68468 - ssomers:btreemap_prefer_middle, r=Mark-Simulacrum
[rust.git] / src / librustc_span / lib.rs
1 //! The source positions and related helper functions.
2 //!
3 //! ## Note
4 //!
5 //! This API is completely unstable and subject to change.
6
7 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
8 #![feature(crate_visibility_modifier)]
9 #![feature(nll)]
10 #![feature(optin_builtin_traits)]
11 #![feature(rustc_attrs)]
12 #![feature(specialization)]
13 #![feature(step_trait)]
14
15 use rustc_data_structures::AtomicRef;
16 use rustc_macros::HashStable_Generic;
17 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
18
19 mod caching_source_map_view;
20 pub mod source_map;
21 pub use self::caching_source_map_view::CachingSourceMapView;
22
23 pub mod edition;
24 use edition::Edition;
25 pub mod hygiene;
26 use hygiene::Transparency;
27 pub use hygiene::{DesugaringKind, ExpnData, ExpnId, ExpnKind, MacroKind, SyntaxContext};
28
29 mod span_encoding;
30 pub use span_encoding::{Span, DUMMY_SP};
31
32 pub mod symbol;
33 pub use symbol::{sym, Symbol};
34
35 mod analyze_source_file;
36 pub mod fatal_error;
37
38 use rustc_data_structures::fingerprint::Fingerprint;
39 use rustc_data_structures::fx::FxHashMap;
40 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
41 use rustc_data_structures::sync::{Lock, Lrc};
42
43 use std::borrow::Cow;
44 use std::cell::RefCell;
45 use std::cmp::{self, Ordering};
46 use std::fmt;
47 use std::hash::{Hash, Hasher};
48 use std::ops::{Add, Sub};
49 use std::path::PathBuf;
50
51 #[cfg(test)]
52 mod tests;
53
54 pub struct Globals {
55     symbol_interner: Lock<symbol::Interner>,
56     span_interner: Lock<span_encoding::SpanInterner>,
57     hygiene_data: Lock<hygiene::HygieneData>,
58 }
59
60 impl Globals {
61     pub fn new(edition: Edition) -> Globals {
62         Globals {
63             symbol_interner: Lock::new(symbol::Interner::fresh()),
64             span_interner: Lock::new(span_encoding::SpanInterner::default()),
65             hygiene_data: Lock::new(hygiene::HygieneData::new(edition)),
66         }
67     }
68 }
69
70 scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
71
72 /// Differentiates between real files and common virtual files.
73 #[derive(
74     Debug,
75     Eq,
76     PartialEq,
77     Clone,
78     Ord,
79     PartialOrd,
80     Hash,
81     RustcDecodable,
82     RustcEncodable,
83     HashStable_Generic
84 )]
85 pub enum FileName {
86     Real(PathBuf),
87     /// A macro. This includes the full name of the macro, so that there are no clashes.
88     Macros(String),
89     /// Call to `quote!`.
90     QuoteExpansion(u64),
91     /// Command line.
92     Anon(u64),
93     /// Hack in `src/libsyntax/parse.rs`.
94     // FIXME(jseyfried)
95     MacroExpansion(u64),
96     ProcMacroSourceCode(u64),
97     /// Strings provided as `--cfg [cfgspec]` stored in a `crate_cfg`.
98     CfgSpec(u64),
99     /// Strings provided as crate attributes in the CLI.
100     CliCrateAttr(u64),
101     /// Custom sources for explicit parser calls from plugins and drivers.
102     Custom(String),
103     DocTest(PathBuf, isize),
104 }
105
106 impl std::fmt::Display for FileName {
107     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108         use FileName::*;
109         match *self {
110             Real(ref path) => write!(fmt, "{}", path.display()),
111             Macros(ref name) => write!(fmt, "<{} macros>", name),
112             QuoteExpansion(_) => write!(fmt, "<quote expansion>"),
113             MacroExpansion(_) => write!(fmt, "<macro expansion>"),
114             Anon(_) => write!(fmt, "<anon>"),
115             ProcMacroSourceCode(_) => write!(fmt, "<proc-macro source code>"),
116             CfgSpec(_) => write!(fmt, "<cfgspec>"),
117             CliCrateAttr(_) => write!(fmt, "<crate attribute>"),
118             Custom(ref s) => write!(fmt, "<{}>", s),
119             DocTest(ref path, _) => write!(fmt, "{}", path.display()),
120         }
121     }
122 }
123
124 impl From<PathBuf> for FileName {
125     fn from(p: PathBuf) -> Self {
126         assert!(!p.to_string_lossy().ends_with('>'));
127         FileName::Real(p)
128     }
129 }
130
131 impl FileName {
132     pub fn is_real(&self) -> bool {
133         use FileName::*;
134         match *self {
135             Real(_) => true,
136             Macros(_)
137             | Anon(_)
138             | MacroExpansion(_)
139             | ProcMacroSourceCode(_)
140             | CfgSpec(_)
141             | CliCrateAttr(_)
142             | Custom(_)
143             | QuoteExpansion(_)
144             | DocTest(_, _) => false,
145         }
146     }
147
148     pub fn is_macros(&self) -> bool {
149         use FileName::*;
150         match *self {
151             Real(_)
152             | Anon(_)
153             | MacroExpansion(_)
154             | ProcMacroSourceCode(_)
155             | CfgSpec(_)
156             | CliCrateAttr(_)
157             | Custom(_)
158             | QuoteExpansion(_)
159             | DocTest(_, _) => false,
160             Macros(_) => true,
161         }
162     }
163
164     pub fn quote_expansion_source_code(src: &str) -> FileName {
165         let mut hasher = StableHasher::new();
166         src.hash(&mut hasher);
167         FileName::QuoteExpansion(hasher.finish())
168     }
169
170     pub fn macro_expansion_source_code(src: &str) -> FileName {
171         let mut hasher = StableHasher::new();
172         src.hash(&mut hasher);
173         FileName::MacroExpansion(hasher.finish())
174     }
175
176     pub fn anon_source_code(src: &str) -> FileName {
177         let mut hasher = StableHasher::new();
178         src.hash(&mut hasher);
179         FileName::Anon(hasher.finish())
180     }
181
182     pub fn proc_macro_source_code(src: &str) -> FileName {
183         let mut hasher = StableHasher::new();
184         src.hash(&mut hasher);
185         FileName::ProcMacroSourceCode(hasher.finish())
186     }
187
188     pub fn cfg_spec_source_code(src: &str) -> FileName {
189         let mut hasher = StableHasher::new();
190         src.hash(&mut hasher);
191         FileName::QuoteExpansion(hasher.finish())
192     }
193
194     pub fn cli_crate_attr_source_code(src: &str) -> FileName {
195         let mut hasher = StableHasher::new();
196         src.hash(&mut hasher);
197         FileName::CliCrateAttr(hasher.finish())
198     }
199
200     pub fn doc_test_source_code(path: PathBuf, line: isize) -> FileName {
201         FileName::DocTest(path, line)
202     }
203 }
204
205 /// Spans represent a region of code, used for error reporting. Positions in spans
206 /// are *absolute* positions from the beginning of the source_map, not positions
207 /// relative to `SourceFile`s. Methods on the `SourceMap` can be used to relate spans back
208 /// to the original source.
209 /// You must be careful if the span crosses more than one file - you will not be
210 /// able to use many of the functions on spans in source_map and you cannot assume
211 /// that the length of the `span = hi - lo`; there may be space in the `BytePos`
212 /// range between files.
213 ///
214 /// `SpanData` is public because `Span` uses a thread-local interner and can't be
215 /// sent to other threads, but some pieces of performance infra run in a separate thread.
216 /// Using `Span` is generally preferred.
217 #[derive(Clone, Copy, Hash, PartialEq, Eq, Ord, PartialOrd)]
218 pub struct SpanData {
219     pub lo: BytePos,
220     pub hi: BytePos,
221     /// Information about where the macro came from, if this piece of
222     /// code was created by a macro expansion.
223     pub ctxt: SyntaxContext,
224 }
225
226 impl SpanData {
227     #[inline]
228     pub fn with_lo(&self, lo: BytePos) -> Span {
229         Span::new(lo, self.hi, self.ctxt)
230     }
231     #[inline]
232     pub fn with_hi(&self, hi: BytePos) -> Span {
233         Span::new(self.lo, hi, self.ctxt)
234     }
235     #[inline]
236     pub fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
237         Span::new(self.lo, self.hi, ctxt)
238     }
239 }
240
241 // The interner is pointed to by a thread local value which is only set on the main thread
242 // with parallelization is disabled. So we don't allow `Span` to transfer between threads
243 // to avoid panics and other errors, even though it would be memory safe to do so.
244 #[cfg(not(parallel_compiler))]
245 impl !Send for Span {}
246 #[cfg(not(parallel_compiler))]
247 impl !Sync for Span {}
248
249 impl PartialOrd for Span {
250     fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
251         PartialOrd::partial_cmp(&self.data(), &rhs.data())
252     }
253 }
254 impl Ord for Span {
255     fn cmp(&self, rhs: &Self) -> Ordering {
256         Ord::cmp(&self.data(), &rhs.data())
257     }
258 }
259
260 /// A collection of spans. Spans have two orthogonal attributes:
261 ///
262 /// - They can be *primary spans*. In this case they are the locus of
263 ///   the error, and would be rendered with `^^^`.
264 /// - They can have a *label*. In this case, the label is written next
265 ///   to the mark in the snippet when we render.
266 #[derive(Clone, Debug, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
267 pub struct MultiSpan {
268     primary_spans: Vec<Span>,
269     span_labels: Vec<(Span, String)>,
270 }
271
272 impl Span {
273     #[inline]
274     pub fn lo(self) -> BytePos {
275         self.data().lo
276     }
277     #[inline]
278     pub fn with_lo(self, lo: BytePos) -> Span {
279         self.data().with_lo(lo)
280     }
281     #[inline]
282     pub fn hi(self) -> BytePos {
283         self.data().hi
284     }
285     #[inline]
286     pub fn with_hi(self, hi: BytePos) -> Span {
287         self.data().with_hi(hi)
288     }
289     #[inline]
290     pub fn ctxt(self) -> SyntaxContext {
291         self.data().ctxt
292     }
293     #[inline]
294     pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
295         self.data().with_ctxt(ctxt)
296     }
297
298     /// Returns `true` if this is a dummy span with any hygienic context.
299     #[inline]
300     pub fn is_dummy(self) -> bool {
301         let span = self.data();
302         span.lo.0 == 0 && span.hi.0 == 0
303     }
304
305     /// Returns `true` if this span comes from a macro or desugaring.
306     #[inline]
307     pub fn from_expansion(self) -> bool {
308         self.ctxt() != SyntaxContext::root()
309     }
310
311     /// Returns `true` if `span` originates in a derive-macro's expansion.
312     pub fn in_derive_expansion(self) -> bool {
313         matches!(self.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Derive, _))
314     }
315
316     #[inline]
317     pub fn with_root_ctxt(lo: BytePos, hi: BytePos) -> Span {
318         Span::new(lo, hi, SyntaxContext::root())
319     }
320
321     /// Returns a new span representing an empty span at the beginning of this span
322     #[inline]
323     pub fn shrink_to_lo(self) -> Span {
324         let span = self.data();
325         span.with_hi(span.lo)
326     }
327     /// Returns a new span representing an empty span at the end of this span.
328     #[inline]
329     pub fn shrink_to_hi(self) -> Span {
330         let span = self.data();
331         span.with_lo(span.hi)
332     }
333
334     /// Returns `self` if `self` is not the dummy span, and `other` otherwise.
335     pub fn substitute_dummy(self, other: Span) -> Span {
336         if self.is_dummy() { other } else { self }
337     }
338
339     /// Returns `true` if `self` fully encloses `other`.
340     pub fn contains(self, other: Span) -> bool {
341         let span = self.data();
342         let other = other.data();
343         span.lo <= other.lo && other.hi <= span.hi
344     }
345
346     /// Returns `true` if `self` touches `other`.
347     pub fn overlaps(self, other: Span) -> bool {
348         let span = self.data();
349         let other = other.data();
350         span.lo < other.hi && other.lo < span.hi
351     }
352
353     /// Returns `true` if the spans are equal with regards to the source text.
354     ///
355     /// Use this instead of `==` when either span could be generated code,
356     /// and you only care that they point to the same bytes of source text.
357     pub fn source_equal(&self, other: &Span) -> bool {
358         let span = self.data();
359         let other = other.data();
360         span.lo == other.lo && span.hi == other.hi
361     }
362
363     /// Returns `Some(span)`, where the start is trimmed by the end of `other`.
364     pub fn trim_start(self, other: Span) -> Option<Span> {
365         let span = self.data();
366         let other = other.data();
367         if span.hi > other.hi { Some(span.with_lo(cmp::max(span.lo, other.hi))) } else { None }
368     }
369
370     /// Returns the source span -- this is either the supplied span, or the span for
371     /// the macro callsite that expanded to it.
372     pub fn source_callsite(self) -> Span {
373         let expn_data = self.ctxt().outer_expn_data();
374         if !expn_data.is_root() { expn_data.call_site.source_callsite() } else { self }
375     }
376
377     /// The `Span` for the tokens in the previous macro expansion from which `self` was generated,
378     /// if any.
379     pub fn parent(self) -> Option<Span> {
380         let expn_data = self.ctxt().outer_expn_data();
381         if !expn_data.is_root() { Some(expn_data.call_site) } else { None }
382     }
383
384     /// Edition of the crate from which this span came.
385     pub fn edition(self) -> edition::Edition {
386         self.ctxt().outer_expn_data().edition
387     }
388
389     #[inline]
390     pub fn rust_2015(&self) -> bool {
391         self.edition() == edition::Edition::Edition2015
392     }
393
394     #[inline]
395     pub fn rust_2018(&self) -> bool {
396         self.edition() >= edition::Edition::Edition2018
397     }
398
399     /// Returns the source callee.
400     ///
401     /// Returns `None` if the supplied span has no expansion trace,
402     /// else returns the `ExpnData` for the macro definition
403     /// corresponding to the source callsite.
404     pub fn source_callee(self) -> Option<ExpnData> {
405         fn source_callee(expn_data: ExpnData) -> ExpnData {
406             let next_expn_data = expn_data.call_site.ctxt().outer_expn_data();
407             if !next_expn_data.is_root() { source_callee(next_expn_data) } else { expn_data }
408         }
409         let expn_data = self.ctxt().outer_expn_data();
410         if !expn_data.is_root() { Some(source_callee(expn_data)) } else { None }
411     }
412
413     /// Checks if a span is "internal" to a macro in which `#[unstable]`
414     /// items can be used (that is, a macro marked with
415     /// `#[allow_internal_unstable]`).
416     pub fn allows_unstable(&self, feature: Symbol) -> bool {
417         self.ctxt().outer_expn_data().allow_internal_unstable.map_or(false, |features| {
418             features
419                 .iter()
420                 .any(|&f| f == feature || f == sym::allow_internal_unstable_backcompat_hack)
421         })
422     }
423
424     /// Checks if this span arises from a compiler desugaring of kind `kind`.
425     pub fn is_desugaring(&self, kind: DesugaringKind) -> bool {
426         match self.ctxt().outer_expn_data().kind {
427             ExpnKind::Desugaring(k) => k == kind,
428             _ => false,
429         }
430     }
431
432     /// Returns the compiler desugaring that created this span, or `None`
433     /// if this span is not from a desugaring.
434     pub fn desugaring_kind(&self) -> Option<DesugaringKind> {
435         match self.ctxt().outer_expn_data().kind {
436             ExpnKind::Desugaring(k) => Some(k),
437             _ => None,
438         }
439     }
440
441     /// Checks if a span is "internal" to a macro in which `unsafe`
442     /// can be used without triggering the `unsafe_code` lint
443     //  (that is, a macro marked with `#[allow_internal_unsafe]`).
444     pub fn allows_unsafe(&self) -> bool {
445         self.ctxt().outer_expn_data().allow_internal_unsafe
446     }
447
448     pub fn macro_backtrace(mut self) -> impl Iterator<Item = ExpnData> {
449         let mut prev_span = DUMMY_SP;
450         std::iter::from_fn(move || {
451             loop {
452                 let expn_data = self.ctxt().outer_expn_data();
453                 if expn_data.is_root() {
454                     return None;
455                 }
456
457                 let is_recursive = expn_data.call_site.source_equal(&prev_span);
458
459                 prev_span = self;
460                 self = expn_data.call_site;
461
462                 // Don't print recursive invocations.
463                 if !is_recursive {
464                     return Some(expn_data);
465                 }
466             }
467         })
468     }
469
470     /// Returns a `Span` that would enclose both `self` and `end`.
471     pub fn to(self, end: Span) -> Span {
472         let span_data = self.data();
473         let end_data = end.data();
474         // FIXME(jseyfried): `self.ctxt` should always equal `end.ctxt` here (cf. issue #23480).
475         // Return the macro span on its own to avoid weird diagnostic output. It is preferable to
476         // have an incomplete span than a completely nonsensical one.
477         if span_data.ctxt != end_data.ctxt {
478             if span_data.ctxt == SyntaxContext::root() {
479                 return end;
480             } else if end_data.ctxt == SyntaxContext::root() {
481                 return self;
482             }
483             // Both spans fall within a macro.
484             // FIXME(estebank): check if it is the *same* macro.
485         }
486         Span::new(
487             cmp::min(span_data.lo, end_data.lo),
488             cmp::max(span_data.hi, end_data.hi),
489             if span_data.ctxt == SyntaxContext::root() { end_data.ctxt } else { span_data.ctxt },
490         )
491     }
492
493     /// Returns a `Span` between the end of `self` to the beginning of `end`.
494     pub fn between(self, end: Span) -> Span {
495         let span = self.data();
496         let end = end.data();
497         Span::new(
498             span.hi,
499             end.lo,
500             if end.ctxt == SyntaxContext::root() { end.ctxt } else { span.ctxt },
501         )
502     }
503
504     /// Returns a `Span` between the beginning of `self` to the beginning of `end`.
505     pub fn until(self, end: Span) -> Span {
506         let span = self.data();
507         let end = end.data();
508         Span::new(
509             span.lo,
510             end.lo,
511             if end.ctxt == SyntaxContext::root() { end.ctxt } else { span.ctxt },
512         )
513     }
514
515     pub fn from_inner(self, inner: InnerSpan) -> Span {
516         let span = self.data();
517         Span::new(
518             span.lo + BytePos::from_usize(inner.start),
519             span.lo + BytePos::from_usize(inner.end),
520             span.ctxt,
521         )
522     }
523
524     /// Equivalent of `Span::def_site` from the proc macro API,
525     /// except that the location is taken from the `self` span.
526     pub fn with_def_site_ctxt(self, expn_id: ExpnId) -> Span {
527         self.with_ctxt_from_mark(expn_id, Transparency::Opaque)
528     }
529
530     /// Equivalent of `Span::call_site` from the proc macro API,
531     /// except that the location is taken from the `self` span.
532     pub fn with_call_site_ctxt(&self, expn_id: ExpnId) -> Span {
533         self.with_ctxt_from_mark(expn_id, Transparency::Transparent)
534     }
535
536     /// Equivalent of `Span::mixed_site` from the proc macro API,
537     /// except that the location is taken from the `self` span.
538     pub fn with_mixed_site_ctxt(&self, expn_id: ExpnId) -> Span {
539         self.with_ctxt_from_mark(expn_id, Transparency::SemiTransparent)
540     }
541
542     /// Produces a span with the same location as `self` and context produced by a macro with the
543     /// given ID and transparency, assuming that macro was defined directly and not produced by
544     /// some other macro (which is the case for built-in and procedural macros).
545     pub fn with_ctxt_from_mark(self, expn_id: ExpnId, transparency: Transparency) -> Span {
546         self.with_ctxt(SyntaxContext::root().apply_mark(expn_id, transparency))
547     }
548
549     #[inline]
550     pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> Span {
551         let span = self.data();
552         span.with_ctxt(span.ctxt.apply_mark(expn_id, transparency))
553     }
554
555     #[inline]
556     pub fn remove_mark(&mut self) -> ExpnId {
557         let mut span = self.data();
558         let mark = span.ctxt.remove_mark();
559         *self = Span::new(span.lo, span.hi, span.ctxt);
560         mark
561     }
562
563     #[inline]
564     pub fn adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
565         let mut span = self.data();
566         let mark = span.ctxt.adjust(expn_id);
567         *self = Span::new(span.lo, span.hi, span.ctxt);
568         mark
569     }
570
571     #[inline]
572     pub fn modernize_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
573         let mut span = self.data();
574         let mark = span.ctxt.modernize_and_adjust(expn_id);
575         *self = Span::new(span.lo, span.hi, span.ctxt);
576         mark
577     }
578
579     #[inline]
580     pub fn glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span) -> Option<Option<ExpnId>> {
581         let mut span = self.data();
582         let mark = span.ctxt.glob_adjust(expn_id, glob_span);
583         *self = Span::new(span.lo, span.hi, span.ctxt);
584         mark
585     }
586
587     #[inline]
588     pub fn reverse_glob_adjust(
589         &mut self,
590         expn_id: ExpnId,
591         glob_span: Span,
592     ) -> Option<Option<ExpnId>> {
593         let mut span = self.data();
594         let mark = span.ctxt.reverse_glob_adjust(expn_id, glob_span);
595         *self = Span::new(span.lo, span.hi, span.ctxt);
596         mark
597     }
598
599     #[inline]
600     pub fn modern(self) -> Span {
601         let span = self.data();
602         span.with_ctxt(span.ctxt.modern())
603     }
604
605     #[inline]
606     pub fn modern_and_legacy(self) -> Span {
607         let span = self.data();
608         span.with_ctxt(span.ctxt.modern_and_legacy())
609     }
610 }
611
612 #[derive(Clone, Debug)]
613 pub struct SpanLabel {
614     /// The span we are going to include in the final snippet.
615     pub span: Span,
616
617     /// Is this a primary span? This is the "locus" of the message,
618     /// and is indicated with a `^^^^` underline, versus `----`.
619     pub is_primary: bool,
620
621     /// What label should we attach to this span (if any)?
622     pub label: Option<String>,
623 }
624
625 impl Default for Span {
626     fn default() -> Self {
627         DUMMY_SP
628     }
629 }
630
631 impl rustc_serialize::UseSpecializedEncodable for Span {
632     fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
633         let span = self.data();
634         s.emit_struct("Span", 2, |s| {
635             s.emit_struct_field("lo", 0, |s| span.lo.encode(s))?;
636
637             s.emit_struct_field("hi", 1, |s| span.hi.encode(s))
638         })
639     }
640 }
641
642 impl rustc_serialize::UseSpecializedDecodable for Span {
643     fn default_decode<D: Decoder>(d: &mut D) -> Result<Span, D::Error> {
644         d.read_struct("Span", 2, |d| {
645             let lo = d.read_struct_field("lo", 0, Decodable::decode)?;
646             let hi = d.read_struct_field("hi", 1, Decodable::decode)?;
647             Ok(Span::with_root_ctxt(lo, hi))
648         })
649     }
650 }
651
652 pub fn default_span_debug(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
653     f.debug_struct("Span")
654         .field("lo", &span.lo())
655         .field("hi", &span.hi())
656         .field("ctxt", &span.ctxt())
657         .finish()
658 }
659
660 impl fmt::Debug for Span {
661     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
662         (*SPAN_DEBUG)(*self, f)
663     }
664 }
665
666 impl fmt::Debug for SpanData {
667     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
668         (*SPAN_DEBUG)(Span::new(self.lo, self.hi, self.ctxt), f)
669     }
670 }
671
672 impl MultiSpan {
673     #[inline]
674     pub fn new() -> MultiSpan {
675         MultiSpan { primary_spans: vec![], span_labels: vec![] }
676     }
677
678     pub fn from_span(primary_span: Span) -> MultiSpan {
679         MultiSpan { primary_spans: vec![primary_span], span_labels: vec![] }
680     }
681
682     pub fn from_spans(vec: Vec<Span>) -> MultiSpan {
683         MultiSpan { primary_spans: vec, span_labels: vec![] }
684     }
685
686     pub fn push_span_label(&mut self, span: Span, label: String) {
687         self.span_labels.push((span, label));
688     }
689
690     /// Selects the first primary span (if any).
691     pub fn primary_span(&self) -> Option<Span> {
692         self.primary_spans.first().cloned()
693     }
694
695     /// Returns all primary spans.
696     pub fn primary_spans(&self) -> &[Span] {
697         &self.primary_spans
698     }
699
700     /// Returns `true` if any of the primary spans are displayable.
701     pub fn has_primary_spans(&self) -> bool {
702         self.primary_spans.iter().any(|sp| !sp.is_dummy())
703     }
704
705     /// Returns `true` if this contains only a dummy primary span with any hygienic context.
706     pub fn is_dummy(&self) -> bool {
707         let mut is_dummy = true;
708         for span in &self.primary_spans {
709             if !span.is_dummy() {
710                 is_dummy = false;
711             }
712         }
713         is_dummy
714     }
715
716     /// Replaces all occurrences of one Span with another. Used to move `Span`s in areas that don't
717     /// display well (like std macros). Returns whether replacements occurred.
718     pub fn replace(&mut self, before: Span, after: Span) -> bool {
719         let mut replacements_occurred = false;
720         for primary_span in &mut self.primary_spans {
721             if *primary_span == before {
722                 *primary_span = after;
723                 replacements_occurred = true;
724             }
725         }
726         for span_label in &mut self.span_labels {
727             if span_label.0 == before {
728                 span_label.0 = after;
729                 replacements_occurred = true;
730             }
731         }
732         replacements_occurred
733     }
734
735     /// Returns the strings to highlight. We always ensure that there
736     /// is an entry for each of the primary spans -- for each primary
737     /// span `P`, if there is at least one label with span `P`, we return
738     /// those labels (marked as primary). But otherwise we return
739     /// `SpanLabel` instances with empty labels.
740     pub fn span_labels(&self) -> Vec<SpanLabel> {
741         let is_primary = |span| self.primary_spans.contains(&span);
742
743         let mut span_labels = self
744             .span_labels
745             .iter()
746             .map(|&(span, ref label)| SpanLabel {
747                 span,
748                 is_primary: is_primary(span),
749                 label: Some(label.clone()),
750             })
751             .collect::<Vec<_>>();
752
753         for &span in &self.primary_spans {
754             if !span_labels.iter().any(|sl| sl.span == span) {
755                 span_labels.push(SpanLabel { span, is_primary: true, label: None });
756             }
757         }
758
759         span_labels
760     }
761
762     /// Returns `true` if any of the span labels is displayable.
763     pub fn has_span_labels(&self) -> bool {
764         self.span_labels.iter().any(|(sp, _)| !sp.is_dummy())
765     }
766 }
767
768 impl From<Span> for MultiSpan {
769     fn from(span: Span) -> MultiSpan {
770         MultiSpan::from_span(span)
771     }
772 }
773
774 impl From<Vec<Span>> for MultiSpan {
775     fn from(spans: Vec<Span>) -> MultiSpan {
776         MultiSpan::from_spans(spans)
777     }
778 }
779
780 /// Identifies an offset of a multi-byte character in a `SourceFile`.
781 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Eq, PartialEq, Debug)]
782 pub struct MultiByteChar {
783     /// The absolute offset of the character in the `SourceMap`.
784     pub pos: BytePos,
785     /// The number of bytes, `>= 2`.
786     pub bytes: u8,
787 }
788
789 /// Identifies an offset of a non-narrow character in a `SourceFile`.
790 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Eq, PartialEq, Debug)]
791 pub enum NonNarrowChar {
792     /// Represents a zero-width character.
793     ZeroWidth(BytePos),
794     /// Represents a wide (full-width) character.
795     Wide(BytePos),
796     /// Represents a tab character, represented visually with a width of 4 characters.
797     Tab(BytePos),
798 }
799
800 impl NonNarrowChar {
801     fn new(pos: BytePos, width: usize) -> Self {
802         match width {
803             0 => NonNarrowChar::ZeroWidth(pos),
804             2 => NonNarrowChar::Wide(pos),
805             4 => NonNarrowChar::Tab(pos),
806             _ => panic!("width {} given for non-narrow character", width),
807         }
808     }
809
810     /// Returns the absolute offset of the character in the `SourceMap`.
811     pub fn pos(&self) -> BytePos {
812         match *self {
813             NonNarrowChar::ZeroWidth(p) | NonNarrowChar::Wide(p) | NonNarrowChar::Tab(p) => p,
814         }
815     }
816
817     /// Returns the width of the character, 0 (zero-width) or 2 (wide).
818     pub fn width(&self) -> usize {
819         match *self {
820             NonNarrowChar::ZeroWidth(_) => 0,
821             NonNarrowChar::Wide(_) => 2,
822             NonNarrowChar::Tab(_) => 4,
823         }
824     }
825 }
826
827 impl Add<BytePos> for NonNarrowChar {
828     type Output = Self;
829
830     fn add(self, rhs: BytePos) -> Self {
831         match self {
832             NonNarrowChar::ZeroWidth(pos) => NonNarrowChar::ZeroWidth(pos + rhs),
833             NonNarrowChar::Wide(pos) => NonNarrowChar::Wide(pos + rhs),
834             NonNarrowChar::Tab(pos) => NonNarrowChar::Tab(pos + rhs),
835         }
836     }
837 }
838
839 impl Sub<BytePos> for NonNarrowChar {
840     type Output = Self;
841
842     fn sub(self, rhs: BytePos) -> Self {
843         match self {
844             NonNarrowChar::ZeroWidth(pos) => NonNarrowChar::ZeroWidth(pos - rhs),
845             NonNarrowChar::Wide(pos) => NonNarrowChar::Wide(pos - rhs),
846             NonNarrowChar::Tab(pos) => NonNarrowChar::Tab(pos - rhs),
847         }
848     }
849 }
850
851 /// Identifies an offset of a character that was normalized away from `SourceFile`.
852 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Eq, PartialEq, Debug)]
853 pub struct NormalizedPos {
854     /// The absolute offset of the character in the `SourceMap`.
855     pub pos: BytePos,
856     /// The difference between original and normalized string at position.
857     pub diff: u32,
858 }
859
860 /// The state of the lazy external source loading mechanism of a `SourceFile`.
861 #[derive(PartialEq, Eq, Clone)]
862 pub enum ExternalSource {
863     /// The external source has been loaded already.
864     Present(String),
865     /// No attempt has been made to load the external source.
866     AbsentOk,
867     /// A failed attempt has been made to load the external source.
868     AbsentErr,
869     /// No external source has to be loaded, since the `SourceFile` represents a local crate.
870     Unneeded,
871 }
872
873 impl ExternalSource {
874     pub fn is_absent(&self) -> bool {
875         match *self {
876             ExternalSource::Present(_) => false,
877             _ => true,
878         }
879     }
880
881     pub fn get_source(&self) -> Option<&str> {
882         match *self {
883             ExternalSource::Present(ref src) => Some(src),
884             _ => None,
885         }
886     }
887 }
888
889 #[derive(Debug)]
890 pub struct OffsetOverflowError;
891
892 /// A single source in the `SourceMap`.
893 #[derive(Clone)]
894 pub struct SourceFile {
895     /// The name of the file that the source came from. Source that doesn't
896     /// originate from files has names between angle brackets by convention
897     /// (e.g., `<anon>`).
898     pub name: FileName,
899     /// `true` if the `name` field above has been modified by `--remap-path-prefix`.
900     pub name_was_remapped: bool,
901     /// The unmapped path of the file that the source came from.
902     /// Set to `None` if the `SourceFile` was imported from an external crate.
903     pub unmapped_path: Option<FileName>,
904     /// Indicates which crate this `SourceFile` was imported from.
905     pub crate_of_origin: u32,
906     /// The complete source code.
907     pub src: Option<Lrc<String>>,
908     /// The source code's hash.
909     pub src_hash: u128,
910     /// The external source code (used for external crates, which will have a `None`
911     /// value as `self.src`.
912     pub external_src: Lock<ExternalSource>,
913     /// The start position of this source in the `SourceMap`.
914     pub start_pos: BytePos,
915     /// The end position of this source in the `SourceMap`.
916     pub end_pos: BytePos,
917     /// Locations of lines beginnings in the source code.
918     pub lines: Vec<BytePos>,
919     /// Locations of multi-byte characters in the source code.
920     pub multibyte_chars: Vec<MultiByteChar>,
921     /// Width of characters that are not narrow in the source code.
922     pub non_narrow_chars: Vec<NonNarrowChar>,
923     /// Locations of characters removed during normalization.
924     pub normalized_pos: Vec<NormalizedPos>,
925     /// A hash of the filename, used for speeding up hashing in incremental compilation.
926     pub name_hash: u128,
927 }
928
929 impl Encodable for SourceFile {
930     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
931         s.emit_struct("SourceFile", 8, |s| {
932             s.emit_struct_field("name", 0, |s| self.name.encode(s))?;
933             s.emit_struct_field("name_was_remapped", 1, |s| self.name_was_remapped.encode(s))?;
934             s.emit_struct_field("src_hash", 2, |s| self.src_hash.encode(s))?;
935             s.emit_struct_field("start_pos", 3, |s| self.start_pos.encode(s))?;
936             s.emit_struct_field("end_pos", 4, |s| self.end_pos.encode(s))?;
937             s.emit_struct_field("lines", 5, |s| {
938                 let lines = &self.lines[..];
939                 // Store the length.
940                 s.emit_u32(lines.len() as u32)?;
941
942                 if !lines.is_empty() {
943                     // In order to preserve some space, we exploit the fact that
944                     // the lines list is sorted and individual lines are
945                     // probably not that long. Because of that we can store lines
946                     // as a difference list, using as little space as possible
947                     // for the differences.
948                     let max_line_length = if lines.len() == 1 {
949                         0
950                     } else {
951                         lines.windows(2).map(|w| w[1] - w[0]).map(|bp| bp.to_usize()).max().unwrap()
952                     };
953
954                     let bytes_per_diff: u8 = match max_line_length {
955                         0..=0xFF => 1,
956                         0x100..=0xFFFF => 2,
957                         _ => 4,
958                     };
959
960                     // Encode the number of bytes used per diff.
961                     bytes_per_diff.encode(s)?;
962
963                     // Encode the first element.
964                     lines[0].encode(s)?;
965
966                     let diff_iter = (&lines[..]).windows(2).map(|w| (w[1] - w[0]));
967
968                     match bytes_per_diff {
969                         1 => {
970                             for diff in diff_iter {
971                                 (diff.0 as u8).encode(s)?
972                             }
973                         }
974                         2 => {
975                             for diff in diff_iter {
976                                 (diff.0 as u16).encode(s)?
977                             }
978                         }
979                         4 => {
980                             for diff in diff_iter {
981                                 diff.0.encode(s)?
982                             }
983                         }
984                         _ => unreachable!(),
985                     }
986                 }
987
988                 Ok(())
989             })?;
990             s.emit_struct_field("multibyte_chars", 6, |s| self.multibyte_chars.encode(s))?;
991             s.emit_struct_field("non_narrow_chars", 7, |s| self.non_narrow_chars.encode(s))?;
992             s.emit_struct_field("name_hash", 8, |s| self.name_hash.encode(s))?;
993             s.emit_struct_field("normalized_pos", 9, |s| self.normalized_pos.encode(s))
994         })
995     }
996 }
997
998 impl Decodable for SourceFile {
999     fn decode<D: Decoder>(d: &mut D) -> Result<SourceFile, D::Error> {
1000         d.read_struct("SourceFile", 8, |d| {
1001             let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
1002             let name_was_remapped: bool =
1003                 d.read_struct_field("name_was_remapped", 1, |d| Decodable::decode(d))?;
1004             let src_hash: u128 = d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?;
1005             let start_pos: BytePos =
1006                 d.read_struct_field("start_pos", 3, |d| Decodable::decode(d))?;
1007             let end_pos: BytePos = d.read_struct_field("end_pos", 4, |d| Decodable::decode(d))?;
1008             let lines: Vec<BytePos> = d.read_struct_field("lines", 5, |d| {
1009                 let num_lines: u32 = Decodable::decode(d)?;
1010                 let mut lines = Vec::with_capacity(num_lines as usize);
1011
1012                 if num_lines > 0 {
1013                     // Read the number of bytes used per diff.
1014                     let bytes_per_diff: u8 = Decodable::decode(d)?;
1015
1016                     // Read the first element.
1017                     let mut line_start: BytePos = Decodable::decode(d)?;
1018                     lines.push(line_start);
1019
1020                     for _ in 1..num_lines {
1021                         let diff = match bytes_per_diff {
1022                             1 => d.read_u8()? as u32,
1023                             2 => d.read_u16()? as u32,
1024                             4 => d.read_u32()?,
1025                             _ => unreachable!(),
1026                         };
1027
1028                         line_start = line_start + BytePos(diff);
1029
1030                         lines.push(line_start);
1031                     }
1032                 }
1033
1034                 Ok(lines)
1035             })?;
1036             let multibyte_chars: Vec<MultiByteChar> =
1037                 d.read_struct_field("multibyte_chars", 6, |d| Decodable::decode(d))?;
1038             let non_narrow_chars: Vec<NonNarrowChar> =
1039                 d.read_struct_field("non_narrow_chars", 7, |d| Decodable::decode(d))?;
1040             let name_hash: u128 = d.read_struct_field("name_hash", 8, |d| Decodable::decode(d))?;
1041             let normalized_pos: Vec<NormalizedPos> =
1042                 d.read_struct_field("normalized_pos", 9, |d| Decodable::decode(d))?;
1043             Ok(SourceFile {
1044                 name,
1045                 name_was_remapped,
1046                 unmapped_path: None,
1047                 // `crate_of_origin` has to be set by the importer.
1048                 // This value matches up with `rustc_hir::def_id::INVALID_CRATE`.
1049                 // That constant is not available here, unfortunately.
1050                 crate_of_origin: std::u32::MAX - 1,
1051                 start_pos,
1052                 end_pos,
1053                 src: None,
1054                 src_hash,
1055                 external_src: Lock::new(ExternalSource::AbsentOk),
1056                 lines,
1057                 multibyte_chars,
1058                 non_narrow_chars,
1059                 normalized_pos,
1060                 name_hash,
1061             })
1062         })
1063     }
1064 }
1065
1066 impl fmt::Debug for SourceFile {
1067     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1068         write!(fmt, "SourceFile({})", self.name)
1069     }
1070 }
1071
1072 impl SourceFile {
1073     pub fn new(
1074         name: FileName,
1075         name_was_remapped: bool,
1076         unmapped_path: FileName,
1077         mut src: String,
1078         start_pos: BytePos,
1079     ) -> Result<SourceFile, OffsetOverflowError> {
1080         let normalized_pos = normalize_src(&mut src, start_pos);
1081
1082         let src_hash = {
1083             let mut hasher: StableHasher = StableHasher::new();
1084             hasher.write(src.as_bytes());
1085             hasher.finish::<u128>()
1086         };
1087         let name_hash = {
1088             let mut hasher: StableHasher = StableHasher::new();
1089             name.hash(&mut hasher);
1090             hasher.finish::<u128>()
1091         };
1092         let end_pos = start_pos.to_usize() + src.len();
1093         if end_pos > u32::max_value() as usize {
1094             return Err(OffsetOverflowError);
1095         }
1096
1097         let (lines, multibyte_chars, non_narrow_chars) =
1098             analyze_source_file::analyze_source_file(&src[..], start_pos);
1099
1100         Ok(SourceFile {
1101             name,
1102             name_was_remapped,
1103             unmapped_path: Some(unmapped_path),
1104             crate_of_origin: 0,
1105             src: Some(Lrc::new(src)),
1106             src_hash,
1107             external_src: Lock::new(ExternalSource::Unneeded),
1108             start_pos,
1109             end_pos: Pos::from_usize(end_pos),
1110             lines,
1111             multibyte_chars,
1112             non_narrow_chars,
1113             normalized_pos,
1114             name_hash,
1115         })
1116     }
1117
1118     /// Returns the `BytePos` of the beginning of the current line.
1119     pub fn line_begin_pos(&self, pos: BytePos) -> BytePos {
1120         let line_index = self.lookup_line(pos).unwrap();
1121         self.lines[line_index]
1122     }
1123
1124     /// Add externally loaded source.
1125     /// If the hash of the input doesn't match or no input is supplied via None,
1126     /// it is interpreted as an error and the corresponding enum variant is set.
1127     /// The return value signifies whether some kind of source is present.
1128     pub fn add_external_src<F>(&self, get_src: F) -> bool
1129     where
1130         F: FnOnce() -> Option<String>,
1131     {
1132         if *self.external_src.borrow() == ExternalSource::AbsentOk {
1133             let src = get_src();
1134             let mut external_src = self.external_src.borrow_mut();
1135             // Check that no-one else have provided the source while we were getting it
1136             if *external_src == ExternalSource::AbsentOk {
1137                 if let Some(src) = src {
1138                     let mut hasher: StableHasher = StableHasher::new();
1139                     hasher.write(src.as_bytes());
1140
1141                     if hasher.finish::<u128>() == self.src_hash {
1142                         *external_src = ExternalSource::Present(src);
1143                         return true;
1144                     }
1145                 } else {
1146                     *external_src = ExternalSource::AbsentErr;
1147                 }
1148
1149                 false
1150             } else {
1151                 self.src.is_some() || external_src.get_source().is_some()
1152             }
1153         } else {
1154             self.src.is_some() || self.external_src.borrow().get_source().is_some()
1155         }
1156     }
1157
1158     /// Gets a line from the list of pre-computed line-beginnings.
1159     /// The line number here is 0-based.
1160     pub fn get_line(&self, line_number: usize) -> Option<Cow<'_, str>> {
1161         fn get_until_newline(src: &str, begin: usize) -> &str {
1162             // We can't use `lines.get(line_number+1)` because we might
1163             // be parsing when we call this function and thus the current
1164             // line is the last one we have line info for.
1165             let slice = &src[begin..];
1166             match slice.find('\n') {
1167                 Some(e) => &slice[..e],
1168                 None => slice,
1169             }
1170         }
1171
1172         let begin = {
1173             let line = if let Some(line) = self.lines.get(line_number) {
1174                 line
1175             } else {
1176                 return None;
1177             };
1178             let begin: BytePos = *line - self.start_pos;
1179             begin.to_usize()
1180         };
1181
1182         if let Some(ref src) = self.src {
1183             Some(Cow::from(get_until_newline(src, begin)))
1184         } else if let Some(src) = self.external_src.borrow().get_source() {
1185             Some(Cow::Owned(String::from(get_until_newline(src, begin))))
1186         } else {
1187             None
1188         }
1189     }
1190
1191     pub fn is_real_file(&self) -> bool {
1192         self.name.is_real()
1193     }
1194
1195     pub fn is_imported(&self) -> bool {
1196         self.src.is_none()
1197     }
1198
1199     pub fn byte_length(&self) -> u32 {
1200         self.end_pos.0 - self.start_pos.0
1201     }
1202     pub fn count_lines(&self) -> usize {
1203         self.lines.len()
1204     }
1205
1206     /// Finds the line containing the given position. The return value is the
1207     /// index into the `lines` array of this `SourceFile`, not the 1-based line
1208     /// number. If the source_file is empty or the position is located before the
1209     /// first line, `None` is returned.
1210     pub fn lookup_line(&self, pos: BytePos) -> Option<usize> {
1211         if self.lines.len() == 0 {
1212             return None;
1213         }
1214
1215         let line_index = lookup_line(&self.lines[..], pos);
1216         assert!(line_index < self.lines.len() as isize);
1217         if line_index >= 0 { Some(line_index as usize) } else { None }
1218     }
1219
1220     pub fn line_bounds(&self, line_index: usize) -> (BytePos, BytePos) {
1221         if self.start_pos == self.end_pos {
1222             return (self.start_pos, self.end_pos);
1223         }
1224
1225         assert!(line_index < self.lines.len());
1226         if line_index == (self.lines.len() - 1) {
1227             (self.lines[line_index], self.end_pos)
1228         } else {
1229             (self.lines[line_index], self.lines[line_index + 1])
1230         }
1231     }
1232
1233     #[inline]
1234     pub fn contains(&self, byte_pos: BytePos) -> bool {
1235         byte_pos >= self.start_pos && byte_pos <= self.end_pos
1236     }
1237
1238     /// Calculates the original byte position relative to the start of the file
1239     /// based on the given byte position.
1240     pub fn original_relative_byte_pos(&self, pos: BytePos) -> BytePos {
1241         // Diff before any records is 0. Otherwise use the previously recorded
1242         // diff as that applies to the following characters until a new diff
1243         // is recorded.
1244         let diff = match self.normalized_pos.binary_search_by(|np| np.pos.cmp(&pos)) {
1245             Ok(i) => self.normalized_pos[i].diff,
1246             Err(i) if i == 0 => 0,
1247             Err(i) => self.normalized_pos[i - 1].diff,
1248         };
1249
1250         BytePos::from_u32(pos.0 - self.start_pos.0 + diff)
1251     }
1252 }
1253
1254 /// Normalizes the source code and records the normalizations.
1255 fn normalize_src(src: &mut String, start_pos: BytePos) -> Vec<NormalizedPos> {
1256     let mut normalized_pos = vec![];
1257     remove_bom(src, &mut normalized_pos);
1258     normalize_newlines(src, &mut normalized_pos);
1259
1260     // Offset all the positions by start_pos to match the final file positions.
1261     for np in &mut normalized_pos {
1262         np.pos.0 += start_pos.0;
1263     }
1264
1265     normalized_pos
1266 }
1267
1268 /// Removes UTF-8 BOM, if any.
1269 fn remove_bom(src: &mut String, normalized_pos: &mut Vec<NormalizedPos>) {
1270     if src.starts_with("\u{feff}") {
1271         src.drain(..3);
1272         normalized_pos.push(NormalizedPos { pos: BytePos(0), diff: 3 });
1273     }
1274 }
1275
1276 /// Replaces `\r\n` with `\n` in-place in `src`.
1277 ///
1278 /// Returns error if there's a lone `\r` in the string
1279 fn normalize_newlines(src: &mut String, normalized_pos: &mut Vec<NormalizedPos>) {
1280     if !src.as_bytes().contains(&b'\r') {
1281         return;
1282     }
1283
1284     // We replace `\r\n` with `\n` in-place, which doesn't break utf-8 encoding.
1285     // While we *can* call `as_mut_vec` and do surgery on the live string
1286     // directly, let's rather steal the contents of `src`. This makes the code
1287     // safe even if a panic occurs.
1288
1289     let mut buf = std::mem::replace(src, String::new()).into_bytes();
1290     let mut gap_len = 0;
1291     let mut tail = buf.as_mut_slice();
1292     let mut cursor = 0;
1293     let original_gap = normalized_pos.last().map_or(0, |l| l.diff);
1294     loop {
1295         let idx = match find_crlf(&tail[gap_len..]) {
1296             None => tail.len(),
1297             Some(idx) => idx + gap_len,
1298         };
1299         tail.copy_within(gap_len..idx, 0);
1300         tail = &mut tail[idx - gap_len..];
1301         if tail.len() == gap_len {
1302             break;
1303         }
1304         cursor += idx - gap_len;
1305         gap_len += 1;
1306         normalized_pos.push(NormalizedPos {
1307             pos: BytePos::from_usize(cursor + 1),
1308             diff: original_gap + gap_len as u32,
1309         });
1310     }
1311
1312     // Account for removed `\r`.
1313     // After `set_len`, `buf` is guaranteed to contain utf-8 again.
1314     let new_len = buf.len() - gap_len;
1315     unsafe {
1316         buf.set_len(new_len);
1317         *src = String::from_utf8_unchecked(buf);
1318     }
1319
1320     fn find_crlf(src: &[u8]) -> Option<usize> {
1321         let mut search_idx = 0;
1322         while let Some(idx) = find_cr(&src[search_idx..]) {
1323             if src[search_idx..].get(idx + 1) != Some(&b'\n') {
1324                 search_idx += idx + 1;
1325                 continue;
1326             }
1327             return Some(search_idx + idx);
1328         }
1329         None
1330     }
1331
1332     fn find_cr(src: &[u8]) -> Option<usize> {
1333         src.iter().position(|&b| b == b'\r')
1334     }
1335 }
1336
1337 // _____________________________________________________________________________
1338 // Pos, BytePos, CharPos
1339 //
1340
1341 pub trait Pos {
1342     fn from_usize(n: usize) -> Self;
1343     fn to_usize(&self) -> usize;
1344     fn from_u32(n: u32) -> Self;
1345     fn to_u32(&self) -> u32;
1346 }
1347
1348 /// A byte offset. Keep this small (currently 32-bits), as AST contains
1349 /// a lot of them.
1350 #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
1351 pub struct BytePos(pub u32);
1352
1353 /// A character offset. Because of multibyte UTF-8 characters, a byte offset
1354 /// is not equivalent to a character offset. The `SourceMap` will convert `BytePos`
1355 /// values to `CharPos` values as necessary.
1356 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
1357 pub struct CharPos(pub usize);
1358
1359 // FIXME: lots of boilerplate in these impls, but so far my attempts to fix
1360 // have been unsuccessful.
1361
1362 impl Pos for BytePos {
1363     #[inline(always)]
1364     fn from_usize(n: usize) -> BytePos {
1365         BytePos(n as u32)
1366     }
1367
1368     #[inline(always)]
1369     fn to_usize(&self) -> usize {
1370         self.0 as usize
1371     }
1372
1373     #[inline(always)]
1374     fn from_u32(n: u32) -> BytePos {
1375         BytePos(n)
1376     }
1377
1378     #[inline(always)]
1379     fn to_u32(&self) -> u32 {
1380         self.0
1381     }
1382 }
1383
1384 impl Add for BytePos {
1385     type Output = BytePos;
1386
1387     #[inline(always)]
1388     fn add(self, rhs: BytePos) -> BytePos {
1389         BytePos((self.to_usize() + rhs.to_usize()) as u32)
1390     }
1391 }
1392
1393 impl Sub for BytePos {
1394     type Output = BytePos;
1395
1396     #[inline(always)]
1397     fn sub(self, rhs: BytePos) -> BytePos {
1398         BytePos((self.to_usize() - rhs.to_usize()) as u32)
1399     }
1400 }
1401
1402 impl Encodable for BytePos {
1403     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
1404         s.emit_u32(self.0)
1405     }
1406 }
1407
1408 impl Decodable for BytePos {
1409     fn decode<D: Decoder>(d: &mut D) -> Result<BytePos, D::Error> {
1410         Ok(BytePos(d.read_u32()?))
1411     }
1412 }
1413
1414 impl Pos for CharPos {
1415     #[inline(always)]
1416     fn from_usize(n: usize) -> CharPos {
1417         CharPos(n)
1418     }
1419
1420     #[inline(always)]
1421     fn to_usize(&self) -> usize {
1422         self.0
1423     }
1424
1425     #[inline(always)]
1426     fn from_u32(n: u32) -> CharPos {
1427         CharPos(n as usize)
1428     }
1429
1430     #[inline(always)]
1431     fn to_u32(&self) -> u32 {
1432         self.0 as u32
1433     }
1434 }
1435
1436 impl Add for CharPos {
1437     type Output = CharPos;
1438
1439     #[inline(always)]
1440     fn add(self, rhs: CharPos) -> CharPos {
1441         CharPos(self.to_usize() + rhs.to_usize())
1442     }
1443 }
1444
1445 impl Sub for CharPos {
1446     type Output = CharPos;
1447
1448     #[inline(always)]
1449     fn sub(self, rhs: CharPos) -> CharPos {
1450         CharPos(self.to_usize() - rhs.to_usize())
1451     }
1452 }
1453
1454 // _____________________________________________________________________________
1455 // Loc, SourceFileAndLine, SourceFileAndBytePos
1456 //
1457
1458 /// A source code location used for error reporting.
1459 #[derive(Debug, Clone)]
1460 pub struct Loc {
1461     /// Information about the original source.
1462     pub file: Lrc<SourceFile>,
1463     /// The (1-based) line number.
1464     pub line: usize,
1465     /// The (0-based) column offset.
1466     pub col: CharPos,
1467     /// The (0-based) column offset when displayed.
1468     pub col_display: usize,
1469 }
1470
1471 // Used to be structural records.
1472 #[derive(Debug)]
1473 pub struct SourceFileAndLine {
1474     pub sf: Lrc<SourceFile>,
1475     pub line: usize,
1476 }
1477 #[derive(Debug)]
1478 pub struct SourceFileAndBytePos {
1479     pub sf: Lrc<SourceFile>,
1480     pub pos: BytePos,
1481 }
1482
1483 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1484 pub struct LineInfo {
1485     /// Index of line, starting from 0.
1486     pub line_index: usize,
1487
1488     /// Column in line where span begins, starting from 0.
1489     pub start_col: CharPos,
1490
1491     /// Column in line where span ends, starting from 0, exclusive.
1492     pub end_col: CharPos,
1493 }
1494
1495 pub struct FileLines {
1496     pub file: Lrc<SourceFile>,
1497     pub lines: Vec<LineInfo>,
1498 }
1499
1500 pub static SPAN_DEBUG: AtomicRef<fn(Span, &mut fmt::Formatter<'_>) -> fmt::Result> =
1501     AtomicRef::new(&(default_span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
1502
1503 // _____________________________________________________________________________
1504 // SpanLinesError, SpanSnippetError, DistinctSources, MalformedSourceMapPositions
1505 //
1506
1507 pub type FileLinesResult = Result<FileLines, SpanLinesError>;
1508
1509 #[derive(Clone, PartialEq, Eq, Debug)]
1510 pub enum SpanLinesError {
1511     DistinctSources(DistinctSources),
1512 }
1513
1514 #[derive(Clone, PartialEq, Eq, Debug)]
1515 pub enum SpanSnippetError {
1516     IllFormedSpan(Span),
1517     DistinctSources(DistinctSources),
1518     MalformedForSourcemap(MalformedSourceMapPositions),
1519     SourceNotAvailable { filename: FileName },
1520 }
1521
1522 #[derive(Clone, PartialEq, Eq, Debug)]
1523 pub struct DistinctSources {
1524     pub begin: (FileName, BytePos),
1525     pub end: (FileName, BytePos),
1526 }
1527
1528 #[derive(Clone, PartialEq, Eq, Debug)]
1529 pub struct MalformedSourceMapPositions {
1530     pub name: FileName,
1531     pub source_len: usize,
1532     pub begin_pos: BytePos,
1533     pub end_pos: BytePos,
1534 }
1535
1536 /// Range inside of a `Span` used for diagnostics when we only have access to relative positions.
1537 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
1538 pub struct InnerSpan {
1539     pub start: usize,
1540     pub end: usize,
1541 }
1542
1543 impl InnerSpan {
1544     pub fn new(start: usize, end: usize) -> InnerSpan {
1545         InnerSpan { start, end }
1546     }
1547 }
1548
1549 // Given a slice of line start positions and a position, returns the index of
1550 // the line the position is on. Returns -1 if the position is located before
1551 // the first line.
1552 fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
1553     match lines.binary_search(&pos) {
1554         Ok(line) => line as isize,
1555         Err(line) => line as isize - 1,
1556     }
1557 }
1558
1559 /// Requirements for a `StableHashingContext` to be used in this crate.
1560 /// This is a hack to allow using the `HashStable_Generic` derive macro
1561 /// instead of implementing everything in librustc.
1562 pub trait HashStableContext {
1563     fn hash_spans(&self) -> bool;
1564     fn byte_pos_to_line_and_col(
1565         &mut self,
1566         byte: BytePos,
1567     ) -> Option<(Lrc<SourceFile>, usize, BytePos)>;
1568 }
1569
1570 impl<CTX> HashStable<CTX> for Span
1571 where
1572     CTX: HashStableContext,
1573 {
1574     /// Hashes a span in a stable way. We can't directly hash the span's `BytePos`
1575     /// fields (that would be similar to hashing pointers, since those are just
1576     /// offsets into the `SourceMap`). Instead, we hash the (file name, line, column)
1577     /// triple, which stays the same even if the containing `SourceFile` has moved
1578     /// within the `SourceMap`.
1579     /// Also note that we are hashing byte offsets for the column, not unicode
1580     /// codepoint offsets. For the purpose of the hash that's sufficient.
1581     /// Also, hashing filenames is expensive so we avoid doing it twice when the
1582     /// span starts and ends in the same file, which is almost always the case.
1583     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
1584         const TAG_VALID_SPAN: u8 = 0;
1585         const TAG_INVALID_SPAN: u8 = 1;
1586         const TAG_EXPANSION: u8 = 0;
1587         const TAG_NO_EXPANSION: u8 = 1;
1588
1589         if !ctx.hash_spans() {
1590             return;
1591         }
1592
1593         if *self == DUMMY_SP {
1594             return std::hash::Hash::hash(&TAG_INVALID_SPAN, hasher);
1595         }
1596
1597         // If this is not an empty or invalid span, we want to hash the last
1598         // position that belongs to it, as opposed to hashing the first
1599         // position past it.
1600         let span = self.data();
1601         let (file_lo, line_lo, col_lo) = match ctx.byte_pos_to_line_and_col(span.lo) {
1602             Some(pos) => pos,
1603             None => {
1604                 return std::hash::Hash::hash(&TAG_INVALID_SPAN, hasher);
1605             }
1606         };
1607
1608         if !file_lo.contains(span.hi) {
1609             return std::hash::Hash::hash(&TAG_INVALID_SPAN, hasher);
1610         }
1611
1612         std::hash::Hash::hash(&TAG_VALID_SPAN, hasher);
1613         // We truncate the stable ID hash and line and column numbers. The chances
1614         // of causing a collision this way should be minimal.
1615         std::hash::Hash::hash(&(file_lo.name_hash as u64), hasher);
1616
1617         let col = (col_lo.0 as u64) & 0xFF;
1618         let line = ((line_lo as u64) & 0xFF_FF_FF) << 8;
1619         let len = ((span.hi - span.lo).0 as u64) << 32;
1620         let line_col_len = col | line | len;
1621         std::hash::Hash::hash(&line_col_len, hasher);
1622
1623         if span.ctxt == SyntaxContext::root() {
1624             TAG_NO_EXPANSION.hash_stable(ctx, hasher);
1625         } else {
1626             TAG_EXPANSION.hash_stable(ctx, hasher);
1627
1628             // Since the same expansion context is usually referenced many
1629             // times, we cache a stable hash of it and hash that instead of
1630             // recursing every time.
1631             thread_local! {
1632                 static CACHE: RefCell<FxHashMap<hygiene::ExpnId, u64>> = Default::default();
1633             }
1634
1635             let sub_hash: u64 = CACHE.with(|cache| {
1636                 let expn_id = span.ctxt.outer_expn();
1637
1638                 if let Some(&sub_hash) = cache.borrow().get(&expn_id) {
1639                     return sub_hash;
1640                 }
1641
1642                 let mut hasher = StableHasher::new();
1643                 expn_id.expn_data().hash_stable(ctx, &mut hasher);
1644                 let sub_hash: Fingerprint = hasher.finish();
1645                 let sub_hash = sub_hash.to_smaller_hash();
1646                 cache.borrow_mut().insert(expn_id, sub_hash);
1647                 sub_hash
1648             });
1649
1650             sub_hash.hash_stable(ctx, hasher);
1651         }
1652     }
1653 }