rustc_span/
lib.rs

1//! Source positions and related helper functions.
2//!
3//! Important concepts in this module include:
4//!
5//! - the *span*, represented by [`SpanData`] and related types;
6//! - source code as represented by a [`SourceMap`]; and
7//! - interned strings, represented by [`Symbol`]s, with some common symbols available statically
8//!   in the [`sym`] module.
9//!
10//! Unlike most compilers, the span contains not only the position in the source code, but also
11//! various other metadata, such as the edition and macro hygiene. This metadata is stored in
12//! [`SyntaxContext`] and [`ExpnData`].
13//!
14//! ## Note
15//!
16//! This API is completely unstable and subject to change.
17
18// tidy-alphabetical-start
19#![allow(internal_features)]
20#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
21#![doc(rust_logo)]
22#![feature(array_windows)]
23#![feature(cfg_select)]
24#![feature(core_io_borrowed_buf)]
25#![feature(if_let_guard)]
26#![feature(map_try_insert)]
27#![feature(negative_impls)]
28#![feature(read_buf)]
29#![feature(round_char_boundary)]
30#![feature(rustc_attrs)]
31#![feature(rustdoc_internals)]
32// tidy-alphabetical-end
33
34// The code produced by the `Encodable`/`Decodable` derive macros refer to
35// `rustc_span::Span{Encoder,Decoder}`. That's fine outside this crate, but doesn't work inside
36// this crate without this line making `rustc_span` available.
37extern crate self as rustc_span;
38
39use derive_where::derive_where;
40use rustc_data_structures::{AtomicRef, outline};
41use rustc_macros::{Decodable, Encodable, HashStable_Generic};
42use rustc_serialize::opaque::{FileEncoder, MemDecoder};
43use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
44use tracing::debug;
45
46mod caching_source_map_view;
47pub mod source_map;
48use source_map::{SourceMap, SourceMapInputs};
49
50pub use self::caching_source_map_view::CachingSourceMapView;
51use crate::fatal_error::FatalError;
52
53pub mod edition;
54use edition::Edition;
55pub mod hygiene;
56use hygiene::Transparency;
57pub use hygiene::{
58    DesugaringKind, ExpnData, ExpnHash, ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext,
59};
60use rustc_data_structures::stable_hasher::HashingControls;
61pub mod def_id;
62use def_id::{CrateNum, DefId, DefIndex, DefPathHash, LOCAL_CRATE, LocalDefId, StableCrateId};
63pub mod edit_distance;
64mod span_encoding;
65pub use span_encoding::{DUMMY_SP, Span};
66
67pub mod symbol;
68pub use symbol::{
69    ByteSymbol, Ident, MacroRulesNormalizedIdent, STDLIB_STABLE_CRATES, Symbol, kw, sym,
70};
71
72mod analyze_source_file;
73pub mod fatal_error;
74
75pub mod profiling;
76
77use std::borrow::Cow;
78use std::cmp::{self, Ordering};
79use std::fmt::Display;
80use std::hash::Hash;
81use std::io::{self, Read};
82use std::ops::{Add, Range, Sub};
83use std::path::{Path, PathBuf};
84use std::str::FromStr;
85use std::sync::Arc;
86use std::{fmt, iter};
87
88use md5::{Digest, Md5};
89use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
90use rustc_data_structures::sync::{FreezeLock, FreezeWriteGuard, Lock};
91use rustc_data_structures::unord::UnordMap;
92use rustc_hashes::{Hash64, Hash128};
93use sha1::Sha1;
94use sha2::Sha256;
95
96#[cfg(test)]
97mod tests;
98
99/// Per-session global variables: this struct is stored in thread-local storage
100/// in such a way that it is accessible without any kind of handle to all
101/// threads within the compilation session, but is not accessible outside the
102/// session.
103pub struct SessionGlobals {
104    symbol_interner: symbol::Interner,
105    span_interner: Lock<span_encoding::SpanInterner>,
106    /// Maps a macro argument token into use of the corresponding metavariable in the macro body.
107    /// Collisions are possible and processed in `maybe_use_metavar_location` on best effort basis.
108    metavar_spans: MetavarSpansMap,
109    hygiene_data: Lock<hygiene::HygieneData>,
110
111    /// The session's source map, if there is one. This field should only be
112    /// used in places where the `Session` is truly not available, such as
113    /// `<Span as Debug>::fmt`.
114    source_map: Option<Arc<SourceMap>>,
115}
116
117impl SessionGlobals {
118    pub fn new(
119        edition: Edition,
120        extra_symbols: &[&'static str],
121        sm_inputs: Option<SourceMapInputs>,
122    ) -> SessionGlobals {
123        SessionGlobals {
124            symbol_interner: symbol::Interner::with_extra_symbols(extra_symbols),
125            span_interner: Lock::new(span_encoding::SpanInterner::default()),
126            metavar_spans: Default::default(),
127            hygiene_data: Lock::new(hygiene::HygieneData::new(edition)),
128            source_map: sm_inputs.map(|inputs| Arc::new(SourceMap::with_inputs(inputs))),
129        }
130    }
131}
132
133pub fn create_session_globals_then<R>(
134    edition: Edition,
135    extra_symbols: &[&'static str],
136    sm_inputs: Option<SourceMapInputs>,
137    f: impl FnOnce() -> R,
138) -> R {
139    assert!(
140        !SESSION_GLOBALS.is_set(),
141        "SESSION_GLOBALS should never be overwritten! \
142         Use another thread if you need another SessionGlobals"
143    );
144    let session_globals = SessionGlobals::new(edition, extra_symbols, sm_inputs);
145    SESSION_GLOBALS.set(&session_globals, f)
146}
147
148pub fn set_session_globals_then<R>(session_globals: &SessionGlobals, f: impl FnOnce() -> R) -> R {
149    assert!(
150        !SESSION_GLOBALS.is_set(),
151        "SESSION_GLOBALS should never be overwritten! \
152         Use another thread if you need another SessionGlobals"
153    );
154    SESSION_GLOBALS.set(session_globals, f)
155}
156
157/// No source map.
158pub fn create_session_if_not_set_then<R, F>(edition: Edition, f: F) -> R
159where
160    F: FnOnce(&SessionGlobals) -> R,
161{
162    if !SESSION_GLOBALS.is_set() {
163        let session_globals = SessionGlobals::new(edition, &[], None);
164        SESSION_GLOBALS.set(&session_globals, || SESSION_GLOBALS.with(f))
165    } else {
166        SESSION_GLOBALS.with(f)
167    }
168}
169
170#[inline]
171pub fn with_session_globals<R, F>(f: F) -> R
172where
173    F: FnOnce(&SessionGlobals) -> R,
174{
175    SESSION_GLOBALS.with(f)
176}
177
178/// Default edition, no source map.
179pub fn create_default_session_globals_then<R>(f: impl FnOnce() -> R) -> R {
180    create_session_globals_then(edition::DEFAULT_EDITION, &[], None, f)
181}
182
183// If this ever becomes non thread-local, `decode_syntax_context`
184// and `decode_expn_id` will need to be updated to handle concurrent
185// deserialization.
186scoped_tls::scoped_thread_local!(static SESSION_GLOBALS: SessionGlobals);
187
188#[derive(Default)]
189pub struct MetavarSpansMap(FreezeLock<UnordMap<Span, (Span, bool)>>);
190
191impl MetavarSpansMap {
192    pub fn insert(&self, span: Span, var_span: Span) -> bool {
193        match self.0.write().try_insert(span, (var_span, false)) {
194            Ok(_) => true,
195            Err(entry) => entry.entry.get().0 == var_span,
196        }
197    }
198
199    /// Read a span and record that it was read.
200    pub fn get(&self, span: Span) -> Option<Span> {
201        if let Some(mut mspans) = self.0.try_write() {
202            if let Some((var_span, read)) = mspans.get_mut(&span) {
203                *read = true;
204                Some(*var_span)
205            } else {
206                None
207            }
208        } else {
209            if let Some((span, true)) = self.0.read().get(&span) { Some(*span) } else { None }
210        }
211    }
212
213    /// Freeze the set, and return the spans which have been read.
214    ///
215    /// After this is frozen, no spans that have not been read can be read.
216    pub fn freeze_and_get_read_spans(&self) -> UnordMap<Span, Span> {
217        self.0.freeze().items().filter(|(_, (_, b))| *b).map(|(s1, (s2, _))| (*s1, *s2)).collect()
218    }
219}
220
221#[inline]
222pub fn with_metavar_spans<R>(f: impl FnOnce(&MetavarSpansMap) -> R) -> R {
223    with_session_globals(|session_globals| f(&session_globals.metavar_spans))
224}
225
226// FIXME: We should use this enum or something like it to get rid of the
227// use of magic `/rust/1.x/...` paths across the board.
228#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Decodable, Encodable)]
229pub enum RealFileName {
230    LocalPath(PathBuf),
231    /// For remapped paths (namely paths into libstd that have been mapped
232    /// to the appropriate spot on the local host's file system, and local file
233    /// system paths that have been remapped with `FilePathMapping`),
234    Remapped {
235        /// `local_path` is the (host-dependent) local path to the file. This is
236        /// None if the file was imported from another crate
237        local_path: Option<PathBuf>,
238        /// `virtual_name` is the stable path rustc will store internally within
239        /// build artifacts.
240        virtual_name: PathBuf,
241    },
242}
243
244impl Hash for RealFileName {
245    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
246        // To prevent #70924 from happening again we should only hash the
247        // remapped (virtualized) path if that exists. This is because
248        // virtualized paths to sysroot crates (/rust/$hash or /rust/$version)
249        // remain stable even if the corresponding local_path changes
250        self.remapped_path_if_available().hash(state)
251    }
252}
253
254impl RealFileName {
255    /// Returns the path suitable for reading from the file system on the local host,
256    /// if this information exists.
257    /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
258    pub fn local_path(&self) -> Option<&Path> {
259        match self {
260            RealFileName::LocalPath(p) => Some(p),
261            RealFileName::Remapped { local_path, virtual_name: _ } => local_path.as_deref(),
262        }
263    }
264
265    /// Returns the path suitable for reading from the file system on the local host,
266    /// if this information exists.
267    /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
268    pub fn into_local_path(self) -> Option<PathBuf> {
269        match self {
270            RealFileName::LocalPath(p) => Some(p),
271            RealFileName::Remapped { local_path: p, virtual_name: _ } => p,
272        }
273    }
274
275    /// Returns the path suitable for embedding into build artifacts. This would still
276    /// be a local path if it has not been remapped. A remapped path will not correspond
277    /// to a valid file system path: see `local_path_if_available()` for something that
278    /// is more likely to return paths into the local host file system.
279    pub fn remapped_path_if_available(&self) -> &Path {
280        match self {
281            RealFileName::LocalPath(p)
282            | RealFileName::Remapped { local_path: _, virtual_name: p } => p,
283        }
284    }
285
286    /// Returns the path suitable for reading from the file system on the local host,
287    /// if this information exists. Otherwise returns the remapped name.
288    /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
289    pub fn local_path_if_available(&self) -> &Path {
290        match self {
291            RealFileName::LocalPath(path)
292            | RealFileName::Remapped { local_path: None, virtual_name: path }
293            | RealFileName::Remapped { local_path: Some(path), virtual_name: _ } => path,
294        }
295    }
296
297    /// Return the path remapped or not depending on the [`FileNameDisplayPreference`].
298    ///
299    /// For the purpose of this function, local and short preference are equal.
300    pub fn to_path(&self, display_pref: FileNameDisplayPreference) -> &Path {
301        match display_pref {
302            FileNameDisplayPreference::Local | FileNameDisplayPreference::Short => {
303                self.local_path_if_available()
304            }
305            FileNameDisplayPreference::Remapped => self.remapped_path_if_available(),
306        }
307    }
308
309    pub fn to_string_lossy(&self, display_pref: FileNameDisplayPreference) -> Cow<'_, str> {
310        match display_pref {
311            FileNameDisplayPreference::Local => self.local_path_if_available().to_string_lossy(),
312            FileNameDisplayPreference::Remapped => {
313                self.remapped_path_if_available().to_string_lossy()
314            }
315            FileNameDisplayPreference::Short => self
316                .local_path_if_available()
317                .file_name()
318                .map_or_else(|| "".into(), |f| f.to_string_lossy()),
319        }
320    }
321}
322
323/// Differentiates between real files and common virtual files.
324#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash, Decodable, Encodable)]
325pub enum FileName {
326    Real(RealFileName),
327    /// Strings provided as `--cfg [cfgspec]`.
328    CfgSpec(Hash64),
329    /// Command line.
330    Anon(Hash64),
331    /// Hack in `src/librustc_ast/parse.rs`.
332    // FIXME(jseyfried)
333    MacroExpansion(Hash64),
334    ProcMacroSourceCode(Hash64),
335    /// Strings provided as crate attributes in the CLI.
336    CliCrateAttr(Hash64),
337    /// Custom sources for explicit parser calls from plugins and drivers.
338    Custom(String),
339    DocTest(PathBuf, isize),
340    /// Post-substitution inline assembly from LLVM.
341    InlineAsm(Hash64),
342}
343
344impl From<PathBuf> for FileName {
345    fn from(p: PathBuf) -> Self {
346        FileName::Real(RealFileName::LocalPath(p))
347    }
348}
349
350#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
351pub enum FileNameEmbeddablePreference {
352    /// If a remapped path is available, only embed the `virtual_path` and omit the `local_path`.
353    ///
354    /// Otherwise embed the local-path into the `virtual_path`.
355    RemappedOnly,
356    /// Embed the original path as well as its remapped `virtual_path` component if available.
357    LocalAndRemapped,
358}
359
360#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
361pub enum FileNameDisplayPreference {
362    /// Display the path after the application of rewrite rules provided via `--remap-path-prefix`.
363    /// This is appropriate for paths that get embedded into files produced by the compiler.
364    Remapped,
365    /// Display the path before the application of rewrite rules provided via `--remap-path-prefix`.
366    /// This is appropriate for use in user-facing output (such as diagnostics).
367    Local,
368    /// Display only the filename, as a way to reduce the verbosity of the output.
369    /// This is appropriate for use in user-facing output (such as diagnostics).
370    Short,
371}
372
373pub struct FileNameDisplay<'a> {
374    inner: &'a FileName,
375    display_pref: FileNameDisplayPreference,
376}
377
378impl fmt::Display for FileNameDisplay<'_> {
379    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
380        use FileName::*;
381        match *self.inner {
382            Real(ref name) => {
383                write!(fmt, "{}", name.to_string_lossy(self.display_pref))
384            }
385            CfgSpec(_) => write!(fmt, "<cfgspec>"),
386            MacroExpansion(_) => write!(fmt, "<macro expansion>"),
387            Anon(_) => write!(fmt, "<anon>"),
388            ProcMacroSourceCode(_) => write!(fmt, "<proc-macro source code>"),
389            CliCrateAttr(_) => write!(fmt, "<crate attribute>"),
390            Custom(ref s) => write!(fmt, "<{s}>"),
391            DocTest(ref path, _) => write!(fmt, "{}", path.display()),
392            InlineAsm(_) => write!(fmt, "<inline asm>"),
393        }
394    }
395}
396
397impl<'a> FileNameDisplay<'a> {
398    pub fn to_string_lossy(&self) -> Cow<'a, str> {
399        match self.inner {
400            FileName::Real(inner) => inner.to_string_lossy(self.display_pref),
401            _ => Cow::from(self.to_string()),
402        }
403    }
404}
405
406impl FileName {
407    pub fn is_real(&self) -> bool {
408        use FileName::*;
409        match *self {
410            Real(_) => true,
411            Anon(_)
412            | MacroExpansion(_)
413            | ProcMacroSourceCode(_)
414            | CliCrateAttr(_)
415            | Custom(_)
416            | CfgSpec(_)
417            | DocTest(_, _)
418            | InlineAsm(_) => false,
419        }
420    }
421
422    pub fn prefer_remapped_unconditionally(&self) -> FileNameDisplay<'_> {
423        FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Remapped }
424    }
425
426    /// This may include transient local filesystem information.
427    /// Must not be embedded in build outputs.
428    pub fn prefer_local(&self) -> FileNameDisplay<'_> {
429        FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Local }
430    }
431
432    pub fn display(&self, display_pref: FileNameDisplayPreference) -> FileNameDisplay<'_> {
433        FileNameDisplay { inner: self, display_pref }
434    }
435
436    pub fn macro_expansion_source_code(src: &str) -> FileName {
437        let mut hasher = StableHasher::new();
438        src.hash(&mut hasher);
439        FileName::MacroExpansion(hasher.finish())
440    }
441
442    pub fn anon_source_code(src: &str) -> FileName {
443        let mut hasher = StableHasher::new();
444        src.hash(&mut hasher);
445        FileName::Anon(hasher.finish())
446    }
447
448    pub fn proc_macro_source_code(src: &str) -> FileName {
449        let mut hasher = StableHasher::new();
450        src.hash(&mut hasher);
451        FileName::ProcMacroSourceCode(hasher.finish())
452    }
453
454    pub fn cfg_spec_source_code(src: &str) -> FileName {
455        let mut hasher = StableHasher::new();
456        src.hash(&mut hasher);
457        FileName::CfgSpec(hasher.finish())
458    }
459
460    pub fn cli_crate_attr_source_code(src: &str) -> FileName {
461        let mut hasher = StableHasher::new();
462        src.hash(&mut hasher);
463        FileName::CliCrateAttr(hasher.finish())
464    }
465
466    pub fn doc_test_source_code(path: PathBuf, line: isize) -> FileName {
467        FileName::DocTest(path, line)
468    }
469
470    pub fn inline_asm_source_code(src: &str) -> FileName {
471        let mut hasher = StableHasher::new();
472        src.hash(&mut hasher);
473        FileName::InlineAsm(hasher.finish())
474    }
475
476    /// Returns the path suitable for reading from the file system on the local host,
477    /// if this information exists.
478    /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
479    pub fn into_local_path(self) -> Option<PathBuf> {
480        match self {
481            FileName::Real(path) => path.into_local_path(),
482            FileName::DocTest(path, _) => Some(path),
483            _ => None,
484        }
485    }
486}
487
488/// Represents a span.
489///
490/// Spans represent a region of code, used for error reporting. Positions in spans
491/// are *absolute* positions from the beginning of the [`SourceMap`], not positions
492/// relative to [`SourceFile`]s. Methods on the `SourceMap` can be used to relate spans back
493/// to the original source.
494///
495/// You must be careful if the span crosses more than one file, since you will not be
496/// able to use many of the functions on spans in source_map and you cannot assume
497/// that the length of the span is equal to `span.hi - span.lo`; there may be space in the
498/// [`BytePos`] range between files.
499///
500/// `SpanData` is public because `Span` uses a thread-local interner and can't be
501/// sent to other threads, but some pieces of performance infra run in a separate thread.
502/// Using `Span` is generally preferred.
503#[derive(Clone, Copy, Hash, PartialEq, Eq)]
504#[derive_where(PartialOrd, Ord)]
505pub struct SpanData {
506    pub lo: BytePos,
507    pub hi: BytePos,
508    /// Information about where the macro came from, if this piece of
509    /// code was created by a macro expansion.
510    #[derive_where(skip)]
511    // `SyntaxContext` does not implement `Ord`.
512    // The other fields are enough to determine in-file order.
513    pub ctxt: SyntaxContext,
514    #[derive_where(skip)]
515    // `LocalDefId` does not implement `Ord`.
516    // The other fields are enough to determine in-file order.
517    pub parent: Option<LocalDefId>,
518}
519
520impl SpanData {
521    #[inline]
522    pub fn span(&self) -> Span {
523        Span::new(self.lo, self.hi, self.ctxt, self.parent)
524    }
525    #[inline]
526    pub fn with_lo(&self, lo: BytePos) -> Span {
527        Span::new(lo, self.hi, self.ctxt, self.parent)
528    }
529    #[inline]
530    pub fn with_hi(&self, hi: BytePos) -> Span {
531        Span::new(self.lo, hi, self.ctxt, self.parent)
532    }
533    /// Avoid if possible, `Span::map_ctxt` should be preferred.
534    #[inline]
535    fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
536        Span::new(self.lo, self.hi, ctxt, self.parent)
537    }
538    /// Avoid if possible, `Span::with_parent` should be preferred.
539    #[inline]
540    fn with_parent(&self, parent: Option<LocalDefId>) -> Span {
541        Span::new(self.lo, self.hi, self.ctxt, parent)
542    }
543    /// Returns `true` if this is a dummy span with any hygienic context.
544    #[inline]
545    pub fn is_dummy(self) -> bool {
546        self.lo.0 == 0 && self.hi.0 == 0
547    }
548    /// Returns `true` if `self` fully encloses `other`.
549    pub fn contains(self, other: Self) -> bool {
550        self.lo <= other.lo && other.hi <= self.hi
551    }
552}
553
554impl Default for SpanData {
555    fn default() -> Self {
556        Self { lo: BytePos(0), hi: BytePos(0), ctxt: SyntaxContext::root(), parent: None }
557    }
558}
559
560impl PartialOrd for Span {
561    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
562        PartialOrd::partial_cmp(&self.data(), &rhs.data())
563    }
564}
565impl Ord for Span {
566    fn cmp(&self, rhs: &Self) -> Ordering {
567        Ord::cmp(&self.data(), &rhs.data())
568    }
569}
570
571impl Span {
572    #[inline]
573    pub fn lo(self) -> BytePos {
574        self.data().lo
575    }
576    #[inline]
577    pub fn with_lo(self, lo: BytePos) -> Span {
578        self.data().with_lo(lo)
579    }
580    #[inline]
581    pub fn hi(self) -> BytePos {
582        self.data().hi
583    }
584    #[inline]
585    pub fn with_hi(self, hi: BytePos) -> Span {
586        self.data().with_hi(hi)
587    }
588    #[inline]
589    pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
590        self.map_ctxt(|_| ctxt)
591    }
592
593    #[inline]
594    pub fn is_visible(self, sm: &SourceMap) -> bool {
595        !self.is_dummy() && sm.is_span_accessible(self)
596    }
597
598    /// Returns whether this span originates in a foreign crate's external macro.
599    ///
600    /// This is used to test whether a lint should not even begin to figure out whether it should
601    /// be reported on the current node.
602    #[inline]
603    pub fn in_external_macro(self, sm: &SourceMap) -> bool {
604        self.ctxt().in_external_macro(sm)
605    }
606
607    /// Returns `true` if `span` originates in a derive-macro's expansion.
608    pub fn in_derive_expansion(self) -> bool {
609        matches!(self.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Derive, _))
610    }
611
612    /// Return whether `span` is generated by `async` or `await`.
613    pub fn is_from_async_await(self) -> bool {
614        matches!(
615            self.ctxt().outer_expn_data().kind,
616            ExpnKind::Desugaring(DesugaringKind::Async | DesugaringKind::Await),
617        )
618    }
619
620    /// Gate suggestions that would not be appropriate in a context the user didn't write.
621    pub fn can_be_used_for_suggestions(self) -> bool {
622        !self.from_expansion()
623        // FIXME: If this span comes from a `derive` macro but it points at code the user wrote,
624        // the callsite span and the span will be pointing at different places. It also means that
625        // we can safely provide suggestions on this span.
626            || (self.in_derive_expansion()
627                && self.parent_callsite().map(|p| (p.lo(), p.hi())) != Some((self.lo(), self.hi())))
628    }
629
630    #[inline]
631    pub fn with_root_ctxt(lo: BytePos, hi: BytePos) -> Span {
632        Span::new(lo, hi, SyntaxContext::root(), None)
633    }
634
635    /// Returns a new span representing an empty span at the beginning of this span.
636    #[inline]
637    pub fn shrink_to_lo(self) -> Span {
638        let span = self.data_untracked();
639        span.with_hi(span.lo)
640    }
641    /// Returns a new span representing an empty span at the end of this span.
642    #[inline]
643    pub fn shrink_to_hi(self) -> Span {
644        let span = self.data_untracked();
645        span.with_lo(span.hi)
646    }
647
648    #[inline]
649    /// Returns `true` if `hi == lo`.
650    pub fn is_empty(self) -> bool {
651        let span = self.data_untracked();
652        span.hi == span.lo
653    }
654
655    /// Returns `self` if `self` is not the dummy span, and `other` otherwise.
656    pub fn substitute_dummy(self, other: Span) -> Span {
657        if self.is_dummy() { other } else { self }
658    }
659
660    /// Returns `true` if `self` fully encloses `other`.
661    pub fn contains(self, other: Span) -> bool {
662        let span = self.data();
663        let other = other.data();
664        span.contains(other)
665    }
666
667    /// Returns `true` if `self` touches `other`.
668    pub fn overlaps(self, other: Span) -> bool {
669        let span = self.data();
670        let other = other.data();
671        span.lo < other.hi && other.lo < span.hi
672    }
673
674    /// Returns `true` if `self` touches or adjoins `other`.
675    pub fn overlaps_or_adjacent(self, other: Span) -> bool {
676        let span = self.data();
677        let other = other.data();
678        span.lo <= other.hi && other.lo <= span.hi
679    }
680
681    /// Returns `true` if the spans are equal with regards to the source text.
682    ///
683    /// Use this instead of `==` when either span could be generated code,
684    /// and you only care that they point to the same bytes of source text.
685    pub fn source_equal(self, other: Span) -> bool {
686        let span = self.data();
687        let other = other.data();
688        span.lo == other.lo && span.hi == other.hi
689    }
690
691    /// Returns `Some(span)`, where the start is trimmed by the end of `other`.
692    pub fn trim_start(self, other: Span) -> Option<Span> {
693        let span = self.data();
694        let other = other.data();
695        if span.hi > other.hi { Some(span.with_lo(cmp::max(span.lo, other.hi))) } else { None }
696    }
697
698    /// Returns `Some(span)`, where the end is trimmed by the start of `other`.
699    pub fn trim_end(self, other: Span) -> Option<Span> {
700        let span = self.data();
701        let other = other.data();
702        if span.lo < other.lo { Some(span.with_hi(cmp::min(span.hi, other.lo))) } else { None }
703    }
704
705    /// Returns the source span -- this is either the supplied span, or the span for
706    /// the macro callsite that expanded to it.
707    pub fn source_callsite(self) -> Span {
708        let ctxt = self.ctxt();
709        if !ctxt.is_root() { ctxt.outer_expn_data().call_site.source_callsite() } else { self }
710    }
711
712    /// The `Span` for the tokens in the previous macro expansion from which `self` was generated,
713    /// if any.
714    pub fn parent_callsite(self) -> Option<Span> {
715        let ctxt = self.ctxt();
716        (!ctxt.is_root()).then(|| ctxt.outer_expn_data().call_site)
717    }
718
719    /// Find the first ancestor span that's contained within `outer`.
720    ///
721    /// This method traverses the macro expansion ancestors until it finds the first span
722    /// that's contained within `outer`.
723    ///
724    /// The span returned by this method may have a different [`SyntaxContext`] than `outer`.
725    /// If you need to extend the span, use [`find_ancestor_inside_same_ctxt`] instead,
726    /// because joining spans with different syntax contexts can create unexpected results.
727    ///
728    /// This is used to find the span of the macro call when a parent expr span, i.e. `outer`, is known.
729    ///
730    /// [`find_ancestor_inside_same_ctxt`]: Self::find_ancestor_inside_same_ctxt
731    pub fn find_ancestor_inside(mut self, outer: Span) -> Option<Span> {
732        while !outer.contains(self) {
733            self = self.parent_callsite()?;
734        }
735        Some(self)
736    }
737
738    /// Find the first ancestor span with the same [`SyntaxContext`] as `other`.
739    ///
740    /// This method traverses the macro expansion ancestors until it finds a span
741    /// that has the same [`SyntaxContext`] as `other`.
742    ///
743    /// Like [`find_ancestor_inside_same_ctxt`], but specifically for when spans might not
744    /// overlap. Take care when using this, and prefer [`find_ancestor_inside`] or
745    /// [`find_ancestor_inside_same_ctxt`] when you know that the spans are nested (modulo
746    /// macro expansion).
747    ///
748    /// [`find_ancestor_inside`]: Self::find_ancestor_inside
749    /// [`find_ancestor_inside_same_ctxt`]: Self::find_ancestor_inside_same_ctxt
750    pub fn find_ancestor_in_same_ctxt(mut self, other: Span) -> Option<Span> {
751        while !self.eq_ctxt(other) {
752            self = self.parent_callsite()?;
753        }
754        Some(self)
755    }
756
757    /// Find the first ancestor span that's contained within `outer` and
758    /// has the same [`SyntaxContext`] as `outer`.
759    ///
760    /// This method traverses the macro expansion ancestors until it finds a span
761    /// that is both contained within `outer` and has the same [`SyntaxContext`] as `outer`.
762    ///
763    /// This method is the combination of [`find_ancestor_inside`] and
764    /// [`find_ancestor_in_same_ctxt`] and should be preferred when extending the returned span.
765    /// If you do not need to modify the span, use [`find_ancestor_inside`] instead.
766    ///
767    /// [`find_ancestor_inside`]: Self::find_ancestor_inside
768    /// [`find_ancestor_in_same_ctxt`]: Self::find_ancestor_in_same_ctxt
769    pub fn find_ancestor_inside_same_ctxt(mut self, outer: Span) -> Option<Span> {
770        while !outer.contains(self) || !self.eq_ctxt(outer) {
771            self = self.parent_callsite()?;
772        }
773        Some(self)
774    }
775
776    /// Find the first ancestor span that does not come from an external macro.
777    ///
778    /// This method traverses the macro expansion ancestors until it finds a span
779    /// that is either from user-written code or from a local macro (defined in the current crate).
780    ///
781    /// External macros are those defined in dependencies or the standard library.
782    /// This method is useful for reporting errors in user-controllable code and avoiding
783    /// diagnostics inside external macros.
784    ///
785    /// # See also
786    ///
787    /// - [`Self::find_ancestor_not_from_macro`]
788    /// - [`Self::in_external_macro`]
789    pub fn find_ancestor_not_from_extern_macro(mut self, sm: &SourceMap) -> Option<Span> {
790        while self.in_external_macro(sm) {
791            self = self.parent_callsite()?;
792        }
793        Some(self)
794    }
795
796    /// Find the first ancestor span that does not come from any macro expansion.
797    ///
798    /// This method traverses the macro expansion ancestors until it finds a span
799    /// that originates from user-written code rather than any macro-generated code.
800    ///
801    /// This method is useful for reporting errors at the exact location users wrote code
802    /// and providing suggestions at directly editable locations.
803    ///
804    /// # See also
805    ///
806    /// - [`Self::find_ancestor_not_from_extern_macro`]
807    /// - [`Span::from_expansion`]
808    pub fn find_ancestor_not_from_macro(mut self) -> Option<Span> {
809        while self.from_expansion() {
810            self = self.parent_callsite()?;
811        }
812        Some(self)
813    }
814
815    /// Edition of the crate from which this span came.
816    pub fn edition(self) -> edition::Edition {
817        self.ctxt().edition()
818    }
819
820    /// Is this edition 2015?
821    #[inline]
822    pub fn is_rust_2015(self) -> bool {
823        self.edition().is_rust_2015()
824    }
825
826    /// Are we allowed to use features from the Rust 2018 edition?
827    #[inline]
828    pub fn at_least_rust_2018(self) -> bool {
829        self.edition().at_least_rust_2018()
830    }
831
832    /// Are we allowed to use features from the Rust 2021 edition?
833    #[inline]
834    pub fn at_least_rust_2021(self) -> bool {
835        self.edition().at_least_rust_2021()
836    }
837
838    /// Are we allowed to use features from the Rust 2024 edition?
839    #[inline]
840    pub fn at_least_rust_2024(self) -> bool {
841        self.edition().at_least_rust_2024()
842    }
843
844    /// Returns the source callee.
845    ///
846    /// Returns `None` if the supplied span has no expansion trace,
847    /// else returns the `ExpnData` for the macro definition
848    /// corresponding to the source callsite.
849    pub fn source_callee(self) -> Option<ExpnData> {
850        let mut ctxt = self.ctxt();
851        let mut opt_expn_data = None;
852        while !ctxt.is_root() {
853            let expn_data = ctxt.outer_expn_data();
854            ctxt = expn_data.call_site.ctxt();
855            opt_expn_data = Some(expn_data);
856        }
857        opt_expn_data
858    }
859
860    /// Checks if a span is "internal" to a macro in which `#[unstable]`
861    /// items can be used (that is, a macro marked with
862    /// `#[allow_internal_unstable]`).
863    pub fn allows_unstable(self, feature: Symbol) -> bool {
864        self.ctxt()
865            .outer_expn_data()
866            .allow_internal_unstable
867            .is_some_and(|features| features.contains(&feature))
868    }
869
870    /// Checks if this span arises from a compiler desugaring of kind `kind`.
871    pub fn is_desugaring(self, kind: DesugaringKind) -> bool {
872        match self.ctxt().outer_expn_data().kind {
873            ExpnKind::Desugaring(k) => k == kind,
874            _ => false,
875        }
876    }
877
878    /// Returns the compiler desugaring that created this span, or `None`
879    /// if this span is not from a desugaring.
880    pub fn desugaring_kind(self) -> Option<DesugaringKind> {
881        match self.ctxt().outer_expn_data().kind {
882            ExpnKind::Desugaring(k) => Some(k),
883            _ => None,
884        }
885    }
886
887    /// Checks if a span is "internal" to a macro in which `unsafe`
888    /// can be used without triggering the `unsafe_code` lint.
889    /// (that is, a macro marked with `#[allow_internal_unsafe]`).
890    pub fn allows_unsafe(self) -> bool {
891        self.ctxt().outer_expn_data().allow_internal_unsafe
892    }
893
894    pub fn macro_backtrace(mut self) -> impl Iterator<Item = ExpnData> {
895        let mut prev_span = DUMMY_SP;
896        iter::from_fn(move || {
897            loop {
898                let ctxt = self.ctxt();
899                if ctxt.is_root() {
900                    return None;
901                }
902
903                let expn_data = ctxt.outer_expn_data();
904                let is_recursive = expn_data.call_site.source_equal(prev_span);
905
906                prev_span = self;
907                self = expn_data.call_site;
908
909                // Don't print recursive invocations.
910                if !is_recursive {
911                    return Some(expn_data);
912                }
913            }
914        })
915    }
916
917    /// Splits a span into two composite spans around a certain position.
918    pub fn split_at(self, pos: u32) -> (Span, Span) {
919        let len = self.hi().0 - self.lo().0;
920        debug_assert!(pos <= len);
921
922        let split_pos = BytePos(self.lo().0 + pos);
923        (
924            Span::new(self.lo(), split_pos, self.ctxt(), self.parent()),
925            Span::new(split_pos, self.hi(), self.ctxt(), self.parent()),
926        )
927    }
928
929    /// Check if you can select metavar spans for the given spans to get matching contexts.
930    fn try_metavars(a: SpanData, b: SpanData, a_orig: Span, b_orig: Span) -> (SpanData, SpanData) {
931        match with_metavar_spans(|mspans| (mspans.get(a_orig), mspans.get(b_orig))) {
932            (None, None) => {}
933            (Some(meta_a), None) => {
934                let meta_a = meta_a.data();
935                if meta_a.ctxt == b.ctxt {
936                    return (meta_a, b);
937                }
938            }
939            (None, Some(meta_b)) => {
940                let meta_b = meta_b.data();
941                if a.ctxt == meta_b.ctxt {
942                    return (a, meta_b);
943                }
944            }
945            (Some(meta_a), Some(meta_b)) => {
946                let meta_b = meta_b.data();
947                if a.ctxt == meta_b.ctxt {
948                    return (a, meta_b);
949                }
950                let meta_a = meta_a.data();
951                if meta_a.ctxt == b.ctxt {
952                    return (meta_a, b);
953                } else if meta_a.ctxt == meta_b.ctxt {
954                    return (meta_a, meta_b);
955                }
956            }
957        }
958
959        (a, b)
960    }
961
962    /// Prepare two spans to a combine operation like `to` or `between`.
963    fn prepare_to_combine(
964        a_orig: Span,
965        b_orig: Span,
966    ) -> Result<(SpanData, SpanData, Option<LocalDefId>), Span> {
967        let (a, b) = (a_orig.data(), b_orig.data());
968        if a.ctxt == b.ctxt {
969            return Ok((a, b, if a.parent == b.parent { a.parent } else { None }));
970        }
971
972        let (a, b) = Span::try_metavars(a, b, a_orig, b_orig);
973        if a.ctxt == b.ctxt {
974            return Ok((a, b, if a.parent == b.parent { a.parent } else { None }));
975        }
976
977        // Context mismatches usually happen when procedural macros combine spans copied from
978        // the macro input with spans produced by the macro (`Span::*_site`).
979        // In that case we consider the combined span to be produced by the macro and return
980        // the original macro-produced span as the result.
981        // Otherwise we just fall back to returning the first span.
982        // Combining locations typically doesn't make sense in case of context mismatches.
983        // `is_root` here is a fast path optimization.
984        let a_is_callsite = a.ctxt.is_root() || a.ctxt == b.span().source_callsite().ctxt();
985        Err(if a_is_callsite { b_orig } else { a_orig })
986    }
987
988    /// This span, but in a larger context, may switch to the metavariable span if suitable.
989    pub fn with_neighbor(self, neighbor: Span) -> Span {
990        match Span::prepare_to_combine(self, neighbor) {
991            Ok((this, ..)) => this.span(),
992            Err(_) => self,
993        }
994    }
995
996    /// Returns a `Span` that would enclose both `self` and `end`.
997    ///
998    /// Note that this can also be used to extend the span "backwards":
999    /// `start.to(end)` and `end.to(start)` return the same `Span`.
1000    ///
1001    /// ```text
1002    ///     ____             ___
1003    ///     self lorem ipsum end
1004    ///     ^^^^^^^^^^^^^^^^^^^^
1005    /// ```
1006    pub fn to(self, end: Span) -> Span {
1007        match Span::prepare_to_combine(self, end) {
1008            Ok((from, to, parent)) => {
1009                Span::new(cmp::min(from.lo, to.lo), cmp::max(from.hi, to.hi), from.ctxt, parent)
1010            }
1011            Err(fallback) => fallback,
1012        }
1013    }
1014
1015    /// Returns a `Span` between the end of `self` to the beginning of `end`.
1016    ///
1017    /// ```text
1018    ///     ____             ___
1019    ///     self lorem ipsum end
1020    ///         ^^^^^^^^^^^^^
1021    /// ```
1022    pub fn between(self, end: Span) -> Span {
1023        match Span::prepare_to_combine(self, end) {
1024            Ok((from, to, parent)) => {
1025                Span::new(cmp::min(from.hi, to.hi), cmp::max(from.lo, to.lo), from.ctxt, parent)
1026            }
1027            Err(fallback) => fallback,
1028        }
1029    }
1030
1031    /// Returns a `Span` from the beginning of `self` until the beginning of `end`.
1032    ///
1033    /// ```text
1034    ///     ____             ___
1035    ///     self lorem ipsum end
1036    ///     ^^^^^^^^^^^^^^^^^
1037    /// ```
1038    pub fn until(self, end: Span) -> Span {
1039        match Span::prepare_to_combine(self, end) {
1040            Ok((from, to, parent)) => {
1041                Span::new(cmp::min(from.lo, to.lo), cmp::max(from.lo, to.lo), from.ctxt, parent)
1042            }
1043            Err(fallback) => fallback,
1044        }
1045    }
1046
1047    /// Returns the `Span` within the syntax context of "within". This is useful when
1048    /// "self" is an expansion from a macro variable, since this can be used for
1049    /// providing extra macro expansion context for certain errors.
1050    ///
1051    /// ```text
1052    /// macro_rules! m {
1053    ///     ($ident:ident) => { ($ident,) }
1054    /// }
1055    ///
1056    /// m!(outer_ident);
1057    /// ```
1058    ///
1059    /// If "self" is the span of the outer_ident, and "within" is the span of the `($ident,)`
1060    /// expr, then this will return the span of the `$ident` macro variable.
1061    pub fn within_macro(self, within: Span, sm: &SourceMap) -> Option<Span> {
1062        match Span::prepare_to_combine(self, within) {
1063            // Only return something if it doesn't overlap with the original span,
1064            // and the span isn't "imported" (i.e. from unavailable sources).
1065            // FIXME: This does limit the usefulness of the error when the macro is
1066            // from a foreign crate; we could also take into account `-Zmacro-backtrace`,
1067            // which doesn't redact this span (but that would mean passing in even more
1068            // args to this function, lol).
1069            Ok((self_, _, parent))
1070                if self_.hi < self.lo() || self.hi() < self_.lo && !sm.is_imported(within) =>
1071            {
1072                Some(Span::new(self_.lo, self_.hi, self_.ctxt, parent))
1073            }
1074            _ => None,
1075        }
1076    }
1077
1078    pub fn from_inner(self, inner: InnerSpan) -> Span {
1079        let span = self.data();
1080        Span::new(
1081            span.lo + BytePos::from_usize(inner.start),
1082            span.lo + BytePos::from_usize(inner.end),
1083            span.ctxt,
1084            span.parent,
1085        )
1086    }
1087
1088    /// Equivalent of `Span::def_site` from the proc macro API,
1089    /// except that the location is taken from the `self` span.
1090    pub fn with_def_site_ctxt(self, expn_id: ExpnId) -> Span {
1091        self.with_ctxt_from_mark(expn_id, Transparency::Opaque)
1092    }
1093
1094    /// Equivalent of `Span::call_site` from the proc macro API,
1095    /// except that the location is taken from the `self` span.
1096    pub fn with_call_site_ctxt(self, expn_id: ExpnId) -> Span {
1097        self.with_ctxt_from_mark(expn_id, Transparency::Transparent)
1098    }
1099
1100    /// Equivalent of `Span::mixed_site` from the proc macro API,
1101    /// except that the location is taken from the `self` span.
1102    pub fn with_mixed_site_ctxt(self, expn_id: ExpnId) -> Span {
1103        self.with_ctxt_from_mark(expn_id, Transparency::SemiOpaque)
1104    }
1105
1106    /// Produces a span with the same location as `self` and context produced by a macro with the
1107    /// given ID and transparency, assuming that macro was defined directly and not produced by
1108    /// some other macro (which is the case for built-in and procedural macros).
1109    fn with_ctxt_from_mark(self, expn_id: ExpnId, transparency: Transparency) -> Span {
1110        self.with_ctxt(SyntaxContext::root().apply_mark(expn_id, transparency))
1111    }
1112
1113    #[inline]
1114    pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> Span {
1115        self.map_ctxt(|ctxt| ctxt.apply_mark(expn_id, transparency))
1116    }
1117
1118    #[inline]
1119    pub fn remove_mark(&mut self) -> ExpnId {
1120        let mut mark = ExpnId::root();
1121        *self = self.map_ctxt(|mut ctxt| {
1122            mark = ctxt.remove_mark();
1123            ctxt
1124        });
1125        mark
1126    }
1127
1128    #[inline]
1129    pub fn adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
1130        let mut mark = None;
1131        *self = self.map_ctxt(|mut ctxt| {
1132            mark = ctxt.adjust(expn_id);
1133            ctxt
1134        });
1135        mark
1136    }
1137
1138    #[inline]
1139    pub fn normalize_to_macros_2_0_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
1140        let mut mark = None;
1141        *self = self.map_ctxt(|mut ctxt| {
1142            mark = ctxt.normalize_to_macros_2_0_and_adjust(expn_id);
1143            ctxt
1144        });
1145        mark
1146    }
1147
1148    #[inline]
1149    pub fn glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span) -> Option<Option<ExpnId>> {
1150        let mut mark = None;
1151        *self = self.map_ctxt(|mut ctxt| {
1152            mark = ctxt.glob_adjust(expn_id, glob_span);
1153            ctxt
1154        });
1155        mark
1156    }
1157
1158    #[inline]
1159    pub fn reverse_glob_adjust(
1160        &mut self,
1161        expn_id: ExpnId,
1162        glob_span: Span,
1163    ) -> Option<Option<ExpnId>> {
1164        let mut mark = None;
1165        *self = self.map_ctxt(|mut ctxt| {
1166            mark = ctxt.reverse_glob_adjust(expn_id, glob_span);
1167            ctxt
1168        });
1169        mark
1170    }
1171
1172    #[inline]
1173    pub fn normalize_to_macros_2_0(self) -> Span {
1174        self.map_ctxt(|ctxt| ctxt.normalize_to_macros_2_0())
1175    }
1176
1177    #[inline]
1178    pub fn normalize_to_macro_rules(self) -> Span {
1179        self.map_ctxt(|ctxt| ctxt.normalize_to_macro_rules())
1180    }
1181}
1182
1183impl Default for Span {
1184    fn default() -> Self {
1185        DUMMY_SP
1186    }
1187}
1188
1189rustc_index::newtype_index! {
1190    #[orderable]
1191    #[debug_format = "AttrId({})"]
1192    pub struct AttrId {}
1193}
1194
1195/// This trait is used to allow encoder specific encodings of certain types.
1196/// It is similar to rustc_type_ir's TyEncoder.
1197pub trait SpanEncoder: Encoder {
1198    fn encode_span(&mut self, span: Span);
1199    fn encode_symbol(&mut self, sym: Symbol);
1200    fn encode_byte_symbol(&mut self, byte_sym: ByteSymbol);
1201    fn encode_expn_id(&mut self, expn_id: ExpnId);
1202    fn encode_syntax_context(&mut self, syntax_context: SyntaxContext);
1203    /// As a local identifier, a `CrateNum` is only meaningful within its context, e.g. within a
1204    /// tcx. Therefore, make sure to include the context when encode a `CrateNum`.
1205    fn encode_crate_num(&mut self, crate_num: CrateNum);
1206    fn encode_def_index(&mut self, def_index: DefIndex);
1207    fn encode_def_id(&mut self, def_id: DefId);
1208}
1209
1210impl SpanEncoder for FileEncoder {
1211    fn encode_span(&mut self, span: Span) {
1212        let span = span.data();
1213        span.lo.encode(self);
1214        span.hi.encode(self);
1215    }
1216
1217    fn encode_symbol(&mut self, sym: Symbol) {
1218        self.emit_str(sym.as_str());
1219    }
1220
1221    fn encode_byte_symbol(&mut self, byte_sym: ByteSymbol) {
1222        self.emit_byte_str(byte_sym.as_byte_str());
1223    }
1224
1225    fn encode_expn_id(&mut self, _expn_id: ExpnId) {
1226        panic!("cannot encode `ExpnId` with `FileEncoder`");
1227    }
1228
1229    fn encode_syntax_context(&mut self, _syntax_context: SyntaxContext) {
1230        panic!("cannot encode `SyntaxContext` with `FileEncoder`");
1231    }
1232
1233    fn encode_crate_num(&mut self, crate_num: CrateNum) {
1234        self.emit_u32(crate_num.as_u32());
1235    }
1236
1237    fn encode_def_index(&mut self, _def_index: DefIndex) {
1238        panic!("cannot encode `DefIndex` with `FileEncoder`");
1239    }
1240
1241    fn encode_def_id(&mut self, def_id: DefId) {
1242        def_id.krate.encode(self);
1243        def_id.index.encode(self);
1244    }
1245}
1246
1247impl<E: SpanEncoder> Encodable<E> for Span {
1248    fn encode(&self, s: &mut E) {
1249        s.encode_span(*self);
1250    }
1251}
1252
1253impl<E: SpanEncoder> Encodable<E> for Symbol {
1254    fn encode(&self, s: &mut E) {
1255        s.encode_symbol(*self);
1256    }
1257}
1258
1259impl<E: SpanEncoder> Encodable<E> for ByteSymbol {
1260    fn encode(&self, s: &mut E) {
1261        s.encode_byte_symbol(*self);
1262    }
1263}
1264
1265impl<E: SpanEncoder> Encodable<E> for ExpnId {
1266    fn encode(&self, s: &mut E) {
1267        s.encode_expn_id(*self)
1268    }
1269}
1270
1271impl<E: SpanEncoder> Encodable<E> for SyntaxContext {
1272    fn encode(&self, s: &mut E) {
1273        s.encode_syntax_context(*self)
1274    }
1275}
1276
1277impl<E: SpanEncoder> Encodable<E> for CrateNum {
1278    fn encode(&self, s: &mut E) {
1279        s.encode_crate_num(*self)
1280    }
1281}
1282
1283impl<E: SpanEncoder> Encodable<E> for DefIndex {
1284    fn encode(&self, s: &mut E) {
1285        s.encode_def_index(*self)
1286    }
1287}
1288
1289impl<E: SpanEncoder> Encodable<E> for DefId {
1290    fn encode(&self, s: &mut E) {
1291        s.encode_def_id(*self)
1292    }
1293}
1294
1295impl<E: SpanEncoder> Encodable<E> for AttrId {
1296    fn encode(&self, _s: &mut E) {
1297        // A fresh id will be generated when decoding
1298    }
1299}
1300
1301/// This trait is used to allow decoder specific encodings of certain types.
1302/// It is similar to rustc_type_ir's TyDecoder.
1303pub trait SpanDecoder: Decoder {
1304    fn decode_span(&mut self) -> Span;
1305    fn decode_symbol(&mut self) -> Symbol;
1306    fn decode_byte_symbol(&mut self) -> ByteSymbol;
1307    fn decode_expn_id(&mut self) -> ExpnId;
1308    fn decode_syntax_context(&mut self) -> SyntaxContext;
1309    fn decode_crate_num(&mut self) -> CrateNum;
1310    fn decode_def_index(&mut self) -> DefIndex;
1311    fn decode_def_id(&mut self) -> DefId;
1312    fn decode_attr_id(&mut self) -> AttrId;
1313}
1314
1315impl SpanDecoder for MemDecoder<'_> {
1316    fn decode_span(&mut self) -> Span {
1317        let lo = Decodable::decode(self);
1318        let hi = Decodable::decode(self);
1319
1320        Span::new(lo, hi, SyntaxContext::root(), None)
1321    }
1322
1323    fn decode_symbol(&mut self) -> Symbol {
1324        Symbol::intern(self.read_str())
1325    }
1326
1327    fn decode_byte_symbol(&mut self) -> ByteSymbol {
1328        ByteSymbol::intern(self.read_byte_str())
1329    }
1330
1331    fn decode_expn_id(&mut self) -> ExpnId {
1332        panic!("cannot decode `ExpnId` with `MemDecoder`");
1333    }
1334
1335    fn decode_syntax_context(&mut self) -> SyntaxContext {
1336        panic!("cannot decode `SyntaxContext` with `MemDecoder`");
1337    }
1338
1339    fn decode_crate_num(&mut self) -> CrateNum {
1340        CrateNum::from_u32(self.read_u32())
1341    }
1342
1343    fn decode_def_index(&mut self) -> DefIndex {
1344        panic!("cannot decode `DefIndex` with `MemDecoder`");
1345    }
1346
1347    fn decode_def_id(&mut self) -> DefId {
1348        DefId { krate: Decodable::decode(self), index: Decodable::decode(self) }
1349    }
1350
1351    fn decode_attr_id(&mut self) -> AttrId {
1352        panic!("cannot decode `AttrId` with `MemDecoder`");
1353    }
1354}
1355
1356impl<D: SpanDecoder> Decodable<D> for Span {
1357    fn decode(s: &mut D) -> Span {
1358        s.decode_span()
1359    }
1360}
1361
1362impl<D: SpanDecoder> Decodable<D> for Symbol {
1363    fn decode(s: &mut D) -> Symbol {
1364        s.decode_symbol()
1365    }
1366}
1367
1368impl<D: SpanDecoder> Decodable<D> for ByteSymbol {
1369    fn decode(s: &mut D) -> ByteSymbol {
1370        s.decode_byte_symbol()
1371    }
1372}
1373
1374impl<D: SpanDecoder> Decodable<D> for ExpnId {
1375    fn decode(s: &mut D) -> ExpnId {
1376        s.decode_expn_id()
1377    }
1378}
1379
1380impl<D: SpanDecoder> Decodable<D> for SyntaxContext {
1381    fn decode(s: &mut D) -> SyntaxContext {
1382        s.decode_syntax_context()
1383    }
1384}
1385
1386impl<D: SpanDecoder> Decodable<D> for CrateNum {
1387    fn decode(s: &mut D) -> CrateNum {
1388        s.decode_crate_num()
1389    }
1390}
1391
1392impl<D: SpanDecoder> Decodable<D> for DefIndex {
1393    fn decode(s: &mut D) -> DefIndex {
1394        s.decode_def_index()
1395    }
1396}
1397
1398impl<D: SpanDecoder> Decodable<D> for DefId {
1399    fn decode(s: &mut D) -> DefId {
1400        s.decode_def_id()
1401    }
1402}
1403
1404impl<D: SpanDecoder> Decodable<D> for AttrId {
1405    fn decode(s: &mut D) -> AttrId {
1406        s.decode_attr_id()
1407    }
1408}
1409
1410impl fmt::Debug for Span {
1411    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1412        // Use the global `SourceMap` to print the span. If that's not
1413        // available, fall back to printing the raw values.
1414
1415        fn fallback(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1416            f.debug_struct("Span")
1417                .field("lo", &span.lo())
1418                .field("hi", &span.hi())
1419                .field("ctxt", &span.ctxt())
1420                .finish()
1421        }
1422
1423        if SESSION_GLOBALS.is_set() {
1424            with_session_globals(|session_globals| {
1425                if let Some(source_map) = &session_globals.source_map {
1426                    write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(*self), self.ctxt())
1427                } else {
1428                    fallback(*self, f)
1429                }
1430            })
1431        } else {
1432            fallback(*self, f)
1433        }
1434    }
1435}
1436
1437impl fmt::Debug for SpanData {
1438    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1439        fmt::Debug::fmt(&self.span(), f)
1440    }
1441}
1442
1443/// Identifies an offset of a multi-byte character in a `SourceFile`.
1444#[derive(Copy, Clone, Encodable, Decodable, Eq, PartialEq, Debug, HashStable_Generic)]
1445pub struct MultiByteChar {
1446    /// The relative offset of the character in the `SourceFile`.
1447    pub pos: RelativeBytePos,
1448    /// The number of bytes, `>= 2`.
1449    pub bytes: u8,
1450}
1451
1452/// Identifies an offset of a character that was normalized away from `SourceFile`.
1453#[derive(Copy, Clone, Encodable, Decodable, Eq, PartialEq, Debug, HashStable_Generic)]
1454pub struct NormalizedPos {
1455    /// The relative offset of the character in the `SourceFile`.
1456    pub pos: RelativeBytePos,
1457    /// The difference between original and normalized string at position.
1458    pub diff: u32,
1459}
1460
1461#[derive(PartialEq, Eq, Clone, Debug)]
1462pub enum ExternalSource {
1463    /// No external source has to be loaded, since the `SourceFile` represents a local crate.
1464    Unneeded,
1465    Foreign {
1466        kind: ExternalSourceKind,
1467        /// Index of the file inside metadata.
1468        metadata_index: u32,
1469    },
1470}
1471
1472/// The state of the lazy external source loading mechanism of a `SourceFile`.
1473#[derive(PartialEq, Eq, Clone, Debug)]
1474pub enum ExternalSourceKind {
1475    /// The external source has been loaded already.
1476    Present(Arc<String>),
1477    /// No attempt has been made to load the external source.
1478    AbsentOk,
1479    /// A failed attempt has been made to load the external source.
1480    AbsentErr,
1481}
1482
1483impl ExternalSource {
1484    pub fn get_source(&self) -> Option<&str> {
1485        match self {
1486            ExternalSource::Foreign { kind: ExternalSourceKind::Present(src), .. } => Some(src),
1487            _ => None,
1488        }
1489    }
1490}
1491
1492#[derive(Debug)]
1493pub struct OffsetOverflowError;
1494
1495#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
1496#[derive(HashStable_Generic)]
1497pub enum SourceFileHashAlgorithm {
1498    Md5,
1499    Sha1,
1500    Sha256,
1501    Blake3,
1502}
1503
1504impl Display for SourceFileHashAlgorithm {
1505    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1506        f.write_str(match self {
1507            Self::Md5 => "md5",
1508            Self::Sha1 => "sha1",
1509            Self::Sha256 => "sha256",
1510            Self::Blake3 => "blake3",
1511        })
1512    }
1513}
1514
1515impl FromStr for SourceFileHashAlgorithm {
1516    type Err = ();
1517
1518    fn from_str(s: &str) -> Result<SourceFileHashAlgorithm, ()> {
1519        match s {
1520            "md5" => Ok(SourceFileHashAlgorithm::Md5),
1521            "sha1" => Ok(SourceFileHashAlgorithm::Sha1),
1522            "sha256" => Ok(SourceFileHashAlgorithm::Sha256),
1523            "blake3" => Ok(SourceFileHashAlgorithm::Blake3),
1524            _ => Err(()),
1525        }
1526    }
1527}
1528
1529/// The hash of the on-disk source file used for debug info and cargo freshness checks.
1530#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
1531#[derive(HashStable_Generic, Encodable, Decodable)]
1532pub struct SourceFileHash {
1533    pub kind: SourceFileHashAlgorithm,
1534    value: [u8; 32],
1535}
1536
1537impl Display for SourceFileHash {
1538    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1539        write!(f, "{}=", self.kind)?;
1540        for byte in self.value[0..self.hash_len()].into_iter() {
1541            write!(f, "{byte:02x}")?;
1542        }
1543        Ok(())
1544    }
1545}
1546
1547impl SourceFileHash {
1548    pub fn new_in_memory(kind: SourceFileHashAlgorithm, src: impl AsRef<[u8]>) -> SourceFileHash {
1549        let mut hash = SourceFileHash { kind, value: Default::default() };
1550        let len = hash.hash_len();
1551        let value = &mut hash.value[..len];
1552        let data = src.as_ref();
1553        match kind {
1554            SourceFileHashAlgorithm::Md5 => {
1555                value.copy_from_slice(&Md5::digest(data));
1556            }
1557            SourceFileHashAlgorithm::Sha1 => {
1558                value.copy_from_slice(&Sha1::digest(data));
1559            }
1560            SourceFileHashAlgorithm::Sha256 => {
1561                value.copy_from_slice(&Sha256::digest(data));
1562            }
1563            SourceFileHashAlgorithm::Blake3 => value.copy_from_slice(blake3::hash(data).as_bytes()),
1564        };
1565        hash
1566    }
1567
1568    pub fn new(kind: SourceFileHashAlgorithm, src: impl Read) -> Result<SourceFileHash, io::Error> {
1569        let mut hash = SourceFileHash { kind, value: Default::default() };
1570        let len = hash.hash_len();
1571        let value = &mut hash.value[..len];
1572        // Buffer size is the recommended amount to fully leverage SIMD instructions on AVX-512 as per
1573        // blake3 documentation.
1574        let mut buf = vec![0; 16 * 1024];
1575
1576        fn digest<T>(
1577            mut hasher: T,
1578            mut update: impl FnMut(&mut T, &[u8]),
1579            finish: impl FnOnce(T, &mut [u8]),
1580            mut src: impl Read,
1581            buf: &mut [u8],
1582            value: &mut [u8],
1583        ) -> Result<(), io::Error> {
1584            loop {
1585                let bytes_read = src.read(buf)?;
1586                if bytes_read == 0 {
1587                    break;
1588                }
1589                update(&mut hasher, &buf[0..bytes_read]);
1590            }
1591            finish(hasher, value);
1592            Ok(())
1593        }
1594
1595        match kind {
1596            SourceFileHashAlgorithm::Sha256 => {
1597                digest(
1598                    Sha256::new(),
1599                    |h, b| {
1600                        h.update(b);
1601                    },
1602                    |h, out| out.copy_from_slice(&h.finalize()),
1603                    src,
1604                    &mut buf,
1605                    value,
1606                )?;
1607            }
1608            SourceFileHashAlgorithm::Sha1 => {
1609                digest(
1610                    Sha1::new(),
1611                    |h, b| {
1612                        h.update(b);
1613                    },
1614                    |h, out| out.copy_from_slice(&h.finalize()),
1615                    src,
1616                    &mut buf,
1617                    value,
1618                )?;
1619            }
1620            SourceFileHashAlgorithm::Md5 => {
1621                digest(
1622                    Md5::new(),
1623                    |h, b| {
1624                        h.update(b);
1625                    },
1626                    |h, out| out.copy_from_slice(&h.finalize()),
1627                    src,
1628                    &mut buf,
1629                    value,
1630                )?;
1631            }
1632            SourceFileHashAlgorithm::Blake3 => {
1633                digest(
1634                    blake3::Hasher::new(),
1635                    |h, b| {
1636                        h.update(b);
1637                    },
1638                    |h, out| out.copy_from_slice(h.finalize().as_bytes()),
1639                    src,
1640                    &mut buf,
1641                    value,
1642                )?;
1643            }
1644        }
1645        Ok(hash)
1646    }
1647
1648    /// Check if the stored hash matches the hash of the string.
1649    pub fn matches(&self, src: &str) -> bool {
1650        Self::new_in_memory(self.kind, src.as_bytes()) == *self
1651    }
1652
1653    /// The bytes of the hash.
1654    pub fn hash_bytes(&self) -> &[u8] {
1655        let len = self.hash_len();
1656        &self.value[..len]
1657    }
1658
1659    fn hash_len(&self) -> usize {
1660        match self.kind {
1661            SourceFileHashAlgorithm::Md5 => 16,
1662            SourceFileHashAlgorithm::Sha1 => 20,
1663            SourceFileHashAlgorithm::Sha256 | SourceFileHashAlgorithm::Blake3 => 32,
1664        }
1665    }
1666}
1667
1668#[derive(Clone)]
1669pub enum SourceFileLines {
1670    /// The source file lines, in decoded (random-access) form.
1671    Lines(Vec<RelativeBytePos>),
1672
1673    /// The source file lines, in undecoded difference list form.
1674    Diffs(SourceFileDiffs),
1675}
1676
1677impl SourceFileLines {
1678    pub fn is_lines(&self) -> bool {
1679        matches!(self, SourceFileLines::Lines(_))
1680    }
1681}
1682
1683/// The source file lines in difference list form. This matches the form
1684/// used within metadata, which saves space by exploiting the fact that the
1685/// lines list is sorted and individual lines are usually not that long.
1686///
1687/// We read it directly from metadata and only decode it into `Lines` form
1688/// when necessary. This is a significant performance win, especially for
1689/// small crates where very little of `std`'s metadata is used.
1690#[derive(Clone)]
1691pub struct SourceFileDiffs {
1692    /// Always 1, 2, or 4. Always as small as possible, while being big
1693    /// enough to hold the length of the longest line in the source file.
1694    /// The 1 case is by far the most common.
1695    bytes_per_diff: usize,
1696
1697    /// The number of diffs encoded in `raw_diffs`. Always one less than
1698    /// the number of lines in the source file.
1699    num_diffs: usize,
1700
1701    /// The diffs in "raw" form. Each segment of `bytes_per_diff` length
1702    /// encodes one little-endian diff. Note that they aren't LEB128
1703    /// encoded. This makes for much faster decoding. Besides, the
1704    /// bytes_per_diff==1 case is by far the most common, and LEB128
1705    /// encoding has no effect on that case.
1706    raw_diffs: Vec<u8>,
1707}
1708
1709/// A single source in the [`SourceMap`].
1710pub struct SourceFile {
1711    /// The name of the file that the source came from. Source that doesn't
1712    /// originate from files has names between angle brackets by convention
1713    /// (e.g., `<anon>`).
1714    pub name: FileName,
1715    /// The complete source code.
1716    pub src: Option<Arc<String>>,
1717    /// The source code's hash.
1718    pub src_hash: SourceFileHash,
1719    /// Used to enable cargo to use checksums to check if a crate is fresh rather
1720    /// than mtimes. This might be the same as `src_hash`, and if the requested algorithm
1721    /// is identical we won't compute it twice.
1722    pub checksum_hash: Option<SourceFileHash>,
1723    /// The external source code (used for external crates, which will have a `None`
1724    /// value as `self.src`.
1725    pub external_src: FreezeLock<ExternalSource>,
1726    /// The start position of this source in the `SourceMap`.
1727    pub start_pos: BytePos,
1728    /// The byte length of this source.
1729    pub source_len: RelativeBytePos,
1730    /// Locations of lines beginnings in the source code.
1731    pub lines: FreezeLock<SourceFileLines>,
1732    /// Locations of multi-byte characters in the source code.
1733    pub multibyte_chars: Vec<MultiByteChar>,
1734    /// Locations of characters removed during normalization.
1735    pub normalized_pos: Vec<NormalizedPos>,
1736    /// A hash of the filename & crate-id, used for uniquely identifying source
1737    /// files within the crate graph and for speeding up hashing in incremental
1738    /// compilation.
1739    pub stable_id: StableSourceFileId,
1740    /// Indicates which crate this `SourceFile` was imported from.
1741    pub cnum: CrateNum,
1742}
1743
1744impl Clone for SourceFile {
1745    fn clone(&self) -> Self {
1746        Self {
1747            name: self.name.clone(),
1748            src: self.src.clone(),
1749            src_hash: self.src_hash,
1750            checksum_hash: self.checksum_hash,
1751            external_src: self.external_src.clone(),
1752            start_pos: self.start_pos,
1753            source_len: self.source_len,
1754            lines: self.lines.clone(),
1755            multibyte_chars: self.multibyte_chars.clone(),
1756            normalized_pos: self.normalized_pos.clone(),
1757            stable_id: self.stable_id,
1758            cnum: self.cnum,
1759        }
1760    }
1761}
1762
1763impl<S: SpanEncoder> Encodable<S> for SourceFile {
1764    fn encode(&self, s: &mut S) {
1765        self.name.encode(s);
1766        self.src_hash.encode(s);
1767        self.checksum_hash.encode(s);
1768        // Do not encode `start_pos` as it's global state for this session.
1769        self.source_len.encode(s);
1770
1771        // We are always in `Lines` form by the time we reach here.
1772        assert!(self.lines.read().is_lines());
1773        let lines = self.lines();
1774        // Store the length.
1775        s.emit_u32(lines.len() as u32);
1776
1777        // Compute and store the difference list.
1778        if lines.len() != 0 {
1779            let max_line_length = if lines.len() == 1 {
1780                0
1781            } else {
1782                lines
1783                    .array_windows()
1784                    .map(|&[fst, snd]| snd - fst)
1785                    .map(|bp| bp.to_usize())
1786                    .max()
1787                    .unwrap()
1788            };
1789
1790            let bytes_per_diff: usize = match max_line_length {
1791                0..=0xFF => 1,
1792                0x100..=0xFFFF => 2,
1793                _ => 4,
1794            };
1795
1796            // Encode the number of bytes used per diff.
1797            s.emit_u8(bytes_per_diff as u8);
1798
1799            // Encode the first element.
1800            assert_eq!(lines[0], RelativeBytePos(0));
1801
1802            // Encode the difference list.
1803            let diff_iter = lines.array_windows().map(|&[fst, snd]| snd - fst);
1804            let num_diffs = lines.len() - 1;
1805            let mut raw_diffs;
1806            match bytes_per_diff {
1807                1 => {
1808                    raw_diffs = Vec::with_capacity(num_diffs);
1809                    for diff in diff_iter {
1810                        raw_diffs.push(diff.0 as u8);
1811                    }
1812                }
1813                2 => {
1814                    raw_diffs = Vec::with_capacity(bytes_per_diff * num_diffs);
1815                    for diff in diff_iter {
1816                        raw_diffs.extend_from_slice(&(diff.0 as u16).to_le_bytes());
1817                    }
1818                }
1819                4 => {
1820                    raw_diffs = Vec::with_capacity(bytes_per_diff * num_diffs);
1821                    for diff in diff_iter {
1822                        raw_diffs.extend_from_slice(&(diff.0).to_le_bytes());
1823                    }
1824                }
1825                _ => unreachable!(),
1826            }
1827            s.emit_raw_bytes(&raw_diffs);
1828        }
1829
1830        self.multibyte_chars.encode(s);
1831        self.stable_id.encode(s);
1832        self.normalized_pos.encode(s);
1833        self.cnum.encode(s);
1834    }
1835}
1836
1837impl<D: SpanDecoder> Decodable<D> for SourceFile {
1838    fn decode(d: &mut D) -> SourceFile {
1839        let name: FileName = Decodable::decode(d);
1840        let src_hash: SourceFileHash = Decodable::decode(d);
1841        let checksum_hash: Option<SourceFileHash> = Decodable::decode(d);
1842        let source_len: RelativeBytePos = Decodable::decode(d);
1843        let lines = {
1844            let num_lines: u32 = Decodable::decode(d);
1845            if num_lines > 0 {
1846                // Read the number of bytes used per diff.
1847                let bytes_per_diff = d.read_u8() as usize;
1848
1849                // Read the difference list.
1850                let num_diffs = num_lines as usize - 1;
1851                let raw_diffs = d.read_raw_bytes(bytes_per_diff * num_diffs).to_vec();
1852                SourceFileLines::Diffs(SourceFileDiffs { bytes_per_diff, num_diffs, raw_diffs })
1853            } else {
1854                SourceFileLines::Lines(vec![])
1855            }
1856        };
1857        let multibyte_chars: Vec<MultiByteChar> = Decodable::decode(d);
1858        let stable_id = Decodable::decode(d);
1859        let normalized_pos: Vec<NormalizedPos> = Decodable::decode(d);
1860        let cnum: CrateNum = Decodable::decode(d);
1861        SourceFile {
1862            name,
1863            start_pos: BytePos::from_u32(0),
1864            source_len,
1865            src: None,
1866            src_hash,
1867            checksum_hash,
1868            // Unused - the metadata decoder will construct
1869            // a new SourceFile, filling in `external_src` properly
1870            external_src: FreezeLock::frozen(ExternalSource::Unneeded),
1871            lines: FreezeLock::new(lines),
1872            multibyte_chars,
1873            normalized_pos,
1874            stable_id,
1875            cnum,
1876        }
1877    }
1878}
1879
1880impl fmt::Debug for SourceFile {
1881    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1882        write!(fmt, "SourceFile({:?})", self.name)
1883    }
1884}
1885
1886/// This is a [SourceFile] identifier that is used to correlate source files between
1887/// subsequent compilation sessions (which is something we need to do during
1888/// incremental compilation).
1889///
1890/// It is a hash value (so we can efficiently consume it when stable-hashing
1891/// spans) that consists of the `FileName` and the `StableCrateId` of the crate
1892/// the source file is from. The crate id is needed because sometimes the
1893/// `FileName` is not unique within the crate graph (think `src/lib.rs`, for
1894/// example).
1895///
1896/// The way the crate-id part is handled is a bit special: source files of the
1897/// local crate are hashed as `(filename, None)`, while source files from
1898/// upstream crates have a hash of `(filename, Some(stable_crate_id))`. This
1899/// is because SourceFiles for the local crate are allocated very early in the
1900/// compilation process when the `StableCrateId` is not yet known. If, due to
1901/// some refactoring of the compiler, the `StableCrateId` of the local crate
1902/// were to become available, it would be better to uniformly make this a
1903/// hash of `(filename, stable_crate_id)`.
1904///
1905/// When `SourceFile`s are exported in crate metadata, the `StableSourceFileId`
1906/// is updated to incorporate the `StableCrateId` of the exporting crate.
1907#[derive(
1908    Debug,
1909    Clone,
1910    Copy,
1911    Hash,
1912    PartialEq,
1913    Eq,
1914    HashStable_Generic,
1915    Encodable,
1916    Decodable,
1917    Default,
1918    PartialOrd,
1919    Ord
1920)]
1921pub struct StableSourceFileId(Hash128);
1922
1923impl StableSourceFileId {
1924    fn from_filename_in_current_crate(filename: &FileName) -> Self {
1925        Self::from_filename_and_stable_crate_id(filename, None)
1926    }
1927
1928    pub fn from_filename_for_export(
1929        filename: &FileName,
1930        local_crate_stable_crate_id: StableCrateId,
1931    ) -> Self {
1932        Self::from_filename_and_stable_crate_id(filename, Some(local_crate_stable_crate_id))
1933    }
1934
1935    fn from_filename_and_stable_crate_id(
1936        filename: &FileName,
1937        stable_crate_id: Option<StableCrateId>,
1938    ) -> Self {
1939        let mut hasher = StableHasher::new();
1940        filename.hash(&mut hasher);
1941        stable_crate_id.hash(&mut hasher);
1942        StableSourceFileId(hasher.finish())
1943    }
1944}
1945
1946impl SourceFile {
1947    const MAX_FILE_SIZE: u32 = u32::MAX - 1;
1948
1949    pub fn new(
1950        name: FileName,
1951        mut src: String,
1952        hash_kind: SourceFileHashAlgorithm,
1953        checksum_hash_kind: Option<SourceFileHashAlgorithm>,
1954    ) -> Result<Self, OffsetOverflowError> {
1955        // Compute the file hash before any normalization.
1956        let src_hash = SourceFileHash::new_in_memory(hash_kind, src.as_bytes());
1957        let checksum_hash = checksum_hash_kind.map(|checksum_hash_kind| {
1958            if checksum_hash_kind == hash_kind {
1959                src_hash
1960            } else {
1961                SourceFileHash::new_in_memory(checksum_hash_kind, src.as_bytes())
1962            }
1963        });
1964        let normalized_pos = normalize_src(&mut src);
1965
1966        let stable_id = StableSourceFileId::from_filename_in_current_crate(&name);
1967        let source_len = src.len();
1968        let source_len = u32::try_from(source_len).map_err(|_| OffsetOverflowError)?;
1969        if source_len > Self::MAX_FILE_SIZE {
1970            return Err(OffsetOverflowError);
1971        }
1972
1973        let (lines, multibyte_chars) = analyze_source_file::analyze_source_file(&src);
1974
1975        Ok(SourceFile {
1976            name,
1977            src: Some(Arc::new(src)),
1978            src_hash,
1979            checksum_hash,
1980            external_src: FreezeLock::frozen(ExternalSource::Unneeded),
1981            start_pos: BytePos::from_u32(0),
1982            source_len: RelativeBytePos::from_u32(source_len),
1983            lines: FreezeLock::frozen(SourceFileLines::Lines(lines)),
1984            multibyte_chars,
1985            normalized_pos,
1986            stable_id,
1987            cnum: LOCAL_CRATE,
1988        })
1989    }
1990
1991    /// This converts the `lines` field to contain `SourceFileLines::Lines` if needed and freezes
1992    /// it.
1993    fn convert_diffs_to_lines_frozen(&self) {
1994        let mut guard = if let Some(guard) = self.lines.try_write() { guard } else { return };
1995
1996        let SourceFileDiffs { bytes_per_diff, num_diffs, raw_diffs } = match &*guard {
1997            SourceFileLines::Diffs(diffs) => diffs,
1998            SourceFileLines::Lines(..) => {
1999                FreezeWriteGuard::freeze(guard);
2000                return;
2001            }
2002        };
2003
2004        // Convert from "diffs" form to "lines" form.
2005        let num_lines = num_diffs + 1;
2006        let mut lines = Vec::with_capacity(num_lines);
2007        let mut line_start = RelativeBytePos(0);
2008        lines.push(line_start);
2009
2010        assert_eq!(*num_diffs, raw_diffs.len() / bytes_per_diff);
2011        match bytes_per_diff {
2012            1 => {
2013                lines.extend(raw_diffs.into_iter().map(|&diff| {
2014                    line_start = line_start + RelativeBytePos(diff as u32);
2015                    line_start
2016                }));
2017            }
2018            2 => {
2019                lines.extend((0..*num_diffs).map(|i| {
2020                    let pos = bytes_per_diff * i;
2021                    let bytes = [raw_diffs[pos], raw_diffs[pos + 1]];
2022                    let diff = u16::from_le_bytes(bytes);
2023                    line_start = line_start + RelativeBytePos(diff as u32);
2024                    line_start
2025                }));
2026            }
2027            4 => {
2028                lines.extend((0..*num_diffs).map(|i| {
2029                    let pos = bytes_per_diff * i;
2030                    let bytes = [
2031                        raw_diffs[pos],
2032                        raw_diffs[pos + 1],
2033                        raw_diffs[pos + 2],
2034                        raw_diffs[pos + 3],
2035                    ];
2036                    let diff = u32::from_le_bytes(bytes);
2037                    line_start = line_start + RelativeBytePos(diff);
2038                    line_start
2039                }));
2040            }
2041            _ => unreachable!(),
2042        }
2043
2044        *guard = SourceFileLines::Lines(lines);
2045
2046        FreezeWriteGuard::freeze(guard);
2047    }
2048
2049    pub fn lines(&self) -> &[RelativeBytePos] {
2050        if let Some(SourceFileLines::Lines(lines)) = self.lines.get() {
2051            return &lines[..];
2052        }
2053
2054        outline(|| {
2055            self.convert_diffs_to_lines_frozen();
2056            if let Some(SourceFileLines::Lines(lines)) = self.lines.get() {
2057                return &lines[..];
2058            }
2059            unreachable!()
2060        })
2061    }
2062
2063    /// Returns the `BytePos` of the beginning of the current line.
2064    pub fn line_begin_pos(&self, pos: BytePos) -> BytePos {
2065        let pos = self.relative_position(pos);
2066        let line_index = self.lookup_line(pos).unwrap();
2067        let line_start_pos = self.lines()[line_index];
2068        self.absolute_position(line_start_pos)
2069    }
2070
2071    /// Add externally loaded source.
2072    /// If the hash of the input doesn't match or no input is supplied via None,
2073    /// it is interpreted as an error and the corresponding enum variant is set.
2074    /// The return value signifies whether some kind of source is present.
2075    pub fn add_external_src<F>(&self, get_src: F) -> bool
2076    where
2077        F: FnOnce() -> Option<String>,
2078    {
2079        if !self.external_src.is_frozen() {
2080            let src = get_src();
2081            let src = src.and_then(|mut src| {
2082                // The src_hash needs to be computed on the pre-normalized src.
2083                self.src_hash.matches(&src).then(|| {
2084                    normalize_src(&mut src);
2085                    src
2086                })
2087            });
2088
2089            self.external_src.try_write().map(|mut external_src| {
2090                if let ExternalSource::Foreign {
2091                    kind: src_kind @ ExternalSourceKind::AbsentOk,
2092                    ..
2093                } = &mut *external_src
2094                {
2095                    *src_kind = if let Some(src) = src {
2096                        ExternalSourceKind::Present(Arc::new(src))
2097                    } else {
2098                        ExternalSourceKind::AbsentErr
2099                    };
2100                } else {
2101                    panic!("unexpected state {:?}", *external_src)
2102                }
2103
2104                // Freeze this so we don't try to load the source again.
2105                FreezeWriteGuard::freeze(external_src)
2106            });
2107        }
2108
2109        self.src.is_some() || self.external_src.read().get_source().is_some()
2110    }
2111
2112    /// Gets a line from the list of pre-computed line-beginnings.
2113    /// The line number here is 0-based.
2114    pub fn get_line(&self, line_number: usize) -> Option<Cow<'_, str>> {
2115        fn get_until_newline(src: &str, begin: usize) -> &str {
2116            // We can't use `lines.get(line_number+1)` because we might
2117            // be parsing when we call this function and thus the current
2118            // line is the last one we have line info for.
2119            let slice = &src[begin..];
2120            match slice.find('\n') {
2121                Some(e) => &slice[..e],
2122                None => slice,
2123            }
2124        }
2125
2126        let begin = {
2127            let line = self.lines().get(line_number).copied()?;
2128            line.to_usize()
2129        };
2130
2131        if let Some(ref src) = self.src {
2132            Some(Cow::from(get_until_newline(src, begin)))
2133        } else {
2134            self.external_src
2135                .borrow()
2136                .get_source()
2137                .map(|src| Cow::Owned(String::from(get_until_newline(src, begin))))
2138        }
2139    }
2140
2141    pub fn is_real_file(&self) -> bool {
2142        self.name.is_real()
2143    }
2144
2145    #[inline]
2146    pub fn is_imported(&self) -> bool {
2147        self.src.is_none()
2148    }
2149
2150    pub fn count_lines(&self) -> usize {
2151        self.lines().len()
2152    }
2153
2154    #[inline]
2155    pub fn absolute_position(&self, pos: RelativeBytePos) -> BytePos {
2156        BytePos::from_u32(pos.to_u32() + self.start_pos.to_u32())
2157    }
2158
2159    #[inline]
2160    pub fn relative_position(&self, pos: BytePos) -> RelativeBytePos {
2161        RelativeBytePos::from_u32(pos.to_u32() - self.start_pos.to_u32())
2162    }
2163
2164    #[inline]
2165    pub fn end_position(&self) -> BytePos {
2166        self.absolute_position(self.source_len)
2167    }
2168
2169    /// Finds the line containing the given position. The return value is the
2170    /// index into the `lines` array of this `SourceFile`, not the 1-based line
2171    /// number. If the source_file is empty or the position is located before the
2172    /// first line, `None` is returned.
2173    pub fn lookup_line(&self, pos: RelativeBytePos) -> Option<usize> {
2174        self.lines().partition_point(|x| x <= &pos).checked_sub(1)
2175    }
2176
2177    pub fn line_bounds(&self, line_index: usize) -> Range<BytePos> {
2178        if self.is_empty() {
2179            return self.start_pos..self.start_pos;
2180        }
2181
2182        let lines = self.lines();
2183        assert!(line_index < lines.len());
2184        if line_index == (lines.len() - 1) {
2185            self.absolute_position(lines[line_index])..self.end_position()
2186        } else {
2187            self.absolute_position(lines[line_index])..self.absolute_position(lines[line_index + 1])
2188        }
2189    }
2190
2191    /// Returns whether or not the file contains the given `SourceMap` byte
2192    /// position. The position one past the end of the file is considered to be
2193    /// contained by the file. This implies that files for which `is_empty`
2194    /// returns true still contain one byte position according to this function.
2195    #[inline]
2196    pub fn contains(&self, byte_pos: BytePos) -> bool {
2197        byte_pos >= self.start_pos && byte_pos <= self.end_position()
2198    }
2199
2200    #[inline]
2201    pub fn is_empty(&self) -> bool {
2202        self.source_len.to_u32() == 0
2203    }
2204
2205    /// Calculates the original byte position relative to the start of the file
2206    /// based on the given byte position.
2207    pub fn original_relative_byte_pos(&self, pos: BytePos) -> RelativeBytePos {
2208        let pos = self.relative_position(pos);
2209
2210        // Diff before any records is 0. Otherwise use the previously recorded
2211        // diff as that applies to the following characters until a new diff
2212        // is recorded.
2213        let diff = match self.normalized_pos.binary_search_by(|np| np.pos.cmp(&pos)) {
2214            Ok(i) => self.normalized_pos[i].diff,
2215            Err(0) => 0,
2216            Err(i) => self.normalized_pos[i - 1].diff,
2217        };
2218
2219        RelativeBytePos::from_u32(pos.0 + diff)
2220    }
2221
2222    /// Calculates a normalized byte position from a byte offset relative to the
2223    /// start of the file.
2224    ///
2225    /// When we get an inline assembler error from LLVM during codegen, we
2226    /// import the expanded assembly code as a new `SourceFile`, which can then
2227    /// be used for error reporting with spans. However the byte offsets given
2228    /// to us by LLVM are relative to the start of the original buffer, not the
2229    /// normalized one. Hence we need to convert those offsets to the normalized
2230    /// form when constructing spans.
2231    pub fn normalized_byte_pos(&self, offset: u32) -> BytePos {
2232        let diff = match self
2233            .normalized_pos
2234            .binary_search_by(|np| (np.pos.0 + np.diff).cmp(&(self.start_pos.0 + offset)))
2235        {
2236            Ok(i) => self.normalized_pos[i].diff,
2237            Err(0) => 0,
2238            Err(i) => self.normalized_pos[i - 1].diff,
2239        };
2240
2241        BytePos::from_u32(self.start_pos.0 + offset - diff)
2242    }
2243
2244    /// Converts an relative `RelativeBytePos` to a `CharPos` relative to the `SourceFile`.
2245    fn bytepos_to_file_charpos(&self, bpos: RelativeBytePos) -> CharPos {
2246        // The number of extra bytes due to multibyte chars in the `SourceFile`.
2247        let mut total_extra_bytes = 0;
2248
2249        for mbc in self.multibyte_chars.iter() {
2250            debug!("{}-byte char at {:?}", mbc.bytes, mbc.pos);
2251            if mbc.pos < bpos {
2252                // Every character is at least one byte, so we only
2253                // count the actual extra bytes.
2254                total_extra_bytes += mbc.bytes as u32 - 1;
2255                // We should never see a byte position in the middle of a
2256                // character.
2257                assert!(bpos.to_u32() >= mbc.pos.to_u32() + mbc.bytes as u32);
2258            } else {
2259                break;
2260            }
2261        }
2262
2263        assert!(total_extra_bytes <= bpos.to_u32());
2264        CharPos(bpos.to_usize() - total_extra_bytes as usize)
2265    }
2266
2267    /// Looks up the file's (1-based) line number and (0-based `CharPos`) column offset, for a
2268    /// given `RelativeBytePos`.
2269    fn lookup_file_pos(&self, pos: RelativeBytePos) -> (usize, CharPos) {
2270        let chpos = self.bytepos_to_file_charpos(pos);
2271        match self.lookup_line(pos) {
2272            Some(a) => {
2273                let line = a + 1; // Line numbers start at 1
2274                let linebpos = self.lines()[a];
2275                let linechpos = self.bytepos_to_file_charpos(linebpos);
2276                let col = chpos - linechpos;
2277                debug!("byte pos {:?} is on the line at byte pos {:?}", pos, linebpos);
2278                debug!("char pos {:?} is on the line at char pos {:?}", chpos, linechpos);
2279                debug!("byte is on line: {}", line);
2280                assert!(chpos >= linechpos);
2281                (line, col)
2282            }
2283            None => (0, chpos),
2284        }
2285    }
2286
2287    /// Looks up the file's (1-based) line number, (0-based `CharPos`) column offset, and (0-based)
2288    /// column offset when displayed, for a given `BytePos`.
2289    pub fn lookup_file_pos_with_col_display(&self, pos: BytePos) -> (usize, CharPos, usize) {
2290        let pos = self.relative_position(pos);
2291        let (line, col_or_chpos) = self.lookup_file_pos(pos);
2292        if line > 0 {
2293            let Some(code) = self.get_line(line - 1) else {
2294                // If we don't have the code available, it is ok as a fallback to return the bytepos
2295                // instead of the "display" column, which is only used to properly show underlines
2296                // in the terminal.
2297                // FIXME: we'll want better handling of this in the future for the sake of tools
2298                // that want to use the display col instead of byte offsets to modify Rust code, but
2299                // that is a problem for another day, the previous code was already incorrect for
2300                // both displaying *and* third party tools using the json output naïvely.
2301                tracing::info!("couldn't find line {line} {:?}", self.name);
2302                return (line, col_or_chpos, col_or_chpos.0);
2303            };
2304            let display_col = code.chars().take(col_or_chpos.0).map(|ch| char_width(ch)).sum();
2305            (line, col_or_chpos, display_col)
2306        } else {
2307            // This is never meant to happen?
2308            (0, col_or_chpos, col_or_chpos.0)
2309        }
2310    }
2311}
2312
2313pub fn char_width(ch: char) -> usize {
2314    // FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is. For now,
2315    // just accept that sometimes the code line will be longer than desired.
2316    match ch {
2317        '\t' => 4,
2318        // Keep the following list in sync with `rustc_errors::emitter::OUTPUT_REPLACEMENTS`. These
2319        // are control points that we replace before printing with a visible codepoint for the sake
2320        // of being able to point at them with underlines.
2321        '\u{0000}' | '\u{0001}' | '\u{0002}' | '\u{0003}' | '\u{0004}' | '\u{0005}'
2322        | '\u{0006}' | '\u{0007}' | '\u{0008}' | '\u{000B}' | '\u{000C}' | '\u{000D}'
2323        | '\u{000E}' | '\u{000F}' | '\u{0010}' | '\u{0011}' | '\u{0012}' | '\u{0013}'
2324        | '\u{0014}' | '\u{0015}' | '\u{0016}' | '\u{0017}' | '\u{0018}' | '\u{0019}'
2325        | '\u{001A}' | '\u{001B}' | '\u{001C}' | '\u{001D}' | '\u{001E}' | '\u{001F}'
2326        | '\u{007F}' | '\u{202A}' | '\u{202B}' | '\u{202D}' | '\u{202E}' | '\u{2066}'
2327        | '\u{2067}' | '\u{2068}' | '\u{202C}' | '\u{2069}' => 1,
2328        _ => unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1),
2329    }
2330}
2331
2332pub fn str_width(s: &str) -> usize {
2333    s.chars().map(char_width).sum()
2334}
2335
2336/// Normalizes the source code and records the normalizations.
2337fn normalize_src(src: &mut String) -> Vec<NormalizedPos> {
2338    let mut normalized_pos = vec![];
2339    remove_bom(src, &mut normalized_pos);
2340    normalize_newlines(src, &mut normalized_pos);
2341    normalized_pos
2342}
2343
2344/// Removes UTF-8 BOM, if any.
2345fn remove_bom(src: &mut String, normalized_pos: &mut Vec<NormalizedPos>) {
2346    if src.starts_with('\u{feff}') {
2347        src.drain(..3);
2348        normalized_pos.push(NormalizedPos { pos: RelativeBytePos(0), diff: 3 });
2349    }
2350}
2351
2352/// Replaces `\r\n` with `\n` in-place in `src`.
2353///
2354/// Leaves any occurrences of lone `\r` unchanged.
2355fn normalize_newlines(src: &mut String, normalized_pos: &mut Vec<NormalizedPos>) {
2356    if !src.as_bytes().contains(&b'\r') {
2357        return;
2358    }
2359
2360    // We replace `\r\n` with `\n` in-place, which doesn't break utf-8 encoding.
2361    // While we *can* call `as_mut_vec` and do surgery on the live string
2362    // directly, let's rather steal the contents of `src`. This makes the code
2363    // safe even if a panic occurs.
2364
2365    let mut buf = std::mem::replace(src, String::new()).into_bytes();
2366    let mut gap_len = 0;
2367    let mut tail = buf.as_mut_slice();
2368    let mut cursor = 0;
2369    let original_gap = normalized_pos.last().map_or(0, |l| l.diff);
2370    loop {
2371        let idx = match find_crlf(&tail[gap_len..]) {
2372            None => tail.len(),
2373            Some(idx) => idx + gap_len,
2374        };
2375        tail.copy_within(gap_len..idx, 0);
2376        tail = &mut tail[idx - gap_len..];
2377        if tail.len() == gap_len {
2378            break;
2379        }
2380        cursor += idx - gap_len;
2381        gap_len += 1;
2382        normalized_pos.push(NormalizedPos {
2383            pos: RelativeBytePos::from_usize(cursor + 1),
2384            diff: original_gap + gap_len as u32,
2385        });
2386    }
2387
2388    // Account for removed `\r`.
2389    // After `set_len`, `buf` is guaranteed to contain utf-8 again.
2390    let new_len = buf.len() - gap_len;
2391    unsafe {
2392        buf.set_len(new_len);
2393        *src = String::from_utf8_unchecked(buf);
2394    }
2395
2396    fn find_crlf(src: &[u8]) -> Option<usize> {
2397        let mut search_idx = 0;
2398        while let Some(idx) = find_cr(&src[search_idx..]) {
2399            if src[search_idx..].get(idx + 1) != Some(&b'\n') {
2400                search_idx += idx + 1;
2401                continue;
2402            }
2403            return Some(search_idx + idx);
2404        }
2405        None
2406    }
2407
2408    fn find_cr(src: &[u8]) -> Option<usize> {
2409        src.iter().position(|&b| b == b'\r')
2410    }
2411}
2412
2413// _____________________________________________________________________________
2414// Pos, BytePos, CharPos
2415//
2416
2417pub trait Pos {
2418    fn from_usize(n: usize) -> Self;
2419    fn to_usize(&self) -> usize;
2420    fn from_u32(n: u32) -> Self;
2421    fn to_u32(&self) -> u32;
2422}
2423
2424macro_rules! impl_pos {
2425    (
2426        $(
2427            $(#[$attr:meta])*
2428            $vis:vis struct $ident:ident($inner_vis:vis $inner_ty:ty);
2429        )*
2430    ) => {
2431        $(
2432            $(#[$attr])*
2433            $vis struct $ident($inner_vis $inner_ty);
2434
2435            impl Pos for $ident {
2436                #[inline(always)]
2437                fn from_usize(n: usize) -> $ident {
2438                    $ident(n as $inner_ty)
2439                }
2440
2441                #[inline(always)]
2442                fn to_usize(&self) -> usize {
2443                    self.0 as usize
2444                }
2445
2446                #[inline(always)]
2447                fn from_u32(n: u32) -> $ident {
2448                    $ident(n as $inner_ty)
2449                }
2450
2451                #[inline(always)]
2452                fn to_u32(&self) -> u32 {
2453                    self.0 as u32
2454                }
2455            }
2456
2457            impl Add for $ident {
2458                type Output = $ident;
2459
2460                #[inline(always)]
2461                fn add(self, rhs: $ident) -> $ident {
2462                    $ident(self.0 + rhs.0)
2463                }
2464            }
2465
2466            impl Sub for $ident {
2467                type Output = $ident;
2468
2469                #[inline(always)]
2470                fn sub(self, rhs: $ident) -> $ident {
2471                    $ident(self.0 - rhs.0)
2472                }
2473            }
2474        )*
2475    };
2476}
2477
2478impl_pos! {
2479    /// A byte offset.
2480    ///
2481    /// Keep this small (currently 32-bits), as AST contains a lot of them.
2482    #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
2483    pub struct BytePos(pub u32);
2484
2485    /// A byte offset relative to file beginning.
2486    #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
2487    pub struct RelativeBytePos(pub u32);
2488
2489    /// A character offset.
2490    ///
2491    /// Because of multibyte UTF-8 characters, a byte offset
2492    /// is not equivalent to a character offset. The [`SourceMap`] will convert [`BytePos`]
2493    /// values to `CharPos` values as necessary.
2494    #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
2495    pub struct CharPos(pub usize);
2496}
2497
2498impl<S: Encoder> Encodable<S> for BytePos {
2499    fn encode(&self, s: &mut S) {
2500        s.emit_u32(self.0);
2501    }
2502}
2503
2504impl<D: Decoder> Decodable<D> for BytePos {
2505    fn decode(d: &mut D) -> BytePos {
2506        BytePos(d.read_u32())
2507    }
2508}
2509
2510impl<H: HashStableContext> HashStable<H> for RelativeBytePos {
2511    fn hash_stable(&self, hcx: &mut H, hasher: &mut StableHasher) {
2512        self.0.hash_stable(hcx, hasher);
2513    }
2514}
2515
2516impl<S: Encoder> Encodable<S> for RelativeBytePos {
2517    fn encode(&self, s: &mut S) {
2518        s.emit_u32(self.0);
2519    }
2520}
2521
2522impl<D: Decoder> Decodable<D> for RelativeBytePos {
2523    fn decode(d: &mut D) -> RelativeBytePos {
2524        RelativeBytePos(d.read_u32())
2525    }
2526}
2527
2528// _____________________________________________________________________________
2529// Loc, SourceFileAndLine, SourceFileAndBytePos
2530//
2531
2532/// A source code location used for error reporting.
2533#[derive(Debug, Clone)]
2534pub struct Loc {
2535    /// Information about the original source.
2536    pub file: Arc<SourceFile>,
2537    /// The (1-based) line number.
2538    pub line: usize,
2539    /// The (0-based) column offset.
2540    pub col: CharPos,
2541    /// The (0-based) column offset when displayed.
2542    pub col_display: usize,
2543}
2544
2545// Used to be structural records.
2546#[derive(Debug)]
2547pub struct SourceFileAndLine {
2548    pub sf: Arc<SourceFile>,
2549    /// Index of line, starting from 0.
2550    pub line: usize,
2551}
2552#[derive(Debug)]
2553pub struct SourceFileAndBytePos {
2554    pub sf: Arc<SourceFile>,
2555    pub pos: BytePos,
2556}
2557
2558#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2559pub struct LineInfo {
2560    /// Index of line, starting from 0.
2561    pub line_index: usize,
2562
2563    /// Column in line where span begins, starting from 0.
2564    pub start_col: CharPos,
2565
2566    /// Column in line where span ends, starting from 0, exclusive.
2567    pub end_col: CharPos,
2568}
2569
2570pub struct FileLines {
2571    pub file: Arc<SourceFile>,
2572    pub lines: Vec<LineInfo>,
2573}
2574
2575pub static SPAN_TRACK: AtomicRef<fn(LocalDefId)> = AtomicRef::new(&((|_| {}) as fn(_)));
2576
2577// _____________________________________________________________________________
2578// SpanLinesError, SpanSnippetError, DistinctSources, MalformedSourceMapPositions
2579//
2580
2581pub type FileLinesResult = Result<FileLines, SpanLinesError>;
2582
2583#[derive(Clone, PartialEq, Eq, Debug)]
2584pub enum SpanLinesError {
2585    DistinctSources(Box<DistinctSources>),
2586}
2587
2588#[derive(Clone, PartialEq, Eq, Debug)]
2589pub enum SpanSnippetError {
2590    IllFormedSpan(Span),
2591    DistinctSources(Box<DistinctSources>),
2592    MalformedForSourcemap(MalformedSourceMapPositions),
2593    SourceNotAvailable { filename: FileName },
2594}
2595
2596#[derive(Clone, PartialEq, Eq, Debug)]
2597pub struct DistinctSources {
2598    pub begin: (FileName, BytePos),
2599    pub end: (FileName, BytePos),
2600}
2601
2602#[derive(Clone, PartialEq, Eq, Debug)]
2603pub struct MalformedSourceMapPositions {
2604    pub name: FileName,
2605    pub source_len: usize,
2606    pub begin_pos: BytePos,
2607    pub end_pos: BytePos,
2608}
2609
2610/// Range inside of a `Span` used for diagnostics when we only have access to relative positions.
2611#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2612pub struct InnerSpan {
2613    pub start: usize,
2614    pub end: usize,
2615}
2616
2617impl InnerSpan {
2618    pub fn new(start: usize, end: usize) -> InnerSpan {
2619        InnerSpan { start, end }
2620    }
2621}
2622
2623/// Requirements for a `StableHashingContext` to be used in this crate.
2624///
2625/// This is a hack to allow using the [`HashStable_Generic`] derive macro
2626/// instead of implementing everything in rustc_middle.
2627pub trait HashStableContext {
2628    fn def_path_hash(&self, def_id: DefId) -> DefPathHash;
2629    fn hash_spans(&self) -> bool;
2630    /// Accesses `sess.opts.unstable_opts.incremental_ignore_spans` since
2631    /// we don't have easy access to a `Session`
2632    fn unstable_opts_incremental_ignore_spans(&self) -> bool;
2633    fn def_span(&self, def_id: LocalDefId) -> Span;
2634    fn span_data_to_lines_and_cols(
2635        &mut self,
2636        span: &SpanData,
2637    ) -> Option<(StableSourceFileId, usize, BytePos, usize, BytePos)>;
2638    fn hashing_controls(&self) -> HashingControls;
2639}
2640
2641impl<CTX> HashStable<CTX> for Span
2642where
2643    CTX: HashStableContext,
2644{
2645    /// Hashes a span in a stable way. We can't directly hash the span's `BytePos`
2646    /// fields (that would be similar to hashing pointers, since those are just
2647    /// offsets into the `SourceMap`). Instead, we hash the (file name, line, column)
2648    /// triple, which stays the same even if the containing `SourceFile` has moved
2649    /// within the `SourceMap`.
2650    ///
2651    /// Also note that we are hashing byte offsets for the column, not unicode
2652    /// codepoint offsets. For the purpose of the hash that's sufficient.
2653    /// Also, hashing filenames is expensive so we avoid doing it twice when the
2654    /// span starts and ends in the same file, which is almost always the case.
2655    fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
2656        const TAG_VALID_SPAN: u8 = 0;
2657        const TAG_INVALID_SPAN: u8 = 1;
2658        const TAG_RELATIVE_SPAN: u8 = 2;
2659
2660        if !ctx.hash_spans() {
2661            return;
2662        }
2663
2664        let span = self.data_untracked();
2665        span.ctxt.hash_stable(ctx, hasher);
2666        span.parent.hash_stable(ctx, hasher);
2667
2668        if span.is_dummy() {
2669            Hash::hash(&TAG_INVALID_SPAN, hasher);
2670            return;
2671        }
2672
2673        if let Some(parent) = span.parent {
2674            let def_span = ctx.def_span(parent).data_untracked();
2675            if def_span.contains(span) {
2676                // This span is enclosed in a definition: only hash the relative position.
2677                Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2678                (span.lo - def_span.lo).to_u32().hash_stable(ctx, hasher);
2679                (span.hi - def_span.lo).to_u32().hash_stable(ctx, hasher);
2680                return;
2681            }
2682        }
2683
2684        // If this is not an empty or invalid span, we want to hash the last
2685        // position that belongs to it, as opposed to hashing the first
2686        // position past it.
2687        let Some((file, line_lo, col_lo, line_hi, col_hi)) = ctx.span_data_to_lines_and_cols(&span)
2688        else {
2689            Hash::hash(&TAG_INVALID_SPAN, hasher);
2690            return;
2691        };
2692
2693        Hash::hash(&TAG_VALID_SPAN, hasher);
2694        Hash::hash(&file, hasher);
2695
2696        // Hash both the length and the end location (line/column) of a span. If we
2697        // hash only the length, for example, then two otherwise equal spans with
2698        // different end locations will have the same hash. This can cause a problem
2699        // during incremental compilation wherein a previous result for a query that
2700        // depends on the end location of a span will be incorrectly reused when the
2701        // end location of the span it depends on has changed (see issue #74890). A
2702        // similar analysis applies if some query depends specifically on the length
2703        // of the span, but we only hash the end location. So hash both.
2704
2705        let col_lo_trunc = (col_lo.0 as u64) & 0xFF;
2706        let line_lo_trunc = ((line_lo as u64) & 0xFF_FF_FF) << 8;
2707        let col_hi_trunc = (col_hi.0 as u64) & 0xFF << 32;
2708        let line_hi_trunc = ((line_hi as u64) & 0xFF_FF_FF) << 40;
2709        let col_line = col_lo_trunc | line_lo_trunc | col_hi_trunc | line_hi_trunc;
2710        let len = (span.hi - span.lo).0;
2711        Hash::hash(&col_line, hasher);
2712        Hash::hash(&len, hasher);
2713    }
2714}
2715
2716/// Useful type to use with `Result<>` indicate that an error has already
2717/// been reported to the user, so no need to continue checking.
2718///
2719/// The `()` field is necessary: it is non-`pub`, which means values of this
2720/// type cannot be constructed outside of this crate.
2721#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
2722#[derive(HashStable_Generic)]
2723pub struct ErrorGuaranteed(());
2724
2725impl ErrorGuaranteed {
2726    /// Don't use this outside of `DiagCtxtInner::emit_diagnostic`!
2727    #[deprecated = "should only be used in `DiagCtxtInner::emit_diagnostic`"]
2728    pub fn unchecked_error_guaranteed() -> Self {
2729        ErrorGuaranteed(())
2730    }
2731
2732    pub fn raise_fatal(self) -> ! {
2733        FatalError.raise()
2734    }
2735}
2736
2737impl<E: rustc_serialize::Encoder> Encodable<E> for ErrorGuaranteed {
2738    #[inline]
2739    fn encode(&self, _e: &mut E) {
2740        panic!(
2741            "should never serialize an `ErrorGuaranteed`, as we do not write metadata or \
2742            incremental caches in case errors occurred"
2743        )
2744    }
2745}
2746impl<D: rustc_serialize::Decoder> Decodable<D> for ErrorGuaranteed {
2747    #[inline]
2748    fn decode(_d: &mut D) -> ErrorGuaranteed {
2749        panic!(
2750            "`ErrorGuaranteed` should never have been serialized to metadata or incremental caches"
2751        )
2752    }
2753}