Skip to content

Commit 3a9bdad

Browse files
committed
unit spans
1 parent cd57156 commit 3a9bdad

File tree

3 files changed

+50
-82
lines changed

3 files changed

+50
-82
lines changed

library/proc_macro/src/bridge/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ mod state {
207207

208208
fn standalone_bridge() -> Bridge<'static> {
209209
let mut store = HandleStore::new(&COUNTERS);
210-
let id = store.Span.alloc(Marked { value: standalone::Span::DUMMY, _marker: PhantomData });
210+
let id = store.Span.alloc(Marked { value: standalone::Span, _marker: PhantomData });
211211
let dummy = super::Span { handle: id };
212212
let dispatcher =
213213
Dispatcher { handle_store: store, server: MarkedTypes(standalone::NoRustc) };

library/proc_macro/src/bridge/standalone.rs

Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,93 @@
1-
#![allow(unused_variables)]
21
#![warn(warnings)]
3-
use std::cell::RefCell;
2+
use std::cell::{Cell, RefCell};
43
use std::ops::{Bound, Range};
54

6-
use crate::{Delimiter, LEGAL_PUNCT_CHARS};
75
use crate::bridge::client::Symbol;
86
use crate::bridge::fxhash::FxHashMap;
9-
use crate::bridge::{server, DelimSpan, Diagnostic, ExpnGlobals, Group, LitKind, Literal, Punct, TokenTree};
7+
use crate::bridge::{DelimSpan, Diagnostic, ExpnGlobals, Group, Literal, Punct, TokenTree, server};
8+
use crate::{Delimiter, LEGAL_PUNCT_CHARS};
109

1110
pub struct NoRustc;
1211

1312
impl server::Span for NoRustc {
14-
fn debug(&mut self, span: Self::Span) -> String {
15-
format!("{} bytes({}..{})", span.hi - span.lo, span.lo, span.hi)
13+
fn debug(&mut self, _: Self::Span) -> String {
14+
"Span".to_string()
1615
}
1716

18-
fn parent(&mut self, span: Self::Span) -> Option<Self::Span> {
19-
todo!()
17+
fn parent(&mut self, _: Self::Span) -> Option<Self::Span> {
18+
None
2019
}
2120

22-
fn source(&mut self, span: Self::Span) -> Self::Span {
23-
todo!()
21+
fn source(&mut self, _: Self::Span) -> Self::Span {
22+
Span
2423
}
2524

26-
fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
27-
span.lo as usize..span.hi as usize
25+
fn byte_range(&mut self, _: Self::Span) -> Range<usize> {
26+
todo!()
2827
}
2928

30-
fn start(&mut self, span: Self::Span) -> Self::Span {
31-
Span { lo: span.lo, hi: span.lo }
29+
fn start(&mut self, _: Self::Span) -> Self::Span {
30+
Span
3231
}
3332

34-
fn end(&mut self, span: Self::Span) -> Self::Span {
35-
Span { lo: span.hi, hi: span.hi }
33+
fn end(&mut self, _: Self::Span) -> Self::Span {
34+
Span
3635
}
3736

38-
fn line(&mut self, span: Self::Span) -> usize {
37+
fn line(&mut self, _: Self::Span) -> usize {
3938
todo!()
4039
}
4140

42-
fn column(&mut self, span: Self::Span) -> usize {
41+
fn column(&mut self, _: Self::Span) -> usize {
4342
todo!()
4443
}
4544

46-
fn file(&mut self, span: Self::Span) -> String {
47-
todo!()
45+
fn file(&mut self, _: Self::Span) -> String {
46+
"<anon>".to_string()
4847
}
4948

50-
fn local_file(&mut self, span: Self::Span) -> Option<String> {
51-
todo!()
49+
fn local_file(&mut self, _: Self::Span) -> Option<String> {
50+
None
5251
}
5352

54-
fn join(&mut self, span: Self::Span, other: Self::Span) -> Option<Self::Span> {
55-
todo!()
53+
fn join(&mut self, _: Self::Span, _: Self::Span) -> Option<Self::Span> {
54+
Some(Span)
5655
}
5756

5857
fn subspan(
5958
&mut self,
60-
span: Self::Span,
61-
start: Bound<usize>,
62-
end: Bound<usize>,
59+
_: Self::Span,
60+
_start: Bound<usize>,
61+
_end: Bound<usize>,
6362
) -> Option<Self::Span> {
64-
let length = span.hi as usize - span.lo as usize;
65-
66-
let start = match start {
67-
Bound::Included(lo) => lo,
68-
Bound::Excluded(lo) => lo.checked_add(1)?,
69-
Bound::Unbounded => 0,
70-
};
71-
72-
let end = match end {
73-
Bound::Included(hi) => hi.checked_add(1)?,
74-
Bound::Excluded(hi) => hi,
75-
Bound::Unbounded => length,
76-
};
77-
78-
// Bounds check the values, preventing addition overflow and OOB spans.
79-
if start > u32::MAX as usize
80-
|| end > u32::MAX as usize
81-
|| (u32::MAX - start as u32) < span.lo
82-
|| (u32::MAX - end as u32) < span.lo
83-
|| start >= end
84-
|| end > length
85-
{
86-
return None;
87-
}
88-
89-
let new_lo = span.lo + start as u32;
90-
let new_hi = span.lo + end as u32;
91-
Some(Span { lo: new_lo, hi: new_hi })
63+
Some(Span)
9264
}
9365

94-
fn resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
95-
todo!()
66+
fn resolved_at(&mut self, _span: Self::Span, _at: Self::Span) -> Self::Span {
67+
Span
9668
}
9769

98-
fn source_text(&mut self, span: Self::Span) -> Option<String> {
99-
todo!()
70+
fn source_text(&mut self, _: Self::Span) -> Option<String> {
71+
None
10072
}
10173

102-
fn save_span(&mut self, span: Self::Span) -> usize {
103-
SAVED_SPANS.with_borrow_mut(|spans| {
104-
let idx = spans.len();
105-
spans.push(span);
106-
idx
107-
})
74+
fn save_span(&mut self, _: Self::Span) -> usize {
75+
let n = SAVED_SPAN_COUNT.get();
76+
SAVED_SPAN_COUNT.set(n + 1);
77+
n
10878
}
10979

11080
fn recover_proc_macro_span(&mut self, id: usize) -> Self::Span {
111-
SAVED_SPANS.with_borrow(|spans| spans[id])
81+
if id < SAVED_SPAN_COUNT.get() {
82+
Span
83+
} else {
84+
panic!("recovered span index out of bounds");
85+
}
11286
}
11387
}
11488

11589
thread_local! {
116-
static SAVED_SPANS: RefCell<Vec<Span>> = const { RefCell::new(Vec::new()) };
90+
static SAVED_SPAN_COUNT: Cell<usize> = const { Cell::new(0) };
11791
static TRACKED_ENV_VARS: RefCell<FxHashMap<String, Option<String>>> = RefCell::new(FxHashMap::default());
11892
}
11993

@@ -144,11 +118,11 @@ impl server::FreeFunctions for NoRustc {
144118
'0'..='9' | '-' => todo!(),
145119
'\'' => todo!(),
146120
'"' => todo!(),
147-
_ => Err(())
121+
_ => Err(()),
148122
}
149123
}
150124

151-
fn emit_diagnostic(&mut self, diagnostic: Diagnostic<Self::Span>) {
125+
fn emit_diagnostic(&mut self, _: Diagnostic<Self::Span>) {
152126
panic!("cannot emit diagnostic in standalone mode");
153127
}
154128
}
@@ -188,7 +162,7 @@ impl server::TokenStream for NoRustc {
188162
let group = TokenTree::<_, _, Symbol>::Group(Group {
189163
delimiter: delim,
190164
stream: unfinished_streams.pop(),
191-
span: DelimSpan::from_single(Span::DUMMY)
165+
span: DelimSpan::from_single(Span),
192166
});
193167
unfinished_streams.last_mut().unwrap().0.push(group);
194168
} else {
@@ -198,7 +172,7 @@ impl server::TokenStream for NoRustc {
198172
unfinished_streams.last_mut().unwrap().0.push(TokenTree::Punct(Punct {
199173
ch: c as u8,
200174
joint: false, // TODO
201-
span: Span::DUMMY,
175+
span: Span,
202176
}));
203177
}
204178
match c {
@@ -365,13 +339,7 @@ impl TokenStream {
365339
}
366340

367341
#[derive(Hash, PartialEq, Eq, Clone, Copy)]
368-
pub struct Span {
369-
pub lo: u32,
370-
pub hi: u32,
371-
}
372-
impl Span {
373-
pub const DUMMY: Self = Self { lo: 0, hi: 0 };
374-
}
342+
pub struct Span;
375343

376344
impl server::Types for NoRustc {
377345
type FreeFunctions = FreeFunctions;
@@ -382,7 +350,7 @@ impl server::Types for NoRustc {
382350

383351
impl server::Server for NoRustc {
384352
fn globals(&mut self) -> ExpnGlobals<Self::Span> {
385-
ExpnGlobals { def_site: Span::DUMMY, call_site: Span::DUMMY, mixed_site: Span::DUMMY }
353+
ExpnGlobals { def_site: Span, call_site: Span, mixed_site: Span }
386354
}
387355

388356
fn intern_symbol(ident: &str) -> Self::Symbol {

library/proc_macro/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,8 @@ pub enum Spacing {
970970
}
971971

972972
pub(crate) const LEGAL_PUNCT_CHARS: &[char] = &[
973-
'=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^', '&', '|', '@', '.', ',', ';',
974-
':', '#', '$', '?', '\'',
973+
'=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^', '&', '|', '@', '.', ',', ';', ':', '#',
974+
'$', '?', '\'',
975975
];
976976

977977
impl Punct {

0 commit comments

Comments
 (0)