Skip to content

Commit 3ef4cf0

Browse files
committed
Move codec over to rustc_type_ir, move more implementations over
1 parent 4f1573a commit 3ef4cf0

15 files changed

Lines changed: 261 additions & 178 deletions

File tree

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rustc_index::Idx;
2424
use rustc_middle::middle::lib_features::LibFeatures;
2525
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2626
use rustc_middle::ty::Visibility;
27-
use rustc_middle::ty::codec::TyDecoder;
2827
use rustc_middle::{bug, implement_ty_decoder};
2928
use rustc_proc_macro::bridge::client::ProcMacro;
3029
use rustc_serialize::opaque::MemDecoder;
@@ -36,6 +35,7 @@ use rustc_span::{
3635
BlobDecoder, BytePos, ByteSymbol, DUMMY_SP, Pos, RemapPathScopeComponents, SpanData,
3736
SpanDecoder, Symbol, SyntaxContext, kw,
3837
};
38+
use rustc_type_ir::codec::TyDecoder;
3939
use tracing::debug;
4040

4141
use crate::creader::CStore;
@@ -387,6 +387,8 @@ impl<'a> BlobDecodeContext<'a> {
387387
}
388388

389389
impl<'a, 'tcx> TyDecoder<'tcx> for MetadataDecodeContext<'a, 'tcx> {
390+
type Interner = TyCtxt<'tcx>;
391+
390392
const CLEAR_CROSS_CRATE: bool = true;
391393

392394
#[inline]

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_middle::mir::interpret;
2323
use rustc_middle::query::Providers;
2424
use rustc_middle::traits::specialization_graph;
2525
use rustc_middle::ty::AssocContainer;
26-
use rustc_middle::ty::codec::TyEncoder;
2726
use rustc_middle::ty::fast_reject::{self, TreatParams};
2827
use rustc_middle::{bug, span_bug};
2928
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque};
@@ -33,6 +32,7 @@ use rustc_span::{
3332
ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId,
3433
Symbol, SyntaxContext, sym,
3534
};
35+
use rustc_type_ir::codec::TyEncoder;
3636
use tracing::{debug, instrument, trace};
3737

3838
use crate::eii::EiiMapEncodedKeyValue;
@@ -369,6 +369,8 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for [u8] {
369369
}
370370

371371
impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> {
372+
type Interner = TyCtxt<'tcx>;
373+
372374
const CLEAR_CROSS_CRATE: bool = true;
373375

374376
fn position(&self) -> usize {

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
2828
use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2929
use crate::mir::mono::MonoItem;
3030
use crate::mir::{self, interpret};
31-
use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
31+
use crate::ty::codec::{RefDecodable, TyEncoder};
3232
use crate::ty::{self, Ty, TyCtxt};
3333

3434
const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
@@ -526,7 +526,9 @@ where
526526
value
527527
}
528528

529-
impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> {
529+
impl<'a, 'tcx> rustc_type_ir::codec::TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> {
530+
type Interner = TyCtxt<'tcx>;
531+
530532
const CLEAR_CROSS_CRATE: bool = false;
531533

532534
#[inline]
@@ -963,7 +965,9 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
963965
}
964966
}
965967

966-
impl<'a, 'tcx> TyEncoder<'tcx> for CacheEncoder<'a, 'tcx> {
968+
impl<'a, 'tcx> rustc_type_ir::codec::TyEncoder<'tcx> for CacheEncoder<'a, 'tcx> {
969+
type Interner = TyCtxt<'tcx>;
970+
967971
const CLEAR_CROSS_CRATE: bool = false;
968972

969973
#[inline]

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 8 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66
//! The functionality in here is shared between persisting to crate metadata and
77
//! persisting to incr. comp. caches.
88
9-
use std::hash::Hash;
10-
use std::intrinsics;
11-
use std::marker::{DiscriminantKind, PointeeSized};
12-
139
use rustc_abi::FieldIdx;
14-
use rustc_data_structures::fx::FxHashMap;
1510
use rustc_hir::def_id::LocalDefId;
1611
use rustc_serialize::{Decodable, Encodable};
12+
use rustc_span::Span;
1713
use rustc_span::source_map::Spanned;
18-
use rustc_span::{Span, SpanDecoder, SpanEncoder};
14+
use rustc_type_ir::codec as ir_codec;
1915

2016
use crate::arena::ArenaAllocatable;
2117
use crate::infer::canonical::{CanonicalVarKind, CanonicalVarKinds};
@@ -25,66 +21,13 @@ use crate::mir::{self};
2521
use crate::traits;
2622
use crate::ty::{self, AdtDef, GenericArgsRef, Ty, TyCtxt};
2723

28-
/// The shorthand encoding uses an enum's variant index `usize`
29-
/// and is offset by this value so it never matches a real variant.
30-
/// This offset is also chosen so that the first byte is never < 0x80.
31-
pub const SHORTHAND_OFFSET: usize = 0x80;
32-
33-
pub trait TyEncoder<'tcx>: SpanEncoder {
34-
const CLEAR_CROSS_CRATE: bool;
35-
36-
fn position(&self) -> usize;
37-
38-
fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>;
39-
40-
fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>;
41-
42-
fn encode_alloc_id(&mut self, alloc_id: &AllocId);
43-
}
44-
45-
pub trait TyDecoder<'tcx>: SpanDecoder {
46-
const CLEAR_CROSS_CRATE: bool;
47-
48-
fn interner(&self) -> TyCtxt<'tcx>;
49-
50-
fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
51-
where
52-
F: FnOnce(&mut Self) -> Ty<'tcx>;
53-
54-
fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
55-
where
56-
F: FnOnce(&mut Self) -> R;
24+
pub trait TyEncoder<'tcx>: ir_codec::TyEncoder<'tcx, Interner = TyCtxt<'tcx>> {}
25+
impl<'tcx, T> TyEncoder<'tcx> for T where T: ir_codec::TyEncoder<'tcx, Interner = TyCtxt<'tcx>> {}
5726

