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`].
2
11
//!
3
12
//! ## Note
4
13
//!
@@ -124,7 +133,7 @@ pub enum RealFileName {
124
133
125
134
impl RealFileName {
126
135
/// 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.
128
137
pub fn local_path ( & self ) -> & Path {
129
138
match self {
130
139
RealFileName :: Named ( p)
@@ -133,7 +142,7 @@ impl RealFileName {
133
142
}
134
143
135
144
/// 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.
137
146
pub fn into_local_path ( self ) -> PathBuf {
138
147
match self {
139
148
RealFileName :: Named ( p)
@@ -143,7 +152,7 @@ impl RealFileName {
143
152
144
153
/// Returns the path suitable for embedding into build artifacts. Note that
145
154
/// 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
147
156
/// local host file system.
148
157
pub fn stable_name ( & self ) -> & Path {
149
158
match self {
@@ -173,7 +182,7 @@ pub enum FileName {
173
182
/// Custom sources for explicit parser calls from plugins and drivers.
174
183
Custom ( String ) ,
175
184
DocTest ( PathBuf , isize ) ,
176
- /// Post-substitution inline assembly from LLVM
185
+ /// Post-substitution inline assembly from LLVM.
177
186
InlineAsm ( u64 ) ,
178
187
}
179
188
@@ -266,14 +275,17 @@ impl FileName {
266
275
}
267
276
}
268
277
278
+ /// Represents a span.
279
+ ///
269
280
/// 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
272
283
/// 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
274
286
/// 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.
277
289
///
278
290
/// `SpanData` is public because `Span` uses a thread-local interner and can't be
279
291
/// sent to other threads, but some pieces of performance infra run in a separate thread.
@@ -384,7 +396,7 @@ impl Span {
384
396
Span :: new ( lo, hi, SyntaxContext :: root ( ) )
385
397
}
386
398
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.
388
400
#[ inline]
389
401
pub fn shrink_to_lo ( self ) -> Span {
390
402
let span = self . data ( ) ;
@@ -398,7 +410,7 @@ impl Span {
398
410
}
399
411
400
412
#[ inline]
401
- /// Returns true if hi == lo
413
+ /// Returns ` true` if ` hi == lo`.
402
414
pub fn is_empty ( & self ) -> bool {
403
415
let span = self . data ( ) ;
404
416
span. hi == span. lo
@@ -512,7 +524,7 @@ impl Span {
512
524
}
513
525
514
526
/// 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.
516
528
// (that is, a macro marked with `#[allow_internal_unsafe]`).
517
529
pub fn allows_unsafe ( & self ) -> bool {
518
530
self . ctxt ( ) . outer_expn_data ( ) . allow_internal_unsafe
@@ -700,6 +712,7 @@ impl Span {
700
712
}
701
713
}
702
714
715
+ /// A span together with some additional data.
703
716
#[ derive( Clone , Debug ) ]
704
717
pub struct SpanLabel {
705
718
/// The span we are going to include in the final snippet.
@@ -743,7 +756,7 @@ impl<D: Decoder> Decodable<D> for Span {
743
756
/// any spans that are debug-printed during the closure's execution.
744
757
///
745
758
/// 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
747
760
/// of the compiler (e.g. `rustc_parse`) may debug-print `Span`s before
748
761
/// a `TyCtxt` is available. In this case, we fall back to
749
762
/// the `SourceMap` provided to this function. If that is not available,
@@ -994,9 +1007,9 @@ pub enum ExternalSource {
994
1007
Unneeded ,
995
1008
Foreign {
996
1009
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.
998
1011
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.
1000
1013
original_end_pos : BytePos ,
1001
1014
} ,
1002
1015
}
@@ -1099,7 +1112,7 @@ impl SourceFileHash {
1099
1112
}
1100
1113
}
1101
1114
1102
- /// A single source in the `SourceMap`.
1115
+ /// A single source in the [ `SourceMap`] .
1103
1116
#[ derive( Clone ) ]
1104
1117
pub struct SourceFile {
1105
1118
/// 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>) {
1580
1593
1581
1594
/// Replaces `\r\n` with `\n` in-place in `src`.
1582
1595
///
1583
- /// Returns error if there's a lone `\r` in the string
1596
+ /// Returns error if there's a lone `\r` in the string.
1584
1597
fn normalize_newlines ( src : & mut String , normalized_pos : & mut Vec < NormalizedPos > ) {
1585
1598
if !src. as_bytes ( ) . contains ( & b'\r' ) {
1586
1599
return ;
@@ -1705,13 +1718,16 @@ macro_rules! impl_pos {
1705
1718
}
1706
1719
1707
1720
impl_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.
1710
1724
#[ derive( Clone , Copy , PartialEq , Eq , Hash , PartialOrd , Ord , Debug ) ]
1711
1725
pub struct BytePos ( pub u32 ) ;
1712
1726
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`]
1715
1731
/// values to `CharPos` values as necessary.
1716
1732
#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
1717
1733
pub struct CharPos ( pub usize ) ;
@@ -1835,8 +1851,9 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
1835
1851
}
1836
1852
1837
1853
/// 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.
1840
1857
pub trait HashStableContext {
1841
1858
fn hash_def_id ( & mut self , _: DefId , hasher : & mut StableHasher ) ;
1842
1859
fn hash_crate_num ( & mut self , _: CrateNum , hasher : & mut StableHasher ) ;
@@ -1856,6 +1873,7 @@ where
1856
1873
/// offsets into the `SourceMap`). Instead, we hash the (file name, line, column)
1857
1874
/// triple, which stays the same even if the containing `SourceFile` has moved
1858
1875
/// within the `SourceMap`.
1876
+ ///
1859
1877
/// Also note that we are hashing byte offsets for the column, not unicode
1860
1878
/// codepoint offsets. For the purpose of the hash that's sufficient.
1861
1879
/// Also, hashing filenames is expensive so we avoid doing it twice when the
0 commit comments