flow_span/lib.rs
1//! Source positions, source files, and interned identifiers.
2//!
3//! Five vocabularies that every later phase speaks, and nothing else:
4//!
5//! - [`Idx`] and [`IndexVec`], the typed indices and index-keyed vectors every
6//! phase artifact stores its nodes in; the `index` module says why.
7//! - [`Span`], a byte extent that carries its [`FileId`]. Everything a
8//! diagnostic points at is one of these, and because the file travels inside
9//! the span, one [`SourceMap`] reads back a span from any file of a
10//! compilation.
11//! - [`SourceMap`], the append-only owner of the text those spans index into,
12//! plus the line index a caret needs.
13//! - [`Symbol`] and [`Interner`], so a name is a four-byte integer comparison
14//! rather than a string comparison.
15//! - [`RunId`], [`TaskPath`], [`CallRef`] and [`Moment`], the places in a
16//! run, as a span is a place in source. The `place` module holds the one
17//! grammar they are written in, so a frame, a history, a diagnostic and a
18//! command line all read and write them the same way.
19//!
20//! # What is deliberately absent
21//!
22//! **No file loading.** This crate never reads a path. Turning a path into text
23//! is an operating-system act, and one crate in this workspace owns those.
24//!
25//! **No `Symbol` serialization.** A symbol index means nothing without the
26//! interner that produced it; a serialized one would be a number that reads
27//! like a name. Code that persists an identifier persists its text.
28//!
29//! **No reserved-word table.** `flow-lexer` owns the reserved words and turns
30//! those spellings into distinct token kinds before any interning happens.
31
32#![cfg_attr(all(coverage, test), feature(coverage_attribute))]
33
34mod index;
35mod place;
36mod source_map;
37mod span;
38mod spelled;
39mod symbol;
40
41#[cfg(any(test, feature = "test-support"))]
42pub use place::count;
43pub use place::{CallRef, Moment, ParsePlaceError, PlaceKind, RunId, TaskPath};
44pub use source_map::{SourceFile, SourceMap, SourceName};
45pub use span::{FileId, LineCol, Span};
46pub use symbol::{Interner, Symbol};
47
48pub use index::{Idx, IndexVec, MAX_INDEX};
49
50const _: () = assert!(
51 usize::BITS >= u32::BITS,
52 "the workspace stores offsets and indices as u32 and widens them to usize",
53);
54
55/// Widens a `u32` offset, index or count to `usize`.
56///
57/// Every offset, index and count this workspace stores is a `u32`, and every
58/// target it builds for has a `usize` at least as wide (the assertion above
59/// refuses one that does not), so the conversion never loses a bit. It is a
60/// `const fn` because indices are widened in `const` contexts, where
61/// `usize::try_from` is unavailable.
62#[expect(
63 clippy::as_conversions,
64 reason = "u32 to usize is lossless on every target, asserted at compile time above"
65)]
66pub const fn widen(value: u32) -> usize {
67 value as usize
68}