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