Skip to content

Commit 6e78d6c

Browse files
committed
Make the CrateNum part of the ExpnId.
1 parent c2d43e1 commit 6e78d6c

File tree

9 files changed

+307
-112
lines changed

9 files changed

+307
-112
lines changed

compiler/rustc_ast/src/node_id.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_span::ExpnId;
1+
use rustc_span::{ExpnId, LocalExpnId};
22
use std::fmt;
33

44
rustc_index::newtype_index! {
@@ -25,11 +25,11 @@ pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
2525

2626
impl NodeId {
2727
pub fn placeholder_from_expn_id(expn_id: ExpnId) -> Self {
28-
NodeId::from_u32(expn_id.as_u32())
28+
NodeId::from_u32(expn_id.expect_local().as_u32())
2929
}
3030

3131
pub fn placeholder_to_expn_id(self) -> ExpnId {
32-
ExpnId::from_u32(self.as_u32())
32+
LocalExpnId::from_u32(self.as_u32()).to_expn_id()
3333
}
3434
}
3535

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ use rustc_middle::ty::codec::TyDecoder;
3030
use rustc_middle::ty::{self, Ty, TyCtxt, Visibility};
3131
use rustc_serialize::{opaque, Decodable, Decoder};
3232
use rustc_session::Session;
33+
use rustc_span::hygiene::{ExpnIndex, MacroKind};
3334
use rustc_span::source_map::{respan, Spanned};
3435
use rustc_span::symbol::{sym, Ident, Symbol};
35-
use rustc_span::{self, hygiene::MacroKind, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP};
36+
use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP};
3637

3738
use proc_macro::bridge::client::ProcMacro;
3839
use std::io;
@@ -348,6 +349,12 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefIndex {
348349
}
349350
}
350351

352+
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnIndex {
353+
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Result<ExpnIndex, String> {
354+
Ok(ExpnIndex::from_u32(d.read_u32()?))
355+
}
356+
}
357+
351358
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SyntaxContext {
352359
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Result<SyntaxContext, String> {
353360
let cdata = decoder.cdata();

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use rustc_middle::ty::query::Providers;
1818
use rustc_middle::ty::{self, TyCtxt, Visibility};
1919
use rustc_session::utils::NativeLibKind;
2020
use rustc_session::{Session, StableCrateId};
21+
use rustc_span::hygiene::{ExpnData, ExpnHash, ExpnId};
2122
use rustc_span::source_map::{Span, Spanned};
2223
use rustc_span::symbol::Symbol;
2324

2425
use rustc_data_structures::sync::Lrc;
25-
use rustc_span::ExpnId;
2626
use smallvec::SmallVec;
2727
use std::any::Any;
2828

@@ -494,6 +494,23 @@ impl CrateStore for CStore {
494494
fn as_any(&self) -> &dyn Any {
495495
self
496496
}
497+
fn decode_expn_data(&self, sess: &Session, expn_id: ExpnId) -> (ExpnData, ExpnHash) {
498+
let crate_data = self.get_crate_data(expn_id.krate);
499+
(
500+
crate_data
501+
.root
502+
.expn_data
503+
.get(&crate_data, expn_id.local_id)
504+
.unwrap()
505+
.decode((&crate_data, sess)),
506+
crate_data
507+
.root
508+
.expn_hashes
509+
.get(&crate_data, expn_id.local_id)
510+
.unwrap()
511+
.decode((&crate_data, sess)),
512+
)
513+
}
497514

498515
fn crate_name(&self, cnum: CrateNum) -> Symbol {
499516
self.get_crate_data(cnum).root.name

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_session::config::CrateType;
3131
use rustc_span::symbol::{sym, Ident, Symbol};
3232
use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SyntaxContext};
3333
use rustc_span::{
34-
hygiene::{HygieneEncodeContext, MacroKind},
34+
hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind},
3535
RealFileName,
3636
};
3737
use rustc_target::abi::VariantIdx;
@@ -168,6 +168,12 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for DefIndex {
168168
}
169169
}
170170

171+
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnIndex {
172+
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
173+
s.emit_u32(self.as_u32())
174+
}
175+
}
176+
171177
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SyntaxContext {
172178
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
173179
rustc_span::hygiene::raw_encode_syntax_context(*self, &s.hygiene_ctxt, s)
@@ -1588,8 +1594,10 @@ impl EncodeContext<'a, 'tcx> {
15881594
Ok(())
15891595
},
15901596
|(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
1591-
expn_data_table.set(index, this.lazy(expn_data));
1592-
expn_hash_table.set(index, this.lazy(hash));
1597+
if let Some(index) = index.as_local() {
1598+
expn_data_table.set(index.as_raw(), this.lazy(expn_data));
1599+
expn_hash_table.set(index.as_raw(), this.lazy(hash));
1600+
}
15931601
Ok(())
15941602
},
15951603
);

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::ty::{self, ReprOptions, Ty};
1919
use rustc_serialize::opaque::Encoder;
2020
use rustc_session::config::SymbolManglingVersion;
2121
use rustc_span::edition::Edition;
22-
use rustc_span::hygiene::MacroKind;
22+
use rustc_span::hygiene::{ExpnIndex, MacroKind};
2323
use rustc_span::symbol::{Ident, Symbol};
2424
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Span};
2525
use rustc_target::spec::{PanicStrategy, TargetTriple};
@@ -170,8 +170,8 @@ macro_rules! Lazy {
170170
}
171171

