Skip to content

Commit 723028f

Browse files
incr.comp.: Remove some code duplication around TyDecoder by factoring things into a macro.
1 parent 2f44ef2 commit 723028f

File tree

4 files changed

+142
-222
lines changed

4 files changed

+142
-222
lines changed

src/librustc/ty/codec.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub trait TyDecoder<'a, 'tcx: 'a>: Decoder {
138138
}
139139
}
140140

141+
#[inline]
141142
pub fn decode_cnum<'a, 'tcx, D>(decoder: &mut D) -> Result<CrateNum, D::Error>
142143
where D: TyDecoder<'a, 'tcx>,
143144
'tcx: 'a,
@@ -146,6 +147,7 @@ pub fn decode_cnum<'a, 'tcx, D>(decoder: &mut D) -> Result<CrateNum, D::Error>
146147
Ok(decoder.map_encoded_cnum_to_current(cnum))
147148
}
148149

150+
#[inline]
149151
pub fn decode_ty<'a, 'tcx, D>(decoder: &mut D) -> Result<Ty<'tcx>, D::Error>
150152
where D: TyDecoder<'a, 'tcx>,
151153
'tcx: 'a,
@@ -165,6 +167,7 @@ pub fn decode_ty<'a, 'tcx, D>(decoder: &mut D) -> Result<Ty<'tcx>, D::Error>
165167
}
166168
}
167169

170+
#[inline]
168171
pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D)
169172
-> Result<ty::GenericPredicates<'tcx>, D::Error>
170173
where D: TyDecoder<'a, 'tcx>,
@@ -188,6 +191,7 @@ pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D)
188191
})
189192
}
190193

194+
#[inline]
191195
pub fn decode_substs<'a, 'tcx, D>(decoder: &mut D) -> Result<&'tcx Substs<'tcx>, D::Error>
192196
where D: TyDecoder<'a, 'tcx>,
193197
'tcx: 'a,
@@ -197,13 +201,15 @@ pub fn decode_substs<'a, 'tcx, D>(decoder: &mut D) -> Result<&'tcx Substs<'tcx>,
197201
Ok(tcx.mk_substs((0..len).map(|_| Decodable::decode(decoder)))?)
198202
}
199203

204+
#[inline]
200205
pub fn decode_region<'a, 'tcx, D>(decoder: &mut D) -> Result<ty::Region<'tcx>, D::Error>
201206
where D: TyDecoder<'a, 'tcx>,
202207
'tcx: 'a,
203208
{
204209
Ok(decoder.tcx().mk_region(Decodable::decode(decoder)?))
205210
}
206211

212+
#[inline]
207213
pub fn decode_ty_slice<'a, 'tcx, D>(decoder: &mut D)
208214
-> Result<&'tcx ty::Slice<Ty<'tcx>>, D::Error>
209215
where D: TyDecoder<'a, 'tcx>,
@@ -213,6 +219,7 @@ pub fn decode_ty_slice<'a, 'tcx, D>(decoder: &mut D)
213219
Ok(decoder.tcx().mk_type_list((0..len).map(|_| Decodable::decode(decoder)))?)
214220
}
215221

222+
#[inline]
216223
pub fn decode_adt_def<'a, 'tcx, D>(decoder: &mut D)
217224
-> Result<&'tcx ty::AdtDef, D::Error>
218225
where D: TyDecoder<'a, 'tcx>,
@@ -222,6 +229,7 @@ pub fn decode_adt_def<'a, 'tcx, D>(decoder: &mut D)
222229
Ok(decoder.tcx().adt_def(def_id))
223230
}
224231

232+
#[inline]
225233
pub fn decode_existential_predicate_slice<'a, 'tcx, D>(decoder: &mut D)
226234
-> Result<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>, D::Error>
227235
where D: TyDecoder<'a, 'tcx>,
@@ -232,6 +240,7 @@ pub fn decode_existential_predicate_slice<'a, 'tcx, D>(decoder: &mut D)
232240
.mk_existential_predicates((0..len).map(|_| Decodable::decode(decoder)))?)
233241
}
234242

243+
#[inline]
235244
pub fn decode_byte_array<'a, 'tcx, D>(decoder: &mut D)
236245
-> Result<ByteArray<'tcx>, D::Error>
237246
where D: TyDecoder<'a, 'tcx>,
@@ -242,10 +251,138 @@ pub fn decode_byte_array<'a, 'tcx, D>(decoder: &mut D)
242251
})
243252
}
244253

