Skip to content

Commit a8775d4

Browse files
committed
Edit rustc_span documentation
Various changes to the `rustc_span` docs, including the following: - Additions to top-level docs - Edits to the source_map module docs - Edits to documentation for `Span` and `SpanData` - Added intra-docs links - Documentation for Levenshtein distances - Fixed missing punctuation
1 parent 3d10d3e commit a8775d4

File tree

5 files changed

+71
-46
lines changed

5 files changed

+71
-46
lines changed

compiler/rustc_span/src/edition.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ use std::str::FromStr;
44

55
use rustc_macros::HashStable_Generic;
66

7-
/// The edition of the compiler (RFC 2052)
7+
/// The edition of the compiler. (See [RFC 2052](https://github.com/rust-lang/rfcs/blob/master/text/2052-epochs.md).)
88
#[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Debug, Encodable, Decodable, Eq)]
99
#[derive(HashStable_Generic)]
1010
pub enum Edition {
11-
// editions must be kept in order, oldest to newest
11+
// When adding new editions, be sure to do the following:
12+
//
13+
// - update the `ALL_EDITIONS` const
14+
// - update the `EDITION_NAME_LIST` const
15+
// - add a `rust_####()` function to the session
16+
// - update the enum in Cargo's sources as well
17+
//
18+
// Editions *must* be kept in order, oldest to newest.
1219
/// The 2015 edition
1320
Edition2015,
1421
/// The 2018 edition
1522
Edition2018,
16-
// when adding new editions, be sure to update:
17-
//
18-
// - Update the `ALL_EDITIONS` const
19-
// - Update the EDITION_NAME_LIST const
20-
// - add a `rust_####()` function to the session
21-
// - update the enum in Cargo's sources as well
2223
}
2324

24-
// must be in order from oldest to newest
25+
// Must be in order from oldest to newest.
2526
pub const ALL_EDITIONS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
2627

2728
pub const EDITION_NAME_LIST: &str = "2015|2018";

compiler/rustc_span/src/lev_distance.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
//! Levenshtein distances.
2+
//!
3+
//! The [Levenshtein distance] is a metric for measuring the difference between two strings.
4+
//!
5+
//! [Levenshtein distance]: https://en.wikipedia.org/wiki/Levenshtein_distance
6+
17
use crate::symbol::Symbol;
28
use std::cmp;
39

410
#[cfg(test)]
511
mod tests;
612

7-
/// Finds the Levenshtein distance between two strings
13+
/// Finds the Levenshtein distance between two strings.
814
pub fn lev_distance(a: &str, b: &str) -> usize {
915
// cases which don't require further computation
1016
if a.is_empty() {
@@ -35,14 +41,14 @@ pub fn lev_distance(a: &str, b: &str) -> usize {
3541
dcol[t_last + 1]
3642
}
3743

38-
/// Finds the best match for a given word in the given iterator
44+
/// Finds the best match for a given word in the given iterator.
3945
///
4046
/// As a loose rule to avoid the obviously incorrect suggestions, it takes
4147
/// an optional limit for the maximum allowable edit distance, which defaults
4248
/// to one-third of the given word.
4349
///
44-
/// Besides Levenshtein, we use case insensitive comparison to improve accuracy on an edge case with
45-
/// a lower(upper)case letters mismatch.
50+
/// Besides Levenshtein, we use case insensitive comparison to improve accuracy
51+
/// on an edge case with a lower(upper)case letters mismatch.
4652
#[cold]
4753
pub fn find_best_match_for_name(
4854
name_vec: &[Symbol],
@@ -98,7 +104,7 @@ fn find_match_by_sorted_words(iter_names: &[Symbol], lookup: &str) -> Option<Sym
98104

99105
fn sort_by_words(name: &str) -> String {
100106
let mut split_words: Vec<&str> = name.split('_').collect();
101-
// We are sorting primitive &strs and can use unstable sort here
107+
// We are sorting primitive &strs and can use unstable sort here.
102108
split_words.sort_unstable();
103109
split_words.join("_")
104110
}

compiler/rustc_span/src/lib.rs

+42-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
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

125134
impl 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)]
704717
pub 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)]
11041117
pub 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.
15841597
fn 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

17071720
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.
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.
18401857
pub 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

compiler/rustc_span/src/source_map.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
//! The `SourceMap` tracks all the source code used within a single crate, mapping
1+
//! Types for tracking pieces of source code within a crate.
2+
//!
3+
//! The [`SourceMap`] tracks all the source code used within a single crate, mapping
24
//! from integer byte positions to the original source code location. Each bit
35
//! of source parsed during crate parsing (typically files, in-memory strings,
46
//! or various bits of macro expansion) cover a continuous range of bytes in the
5-
//! `SourceMap` and are represented by `SourceFile`s. Byte positions are stored in
6-
//! `Span` and used pervasively in the compiler. They are absolute positions
7+
//! `SourceMap` and are represented by [`SourceFile`]s. Byte positions are stored in
8+
//! [`Span`] and used pervasively in the compiler. They are absolute positions
79
//! within the `SourceMap`, which upon request can be converted to line and column
810
//! information, source code snippets, etc.
911

compiler/rustc_span/src/span_encoding.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::fx::FxIndexSet;
1212

1313
/// A compressed span.
1414
///
15-
/// `SpanData` is 12 bytes, which is a bit too big to stick everywhere. `Span`
15+
/// Whereas [`SpanData`] is 12 bytes, which is a bit too big to stick everywhere, `Span`
1616
/// is a form that only takes up 8 bytes, with less space for the length and
1717
/// context. The vast majority (99.9%+) of `SpanData` instances will fit within
1818
/// those 8 bytes; any `SpanData` whose fields don't fit into a `Span` are
@@ -42,13 +42,11 @@ use rustc_data_structures::fx::FxIndexSet;
4242
/// - `base` is 32 bits in both `Span` and `SpanData`, which means that `base`
4343
/// values never cause interning. The number of bits needed for `base`
4444
/// depends on the crate size. 32 bits allows up to 4 GiB of code in a crate.
45-
/// `script-servo` is the largest crate in `rustc-perf`, requiring 26 bits
46-
/// for some spans.
4745
/// - `len` is 15 bits in `Span` (a u16, minus 1 bit for the tag) and 32 bits
4846
/// in `SpanData`, which means that large `len` values will cause interning.
4947
/// The number of bits needed for `len` does not depend on the crate size.
50-
/// The most common number of bits for `len` are 0--7, with a peak usually at
51-
/// 3 or 4, and then it drops off quickly from 8 onwards. 15 bits is enough
48+
/// The most common numbers of bits for `len` are from 0 to 7, with a peak usually
49+
/// at 3 or 4, and then it drops off quickly from 8 onwards. 15 bits is enough
5250
/// for 99.99%+ of cases, but larger values (sometimes 20+ bits) might occur
5351
/// dozens of times in a typical crate.
5452
/// - `ctxt` is 16 bits in `Span` and 32 bits in `SpanData`, which means that

0 commit comments

Comments
 (0)