Skip to content

Commit 39e593a

Browse files
committed
Auto merge of #75137 - Aaron1011:fix/hygiene-skip-expndata, r=petrochenkov
Don't serialize ExpnData for foreign crates When we encode an ExpnId into the crate metadata, we write out the CrateNum of the crate that 'owns' the corresponding `ExpnData`, which is later used to decode the `ExpnData` from its owning crate. However, we current serialize the `ExpnData` for all `ExpnIds` that we serialize, even if the `ExpnData` was already serialized into a foreign crate. This commit skips encoding this kind of `ExpnData`, which should hopefully speed up metadata encoding and reduce the total metadata size.
2 parents 543f03d + ef49032 commit 39e593a

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/librustc_metadata/rmeta/encoder.rs

+4
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ impl<'a, 'tcx> SpecializedEncoder<Span> for EncodeContext<'a, 'tcx> {
279279
// cross-crate inconsistencies (getting one behavior in the same
280280
// crate, and a different behavior in another crate) due to the
281281
// limited surface that proc-macros can expose.
282+
//
283+
// IMPORTANT: If this is ever changed, be sure to update
284+
// `rustc_span::hygiene::raw_encode_expn_id` to handle
285+
// encoding `ExpnData` for proc-macro crates.
282286
if self.is_proc_macro {
283287
SyntaxContext::root().encode(self)?;
284288
} else {

src/librustc_span/hygiene.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -1172,13 +1172,30 @@ pub fn raw_encode_expn_id<E: Encoder>(
11721172
mode: ExpnDataEncodeMode,
11731173
e: &mut E,
11741174
) -> Result<(), E::Error> {
1175-
if !context.serialized_expns.lock().contains(&expn) {
1176-
context.latest_expns.lock().insert(expn);
1177-
}
1175+
// Record the fact that we need to serialize the corresponding
1176+
// `ExpnData`
1177+
let needs_data = || {
1178+
if !context.serialized_expns.lock().contains(&expn) {
1179+
context.latest_expns.lock().insert(expn);
1180+
}
1181+
};
1182+
11781183
match mode {
1179-
ExpnDataEncodeMode::IncrComp => expn.0.encode(e),
1184+
ExpnDataEncodeMode::IncrComp => {
1185+
// Always serialize the `ExpnData` in incr comp mode
1186+
needs_data();
1187+
expn.0.encode(e)
1188+
}
11801189
ExpnDataEncodeMode::Metadata => {
11811190
let data = expn.expn_data();
1191+
// We only need to serialize the ExpnData
1192+
// if it comes from this crate.
1193+
// We currently don't serialize any hygiene information data for
1194+
// proc-macro crates: see the `SpecializedEncoder<Span>` impl
1195+
// for crate metadata.
1196+
if data.krate == LOCAL_CRATE {
1197+
needs_data();
1198+
}
11821199
data.orig_id.expect("Missing orig_id").encode(e)?;
11831200
data.krate.encode(e)
11841201
}

0 commit comments

Comments
 (0)