254+
#[inline]
245255
pub fn decode_const<'a, 'tcx, D>(decoder: &mut D)
246256
-> Result<&'tcx ty::Const<'tcx>, D::Error>
247257
where D: TyDecoder<'a, 'tcx>,
248258
'tcx: 'a,
249259
{
250260
Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?))
251261
}
262+
263+
#[macro_export]
264+
macro_rules! __impl_decoder_methods {
265+
($($name:ident -> $ty:ty;)*) => {
266+
$(fn $name(&mut self) -> Result<$ty, Self::Error> {
267+
self.opaque.$name()
268+
})*
269+
}
270+
}
271+
272+
#[macro_export]
273+
macro_rules! implement_ty_decoder {
274+
($DecoderName:ident <$($typaram:tt),*>) => {
275+
mod __ty_decoder_impl {
276+
use super::$DecoderName;
277+
use $crate::ty;
278+
use $crate::ty::codec::*;
279+
use $crate::ty::subst::Substs;
280+
use $crate::hir::def_id::{CrateNum};
281+
use $crate::middle::const_val::ByteArray;
282+
use rustc_serialize::{Decoder, SpecializedDecoder};
283+
use std::borrow::Cow;
284+
285+
impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
286+
type Error = String;
287+
288+
__impl_decoder_methods! {
289+
read_nil -> ();
290+
291+
read_u128 -> u128;
292+
read_u64 -> u64;
293+
read_u32 -> u32;
294+
read_u16 -> u16;
295+
read_u8 -> u8;
296+
read_usize -> usize;
297+
298+
read_i128 -> i128;
299+
read_i64 -> i64;
300+
read_i32 -> i32;
301+
read_i16 -> i16;
302+
read_i8 -> i8;
303+
read_isize -> isize;
304+
305+
read_bool -> bool;
306+
read_f64 -> f64;
307+
read_f32 -> f32;
308+
read_char -> char;
309+
read_str -> Cow<str>;
310+
}
311+
312+
fn error(&mut self, err: &str) -> Self::Error {
313+
self.opaque.error(err)
314+
}
315+
}
316+
317+
// FIXME(#36588) These impls are horribly unsound as they allow
318+
// the caller to pick any lifetime for 'tcx, including 'static,
319+
// by using the unspecialized proxies to them.
320+
321+
impl<$($typaram),*> SpecializedDecoder<CrateNum> for $DecoderName<$($typaram),*> {
322+
fn specialized_decode(&mut self) -> Result<CrateNum, Self::Error> {
323+
decode_cnum(self)
324+
}
325+
}
326+
327+
impl<$($typaram),*> SpecializedDecoder<ty::Ty<'tcx>> for $DecoderName<$($typaram),*> {
328+
fn specialized_decode(&mut self) -> Result<ty::Ty<'tcx>, Self::Error> {
329+
decode_ty(self)
330+
}
331+
}
332+
333+
impl<$($typaram),*> SpecializedDecoder<ty::GenericPredicates<'tcx>>
334+
for $DecoderName<$($typaram),*> {
335+
fn specialized_decode(&mut self) -> Result<ty::GenericPredicates<'tcx>, Self::Error> {
336+
decode_predicates(self)
337+
}
338+
}
339+
340+
impl<$($typaram),*> SpecializedDecoder<&'tcx Substs<'tcx>> for $DecoderName<$($typaram),*> {
341+
fn specialized_decode(&mut self) -> Result<&'tcx Substs<'tcx>, Self::Error> {
342+
decode_substs(self)
343+
}
344+
}
345+
346+
impl<$($typaram),*> SpecializedDecoder<ty::Region<'tcx>> for $DecoderName<$($typaram),*> {
347+
fn specialized_decode(&mut self) -> Result<ty::Region<'tcx>, Self::Error> {
348+
decode_region(self)
349+
}
350+
}
351+
352+
impl<$($typaram),*> SpecializedDecoder<&'tcx ty::Slice<ty::Ty<'tcx>>>
353+
for $DecoderName<$($typaram),*> {
354+
fn specialized_decode(&mut self) -> Result<&'tcx ty::Slice<ty::Ty<'tcx>>, Self::Error> {
355+
decode_ty_slice(self)
356+
}
357+
}
358+
359+
impl<$($typaram),*> SpecializedDecoder<&'tcx ty::AdtDef> for $DecoderName<$($typaram),*> {
360+
fn specialized_decode(&mut self) -> Result<&'tcx ty::AdtDef, Self::Error> {
361+
decode_adt_def(self)
362+
}
363+
}
364+
365+
impl<$($typaram),*> SpecializedDecoder<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>>
366+
for $DecoderName<$($typaram),*> {
367+
fn specialized_decode(&mut self)
368+
-> Result<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>, Self::Error> {
369+
decode_existential_predicate_slice(self)
370+
}
371+
}
372+
373+
impl<$($typaram),*> SpecializedDecoder<ByteArray<'tcx>> for $DecoderName<$($typaram),*> {
374+
fn specialized_decode(&mut self) -> Result<ByteArray<'tcx>, Self::Error> {
375+
decode_byte_array(self)
376+
}
377+
}
378+
379+
impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::Const<'tcx>>
380+
for $DecoderName<$($typaram),*> {
381+
fn specialized_decode(&mut self) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
382+
decode_const(self)
383+
}
384+
}
385+
}
386+
}
387+
}
388+

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 2 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ use hir;
1414
use hir::def_id::{CrateNum, DefIndex, DefId, LocalDefId,
1515
RESERVED_FOR_INCR_COMP_CACHE, LOCAL_CRATE};
1616
use hir::map::definitions::DefPathHash;
17-
use middle::const_val::ByteArray;
1817
use middle::cstore::CrateStore;
1918
use rustc_data_structures::fx::FxHashMap;
2019
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
2120
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque,
2221
SpecializedDecoder, SpecializedEncoder,
2322
UseSpecializedDecodable, UseSpecializedEncodable};
2423
use session::{CrateDisambiguator, Session};
25-
use std::borrow::Cow;
2624
use std::cell::RefCell;
2725
use std::collections::BTreeMap;
2826
use std::mem;
@@ -32,7 +30,6 @@ use syntax_pos::{BytePos, Span, NO_EXPANSION, DUMMY_SP};
3230
use ty;
3331
use ty::codec::{self as ty_codec, TyDecoder, TyEncoder};
3432
use ty::context::TyCtxt;
35-
use ty::subst::Substs;
3633

