Skip to content

Commit 09c1c94

Browse files
committed
rustc_metadata: Stop leaking Lazy from the rmeta module
1 parent c157023 commit 09c1c94

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

src/librustc_metadata/creader.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,18 @@ impl<'a> CrateLoader<'a> {
213213

214214
let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, span, dep_kind);
215215

216-
let raw_proc_macros = crate_root.proc_macro_data.map(|_| {
216+
let raw_proc_macros = if crate_root.is_proc_macro_crate() {
217217
let temp_root;
218218
let (dlsym_source, dlsym_root) = match &host_lib {
219219
Some(host_lib) =>
220220
(&host_lib.source, { temp_root = host_lib.metadata.get_root(); &temp_root }),
221221
None => (&source, &crate_root),
222222
};
223223
let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
224-
self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator, span)
225-
});
224+
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator, span))
225+
} else {
226+
None
227+
};
226228

227229
self.cstore.set_crate_data(cnum, CrateMetadata::new(
228230
self.sess,
@@ -348,7 +350,7 @@ impl<'a> CrateLoader<'a> {
348350
match result {
349351
(LoadResult::Previous(cnum), None) => {
350352
let data = self.cstore.get_crate_data(cnum);
351-
if data.root.proc_macro_data.is_some() {
353+
if data.root.is_proc_macro_crate() {
352354
dep_kind = DepKind::UnexportedMacrosOnly;
353355
}
354356
data.dep_kind.with_lock(|data_dep_kind| {
@@ -441,14 +443,14 @@ impl<'a> CrateLoader<'a> {
441443
dep_kind: DepKind)
442444
-> CrateNumMap {
443445
debug!("resolving deps of external crate");
444-
if crate_root.proc_macro_data.is_some() {
446+
if crate_root.is_proc_macro_crate() {
445447
return CrateNumMap::new();
446448
}
447449

448450
// The map from crate numbers in the crate we're resolving to local crate numbers.
449451
// We map 0 and all other holes in the map to our parent crate. The "additional"
450452
// self-dependencies should be harmless.
451-
std::iter::once(krate).chain(crate_root.crate_deps.decode(metadata).map(|dep| {
453+
std::iter::once(krate).chain(crate_root.decode_crate_deps(metadata).map(|dep| {
452454
info!("resolving dep crate {} hash: `{}` extra filename: `{}`", dep.name, dep.hash,
453455
dep.extra_filename);
454456
if dep.kind == DepKind::UnexportedMacrosOnly {

src/librustc_metadata/locator.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -768,10 +768,11 @@ impl<'a> CrateLocator<'a> {
768768
}
769769

770770
let root = metadata.get_root();
771-
if let Some(is_proc_macro) = self.is_proc_macro {
772-
if root.proc_macro_data.is_some() != is_proc_macro {
771+
if let Some(expected_is_proc_macro) = self.is_proc_macro {
772+
let is_proc_macro = root.is_proc_macro_crate();
773+
if is_proc_macro != expected_is_proc_macro {
773774
info!("Rejecting via proc macro: expected {} got {}",
774-
is_proc_macro, root.proc_macro_data.is_some());
775+
expected_is_proc_macro, is_proc_macro);
775776
return None;
776777
}
777778
}

src/librustc_metadata/rmeta/decoder.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct ImportedSourceFile {
126126
translated_source_file: Lrc<syntax_pos::SourceFile>,
127127
}
128128

129-
crate struct DecodeContext<'a, 'tcx> {
129+
pub(super) struct DecodeContext<'a, 'tcx> {
130130
opaque: opaque::Decoder<'a>,
131131
cdata: Option<&'a CrateMetadata>,
132132
sess: Option<&'tcx Session>,
@@ -142,7 +142,7 @@ crate struct DecodeContext<'a, 'tcx> {
142142
}
143143

144144
/// Abstract over the various ways one can create metadata decoders.
145-
crate trait Metadata<'a, 'tcx>: Copy {
145+
pub(super) trait Metadata<'a, 'tcx>: Copy {
146146
fn raw_bytes(self) -> &'a [u8];
147147
fn cdata(self) -> Option<&'a CrateMetadata> { None }
148148
fn sess(self) -> Option<&'tcx Session> { None }
@@ -218,15 +218,15 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'tcx>) {
218218
}
219219

220220
impl<'a, 'tcx, T: Encodable + Decodable> Lazy<T> {
221-
crate fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> T {
221+
fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> T {
222222
let mut dcx = metadata.decoder(self.position.get());
223223
dcx.lazy_state = LazyState::NodeStart(self.position);
224224
T::decode(&mut dcx).unwrap()
225225
}
226226
}
227227

228228
impl<'a: 'x, 'tcx: 'x, 'x, T: Encodable + Decodable> Lazy<[T]> {
229-
crate fn decode<M: Metadata<'a, 'tcx>>(
229+
fn decode<M: Metadata<'a, 'tcx>>(
230230
self,
231231
metadata: M,
232232
) -> impl ExactSizeIterator<Item = T> + Captures<'a> + Captures<'tcx> + 'x {
@@ -553,6 +553,19 @@ impl<'tcx> EntryKind<'tcx> {
553553
}
554554
}
555555

556+
impl CrateRoot<'_> {
557+
crate fn is_proc_macro_crate(&self) -> bool {
558+
self.proc_macro_data.is_some()
559+
}
560+
561+
crate fn decode_crate_deps(
562+
&self,
563+
metadata: &'a MetadataBlob,
564+
) -> impl ExactSizeIterator<Item = CrateDep> + Captures<'a> {
565+
self.crate_deps.decode(metadata)
566+
}
567+
}
568+
556569
impl<'a, 'tcx> CrateMetadata {
557570
crate fn new(
558571
sess: &Session,
@@ -595,12 +608,11 @@ impl<'a, 'tcx> CrateMetadata {
595608
}
596609

597610
fn is_proc_macro_crate(&self) -> bool {
598-
self.root.proc_macro_decls_static.is_some()
611+
self.root.is_proc_macro_crate()
599612
}
600613

601614
fn is_proc_macro(&self, id: DefIndex) -> bool {
602-
self.is_proc_macro_crate() &&
603-
self.root.proc_macro_data.unwrap().decode(self).find(|x| *x == id).is_some()
615+
self.root.proc_macro_data.and_then(|data| data.decode(self).find(|x| *x == id)).is_some()
604616
}
605617

606618
fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind<'tcx>> {

src/librustc_metadata/rmeta/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ crate const METADATA_HEADER: &[u8; 8] =
5151

5252
/// Additional metadata for a `Lazy<T>` where `T` may not be `Sized`,
5353
/// e.g. for `Lazy<[T]>`, this is the length (count of `T` values).
54-
crate trait LazyMeta {
54+
trait LazyMeta {
5555
type Meta: Copy + 'static;
5656

5757
/// Returns the minimum encoded size.
@@ -105,7 +105,7 @@ impl<T: Encodable> LazyMeta for [T] {
105105
#[must_use]
106106
// FIXME(#59875) the `Meta` parameter only exists to dodge
107107
// invariance wrt `T` (coming from the `meta: T::Meta` field).
108-
crate struct Lazy<T, Meta = <T as LazyMeta>::Meta>
108+
struct Lazy<T, Meta = <T as LazyMeta>::Meta>
109109
where T: ?Sized + LazyMeta<Meta = Meta>,
110110
Meta: 'static + Copy,
111111
{
@@ -188,7 +188,7 @@ crate struct CrateRoot<'tcx> {
188188
proc_macro_decls_static: Option<DefIndex>,
189189
proc_macro_stability: Option<attr::Stability>,
190190

191-
pub crate_deps: Lazy<[CrateDep]>,
191+
crate_deps: Lazy<[CrateDep]>,
192192
dylib_dependency_formats: Lazy<[Option<LinkagePreference>]>,
193193
lib_features: Lazy<[(Symbol, Option<Symbol>)]>,
194194
lang_items: Lazy<[(DefIndex, usize)]>,
@@ -204,9 +204,8 @@ crate struct CrateRoot<'tcx> {
204204

205205
per_def: LazyPerDefTables<'tcx>,
206206

207-
/// The DefIndex's of any proc macros delcared by
208-
/// this crate
209-
pub proc_macro_data: Option<Lazy<[DefIndex]>>,
207+
/// The DefIndex's of any proc macros delcared by this crate.
208+
proc_macro_data: Option<Lazy<[DefIndex]>>,
210209

211210
compiler_builtins: bool,
212211
pub needs_allocator: bool,

0 commit comments

Comments
 (0)