Skip to content

Commit dad587b

Browse files
committed
Implemented span expansion info encoding in metadata.
This change adds an encoded version of hygiene related datastructures to the metadata of a crate. It is then decoded on-demand when spans and AST items from a foreign crate are accessed. Some changes might still be necessary to implement the correct behaviour, however.
1 parent bb4d149 commit dad587b

File tree

17 files changed

+226
-31
lines changed

17 files changed

+226
-31
lines changed

src/doc/book

Submodule book updated 53 files

src/doc/nomicon

src/doc/reference

src/liblibc

Submodule liblibc updated 90 files

src/librustc_metadata/creader.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ impl<'a> CrateLoader<'a> {
293293
// Initialize this with an empty set. The field is populated below
294294
// after we were able to deserialize its contents.
295295
dllimport_foreign_items: FxHashSet(),
296+
hygiene_data_import_info: RefCell::new(None),
296297
};
297298

298299
let dllimports: FxHashSet<_> = cmeta

src/librustc_metadata/cstore.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use owning_ref::ErasedBoxRef;
2727
use syntax::{ast, attr};
2828
use syntax::ext::base::SyntaxExtension;
2929
use syntax::symbol::Symbol;
30-
use syntax_pos;
30+
use syntax_pos::{self, hygiene};
3131

3232
pub use rustc::middle::cstore::{NativeLibrary, NativeLibraryKind, LinkagePreference};
3333
pub use rustc::middle::cstore::NativeLibraryKind::*;
@@ -87,6 +87,8 @@ pub struct CrateMetadata {
8787
pub proc_macros: Option<Vec<(ast::Name, Rc<SyntaxExtension>)>>,
8888
// Foreign items imported from a dylib (Windows only)
8989
pub dllimport_foreign_items: FxHashSet<DefIndex>,
90+
91+
pub hygiene_data_import_info: RefCell<Option<hygiene::ImportedHygieneData>>,
9092
}
9193

9294
pub struct CStore {

src/librustc_metadata/decoder.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ use syntax::ast::{self, Ident};
4545
use syntax::codemap;
4646
use syntax::symbol::{InternedString, Symbol};
4747
use syntax::ext::base::MacroKind;
48-
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP, NO_EXPANSION};
48+
use syntax_pos::{self, Span, SyntaxContext, BytePos, Pos, DUMMY_SP};
49+
use syntax_pos::hygiene;
4950

5051
pub struct DecodeContext<'a, 'tcx: 'a> {
5152
opaque: opaque::Decoder<'a>,
@@ -237,15 +238,42 @@ impl<'a, 'tcx> SpecializedDecoder<CrateNum> for DecodeContext<'a, 'tcx> {
237238
}
238239
}
239240

241+
impl<'a, 'tcx> SpecializedDecoder<hygiene::Mark> for DecodeContext<'a, 'tcx> {
242+
fn specialized_decode(&mut self) -> Result<hygiene::Mark, Self::Error> {
243+
let mark = u32::decode(self)?;
244+
if self.cdata().hygiene_data_import_info.borrow().is_some() && mark != 0 {
245+
let imported_hygiene = self.cdata().imported_hygiene_data();
246+
247+
Ok(hygiene::Mark::from_u32(mark + imported_hygiene.mark_translation_offset))
248+
} else {
249+
Ok(hygiene::Mark::from_u32(mark))
250+
}
251+
}
252+
}
253+
254+
impl<'a, 'tcx> SpecializedDecoder<SyntaxContext> for DecodeContext<'a, 'tcx> {
255+
fn specialized_decode(&mut self) -> Result<SyntaxContext, Self::Error> {
256+
let ctxt = u32::decode(self)?;
257+
if self.cdata().hygiene_data_import_info.borrow().is_some() && ctxt != 0 {
258+
let imported_hygiene = self.cdata().imported_hygiene_data();
259+
260+
Ok(SyntaxContext::from_u32(ctxt + imported_hygiene.ctxt_translation_offset))
261+
} else {
262+
Ok(SyntaxContext::from_u32(ctxt))
263+
}
264+
}
265+
}
266+
240267
impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
241268
fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
242269
let lo = BytePos::decode(self)?;
243270
let hi = BytePos::decode(self)?;
271+
let ctxt = SyntaxContext::decode(self)?;
244272

245273
let sess = if let Some(sess) = self.sess {
246274
sess
247275
} else {
248-
return Ok(Span::new(lo, hi, NO_EXPANSION));
276+
return Ok(Span { lo, hi, ctxt });
249277
};
250278

