1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! Byte-offset spans — minimal and cheap. Line/column are computed on demand.
use std::fmt;
use serde::{Deserialize, Serialize};
/// A half-open byte range `[start, end)` into some source string.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub struct Span {
pub start: u32,
pub end: u32,
}
impl Span {
#[must_use]
pub const fn new(start: u32, end: u32) -> Self {
Self { start, end }
}
#[must_use]
pub const fn point(offset: u32) -> Self {
Self {
start: offset,
end: offset,
}
}
/// Byte-width of the half-open range `[start, end)`. `pub const fn` —
/// `u32::saturating_sub` is const-stable since Rust 1.47, well before
/// this workspace's 1.89 MSRV floor, so the promotion is a body-
/// preserving type-signature widening. Matches the sibling
/// [`Self::new`] / [`Self::point`] / [`Self::contains`] /
/// [`Self::union`] `pub const fn` shape on the same [`Span`] primitive
/// — every downstream consumer that wants a compile-time span-width
/// fixture (a `const WIDTH: u32 = SPAN.len();` LSP hover-registry
/// entry, a per-diagnostic const-context width oracle a future
/// admission webhook consults, a compile-time span-partition truth
/// table the caixa-fmt trivia-owner resolver keys off) now reads
/// through one substrate-primitive const dispatch rather than being
/// forced onto the runtime code path.
#[must_use]
pub const fn len(self) -> u32 {
self.end.saturating_sub(self.start)
}
/// Half-open emptiness predicate — `true` iff `self.start == self.end`.
/// `pub const fn` — folds onto the sibling [`Self::len`] `pub const
/// fn` promotion (integer equality is const in Rust since long before
/// this workspace's 1.89 MSRV floor). Matches every other accessor /
/// predicate on this [`Span`] primitive's const-eval surface; only
/// the fundamentally-runtime-only `slice(&str)` method (string-slice
/// indexing outside const-eval) remains `pub fn`.
#[must_use]
pub const fn is_empty(self) -> bool {
self.len() == 0
}
/// The smallest span covering both. Useful for building a list node's
/// span from its children — every parser code path that composes a
/// parent span from its immediate child boundaries reads through this
/// (`open.union(close)` on a delimited list, `head.union(target.span)`
/// on a quote-form target, and every downstream trivia-owner /
/// diagnostic-aggregator / fmt-region parent-span builder). `pub const
/// fn` — the body reaches for `u32::min` / `u32::max`, both const
/// stable since Rust 1.83 (well before this workspace's 1.89 MSRV
/// floor), so the promotion is a body-preserving type-signature
/// widening. Matches the sibling [`Self::new`] / [`Self::point`] /
/// [`Self::contains`] `pub const fn` shape on the same [`Span`]
/// primitive — every downstream consumer that wants a compile-time
/// span-composition fixture (a `const OUTER: Span = INNER1.union(
/// INNER2);` LSP hover-registry entry, a per-diagnostic const-context
/// parent-span oracle a future admission webhook consults, a
/// compile-time span-partition truth table the caixa-fmt trivia-owner
/// resolver keys off) now reads through one substrate-primitive const
/// dispatch rather than being forced onto the runtime code path.
#[must_use]
pub const fn union(self, other: Span) -> Span {
// Body-preserving `Ord::min` / `Ord::max` open-coding: the trait
// dispatch is not yet stable in `const` context (rust-lang/rust
// #143874), so the pub-const-fn promotion reads through inline
// `if`/`else` on the same `u32 < u32` / `u32 > u32` comparisons
// the primitive-integer inherent methods lower to.
let start = if self.start < other.start {
self.start
} else {
other.start
};
let end = if self.end > other.end {
self.end
} else {
other.end
};
Span { start, end }
}
#[must_use]
pub fn slice<'a>(self, src: &'a str) -> &'a str {
let start = self.start as usize;
let end = self.end as usize;
if start >= src.len() {
""
} else {
let end = end.min(src.len());
&src[start..end]
}
}
/// Byte-offset half-open containment predicate every consumer that
/// keys off an author-authored source position (LSP hover
/// span-lookup at the cursor, per-diagnostic span-registry probe,
/// per-trivia leading/trailing-owner attachment gate) reads through
/// — returns `true` iff `offset` lies inside the half-open range
/// `[self.start, self.end)`. `pub const fn` — matches the sibling
/// [`Self::new`] / [`Self::point`] `pub const fn` shape on the same
/// [`Span`] primitive's construction axis, extending the const-eval
/// surface onto the primitive's containment-predicate axis without
/// a body change (integer comparison is const in Rust since long
/// before this workspace's 1.89 MSRV floor). Every downstream
/// consumer that wants a compile-time span-containment fixture —
/// a `const IS_INSIDE: bool = SPAN.contains(OFFSET);` LSP hover-
/// registry entry, a per-diagnostic const-context span oracle a
/// future admission webhook consults, a compile-time span-partition
/// truth table the caixa-fmt trivia-owner resolver keys off — now
/// reads through one substrate-primitive const dispatch rather than
/// being forced onto the runtime code path.
#[must_use]
pub const fn contains(self, offset: u32) -> bool {
offset >= self.start && offset < self.end
}
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}..{}", self.start, self.end)
}
}
/// 1-indexed line/column pair — what humans see in editors.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Position {
pub line: u32,
pub column: u32,
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}
/// Compute (line, column) for a byte offset. Line and column are 1-indexed.
/// O(offset); fine for diagnostics, not for hot paths.
#[must_use]
pub fn line_column(src: &str, offset: u32) -> Position {
let mut line: u32 = 1;
let mut col: u32 = 1;
let offset = offset as usize;
for (i, ch) in src.char_indices() {
if i >= offset {
break;
}
if ch == '\n' {
line += 1;
col = 1;
} else {
col += 1;
}
}
Position { line, column: col }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn point_span_is_empty() {
let s = Span::point(5);
assert!(s.is_empty());
assert_eq!(s.len(), 0);
}
#[test]
fn union_widens() {
let a = Span::new(2, 5);
let b = Span::new(4, 9);
let u = a.union(b);
assert_eq!(u.start, 2);
assert_eq!(u.end, 9);
}
#[test]
fn union_is_const() {
// Pin the const-eval surface: the substrate-primitive parent-span
// builder reaches into `const` context, so a future compile-time
// parser-fixture / diagnostic-aggregator / fmt-region template
// can build a `const OUTER: Span = INNER1.union(INNER2);` without
// being forced onto the runtime code path. The four
// `const _: () = assert!(…)` bindings resolve the composition at
// compile time — any regression that drops `pub const fn` back
// to `pub fn` (a body edit that reaches for a non-const
// operation) fails this test at compile time rather than at
// runtime, matching the sibling `Span::new` / `Span::point` /
// `Span::contains` `pub const fn` shape's const-eval discipline.
const A: Span = Span::new(2, 5);
const B: Span = Span::new(4, 9);
const U: Span = A.union(B);
const _: () = assert!(U.start == 2);
const _: () = assert!(U.end == 9);
// Half-open containment on the const-composed parent span keys
// through `Span::contains`'s own `pub const fn` promotion — so
// both const-eval surfaces (composition and containment) resolve
// in one compile-time expression, matching the [start, end)
// boundary discipline the sibling `contains_is_const` fixture
// already pins.
const _: () = assert!(U.contains(2));
const _: () = assert!(!U.contains(9));
}
#[test]
fn slice_extracts_substring() {
let src = "hello world";
assert_eq!(Span::new(6, 11).slice(src), "world");
}
#[test]
fn line_column_handles_newlines() {
let src = "abc\ndef\nghi";
assert_eq!(line_column(src, 0), Position { line: 1, column: 1 });
assert_eq!(line_column(src, 4), Position { line: 2, column: 1 });
assert_eq!(line_column(src, 9), Position { line: 3, column: 2 });
}
#[test]
fn contains_is_half_open() {
let s = Span::new(3, 7);
assert!(!s.contains(2));
assert!(s.contains(3));
assert!(s.contains(6));
assert!(!s.contains(7));
}
#[test]
fn len_and_is_empty_are_const() {
// Pin the const-eval surface: the substrate-primitive width
// accessor and emptiness predicate reach into `const` context, so
// a future compile-time span-registry / LSP hover-oracle /
// trivia-owner truth-table fixture can key off `Span::len` /
// `Span::is_empty` without being forced onto the runtime code
// path. Any regression that drops `pub const fn` back to `pub fn`
// (a body edit that reaches for a non-const operation) fails this
// test at compile time rather than at runtime, matching the
// sibling `Span::new` / `Span::point` / `Span::contains` /
// `Span::union` `pub const fn` shape's const-eval discipline.
const RANGE: Span = Span::new(3, 7);
const POINT: Span = Span::point(5);
const RANGE_LEN: u32 = RANGE.len();
const POINT_LEN: u32 = POINT.len();
const RANGE_EMPTY: bool = RANGE.is_empty();
const POINT_EMPTY: bool = POINT.is_empty();
const _: () = assert!(RANGE_LEN == 4);
const _: () = assert!(POINT_LEN == 0);
const _: () = assert!(!RANGE_EMPTY);
const _: () = assert!(POINT_EMPTY);
// Saturating-sub floor: an inverted (end < start) fixture must
// clamp to 0 at compile time, matching the runtime
// `u32::saturating_sub` semantics the pre-lift body carried.
const INVERTED: Span = Span::new(9, 2);
const INVERTED_LEN: u32 = INVERTED.len();
const INVERTED_EMPTY: bool = INVERTED.is_empty();
const _: () = assert!(INVERTED_LEN == 0);
const _: () = assert!(INVERTED_EMPTY);
}
#[test]
fn contains_is_const() {
// Pin the const-eval surface: the substrate-primitive
// containment predicate reaches into `const` context, so a
// future compile-time span-registry / LSP hover-oracle /
// trivia-owner truth-table fixture can key off `Span::contains`
// without being forced onto the runtime code path. The four
// `const _: () = assert!(…)` bindings resolve the predicate at
// compile time — any regression that drops `pub const fn` back
// to `pub fn` (a body edit that reaches for a non-const
// operation) fails this test at compile time rather than at
// runtime, matching the sibling `Span::new` / `Span::point`
// `pub const fn` shape's const-eval discipline.
const SPAN: Span = Span::new(3, 7);
const _: () = assert!(!SPAN.contains(2));
const _: () = assert!(SPAN.contains(3));
const _: () = assert!(SPAN.contains(5));
const _: () = assert!(!SPAN.contains(7));
}
}