3734
// Some magic values used for verifying that encoding and decoding. These are
3835
// basically random numbers.
@@ -359,46 +356,6 @@ impl<'a, 'tcx, 'x> CacheDecoder<'a, 'tcx, 'x> {
359356
}
360357
}
361358

362-
macro_rules! decoder_methods {
363-
($($name:ident -> $ty:ty;)*) => {
364-
$(fn $name(&mut self) -> Result<$ty, Self::Error> {
365-
self.opaque.$name()
366-
})*
367-
}
368-
}
369-
370-
impl<'a, 'tcx, 'x> Decoder for CacheDecoder<'a, 'tcx, 'x> {
371-
type Error = String;
372-
373-
decoder_methods! {
374-
read_nil -> ();
375-
376-
read_u128 -> u128;
377-
read_u64 -> u64;
378-
read_u32 -> u32;
379-
read_u16 -> u16;
380-
read_u8 -> u8;
381-
read_usize -> usize;
382-
383-
read_i128 -> i128;
384-
read_i64 -> i64;
385-
read_i32 -> i32;
386-
read_i16 -> i16;
387-
read_i8 -> i8;
388-
read_isize -> isize;
389-
390-
read_bool -> bool;
391-
read_f64 -> f64;
392-
read_f32 -> f32;
393-
read_char -> char;
394-
read_str -> Cow<str>;
395-
}
396-
397-
fn error(&mut self, err: &str) -> Self::Error {
398-
self.opaque.error(err)
399-
}
400-
}
401-
402359
// Decode something that was encoded with encode_tagged() and verify that the
403360
// tag matches and the correct amount of bytes was read.
404361
fn decode_tagged<'a, 'tcx, D, T, V>(decoder: &mut D,
@@ -481,6 +438,8 @@ impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx,
481438
}
482439
}
483440

441+
implement_ty_decoder!( CacheDecoder<'a, 'tcx, 'x> );
442+
484443
impl<'a, 'tcx, 'x> SpecializedDecoder<Span> for CacheDecoder<'a, 'tcx, 'x> {
485444
fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
486445
let lo = BytePos::decode(self)?;
@@ -498,12 +457,6 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<Span> for CacheDecoder<'a, 'tcx, 'x> {
498457
}
499458
}
500459

