Skip to content

Commit c1df945

Browse files
committed
rustc_metadata: Give decoder access to whole crate store
1 parent c79f5f0 commit c1df945

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

src/librustc_metadata/creader.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ impl<'a> LoadError<'a> {
7676
}
7777
}
7878

79+
/// A reference to `CrateMetadata` that can also give access to whole crate store when necessary.
80+
#[derive(Clone, Copy)]
81+
crate struct CrateMetadataRef<'a> {
82+
pub cdata: &'a CrateMetadata,
83+
pub cstore: &'a CStore,
84+
}
85+
86+
impl std::ops::Deref for CrateMetadataRef<'_> {
87+
type Target = CrateMetadata;
88+
89+
fn deref(&self) -> &Self::Target {
90+
self.cdata
91+
}
92+
}
93+
7994
fn dump_crates(cstore: &CStore) {
8095
info!("resolved crates:");
8196
cstore.iter_crate_data(|cnum, data| {
@@ -100,10 +115,11 @@ impl CStore {
100115
CrateNum::new(self.metas.len() - 1)
101116
}
102117

103-
crate fn get_crate_data(&self, cnum: CrateNum) -> &CrateMetadata {
104-
self.metas[cnum]
118+
crate fn get_crate_data(&self, cnum: CrateNum) -> CrateMetadataRef<'_> {
119+
let cdata = self.metas[cnum]
105120
.as_ref()
106-
.unwrap_or_else(|| panic!("Failed to get crate data for {:?}", cnum))
121+
.unwrap_or_else(|| panic!("Failed to get crate data for {:?}", cnum));
122+
CrateMetadataRef { cdata, cstore: self }
107123
}
108124

109125
fn set_crate_data(&mut self, cnum: CrateNum, data: CrateMetadata) {
@@ -217,7 +233,7 @@ impl<'a> CrateLoader<'a> {
217233
// We're also sure to compare *paths*, not actual byte slices. The
218234
// `source` stores paths which are normalized which may be different
219235
// from the strings on the command line.
220-
let source = self.cstore.get_crate_data(cnum).source();
236+
let source = self.cstore.get_crate_data(cnum).cdata.source();
221237
if let Some(entry) = self.sess.opts.externs.get(&name.as_str()) {
222238
// Only use `--extern crate_name=path` here, not `--extern crate_name`.
223239
if let Some(mut files) = entry.files() {

src/librustc_metadata/rmeta/decoder.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Decoding metadata from a single crate's metadata
22

3+
use crate::creader::CrateMetadataRef;
34
use crate::rmeta::table::{FixedSizeEncoding, Table};
45
use crate::rmeta::*;
56

@@ -125,7 +126,7 @@ struct ImportedSourceFile {
125126

126127
pub(super) struct DecodeContext<'a, 'tcx> {
127128
opaque: opaque::Decoder<'a>,
128-
cdata: Option<&'a CrateMetadata>,
129+
cdata: Option<CrateMetadataRef<'a>>,
129130
sess: Option<&'tcx Session>,
130131
tcx: Option<TyCtxt<'tcx>>,
131132

@@ -141,7 +142,7 @@ pub(super) struct DecodeContext<'a, 'tcx> {
141142
/// Abstract over the various ways one can create metadata decoders.
142143
pub(super) trait Metadata<'a, 'tcx>: Copy {
143144
fn raw_bytes(self) -> &'a [u8];
144-
fn cdata(self) -> Option<&'a CrateMetadata> {
145+
fn cdata(self) -> Option<CrateMetadataRef<'a>> {
145146
None
146147
}
147148
fn sess(self) -> Option<&'tcx Session> {
@@ -162,7 +163,7 @@ pub(super) trait Metadata<'a, 'tcx>: Copy {
162163
lazy_state: LazyState::NoNode,
163164
alloc_decoding_session: self
164165
.cdata()
165-
.map(|cdata| cdata.alloc_decoding_state.new_decoding_session()),
166+
.map(|cdata| cdata.cdata.alloc_decoding_state.new_decoding_session()),
166167
}
167168
}
168169
}
@@ -185,33 +186,33 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a MetadataBlob, &'tcx Session) {
185186
}
186187
}
187188

188-
impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a CrateMetadata {
189+
impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a CrateMetadataRef<'a> {
189190
fn raw_bytes(self) -> &'a [u8] {
190191
self.blob.raw_bytes()
191192
}
192-
fn cdata(self) -> Option<&'a CrateMetadata> {
193-
Some(self)
193+
fn cdata(self) -> Option<CrateMetadataRef<'a>> {
194+
Some(*self)
194195
}
195196
}
196197

197-
impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, &'tcx Session) {
198+
impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadataRef<'a>, &'tcx Session) {
198199
fn raw_bytes(self) -> &'a [u8] {
199200
self.0.raw_bytes()
200201
}
201-
fn cdata(self) -> Option<&'a CrateMetadata> {
202-
Some(self.0)
202+
fn cdata(self) -> Option<CrateMetadataRef<'a>> {
203+
Some(*self.0)
203204
}
204205
fn sess(self) -> Option<&'tcx Session> {
205206
Some(&self.1)
206207
}
207208
}
208209