58-
fn positioned_at_shorthand(&self) -> bool {
59-
(self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0
60-
}
61-
62-
fn decode_alloc_id(&mut self) -> AllocId;
63-
}
27+
pub trait TyDecoder<'tcx>: ir_codec::TyDecoder<'tcx, Interner = TyCtxt<'tcx>> {}
28+
impl<'tcx, T> TyDecoder<'tcx> for T where T: ir_codec::TyDecoder<'tcx, Interner = TyCtxt<'tcx>> {}
6429

65-
pub trait EncodableWithShorthand<'tcx, E: TyEncoder<'tcx>>: Copy + Eq + Hash {
66-
type Variant: Encodable<E>;
67-
fn variant(&self) -> &Self::Variant;
68-
}
69-
70-
#[allow(rustc::usage_of_ty_tykind)]
71-
impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> {
72-
type Variant = ty::TyKind<'tcx>;
73-
74-
#[inline]
75-
fn variant(&self) -> &Self::Variant {
76-
self.kind()
77-
}
78-
}
79-
80-
impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> {
81-
type Variant = ty::PredicateKind<'tcx>;
82-
83-
#[inline]
84-
fn variant(&self) -> &Self::Variant {
85-
self
86-
}
87-
}
30+
pub use ir_codec::{EncodableWithShorthand, SHORTHAND_OFFSET, encode_with_shorthand};
8831

8932
/// Trait for decoding to a reference.
9033
///
@@ -96,55 +39,10 @@ impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::Predicate
9639
///
9740
/// `Decodable` can still be implemented in cases where `Decodable` is required
9841
/// by a trait bound.
99-
pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>>: PointeeSized {
42+
pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>>: std::marker::PointeeSized {
10043
fn decode(d: &mut D) -> &'tcx Self;
10144
}
10245

103-
/// Encode the given value or a previously cached shorthand.
104-
pub fn encode_with_shorthand<'tcx, E, T, M>(encoder: &mut E, value: &T, cache: M)
105-
where
106-
E: TyEncoder<'tcx>,
107-
M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
108-
T: EncodableWithShorthand<'tcx, E>,
109-
// The discriminant and shorthand must have the same size.
110-
T::Variant: DiscriminantKind<Discriminant = isize>,
111-
{
112-
let existing_shorthand = cache(encoder).get(value).copied();
113-
if let Some(shorthand) = existing_shorthand {
114-
encoder.emit_usize(shorthand);
115-
return;
116-
}
117-
118-
let variant = value.variant();
119-
120-
let start = encoder.position();
121-
variant.encode(encoder);
122-
let len = encoder.position() - start;
123-
124-
// The shorthand encoding uses the same usize as the
125-
// discriminant, with an offset so they can't conflict.
126-
let discriminant = intrinsics::discriminant_value(variant);
127-
assert!(SHORTHAND_OFFSET > discriminant as usize);
128-
129-
let shorthand = start + SHORTHAND_OFFSET;
130-
131-
// Get the number of bits that leb128 could fit
132-
// in the same space as the fully encoded type.
133-
let leb128_bits = len * 7;
134-
135-
// Check that the shorthand is a not longer than the
136-
// full encoding itself, i.e., it's an obvious win.
137-
if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
138-
cache(encoder).insert(*value, shorthand);
139-
}
140-
}
141-
142-
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for Ty<'tcx> {
143-
fn encode(&self, e: &mut E) {
144-
encode_with_shorthand(e, self, TyEncoder::type_shorthands);
145-
}
146-
}
147-
14846
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Predicate<'tcx> {
14947
fn encode(&self, e: &mut E) {
15048
let kind = self.kind();
@@ -231,25 +129,6 @@ fn decode_arena_allocable_slice<
231129
decoder.interner().arena.alloc_from_iter(<Vec<T> as Decodable<D>>::decode(decoder))
232130
}
233131

