Skip to content

Commit cff3ae9

Browse files
committed
Hack together support for Span serialization
1 parent 2c1eca3 commit cff3ae9

File tree

1 file changed

+73
-15
lines changed

1 file changed

+73
-15
lines changed

src/rustc/src/util/meta.rs

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_serialize::{
1919
};
2020
use rustc_session::StableCrateId;
2121
use rustc_span::{
22-
hygiene::{ExpnIndex, Transparency},
23-
ExpnId, Span, SpanDecoder, SpanEncoder, Symbol, SyntaxContext, DUMMY_SP,
22+
hygiene::ExpnIndex, BytePos, ExpnId, FileName, RealFileName, Span, SpanData, SpanDecoder,
23+
SpanEncoder, StableSourceFileId, Symbol, SyntaxContext, DUMMY_SP,
2424
};
2525
use rustc_type_ir::{TyDecoder, TyEncoder};
2626

@@ -43,6 +43,26 @@ where
4343
predicate_shorthands: FxHashMap::default(),
4444
};
4545

46+
// Encode set of source files to preload for span generation
47+
for file in tcx.sess.source_map().files().iter() {
48+
let FileName::Real(RealFileName::LocalPath(path)) = &file.name else {
49+
continue;
50+
};
51+
52+
let Some(path) = path.to_str() else {
53+
continue;
54+
};
55+
56+
if path.is_empty() {
57+
continue;
58+
}
59+
60+
encoder.emit_str(path);
61+
}
62+
63+
encoder.emit_str("");
64+
65+
// Encode item
4666
item.encode(&mut encoder);
4767

4868
if let Err((_, err)) = encoder.encoder.finish() {
@@ -72,6 +92,17 @@ where
7292
ty_cache: FxHashMap::default(),
7393
};
7494

95+
// Load preloaded source files
96+
loop {
97+
let preload_path = decoder.read_str();
98+
if preload_path.is_empty() {
99+
break;
100+
}
101+
102+
let _ = tcx.sess.source_map().load_file(Path::new(&preload_path));
103+
}
104+
105+
// Load decoded value
75106
Some(T::decode(&mut decoder))
76107
}
77108

@@ -112,12 +143,21 @@ impl<'tcx, 'a> TyEncoder for AutokenEncoder<'tcx, 'a> {
112143
}
113144

114145
fn encode_alloc_id(&mut self, alloc_id: &AllocId) {
115-
todo!();
146+
let _ = alloc_id;
147+
unimplemented!("not used by analyzer");
116148
}
117149
}
118150

119151
impl<'tcx, 'a> SpanEncoder for AutokenEncoder<'tcx, 'a> {
120-
fn encode_span(&mut self, _span: Span) {}
152+
fn encode_span(&mut self, span: Span) {
153+
let src_file = self.tcx.sess.source_map().lookup_source_file(span.lo());
154+
let src_offset = src_file.start_pos;
155+
156+
src_file.stable_id.encode(self);
157+
(span.lo() - src_offset).encode(self);
158+
(span.hi() - src_offset).encode(self);
159+
span.data().ctxt.encode(self);
160+
}
121161

122162
fn encode_symbol(&mut self, symbol: Symbol) {
123163
self.emit_str(symbol.as_str());
@@ -129,7 +169,8 @@ impl<'tcx, 'a> SpanEncoder for AutokenEncoder<'tcx, 'a> {
129169
}
130170

131171
fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
132-
syntax_context.marks().encode(self);
172+
// Unimplemented: not used by our analyzer
173+
let _ = syntax_context;
133174
}
134175

135176
fn encode_crate_num(&mut self, crate_num: CrateNum) {
@@ -238,13 +279,35 @@ impl<'tcx, 'a> TyDecoder for AutokenDecoder<'tcx, 'a> {
238279
}
239280

240281
fn decode_alloc_id(&mut self) -> AllocId {
241-
todo!()
282+
unimplemented!("not used by analyzer");
242283
}
243284
}
244285

245286
impl<'tcx, 'a> SpanDecoder for AutokenDecoder<'tcx, 'a> {
246287
fn decode_span(&mut self) -> Span {
247-
DUMMY_SP
288+
let src_file = StableSourceFileId::decode(self);
289+
let rel_lo = BytePos::decode(self);
290+
let rel_hi = BytePos::decode(self);
291+
let ctxt = SyntaxContext::decode(self);
292+
293+
let Some(src_file) = self
294+
.tcx
295+
.sess
296+
.source_map()
297+
.source_file_by_stable_id(src_file)
298+
else {
299+
return DUMMY_SP;
300+
};
301+
302+
let file_offset = src_file.start_pos;
303+
304+
SpanData {
305+
lo: file_offset + rel_lo,
306+
hi: file_offset + rel_hi,
307+
ctxt,
308+
parent: None,
309+
}
310+
.span()
248311
}
249312

250313
fn decode_symbol(&mut self) -> Symbol {
@@ -258,13 +321,8 @@ impl<'tcx, 'a> SpanDecoder for AutokenDecoder<'tcx, 'a> {
258321
}
259322

260323
fn decode_syntax_context(&mut self) -> SyntaxContext {
261-
let mut cx = SyntaxContext::root();
262-
263-
for (expn, trans) in Vec::<(ExpnId, Transparency)>::decode(self) {
264-
cx = cx.apply_mark(expn, trans);
265-
}
266-
267-
cx
324+
// Unimplemented: not used by our analyzer
325+
SyntaxContext::root()
268326
}
269327

270328
fn decode_crate_num(&mut self) -> CrateNum {
@@ -286,7 +344,7 @@ impl<'tcx, 'a> SpanDecoder for AutokenDecoder<'tcx, 'a> {
286344
}
287345

288346
fn decode_attr_id(&mut self) -> AttrId {
289-
todo!()
347+
unimplemented!("not used by analyzer");
290348
}
291349
}
292350

0 commit comments

Comments
 (0)