209-
impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'tcx>) {
210+
impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadataRef<'a>, TyCtxt<'tcx>) {
210211
fn raw_bytes(self) -> &'a [u8] {
211212
self.0.raw_bytes()
212213
}
213-
fn cdata(self) -> Option<&'a CrateMetadata> {
214-
Some(self.0)
214+
fn cdata(self) -> Option<CrateMetadataRef<'a>> {
215+
Some(*self.0)
215216
}
216217
fn tcx(self) -> Option<TyCtxt<'tcx>> {
217218
Some(self.1)
@@ -242,7 +243,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
242243
self.tcx.expect("missing TyCtxt in DecodeContext")
243244
}
244245

245-
fn cdata(&self) -> &'a CrateMetadata {
246+
fn cdata(&self) -> CrateMetadataRef<'a> {
246247
self.cdata.expect("missing CrateMetadata in DecodeContext")
247248
}
248249

@@ -558,7 +559,7 @@ impl CrateRoot<'_> {
558559
}
559560
}
560561

561-
impl<'a, 'tcx> CrateMetadata {
562+
impl CrateMetadata {
562563
crate fn new(
563564
sess: &Session,
564565
blob: MetadataBlob,
@@ -601,7 +602,9 @@ impl<'a, 'tcx> CrateMetadata {
601602
extern_crate: Lock::new(None),
602603
}
603604
}
605+
}
604606

607+
impl<'a, 'tcx> CrateMetadataRef<'a> {
605608
fn is_proc_macro(&self, id: DefIndex) -> bool {
606609
self.root.proc_macro_data.and_then(|data| data.decode(self).find(|x| *x == id)).is_some()
607610
}
@@ -1440,10 +1443,10 @@ impl<'a, 'tcx> CrateMetadata {
14401443
/// Proc macro crates don't currently export spans, so this function does not have
14411444
/// to work for them.
14421445
fn imported_source_files(
1443-
&'a self,
1446+
&self,
14441447
local_source_map: &source_map::SourceMap,
1445-
) -> &[ImportedSourceFile] {
1446-
self.source_map_import_info.init_locking(|| {
1448+
) -> &'a [ImportedSourceFile] {
1449+
self.cdata.source_map_import_info.init_locking(|| {
14471450
let external_source_map = self.root.source_map.decode(self);
14481451

14491452
external_source_map
@@ -1540,7 +1543,9 @@ impl<'a, 'tcx> CrateMetadata {
15401543

15411544
dep_node_index
15421545
}
1546+
}
15431547

1548+
impl CrateMetadata {
15441549
crate fn dependencies(&self) -> LockGuard<'_, Vec<CrateNum>> {
15451550
self.dependencies.borrow()
15461551
}

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ impl CrateStore for CStore {
517517
}
518518

519519
fn def_path_table(&self, cnum: CrateNum) -> &DefPathTable {
520-
&self.get_crate_data(cnum).def_path_table
520+
&self.get_crate_data(cnum).cdata.def_path_table
521521
}
522522

523523
fn crates_untracked(&self) -> Vec<CrateNum> {

0 commit comments

Comments
 (0)