Skip to content

Commit 694a8a4

Browse files
Auto merge of #149455 - jdonszelmann:metadata-decoding-s, r=<try>
Metadata decoding s
2 parents 791c041 + cf9344a commit 694a8a4

File tree

27 files changed

+640
-596
lines changed

27 files changed

+640
-596
lines changed

compiler/rustc_hir/src/definitions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_data_structures::stable_hasher::StableHasher;
1111
use rustc_data_structures::unord::UnordMap;
1212
use rustc_hashes::Hash64;
1313
use rustc_index::IndexVec;
14-
use rustc_macros::{Decodable, Encodable};
14+
use rustc_macros::{BlobDecodable, Decodable, Encodable};
1515
use rustc_span::{Symbol, kw, sym};
1616
use tracing::{debug, instrument};
1717

@@ -127,7 +127,7 @@ pub struct Definitions {
127127
/// A unique identifier that we can use to lookup a definition
128128
/// precisely. It combines the index of the definition's parent (if
129129
/// any) with a `DisambiguatedDefPathData`.
130-
#[derive(Copy, Clone, PartialEq, Debug, Encodable, Decodable)]
130+
#[derive(Copy, Clone, PartialEq, Debug, Encodable, BlobDecodable)]
131131
pub struct DefKey {
132132
/// The parent path.
133133
pub parent: Option<DefIndex>,
@@ -176,7 +176,7 @@ impl DefKey {
176176
/// between them. This introduces some artificial ordering dependency
177177
/// but means that if you have, e.g., two impls for the same type in
178178
/// the same module, they do get distinct `DefId`s.
179-
#[derive(Copy, Clone, PartialEq, Debug, Encodable, Decodable)]
179+
#[derive(Copy, Clone, PartialEq, Debug, Encodable, BlobDecodable)]
180180
pub struct DisambiguatedDefPathData {
181181
pub data: DefPathData,
182182
pub disambiguator: u32,
@@ -270,7 +270,7 @@ impl DefPath {
270270
}
271271

272272
/// New variants should only be added in synchronization with `enum DefKind`.
273-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
273+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)]
274274
pub enum DefPathData {
275275
// Root: these should only be used for the root nodes, because
276276
// they are treated specially by the `def_path` function.

compiler/rustc_hir/src/lang_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use rustc_ast::attr::AttributeExt;
1111
use rustc_data_structures::fx::FxIndexMap;
1212
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
13-
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
13+
use rustc_macros::{BlobDecodable, Encodable, HashStable_Generic};
1414
use rustc_span::{Span, Symbol, kw, sym};
1515

1616
use crate::def_id::DefId;
@@ -75,7 +75,7 @@ macro_rules! language_item_table {
7575
$( $(#[$attr:meta])* $variant:ident, $module:ident :: $name:ident, $method:ident, $target:expr, $generics:expr; )*
7676
) => {
7777
/// A representation of all the valid lang items in Rust.
78-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable)]
78+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encodable, BlobDecodable)]
7979
pub enum LangItem {
8080
$(
8181
#[doc = concat!("The `", stringify!($name), "` lang item.")]

compiler/rustc_hir/src/stability.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::num::NonZero;
22

3-
use rustc_macros::{Decodable, Encodable, HashStable_Generic, PrintAttribute};
3+
use rustc_macros::{BlobDecodable, Decodable, Encodable, HashStable_Generic, PrintAttribute};
44
use rustc_span::{ErrorGuaranteed, Symbol, sym};
55

66
use crate::RustcVersion;
@@ -21,7 +21,7 @@ pub const VERSION_PLACEHOLDER: &str = concat!("CURRENT_RUSTC_VERSIO", "N");
2121
///
2222
/// - `#[stable]`
2323
/// - `#[unstable]`
24-
#[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, Hash)]
24+
#[derive(Encodable, BlobDecodable, Copy, Clone, Debug, PartialEq, Eq, Hash)]
2525
#[derive(HashStable_Generic, PrintAttribute)]
2626
pub struct Stability {
2727
pub level: StabilityLevel,
@@ -103,7 +103,7 @@ impl PartialConstStability {
103103
}
104104

105105
/// The available stability levels.
106-
#[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
106+
#[derive(Encodable, BlobDecodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
107107
#[derive(HashStable_Generic, PrintAttribute)]
108108
pub enum StabilityLevel {
109109
/// `#[unstable]`
@@ -146,7 +146,7 @@ pub enum StabilityLevel {
146146
}
147147

148148
/// Rust release in which a feature is stabilized.
149-
#[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, PartialOrd, Ord, Hash)]
149+
#[derive(Encodable, BlobDecodable, PartialEq, Copy, Clone, Debug, Eq, PartialOrd, Ord, Hash)]
150150
#[derive(HashStable_Generic, PrintAttribute)]
151151
pub enum StableSince {
152152
/// also stores the original symbol for printing
@@ -172,7 +172,7 @@ impl StabilityLevel {
172172
}
173173
}
174174

175-
#[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
175+
#[derive(Encodable, BlobDecodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
176176
#[derive(HashStable_Generic, PrintAttribute)]
177177
pub enum UnstableReason {
178178
None,

compiler/rustc_hir/src/version.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use std::sync::OnceLock;
44

55
use rustc_error_messages::{DiagArgValue, IntoDiagArg};
66
use rustc_macros::{
7-
Decodable, Encodable, HashStable_Generic, PrintAttribute, current_rustc_version,
7+
BlobDecodable, Encodable, HashStable_Generic, PrintAttribute, current_rustc_version,
88
};
99

1010
use crate::attrs::PrintAttribute;
1111

12-
#[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12+
#[derive(Encodable, BlobDecodable, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1313
#[derive(HashStable_Generic, PrintAttribute)]
1414
pub struct RustcVersion {
1515
pub major: u16,

compiler/rustc_macros/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,18 @@ decl_derive!([Decodable] => serialize::decodable_derive);
7878
decl_derive!([Encodable] => serialize::encodable_derive);
7979
decl_derive!([TyDecodable] => serialize::type_decodable_derive);
8080
decl_derive!([TyEncodable] => serialize::type_encodable_derive);
81-
decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
81+
decl_derive!([MetadataDecodable] =>
82+
/// This constrains the decoder to be specifically the decoder that can decode LazyArrays in metadata.
83+
/// Therefore, we only use this on things containing LazyArray really.
84+
/// Anything else should either be `NoContext`, if possible `BlobDecodable`, or otherwise just `Decodable`.
85+
serialize::meta_decodable_derive
86+
);
87+
decl_derive!([BlobDecodable] =>
88+
/// For anything that is "simple" to decode, without needing anything but the original data,
89+
/// but for which the Decoder can customize some things
90+
/// (unlike Decodable_NoContext which individual decoders can't customize).
91+
serialize::blob_decodable_derive
92+
);
8293
decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
8394
decl_derive!(
8495
[TypeFoldable, attributes(type_foldable)] =>

compiler/rustc_macros/src/serialize.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ pub(super) fn type_decodable_derive(
1616
decodable_body(s, decoder_ty)
1717
}
1818

19+
pub(super) fn blob_decodable_derive(
20+
mut s: synstructure::Structure<'_>,
21+
) -> proc_macro2::TokenStream {
22+
let decoder_ty = quote! { __D };
23+
s.add_impl_generic(parse_quote! { #decoder_ty: ::rustc_span::BlobDecoder });
24+
s.add_bounds(synstructure::AddBounds::Generics);
25+
26+
decodable_body(s, decoder_ty)
27+
}
28+
1929
pub(super) fn meta_decodable_derive(
2030
mut s: synstructure::Structure<'_>,
2131
) -> proc_macro2::TokenStream {
22-
if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
23-
s.add_impl_generic(parse_quote! { 'tcx });
24-
}
25-
s.add_impl_generic(parse_quote! { '__a });
26-
let decoder_ty = quote! { DecodeContext<'__a, 'tcx> };
32+
let decoder_ty = quote! { __D };
33+
s.add_impl_generic(parse_quote! { #decoder_ty: LazyDecoder });
2734
s.add_bounds(synstructure::AddBounds::Generics);
2835

2936
decodable_body(s, decoder_ty)

compiler/rustc_metadata/src/creader.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,9 @@ impl CStore {
276276
.filter_map(|(cnum, data)| data.as_deref().map(|data| (cnum, data)))
277277
}
278278

279-
pub fn all_proc_macro_def_ids(&self) -> impl Iterator<Item = DefId> {
280-
self.iter_crate_data().flat_map(|(krate, data)| data.proc_macros_for_crate(krate, self))
279+
pub fn all_proc_macro_def_ids(&self, tcx: TyCtxt<'_>) -> impl Iterator<Item = DefId> {
280+
self.iter_crate_data()
281+
.flat_map(move |(krate, data)| data.proc_macros_for_crate(tcx, krate, self))
281282
}
282283

283284
fn push_dependencies_in_postorder(&self, deps: &mut IndexSet<CrateNum>, cnum: CrateNum) {
@@ -683,6 +684,7 @@ impl CStore {
683684
};
684685

685686
let crate_metadata = CrateMetadata::new(
687+
tcx,
686688
self,
687689
metadata,
688690
crate_root,
@@ -778,7 +780,7 @@ impl CStore {
778780
self.used_extern_options.insert(name);
779781
match self.maybe_resolve_crate(tcx, name, dep_kind, origin) {
780782
Ok(cnum) => {
781-
self.set_used_recursively(cnum);
783+
self.set_used_recursively(tcx, cnum);
782784
Some(cnum)
783785
}
784786
Err(err) => {

0 commit comments

Comments
 (0)