Skip to content

Commit cc8bdcf

Browse files
committed
Make Span internals public
1 parent d2223aa commit cc8bdcf

File tree

6 files changed

+115
-66
lines changed

6 files changed

+115
-66
lines changed

src/debug.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ fn print_most_recent_spans(linker_files: &LinkerFiles, history: &SpanDebuggerSta
6969
);
7070
pretty_print_many_spans(
7171
linker_files,
72-
spans_to_print.iter().rev().enumerate().map(|(idx, span)| {
73-
let sp = span.as_range();
74-
(*span, format!("-{idx}: Span({}, {})", sp.start, sp.end))
75-
}),
72+
spans_to_print
73+
.iter()
74+
.rev()
75+
.enumerate()
76+
.map(|(idx, span)| (*span, format!("-{idx}: Span({}, {})", span.start, span.end))),
7677
);
7778
}
7879

src/dev_aid/ariadne_interface.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ impl ariadne::Span for Span {
4141
type SourceId = FileUUID;
4242

4343
fn source(&self) -> &Self::SourceId {
44-
self.get_file_ref()
44+
&self.file
4545
}
4646

4747
fn start(&self) -> usize {
48-
self.as_range().start
48+
self.start
4949
}
5050

5151
fn end(&self) -> usize {
52-
self.as_range().end
52+
self.end
5353
}
5454
}
5555

@@ -137,11 +137,7 @@ pub fn pretty_print_many_spans(
137137
Report::build(ReportKind::Advice, first_span.0).with_config(config);
138138

139139
for (span, label) in std::iter::once(first_span).chain(spans_iter) {
140-
report = report.with_label(
141-
Label::new(span)
142-
.with_message(label)
143-
.with_color(Color::Blue),
144-
);
140+
report = report.with_label(Label::new(span).with_message(label).with_color(Color::Blue));
145141
}
146142
report.finish().eprint(&mut linker_files).unwrap();
147143
}

