1- //! The source positions and related helper functions.
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 in the [`sym`] module.
8+ //!
9+ //! Unlike most compilers, the span contains not only the position in the source code, but also various other metadata,
10+ //! such as the edition and macro hygiene. This metadata is stored in [`SyntaxContext`] and [`ExpnData`].
211//!
312//! ## Note
413//!
@@ -124,7 +133,7 @@ pub enum RealFileName {
124133
125134impl RealFileName {
126135 /// Returns the path suitable for reading from the file system on the local host.
127- /// Avoid embedding this in build artifacts; see `stable_name` for that.
136+ /// Avoid embedding this in build artifacts; see `stable_name() ` for that.
128137 pub fn local_path ( & self ) -> & Path {
129138 match self {
130139 RealFileName :: Named ( p)
@@ -133,7 +142,7 @@ impl RealFileName {
133142 }
134143
135144 /// Returns the path suitable for reading from the file system on the local host.
136- /// Avoid embedding this in build artifacts; see `stable_name` for that.
145+ /// Avoid embedding this in build artifacts; see `stable_name() ` for that.
137146 pub fn into_local_path ( self ) -> PathBuf {
138147 match self {
139148 RealFileName :: Named ( p)
@@ -143,7 +152,7 @@ impl RealFileName {
143152
144153 /// Returns the path suitable for embedding into build artifacts. Note that
145154 /// a virtualized path will not correspond to a valid file system path; see
146- /// `local_path` for something that is more likely to return paths into the
155+ /// `local_path() ` for something that is more likely to return paths into the
147156 /// local host file system.
148157 pub fn stable_name ( & self ) -> & Path {
149158 match self {
@@ -173,7 +182,7 @@ pub enum FileName {
173182 /// Custom sources for explicit parser calls from plugins and drivers.
174183 Custom ( String ) ,
175184 DocTest ( PathBuf , isize ) ,
176- /// Post-substitution inline assembly from LLVM
185+ /// Post-substitution inline assembly from LLVM.
177186 InlineAsm ( u64 ) ,
178187}
179188
@@ -266,14 +275,17 @@ impl FileName {
266275 }
267276}
268277
278+ /// Represents a span.
279+ ///
269280/// Spans represent a region of code, used for error reporting. Positions in spans
270- /// are *absolute* positions from the beginning of the source_map , not positions
271- /// relative to `SourceFile`s. Methods on the `SourceMap` can be used to relate spans back
281+ /// are *absolute* positions from the beginning of the [`SourceMap`] , not positions
282+ /// relative to [ `SourceFile`] s. Methods on the `SourceMap` can be used to relate spans back
272283/// to the original source.
273- /// You must be careful if the span crosses more than one file - you will not be
284+ ///
285+ /// You must be careful if the span crosses more than one file, since you will not be
274286/// able to use many of the functions on spans in source_map and you cannot assume
275- /// that the length of the ` span = hi - lo`; there may be space in the `BytePos`
276- /// range between files.
287+ /// that the length of the span is equal to `span. hi - span. lo`; there may be space in the
288+ /// [`BytePos`] range between files.
277289///
278290/// `SpanData` is public because `Span` uses a thread-local interner and can't be
279291/// sent to other threads, but some pieces of performance infra run in a separate thread.
@@ -384,7 +396,7 @@ impl Span {
384396 Span :: new ( lo, hi, SyntaxContext :: root ( ) )
385397 }
386398
387- /// Returns a new span representing an empty span at the beginning of this span
399+ /// Returns a new span representing an empty span at the beginning of this span.
388400 #[ inline]
389401 pub fn shrink_to_lo ( self ) -> Span {
390402 let span = self . data ( ) ;
@@ -398,7 +410,7 @@ impl Span {
398410 }
399411
400412 #[ inline]
401- /// Returns true if hi == lo
413+ /// Returns ` true` if ` hi == lo`.
402414 pub fn is_empty ( & self ) -> bool {
403415 let span = self . data ( ) ;
404416 span. hi == span. lo
@@ -512,7 +524,7 @@ impl Span {
512524 }
513525
514526 /// Checks if a span is "internal" to a macro in which `unsafe`
515- /// can be used without triggering the `unsafe_code` lint
527+ /// can be used without triggering the `unsafe_code` lint.
516528 // (that is, a macro marked with `#[allow_internal_unsafe]`).
517529 pub fn allows_unsafe ( & self ) -> bool {
518530 self . ctxt ( ) . outer_expn_data ( ) . allow_internal_unsafe
@@ -700,6 +712,7 @@ impl Span {
700712 }
701713}
702714
715+ /// A span together with some additional data.
703716#[ derive( Clone , Debug ) ]
704717pub struct SpanLabel {
705718 /// The span we are going to include in the final snippet.
@@ -743,7 +756,7 @@ impl<D: Decoder> Decodable<D> for Span {
743756/// any spans that are debug-printed during the closure's execution.
744757///
745758/// Normally, the global `TyCtxt` is used to retrieve the `SourceMap`
746- /// (see `rustc_interface::callbacks::span_debug1). However, some parts
759+ /// (see `rustc_interface::callbacks::span_debug1` ). However, some parts
747760/// of the compiler (e.g. `rustc_parse`) may debug-print `Span`s before
748761/// a `TyCtxt` is available. In this case, we fall back to
749762/// the `SourceMap` provided to this function. If that is not available,
@@ -994,9 +1007,9 @@ pub enum ExternalSource {
9941007 Unneeded ,
9951008 Foreign {
9961009 kind : ExternalSourceKind ,
997- /// This SourceFile's byte-offset within the source_map of its original crate
1010+ /// This SourceFile's byte-offset within the source_map of its original crate.
9981011 original_start_pos : BytePos ,
999- /// The end of this SourceFile within the source_map of its original crate
1012+ /// The end of this SourceFile within the source_map of its original crate.
10001013 original_end_pos : BytePos ,
10011014 } ,
10021015}
@@ -1099,7 +1112,7 @@ impl SourceFileHash {
10991112 }
11001113}
11011114
1102- /// A single source in the `SourceMap`.
1115+ /// A single source in the [ `SourceMap`] .
11031116#[ derive( Clone ) ]
11041117pub struct SourceFile {
11051118 /// The name of the file that the source came from. Source that doesn't
@@ -1580,7 +1593,7 @@ fn remove_bom(src: &mut String, normalized_pos: &mut Vec<NormalizedPos>) {
15801593
15811594/// Replaces `\r\n` with `\n` in-place in `src`.
15821595///
1583- /// Returns error if there's a lone `\r` in the string
1596+ /// Returns error if there's a lone `\r` in the string.
15841597fn normalize_newlines ( src : & mut String , normalized_pos : & mut Vec < NormalizedPos > ) {
15851598 if !src. as_bytes ( ) . contains ( & b'\r' ) {
15861599 return ;
@@ -1705,13 +1718,16 @@ macro_rules! impl_pos {
17051718}
17061719
17071720impl_pos ! {
1708- /// A byte offset. Keep this small (currently 32-bits), as AST contains
1709- /// a lot of them.
1721+ /// A byte offset.
1722+ ///
1723+ /// Keep this small (currently 32-bits), as AST contains a lot of them.
17101724 #[ derive( Clone , Copy , PartialEq , Eq , Hash , PartialOrd , Ord , Debug ) ]
17111725 pub struct BytePos ( pub u32 ) ;
17121726
1713- /// A character offset. Because of multibyte UTF-8 characters, a byte offset
1714- /// is not equivalent to a character offset. The `SourceMap` will convert `BytePos`
1727+ /// A character offset.
1728+ ///
1729+ /// Because of multibyte UTF-8 characters, a byte offset
1730+ /// is not equivalent to a character offset. The [`SourceMap`] will convert [`BytePos`]
17151731 /// values to `CharPos` values as necessary.
17161732 #[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
17171733 pub struct CharPos ( pub usize ) ;
@@ -1835,8 +1851,9 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
18351851}
18361852
18371853/// Requirements for a `StableHashingContext` to be used in this crate.
1838- /// This is a hack to allow using the `HashStable_Generic` derive macro
1839- /// instead of implementing everything in librustc_middle.
1854+ ///
1855+ /// This is a hack to allow using the [`HashStable_Generic`] derive macro
1856+ /// instead of implementing everything in rustc_middle.
18401857pub trait HashStableContext {
18411858 fn hash_def_id ( & mut self , _: DefId , hasher : & mut StableHasher ) ;
18421859 fn hash_crate_num ( & mut self , _: CrateNum , hasher : & mut StableHasher ) ;
@@ -1856,6 +1873,7 @@ where
18561873 /// offsets into the `SourceMap`). Instead, we hash the (file name, line, column)
18571874 /// triple, which stays the same even if the containing `SourceFile` has moved
18581875 /// within the `SourceMap`.
1876+ ///
18591877 /// Also note that we are hashing byte offsets for the column, not unicode
18601878 /// codepoint offsets. For the purpose of the hash that's sufficient.
18611879 /// Also, hashing filenames is expensive so we avoid doing it twice when the
0 commit comments