501-
impl<'a, 'tcx, 'x> SpecializedDecoder<CrateNum> for CacheDecoder<'a, 'tcx, 'x> {
502-
fn specialized_decode(&mut self) -> Result<CrateNum, Self::Error> {
503-
ty_codec::decode_cnum(self)
504-
}
505-
}
506-
507460
// This impl makes sure that we get a runtime error when we try decode a
508461
// DefIndex that is not contained in a DefId. Such a case would be problematic
509462
// because we would not know how to transform the DefIndex to the current
@@ -567,66 +520,6 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<NodeId> for CacheDecoder<'a, 'tcx, 'x> {
567520
}
568521
}
569522

570-
impl<'a, 'tcx, 'x> SpecializedDecoder<ty::Ty<'tcx>> for CacheDecoder<'a, 'tcx, 'x> {
571-
fn specialized_decode(&mut self) -> Result<ty::Ty<'tcx>, Self::Error> {
572-
ty_codec::decode_ty(self)
573-
}
574-
}
575-
576-
impl<'a, 'tcx, 'x> SpecializedDecoder<ty::GenericPredicates<'tcx>>
577-
for CacheDecoder<'a, 'tcx, 'x> {
578-
fn specialized_decode(&mut self) -> Result<ty::GenericPredicates<'tcx>, Self::Error> {
579-
ty_codec::decode_predicates(self)
580-
}
581-
}
582-
583-
impl<'a, 'tcx, 'x> SpecializedDecoder<&'tcx Substs<'tcx>> for CacheDecoder<'a, 'tcx, 'x> {
584-
fn specialized_decode(&mut self) -> Result<&'tcx Substs<'tcx>, Self::Error> {
585-
ty_codec::decode_substs(self)
586-
}
587-
}
588-
589-
impl<'a, 'tcx, 'x> SpecializedDecoder<ty::Region<'tcx>> for CacheDecoder<'a, 'tcx, 'x> {
590-
fn specialized_decode(&mut self) -> Result<ty::Region<'tcx>, Self::Error> {
591-
ty_codec::decode_region(self)
592-
}
593-
}
594-
595-
impl<'a, 'tcx, 'x> SpecializedDecoder<&'tcx ty::Slice<ty::Ty<'tcx>>>
596-
for CacheDecoder<'a, 'tcx, 'x> {
597-
fn specialized_decode(&mut self) -> Result<&'tcx ty::Slice<ty::Ty<'tcx>>, Self::Error> {
598-
ty_codec::decode_ty_slice(self)
599-
}
600-
}
601-
602-
impl<'a, 'tcx, 'x> SpecializedDecoder<&'tcx ty::AdtDef> for CacheDecoder<'a, 'tcx, 'x> {
603-
fn specialized_decode(&mut self) -> Result<&'tcx ty::AdtDef, Self::Error> {
604-
ty_codec::decode_adt_def(self)
605-
}
606-
}
607-
608-
impl<'a, 'tcx, 'x> SpecializedDecoder<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>>
609-
for CacheDecoder<'a, 'tcx, 'x> {
610-
fn specialized_decode(&mut self)
611-
-> Result<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>, Self::Error> {
612-
ty_codec::decode_existential_predicate_slice(self)
613-
}
614-
}
615-
616-
impl<'a, 'tcx, 'x> SpecializedDecoder<ByteArray<'tcx>> for CacheDecoder<'a, 'tcx, 'x> {
617-
fn specialized_decode(&mut self) -> Result<ByteArray<'tcx>, Self::Error> {
618-
ty_codec::decode_byte_array(self)
619-
}
620-
}
621-
622-
impl<'a, 'tcx, 'x> SpecializedDecoder<&'tcx ty::Const<'tcx>>
623-
for CacheDecoder<'a, 'tcx, 'x> {
624-
fn specialized_decode(&mut self) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
625-
ty_codec::decode_const(self)
626-
}
627-
}
628-
629-
630523
//- ENCODING -------------------------------------------------------------------
631524

632525
struct CacheEncoder<'enc, 'a, 'tcx, E>

src/librustc/ty/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub use self::maps::queries;
8989
pub mod adjustment;
9090
pub mod binding;
9191
pub mod cast;
92+
#[macro_use]
9293
pub mod codec;
9394
pub mod error;
9495
mod erase_regions;

0 commit comments

Comments
 (0)