251279
let (lo, hi) = if lo > hi {
@@ -292,7 +320,7 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
292320
let lo = (lo - filemap.original_start_pos) + filemap.translated_filemap.start_pos;
293321
let hi = (hi - filemap.original_start_pos) + filemap.translated_filemap.start_pos;
294322

295-
Ok(Span::new(lo, hi, NO_EXPANSION))
323+
Ok(Span::new(lo, hi, ctxt))
296324
}
297325
}
298326

@@ -1224,4 +1252,20 @@ impl<'a, 'tcx> CrateMetadata {
12241252
*self.codemap_import_info.borrow_mut() = imported_filemaps;
12251253
self.codemap_import_info.borrow()
12261254
}
1255+
1256+
pub fn imported_hygiene_data(&'a self) -> Ref<'a, hygiene::ImportedHygieneData> {
1257+
{
1258+
let hygiene_data = self.hygiene_data_import_info.borrow();
1259+
if hygiene_data.is_some() {
1260+
return Ref::map(hygiene_data, |d| d.as_ref().unwrap());
1261+
}
1262+
}
1263+
1264+
let external_hygiene_data = self.root.hygiene_data.decode(self);
1265+
1266+
// This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref.
1267+
*self.hygiene_data_import_info.borrow_mut() =
1268+
Some(hygiene::extend_hygiene_data(external_hygiene_data));
1269+
Ref::map(self.hygiene_data_import_info.borrow(), |d| d.as_ref().unwrap())
1270+
}
12271271
}

src/librustc_metadata/encoder.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use syntax::ast::{self, CRATE_NODE_ID};
4242
use syntax::codemap::Spanned;
4343
use syntax::attr;
4444
use syntax::symbol::Symbol;
45-
use syntax_pos;
45+
use syntax_pos::{self, hygiene};
4646

4747
use rustc::hir::{self, PatKind};
4848
use rustc::hir::itemlikevisit::ItemLikeVisitor;
@@ -323,6 +323,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
323323
self.lazy_seq_ref(adapted.iter().map(|rc| &**rc))
324324
}
325325

326+
fn encode_hygiene_data(&mut self) -> Lazy<hygiene::HygieneData> {
327+
hygiene::HygieneData::safe_with(|data| self.lazy(data))
328+
}
329+
326330
fn encode_crate_root(&mut self) -> Lazy<CrateRoot> {
327331
let mut i = self.position();
328332

@@ -367,6 +371,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
367371
let codemap = self.encode_codemap();
368372
let codemap_bytes = self.position() - i;
369373

374+
// Encode hygiene data
375+
i = self.position();
376+
let hygiene_data = self.encode_hygiene_data();
377+
let hygiene_data_bytes = self.position() - i;
378+
370379
// Encode DefPathTable
371380
i = self.position();
372381
let def_path_table = self.encode_def_path_table();
@@ -421,13 +430,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
421430
} else {
422431
None
423432
},
424-
425433
crate_deps,
426434
dylib_dependency_formats,
427435
lang_items,
428436
lang_items_missing,
429437
native_libraries,
430438
codemap,
439+
hygiene_data,
431440
def_path_table,
432441
impls,
433442
exported_symbols,
@@ -456,6 +465,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
456465
println!(" codemap bytes: {}", codemap_bytes);
457466
println!(" impl bytes: {}", impl_bytes);
458467
println!(" exp. symbols bytes: {}", exported_symbols_bytes);
468+
println!(" hygiene data bytes: {}", hygiene_data_bytes);
459469
println!(" def-path table bytes: {}", def_path_table_bytes);
460470
println!(" item bytes: {}", item_bytes);
461471
println!(" index bytes: {}", index_bytes);

src/librustc_metadata/schema.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_back::PanicStrategy;
2424
use rustc_serialize as serialize;
2525
use syntax::{ast, attr};
2626
use syntax::symbol::Symbol;
27-
use syntax_pos::{self, Span};
27+
use syntax_pos::{self, hygiene, Span};
2828

2929
use std::marker::PhantomData;
3030
use std::mem;
@@ -204,6 +204,7 @@ pub struct CrateRoot {
204204
pub lang_items_missing: LazySeq<lang_items::LangItem>,
205205
pub native_libraries: LazySeq<NativeLibrary>,
206206
pub codemap: LazySeq<syntax_pos::FileMap>,
207+
pub hygiene_data: Lazy<hygiene::HygieneData>,
207208
pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
208209
pub impls: LazySeq<TraitImpls>,
209210
pub exported_symbols: LazySeq<DefIndex>,

0 commit comments

Comments
 (0)