172172
type SyntaxContextTable = Lazy<Table<u32, Lazy<SyntaxContextData>>>;
173-
type ExpnDataTable = Lazy<Table<u32, Lazy<ExpnData>>>;
174-
type ExpnHashTable = Lazy<Table<u32, Lazy<ExpnHash>>>;
173+
type ExpnDataTable = Lazy<Table<ExpnIndex, Lazy<ExpnData>>>;
174+
type ExpnHashTable = Lazy<Table<ExpnIndex, Lazy<ExpnHash>>>;
175175

176176
#[derive(MetadataEncodable, MetadataDecodable)]
177177
crate struct ProcMacroData {

compiler/rustc_middle/src/middle/cstore.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ use crate::ty::TyCtxt;
66

77
use rustc_ast as ast;
88
use rustc_data_structures::sync::{self, MetadataRef};
9-
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
9+
use rustc_hir::def_id::{CrateNum, DefId, StableCrateId, LOCAL_CRATE};
1010
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1111
use rustc_macros::HashStable;
1212
use rustc_session::search_paths::PathKind;
1313
use rustc_session::utils::NativeLibKind;
14-
use rustc_session::StableCrateId;
14+
use rustc_session::Session;
15+
use rustc_span::hygiene::{ExpnData, ExpnHash, ExpnId};
1516
use rustc_span::symbol::Symbol;
1617
use rustc_span::Span;
1718
use rustc_target::spec::Target;
@@ -187,6 +188,7 @@ pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
187188
/// during resolve)
188189
pub trait CrateStore: std::fmt::Debug {
189190
fn as_any(&self) -> &dyn Any;
191+
fn decode_expn_data(&self, sess: &Session, expn_id: ExpnId) -> (ExpnData, ExpnHash);
190192

191193
// Foreign definitions.
192194
// This information is safe to access, since it's hashed as part of the DefPathHash, which incr.

compiler/rustc_middle/src/ty/query/on_disk_cache.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,12 @@ impl<'sess> OnDiskCache<'sess> {
364364
Ok(())
365365
},
366366
|encoder, index, expn_data, hash| -> FileEncodeResult {
367-
let pos = AbsoluteBytePos::new(encoder.position());
368-
encoder.encode_tagged(TAG_EXPN_DATA, &(expn_data, hash))?;
369-
expn_ids.insert(index, pos);
367+
if index.krate == LOCAL_CRATE {
368+
let pos = AbsoluteBytePos::new(encoder.position());
369+
encoder.encode_tagged(TAG_EXPN_DATA, &(expn_data, hash))?;
370+
expn_ids.insert(index.local_id.as_u32(), pos);
371+
}
372+
// TODO Handle foreign expansions.
370373
Ok(())
371374
},
372375
)?;
@@ -807,6 +810,9 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for ExpnId {
807810
Ok(data)
808811
})
809812
},
813+
|this, expn_id| {
814+
Ok(this.tcx.untracked_resolutions.cstore.decode_expn_data(this.tcx.sess, expn_id))
815+
},
810816
)
811817
}
812818
}

0 commit comments

Comments
 (0)