Skip to content

Commit 8d598b0

Browse files
committed
Remove almost all uses of specialization from the metadata encoding code
1 parent 6ed37bd commit 8d598b0

File tree

7 files changed

+301
-284
lines changed

7 files changed

+301
-284
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+49-57
Original file line numberDiff line numberDiff line change
@@ -409,21 +409,6 @@ impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> {
409409
}
410410
}
411411

412-
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for CrateNum {
413-
#[inline]
414-
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> CrateNum {
415-
let cnum = CrateNum::from_u32(d.read_u32());
416-
d.map_encoded_cnum_to_current(cnum)
417-
}
418-
}
419-
420-
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefIndex {
421-
#[inline]
422-
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> DefIndex {
423-
DefIndex::from_u32(d.read_u32())
424-
}
425-
}
426-
427412
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnIndex {
428413
#[inline]
429414
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> ExpnIndex {
@@ -439,19 +424,32 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ast::AttrId {
439424
}
440425
}
441426

442-
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SyntaxContext {
443-
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> SyntaxContext {
444-
let cdata = decoder.cdata();
427+
impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
428+
fn decode_crate_num(&mut self) -> CrateNum {
429+
let cnum = CrateNum::from_u32(self.read_u32());
430+
self.map_encoded_cnum_to_current(cnum)
431+
}
445432

446-
let Some(sess) = decoder.sess else {
433+
fn decode_def_index(&mut self) -> DefIndex {
434+
DefIndex::from_u32(self.read_u32())
435+
}
436+
437+
fn decode_def_id(&mut self) -> DefId {
438+
DefId { krate: Decodable::decode(self), index: Decodable::decode(self) }
439+
}
440+
441+
fn decode_syntax_context(&mut self) -> SyntaxContext {
442+
let cdata = self.cdata();
443+
444+
let Some(sess) = self.sess else {
447445
bug!(
448446
"Cannot decode SyntaxContext without Session.\
449447
You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`."
450448
);
451449
};
452450

453451
let cname = cdata.root.name();
454-
rustc_span::hygiene::decode_syntax_context(decoder, &cdata.hygiene_context, |_, id| {
452+
rustc_span::hygiene::decode_syntax_context(self, &cdata.hygiene_context, |_, id| {
455453
debug!("SpecializedDecoder<SyntaxContext>: decoding {}", id);
456454
cdata
457455
.root
@@ -461,21 +459,19 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SyntaxContext {
461459
.decode((cdata, sess))
462460
})
463461
}
464-
}
465462

466-
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnId {
467-
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> ExpnId {
468-
let local_cdata = decoder.cdata();
463+
fn decode_expn_id(&mut self) -> ExpnId {
464+
let local_cdata = self.cdata();
469465

470-
let Some(sess) = decoder.sess else {
466+
let Some(sess) = self.sess else {
471467
bug!(
472468
"Cannot decode ExpnId without Session. \
473469
You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`."
474470
);
475471
};
476472

477-
let cnum = CrateNum::decode(decoder);
478-
let index = u32::decode(decoder);
473+
let cnum = CrateNum::decode(self);
474+
let index = u32::decode(self);
479475

480476
let expn_id = rustc_span::hygiene::decode_expn_id(cnum, index, |expn_id| {
481477
let ExpnId { krate: cnum, local_id: index } = expn_id;
@@ -503,9 +499,7 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnId {
503499
});
504500
expn_id
505501
}
506-
}
507502

508-
impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
509503
fn decode_span(&mut self) -> Span {
510504
let start = self.position();
511505
let tag = SpanTag(self.peek_byte());
@@ -524,6 +518,32 @@ impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
524518
};
525519
Span::new(data.lo, data.hi, data.ctxt, data.parent)
526520
}
521+
522+
fn decode_symbol(&mut self) -> Symbol {
523+
let tag = self.read_u8();
524+
525+
match tag {
526+
SYMBOL_STR => {
527+
let s = self.read_str();
528+
Symbol::intern(s)
529+
}
530+
SYMBOL_OFFSET => {
531+
// read str offset
532+
let pos = self.read_usize();
533+
534+
// move to str offset and read
535+
self.opaque.with_position(pos, |d| {
536+
let s = d.read_str();
537+
Symbol::intern(s)
538+
})
539+
}
540+
SYMBOL_PREINTERNED => {
541+
let symbol_index = self.read_u32();
542+
Symbol::new_from_decoded(symbol_index)
543+
}
544+
_ => unreachable!(),
545+
}
546+
}
527547
}
528548

529549
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SpanData {
@@ -631,34 +651,6 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SpanData {
631651
}
632652
}
633653

634-
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Symbol {
635-
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self {
636-
let tag = d.read_u8();
637-
638-
match tag {
639-
SYMBOL_STR => {
640-
let s = d.read_str();
641-
Symbol::intern(s)
642-
}
643-
SYMBOL_OFFSET => {
644-
// read str offset
645-
let pos = d.read_usize();
646-
647-
// move to str offset and read
648-
d.opaque.with_position(pos, |d| {
649-
let s = d.read_str();
650-
Symbol::intern(s)
651-
})
652-
}
653-
SYMBOL_PREINTERNED => {
654-
let symbol_index = d.read_u32();
655-
Symbol::new_from_decoded(symbol_index)
656-
}
657-
_ => unreachable!(),
658-
}
659-
}
660-
}
661-
662654
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [(ty::Clause<'tcx>, Span)] {
663655
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self {
664656
ty::codec::RefDecodable::decode(d)

compiler/rustc_metadata/src/rmeta/encoder.rs

+44-49
Original file line numberDiff line numberDiff line change
@@ -125,48 +125,45 @@ impl<'a, 'tcx, I, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<I, T> {
125125
}
126126
}
127127

128-
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for CrateNum {
128+
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnIndex {
129129
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
130-
if *self != LOCAL_CRATE && s.is_proc_macro {
131-
panic!("Attempted to encode non-local CrateNum {self:?} for proc-macro crate");
132-
}
133130
s.emit_u32(self.as_u32());
134131
}
135132
}
136133

137-
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for DefIndex {
138-
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
139-
s.emit_u32(self.as_u32());
134+
impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
135+
fn encode_crate_num(&mut self, crate_num: CrateNum) {
136+
if crate_num != LOCAL_CRATE && self.is_proc_macro {
137+
panic!("Attempted to encode non-local CrateNum {crate_num:?} for proc-macro crate");
138+
}
139+
self.emit_u32(crate_num.as_u32());
140140
}
141-
}
142141

143-
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnIndex {
144-
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
145-
s.emit_u32(self.as_u32());
142+
fn encode_def_index(&mut self, def_index: DefIndex) {
143+
self.emit_u32(def_index.as_u32());
146144
}
147-
}
148145

149-
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SyntaxContext {
150-
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
151-
rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_ctxt, s);
146+
fn encode_def_id(&mut self, def_id: DefId) {
147+
def_id.krate.encode(self);
148+
def_id.index.encode(self);
152149
}
153-
}
154150

155-
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnId {
156-
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
157-
if self.krate == LOCAL_CRATE {
151+
fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
152+
rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_ctxt, self);
153+
}
154+
155+
fn encode_expn_id(&mut self, expn_id: ExpnId) {
156+
if expn_id.krate == LOCAL_CRATE {
158157
// We will only write details for local expansions. Non-local expansions will fetch
159158
// data from the corresponding crate's metadata.
160159
// FIXME(#43047) FIXME(#74731) We may eventually want to avoid relying on external
161160
// metadata from proc-macro crates.
162-
s.hygiene_ctxt.schedule_expn_data_for_encoding(*self);
161+
self.hygiene_ctxt.schedule_expn_data_for_encoding(expn_id);
163162
}
164-
self.krate.encode(s);
165-
self.local_id.encode(s);
163+
expn_id.krate.encode(self);
164+
expn_id.local_id.encode(self);
166165
}
167-
}
168166

169-
impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
170167
fn encode_span(&mut self, span: Span) {
171168
match self.span_shorthands.entry(span) {
172169
Entry::Occupied(o) => {
@@ -192,6 +189,29 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
192189
}
193190
}
194191
}
192+
193+
fn encode_symbol(&mut self, symbol: Symbol) {
194+
// if symbol preinterned, emit tag and symbol index
195+
if symbol.is_preinterned() {
196+
self.opaque.emit_u8(SYMBOL_PREINTERNED);
197+
self.opaque.emit_u32(symbol.as_u32());
198+
} else {
199+
// otherwise write it as string or as offset to it
200+
match self.symbol_table.entry(symbol) {
201+
Entry::Vacant(o) => {
202+
self.opaque.emit_u8(SYMBOL_STR);
203+
let pos = self.opaque.position();
204+
o.insert(pos);
205+
self.emit_str(symbol.as_str());
206+
}
207+
Entry::Occupied(o) => {
208+
let x = *o.get();
209+
self.emit_u8(SYMBOL_OFFSET);
210+
self.emit_usize(x);
211+
}
212+
}
213+
}
214+
}
195215
}
196216

197217
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
@@ -337,31 +357,6 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
337357
}
338358
}
339359

340-
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Symbol {
341-
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
342-
// if symbol preinterned, emit tag and symbol index
343-
if self.is_preinterned() {
344-
s.opaque.emit_u8(SYMBOL_PREINTERNED);
345-
s.opaque.emit_u32(self.as_u32());
346-
} else {
347-
// otherwise write it as string or as offset to it
348-
match s.symbol_table.entry(*self) {
349-
Entry::Vacant(o) => {
350-
s.opaque.emit_u8(SYMBOL_STR);
351-
let pos = s.opaque.position();
352-
o.insert(pos);
353-
s.emit_str(self.as_str());
354-
}
355-
Entry::Occupied(o) => {
356-
let x = *o.get();
357-
s.emit_u8(SYMBOL_OFFSET);
358-
s.emit_usize(x);
359-
}
360-
}
361-
}
362-
}
363-
}
364-
365360
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for [u8] {
366361
fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) {
367362
Encoder::emit_usize(e, self.len());

0 commit comments

Comments
 (0)