src/dev_aid/lsp/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn cvt_location_list(location_vec: Vec<Span>, linker: &Linker) -> Vec<Location>
5252
location_vec
5353
.into_iter()
5454
.map(|span| {
55-
let file = &linker.files[span.get_file()];
55+
let file = &linker.files[span.file];
5656
let uri = Url::parse(&file.file_identifier).unwrap();
5757
let range = span_to_lsp_range(&file.file_text, span);
5858
Location { uri, range }
@@ -133,7 +133,7 @@ fn convert_diagnostic(err: CompileError, main_file_text: &FileText, linker: &Lin
133133
};
134134
let mut related_info = Vec::new();
135135
for info in err.infos {
136-
let info_file = &linker.files[info.span.get_file()];
136+
let info_file = &linker.files[info.span.file];
137137
let info_span = info.span;
138138
assert!(
139139
info_file.file_text.is_span_valid(info_span),

src/errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ impl ErrorStore {
9393
pub fn sort(&mut self) {
9494
self.errors.sort_by(|a, b| {
9595
a.position
96-
.as_range()
9796
.start
98-
.cmp(&b.position.as_range().start)
97+
.cmp(&b.position.start)
9998
.then_with(|| a.reason.cmp(&b.reason))
10099
});
101100
}

src/file_position.rs

Lines changed: 102 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,111 @@ use crate::prelude::FileUUID;
77

88
/// [Span] is defined as byte-byte idx. Start inclusive, end exclusive
99
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
10-
pub struct Span(usize, usize, FileUUID);
10+
pub struct Span {
11+
pub start: usize,
12+
pub end: usize,
13+
pub file: FileUUID,
14+
}
1115

1216
impl Span {
1317
pub fn from_range(range: Range<usize>, file: FileUUID) -> Span {
1418
assert!(range.end >= range.start);
15-
Span(range.start, range.end, file).debug()
19+
Span {
20+
start: range.start,
21+
end: range.end,
22+
file,
23+
}
24+
.debug()
25+
}
26+
pub fn to_range(self) -> Range<usize> {
27+
assert!(self.end >= self.start);
28+
self.debug();
29+
Range {
30+
start: self.start,
31+
end: self.end,
32+
}
1633
}
1734
/// Register that we have visited this span. Eases debugging when errors occur
18-
pub fn debug(&self) -> Span {
19-
crate::debug::add_debug_span(*self);
20-
*self
35+
pub fn debug(self) -> Span {
36+
crate::debug::add_debug_span(self);
37+
self
2138
}
2239

23-
pub const PLACEHOLDER: Span = Span(0, usize::MAX, FileUUID::PLACEHOLDER);
40+
pub const PLACEHOLDER: Span = Span {
41+
start: 0,
42+
end: usize::MAX,
43+
file: FileUUID::PLACEHOLDER,
44+
};
2445

2546
/// Only really used for having a span with the maximum size.
2647
pub fn make_max_possible_span(file: FileUUID) -> Span {
27-
Span(0, usize::MAX, file)
48+
Span {
49+
start: 0,
50+
end: usize::MAX,
51+
file,
52+
}
2853
}
2954

30-
pub fn as_range(&self) -> Range<usize> {
31-
self.0..self.1
32-
}
33-
pub fn get_file(&self) -> FileUUID {
34-
self.2
35-
}
36-
pub fn get_file_ref(&self) -> &FileUUID {
37-
&self.2
38-
}
3955
#[track_caller]
4056
pub fn new_overarching(left: Span, right: Span) -> Span {
4157
left.debug();
4258
right.debug();
43-
assert!(left.0 <= right.0);
44-
assert!(left.1 <= right.1);
45-
assert_eq!(left.2, right.2);
46-
Span(left.0, right.1, left.2).debug()
59+
assert!(left.start <= right.start);
60+
assert!(left.end <= right.end);
61+
assert_eq!(left.file, right.file);
62+
Span {
63+
start: left.start,
64+
end: right.end,
65+
file: left.file,
66+
}
67+
.debug()
4768
}
4869
pub fn contains(self, other: Span) -> bool {
49-
self.0 <= other.0 && self.1 >= other.1 && self.2 == other.2
70+
self.start <= other.start && self.end >= other.end && self.file == other.file
5071
}
5172
pub fn contains_pos(&self, pos: usize) -> bool {
5273
self.debug();
53-
pos >= self.0 && pos <= self.1
74+
pos >= self.start && pos <= self.end
5475
}
5576
// Not really a useful quantity. Should only be used comparatively, find which is the nested-most span
56-
pub fn size(&self) -> usize {
77+
pub fn size(self) -> usize {
5778
self.debug();
58-
self.1 - self.0
79+
self.end - self.start
5980
}
6081
pub fn empty_span_at_front(self) -> Span {
6182
self.debug();
62-
Span(self.0, self.0, self.2).debug()
83+
Span {
84+
start: self.start,
85+
end: self.start,
86+
file: self.file,
87+
}
88+
.debug()
6389
}
6490
pub fn empty_span_at_end(self) -> Span {
6591
self.debug();
66-
Span(self.1, self.1, self.2).debug()
92+
Span {
93+
start: self.end,
94+
end: self.end,
95+
file: self.file,
96+
}
97+
.debug()
6798
}
68-
pub fn sub_span<R: RangeBounds<usize>>(&self, bound: R) -> Span {
99+
pub fn sub_span<R: RangeBounds<usize>>(self, bound: R) -> Span {
69100
let start = match bound.start_bound() {
70-
std::ops::Bound::Included(from) => self.0 + from,
71-
std::ops::Bound::Excluded(from) => self.0 + from + 1,
72-
std::ops::Bound::Unbounded => self.0,
101+
std::ops::Bound::Included(from) => self.start + from,
102+
std::ops::Bound::Excluded(from) => self.start + from + 1,
103+
std::ops::Bound::Unbounded => self.start,
73104
};
74105
let end = match bound.end_bound() {
75-
std::ops::Bound::Included(to) => self.0 + to + 1,
76-
std::ops::Bound::Excluded(to) => self.0 + to,
77-
std::ops::Bound::Unbounded => self.1,
106+
std::ops::Bound::Included(to) => self.start + to + 1,
107+
std::ops::Bound::Excluded(to) => self.start + to,
108+
std::ops::Bound::Unbounded => self.end,
78109
};
79-
Span(start, end, self.2)
110+
Span {
111+
start,
112+
end,
113+
file: self.file,
114+
}
80115
}
81116
}
82117

@@ -87,15 +122,18 @@ impl PartialOrd for Span {
87122
}
88123
impl Ord for Span {
89124
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
90-
assert_eq!(self.2, other.2);
91-
self.0.cmp(&other.0)
125+
assert_eq!(self.file, other.file);
126+
self.start.cmp(&other.start)
92127
}
93128
}
94129

95130
impl Debug for Span {
96131
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97132
self.debug();
98-
f.debug_tuple("Span").field(&self.0).field(&self.1).finish()
133+
f.debug_tuple("Span")
134+
.field(&self.start)
135+
.field(&self.end)
136+
.finish()
99137
}
100138
}
101139

@@ -109,21 +147,36 @@ impl BracketSpan {
109147
pub fn from_outer(span: Span) -> Self {
110148
Self(span.debug())
111149
}
112-
pub fn inner_span(&self) -> Span {
150+
pub fn inner_span(self) -> Span {
113151
self.0.debug();
114-
Span(self.0.0 + 1, self.0.1 - 1, self.0.2).debug()
152+
Span {
153+
start: self.0.start + 1,
154+
end: self.0.end - 1,
155+
file: self.0.file,
156+
}
157+
.debug()
115158
}
116-
pub fn outer_span(&self) -> Span {
159+
pub fn outer_span(self) -> Span {
117160
self.0.debug();
118161
self.0
119162
}
120-
pub fn open_bracket(&self) -> Span {
163+
pub fn open_bracket(self) -> Span {
121164
self.0.debug();
122-
Span(self.0.0, self.0.0 + 1, self.0.2).debug()
165+
Span {
166+
start: self.0.start,
167+
end: self.0.start + 1,
168+
file: self.0.file,
169+
}
170+
.debug()
123171
}
124-
pub fn close_bracket(&self) -> Span {
172+
pub fn close_bracket(self) -> Span {
125173
self.0.debug();
126-
Span(self.0.1 - 1, self.0.1, self.0.2).debug()
174+
Span {
175+
start: self.0.end - 1,
176+
end: self.0.end,
177+
file: self.0.file,
178+
}
179+
.debug()
127180
}
128181
}
129182

@@ -200,12 +253,12 @@ impl FileText {
200253
}
201254
pub fn get_span_linecol_range(&self, span: Span) -> Range<LineCol> {
202255
span.debug();
203-
self.byte_to_linecol(span.0)..self.byte_to_linecol(span.1)
256+
self.byte_to_linecol(span.start)..self.byte_to_linecol(span.end)
204257
}
205258

206259
pub fn is_span_valid(&self, span: Span) -> bool {
207260
span.debug();
208-
span.1 <= self.file_text.len()
261+
span.end <= self.file_text.len()
209262
}
210263

211264
pub fn len(&self) -> usize {
@@ -216,8 +269,8 @@ impl FileText {
216269
impl Index<Span> for FileText {
217270
type Output = str;
218271

219-
fn index(&self, index: Span) -> &str {
220-
index.debug();
221-
&self.file_text[index.as_range()]
272+
fn index(&self, span: Span) -> &str {
273+
span.debug();
274+
&self.file_text[span.to_range()]
222275
}
223276
}

src/linker/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub struct LinkInfo {
107107

108108
impl LinkInfo {
109109
pub fn get_file(&self) -> FileUUID {
110-
self.span.get_file()
110+
self.span.file
111111
}
112112
pub fn get_instruction_span(&self, instr_id: FlatID) -> Span {
113113
match &self.instructions[instr_id] {

0 commit comments

Comments
 (0)