234-
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for Ty<'tcx> {
235-
#[allow(rustc::usage_of_ty_tykind)]
236-
fn decode(decoder: &mut D) -> Ty<'tcx> {
237-
// Handle shorthands first, if we have a usize > 0x80.
238-
if decoder.positioned_at_shorthand() {
239-
let pos = decoder.read_usize();
240-
assert!(pos >= SHORTHAND_OFFSET);
241-
let shorthand = pos - SHORTHAND_OFFSET;
242-
243-
decoder.cached_ty_for_shorthand(shorthand, |decoder| {
244-
decoder.with_position(shorthand, Ty::decode)
245-
})
246-
} else {
247-
let tcx = decoder.interner();
248-
tcx.mk_ty_from_kind(ty::TyKind::decode(decoder))
249-
}
250-
}
251-
}
252-
253132
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Predicate<'tcx> {
254133
fn decode(decoder: &mut D) -> ty::Predicate<'tcx> {
255134
let bound_vars = Decodable::decode(decoder);

compiler/rustc_middle/src/ty/context/impl_interner.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt;
44

55
use rustc_abi::ExternAbi;
66
use rustc_data_structures::debug_assert_matches;
7+
use rustc_data_structures::intern::Interned;
78
use rustc_errors::ErrorGuaranteed;
89
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
910
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -43,6 +44,8 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
4344
type ImplId = DefId;
4445
type UnevaluatedConstId = DefId;
4546
type Span = Span;
47+
type Interned<T: Copy + Clone + std::fmt::Debug + std::hash::Hash + Eq + PartialEq> =
48+
Interned<'tcx, T>;
4649

4750
type GenericArgs = ty::GenericArgsRef<'tcx>;
4851

@@ -79,7 +82,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
7982
fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, DepNodeIndex) {
8083
self.dep_graph.with_anon_task(self, crate::dep_graph::dep_kinds::TraitSelect, task)
8184
}
82-
type Ty = Ty<'tcx>;
8385
type Tys = &'tcx List<Ty<'tcx>>;
8486

8587
type FnInputTys = &'tcx [Ty<'tcx>];
@@ -292,6 +294,22 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
292294
self.mk_type_list_from_iter(args)
293295
}
294296

297+
fn mk_ty_from_kind(self, kind: ty::TyKind<'tcx>) -> Ty<'tcx> {
298+
self.mk_ty_from_kind(kind)
299+
}
300+
301+
fn mk_coroutine_witness_for_coroutine(
302+
self,
303+
def_id: DefId,
304+
args: Self::GenericArgs,
305+
) -> Ty<'tcx> {
306+
Ty::new_coroutine_witness_for_coroutine(self, def_id, args)
307+
}
308+
309+
fn ty_discriminant_ty(self, ty: Ty<'tcx>) -> Ty<'tcx> {
310+
ty.discriminant_ty(self)
311+
}
312+
295313
fn parent(self, def_id: DefId) -> DefId {
296314
self.parent(def_id)
297315
}

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ use crate::ty::{
1919
TypeSuperVisitable, TypeVisitable, TypeVisitor,
2020
};
2121

22-
impl IntoDiagArg for Ty<'_> {
23-
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
24-
ty::tls::with(|tcx| {
25-
let ty = tcx.short_string(self, path);
26-
DiagArgValue::Str(std::borrow::Cow::Owned(ty))
27-
})
28-
}
29-
}
30-
3122
impl IntoDiagArg for Instance<'_> {
3223
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
3324
ty::tls::with(|tcx| {

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3094,7 +3094,6 @@ macro_rules! define_print_and_forward_display {
30943094

30953095
forward_display_to_print! {
30963096
ty::Region<'tcx>,
3097-
Ty<'tcx>,
30983097
&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
30993098
ty::Const<'tcx>
31003099
}

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ impl fmt::Debug for ty::LateParamRegionKind {
8686
}
8787
}
8888

89-
impl<'tcx> fmt::Debug for Ty<'tcx> {
90-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91-
with_no_trimmed_paths!(fmt::Debug::fmt(self.kind(), f))
92-
}
93-
}
94-
9589
impl fmt::Debug for ty::ParamTy {
9690
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9791
write!(f, "{}/#{}", self.name, self.index)

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ops::{ControlFlow, Range};
88
use hir::def::{CtorKind, DefKind};
99
use rustc_abi::{FIRST_VARIANT, FieldIdx, ScalableElt, VariantIdx};
1010
use rustc_data_structures::debug_assert_matches;
11-
use rustc_errors::{ErrorGuaranteed, MultiSpan};
11+
use rustc_errors::MultiSpan;
1212
use rustc_hir as hir;
1313
use rustc_hir::LangItem;
1414
use rustc_hir::def_id::DefId;
@@ -27,7 +27,7 @@ use crate::traits::ObligationCause;
2727
use crate::ty::InferTy::*;
2828
use crate::ty::{
2929
self, AdtDef, Discr, GenericArg, GenericArgs, GenericArgsRef, List, ParamEnv, Region, TyCtxt,
30-
TypeFlags, TypeSuperVisitable, TypeVisitable, TypeVisitor, UintTy,
30+
TypeSuperVisitable, TypeVisitable, TypeVisitor, UintTy,
3131
};
3232

3333
// Re-export and re-parameterize some `I = TyCtxt<'tcx>` types here

0 commit comments

Comments
 (0)