Skip to content

Commit f3652c5

Browse files
Intern binders in TyKind
1 parent 531d3d5 commit f3652c5

File tree

18 files changed

+145
-127
lines changed

18 files changed

+145
-127
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5555
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5656
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5757
use rustc_hir::{
58-
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, ParamName, PredicateOrigin,
59-
TraitCandidate,
58+
self as hir, ConstArg, GenericArg, HirId, IsAnonInPath, ItemLocalMap, LangItem, ParamName,
59+
PredicateOrigin, TraitCandidate,
6060
};
6161
use rustc_index::{Idx, IndexSlice, IndexVec};
6262
use rustc_macros::extension;

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
16411641
let expected_ty = self.infcx.instantiate_binder_with_fresh_vars(
16421642
self.body().source_info(location).span,
16431643
BoundRegionConversionTime::HigherRankedType,
1644-
binder_ty.into(),
1644+
*binder_ty,
16451645
);
16461646
self.sub_types(
16471647
operand_ty,
@@ -1891,7 +1891,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
18911891
let found_ty = self.infcx.instantiate_binder_with_fresh_vars(
18921892
self.body.source_info(location).span,
18931893
BoundRegionConversionTime::HigherRankedType,
1894-
binder_ty.into(),
1894+
*binder_ty,
18951895
);
18961896
self.relate_types(
18971897
ty,

Diff for: compiler/rustc_hir_typeck/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16901690
ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
16911691
inner_expr.span,
16921692
infer::BoundRegionConversionTime::HigherRankedType,
1693-
binder.into(),
1693+
*binder,
16941694
),
16951695
ty::Error(e) => Ty::new_error(self.tcx, e),
16961696
_ => {
@@ -1727,7 +1727,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17271727
ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
17281728
inner_expr.span,
17291729
infer::BoundRegionConversionTime::HigherRankedType,
1730-
binder.into(),
1730+
*binder,
17311731
),
17321732
ty::Error(e) => Ty::new_error(self.tcx, e),
17331733
_ => {

Diff for: compiler/rustc_middle/src/ty/codec.rs

+24
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,27 @@ macro_rules! implement_ty_decoder {
621621
}
622622
}
623623
}
624+
625+
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::TyBinderRef<'tcx> {
626+
fn encode(&self, encoder: &mut E) {
627+
(**self).encode(encoder)
628+
}
629+
}
630+
631+
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::TyBinderRef<'tcx> {
632+
fn decode(decoder: &mut D) -> Self {
633+
decoder.interner().mk_ty_binder(Decodable::decode(decoder))
634+
}
635+
}
636+
637+
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::SigBinderRef<'tcx> {
638+
fn encode(&self, encoder: &mut E) {
639+
(**self).encode(encoder)
640+
}
641+
}
642+
643+
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::SigBinderRef<'tcx> {
644+
fn decode(decoder: &mut D) -> Self {
645+
decoder.interner().mk_sig_binder(Decodable::decode(decoder))
646+
}
647+
}

Diff for: compiler/rustc_middle/src/ty/context.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ use crate::ty::{
8080
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Clauses, Const, GenericArg, GenericArgs,
8181
GenericArgsRef, GenericParamDefKind, List, ListWithCachedTypeInfo, ParamConst, ParamTy,
8282
Pattern, PatternKind, PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind,
83-
PredicatePolarity, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
84-
ValTree, ValTreeKind, Visibility,
83+
PredicatePolarity, Region, RegionKind, ReprOptions, SigBinderRef, TraitObjectVisitor, Ty,
84+
TyBinderRef, TyKind, TyVid, ValTree, ValTreeKind, Visibility,
8585
};
8686

8787
#[allow(rustc::usage_of_ty_tykind)]
@@ -97,6 +97,9 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9797
type Term = ty::Term<'tcx>;
9898
type BoundVarKinds = &'tcx List<ty::BoundVariableKind>;
9999

100+
type SigBinderRef = ty::SigBinderRef<'tcx>;
101+
type TyBinderRef = ty::TyBinderRef<'tcx>;
102+
100103
type BoundVarKind = ty::BoundVariableKind;
101104
type PredefinedOpaques = solve::PredefinedOpaques<'tcx>;
102105

@@ -813,6 +816,8 @@ pub struct CtxtInterners<'tcx> {
813816
captures: InternedSet<'tcx, List<&'tcx ty::CapturedPlace<'tcx>>>,
814817
offset_of: InternedSet<'tcx, List<(VariantIdx, FieldIdx)>>,
815818
valtree: InternedSet<'tcx, ty::ValTreeKind<'tcx>>,
819+
ty_binder: InternedSet<'tcx, ty::Binder<'tcx, Ty<'tcx>>>,
820+
sig_binder: InternedSet<'tcx, ty::Binder<'tcx, ty::FnSigTys<'tcx>>>,
816821
}
817822

818823
impl<'tcx> CtxtInterners<'tcx> {
@@ -849,6 +854,8 @@ impl<'tcx> CtxtInterners<'tcx> {
849854
captures: InternedSet::with_capacity(N),
850855
offset_of: InternedSet::with_capacity(N),
851856
valtree: InternedSet::with_capacity(N),
857+
ty_binder: InternedSet::with_capacity(N),
858+
sig_binder: InternedSet::with_capacity(N),
852859
}
853860
}
854861

@@ -2331,6 +2338,8 @@ nop_lift! { predicate; Predicate<'a> => Predicate<'tcx> }
23312338
nop_lift! { predicate; Clause<'a> => Clause<'tcx> }
23322339
nop_lift! { layout; Layout<'a> => Layout<'tcx> }
23332340
nop_lift! { valtree; ValTree<'a> => ValTree<'tcx> }
2341+
nop_lift! { ty_binder; TyBinderRef<'a> => TyBinderRef<'tcx> }
2342+
nop_lift! { sig_binder; SigBinderRef<'a> => SigBinderRef<'tcx> }
23342343

23352344
nop_list_lift! { type_lists; Ty<'a> => Ty<'tcx> }
23362345
nop_list_lift! {
@@ -2595,6 +2604,8 @@ direct_interners! {
25952604
ExternalConstraints -> ExternalConstraints<'tcx>,
25962605
predefined_opaques_in_body: pub mk_predefined_opaques_in_body(PredefinedOpaquesData<TyCtxt<'tcx>>):
25972606
PredefinedOpaques -> PredefinedOpaques<'tcx>,
2607+
ty_binder: pub mk_ty_binder(ty::Binder<'tcx, Ty<'tcx>>): TyBinderRef -> TyBinderRef<'tcx>,
2608+
sig_binder: pub mk_sig_binder(ty::Binder<'tcx, ty::FnSigTys<'tcx>>): SigBinderRef -> SigBinderRef<'tcx>,
25982609
}
25992610

26002611
macro_rules! slice_interners {

Diff for: compiler/rustc_middle/src/ty/flags.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,13 @@ impl FlagComputation {
192192
self.add_args(args);
193193
}
194194

195-
&ty::FnPtr(sig_tys, _) => self.bound_computation(sig_tys, |computation, sig_tys| {
195+
&ty::FnPtr(sig_tys, _) => self.bound_computation(*sig_tys, |computation, sig_tys| {
196196
computation.add_tys(sig_tys.inputs_and_output);
197197
}),
198198

199-
&ty::UnsafeBinder(bound_ty) => {
200-
self.bound_computation(bound_ty.into(), |computation, ty| {
201-
computation.add_ty(ty);
202-
})
203-
}
199+
&ty::UnsafeBinder(bound_ty) => self.bound_computation(*bound_ty, |computation, ty| {
200+
computation.add_ty(ty);
201+
}),
204202
}
205203
}
206204

Diff for: compiler/rustc_middle/src/ty/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ where
828828
}
829829

830830
ty::UnsafeBinder(bound_ty) => {
831-
let ty = tcx.instantiate_bound_regions_with_erased(bound_ty.into());
831+
let ty = tcx.instantiate_bound_regions_with_erased(*bound_ty);
832832
field_ty_or_layout(TyAndLayout { ty, ..this }, cx, i)
833833
}
834834

Diff for: compiler/rustc_middle/src/ty/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ pub use self::region::{
9191
pub use self::rvalue_scopes::RvalueScopes;
9292
pub use self::sty::{
9393
AliasTy, Article, Binder, BoundTy, BoundTyKind, BoundVariableKind, CanonicalPolyFnSig,
94-
CoroutineArgsExt, EarlyBinder, FnSig, InlineConstArgs, InlineConstArgsParts, ParamConst,
95-
ParamTy, PolyFnSig, TyKind, TypeAndMut, TypingMode, UpvarArgs,
94+
CoroutineArgsExt, EarlyBinder, FnSig, FnSigTys, InlineConstArgs, InlineConstArgsParts,
95+
ParamConst, ParamTy, PolyFnSig, SigBinderRef, TyBinderRef, TyKind, TypeAndMut, TypingMode,
96+
UpvarArgs,
9697
};
9798
pub use self::trait_def::TraitDef;
9899
pub use self::typeck_results::{
@@ -2224,6 +2225,6 @@ mod size_asserts {
22242225
use super::*;
22252226
// tidy-alphabetical-start
22262227
static_assert_size!(PredicateKind<'_>, 32);
2227-
static_assert_size!(WithCachedTypeInfo<TyKind<'_>>, 56);
2228+
static_assert_size!(WithCachedTypeInfo<TyKind<'_>>, 48);
22282229
// tidy-alphabetical-end
22292230
}

Diff for: compiler/rustc_middle/src/ty/structural_impls.rs

+32
Original file line numberDiff line numberDiff line change
@@ -720,3 +720,35 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<PlaceElem<'tcx>> {
720720
ty::util::fold_list(self, folder, |tcx, v| tcx.mk_place_elems(v))
721721
}
722722
}
723+
724+
impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::TyBinderRef<'tcx> {
725+
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
726+
(**self).visit_with(visitor)
727+
}
728+
}
729+
730+
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::TyBinderRef<'tcx> {
731+
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
732+
self,
733+
folder: &mut F,
734+
) -> Result<Self, F::Error> {
735+
let folded = (*self).try_fold_with(folder)?;
736+
Ok(if *self == folded { self } else { folder.cx().mk_ty_binder(folded) })
737+
}
738+
}
739+
740+
impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ty::SigBinderRef<'tcx> {
741+
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
742+
(**self).visit_with(visitor)
743+
}
744+
}
745+
746+
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ty::SigBinderRef<'tcx> {
747+
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
748+
self,
749+
folder: &mut F,
750+
) -> Result<Self, F::Error> {
751+
let folded = (*self).try_fold_with(folder)?;
752+
Ok(if *self == folded { self } else { folder.cx().mk_sig_binder(folded) })
753+
}
754+
}

Diff for: compiler/rustc_middle/src/ty/sty.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::assert_matches::debug_assert_matches;
66
use std::borrow::Cow;
77
use std::iter;
8-
use std::ops::{ControlFlow, Range};
8+
use std::ops::{ControlFlow, Deref, Range};
99

1010
use hir::def::{CtorKind, DefKind};
1111
use rustc_abi::{ExternAbi, FIRST_VARIANT, FieldIdx, VariantIdx};
@@ -37,6 +37,7 @@ pub type FnSig<'tcx> = ir::FnSig<TyCtxt<'tcx>>;
3737
pub type Binder<'tcx, T> = ir::Binder<TyCtxt<'tcx>, T>;
3838
pub type EarlyBinder<'tcx, T> = ir::EarlyBinder<TyCtxt<'tcx>, T>;
3939
pub type TypingMode<'tcx> = ir::TypingMode<TyCtxt<'tcx>>;
40+
pub type FnSigTys<'tcx> = ir::FnSigTys<TyCtxt<'tcx>>;
4041

4142
pub trait Article {
4243
fn article(&self) -> &'static str;
@@ -704,12 +705,12 @@ impl<'tcx> Ty<'tcx> {
704705
#[inline]
705706
pub fn new_fn_ptr(tcx: TyCtxt<'tcx>, fty: PolyFnSig<'tcx>) -> Ty<'tcx> {
706707
let (sig_tys, hdr) = fty.split();
707-
Ty::new(tcx, FnPtr(sig_tys, hdr))
708+
Ty::new(tcx, FnPtr(tcx.mk_sig_binder(sig_tys), hdr))
708709
}
709710

710711
#[inline]
711712
pub fn new_unsafe_binder(tcx: TyCtxt<'tcx>, b: Binder<'tcx, Ty<'tcx>>) -> Ty<'tcx> {
712-
Ty::new(tcx, UnsafeBinder(b.into()))
713+
Ty::new(tcx, UnsafeBinder(tcx.mk_ty_binder(b)))
713714
}
714715

715716
#[inline]
@@ -2090,6 +2091,30 @@ mod size_asserts {
20902091
use super::*;
20912092
// tidy-alphabetical-start
20922093
static_assert_size!(ty::RegionKind<'_>, 24);
2093-
static_assert_size!(ty::TyKind<'_>, 32);
2094+
static_assert_size!(ty::TyKind<'_>, 24);
20942095
// tidy-alphabetical-end
20952096
}
2097+
2098+
// FIXME: this is a distinct type because we need to define `Encode`/`Decode` impls.
2099+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, HashStable)]
2100+
pub struct TyBinderRef<'tcx>(pub ty::Interned<'tcx, ty::Binder<'tcx, Ty<'tcx>>>);
2101+
2102+
impl<'tcx> Deref for TyBinderRef<'tcx> {
2103+
type Target = ty::Binder<'tcx, Ty<'tcx>>;
2104+
2105+
fn deref(&self) -> &Self::Target {
2106+
&self.0
2107+
}
2108+
}
2109+
2110+
// FIXME: this is a distinct type because we need to define `Encode`/`Decode` impls.
2111+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, HashStable)]
2112+
pub struct SigBinderRef<'tcx>(pub ty::Interned<'tcx, ty::Binder<'tcx, FnSigTys<'tcx>>>);
2113+
2114+
impl<'tcx> Deref for SigBinderRef<'tcx> {
2115+
type Target = ty::Binder<'tcx, FnSigTys<'tcx>>;
2116+
2117+
fn deref(&self) -> &Self::Target {
2118+
&self.0
2119+
}
2120+
}

Diff for: compiler/rustc_smir/src/rustc_internal/internal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl RustcInternal for RigidTy {
132132
}
133133
RigidTy::FnPtr(sig) => {
134134
let (sig_tys, hdr) = sig.internal(tables, tcx).split();
135-
rustc_ty::TyKind::FnPtr(sig_tys, hdr)
135+
rustc_ty::TyKind::FnPtr(tcx.mk_sig_binder(sig_tys), hdr)
136136
}
137137
RigidTy::Closure(def, args) => {
138138
rustc_ty::TyKind::Closure(def.0.internal(tables, tcx), args.internal(tables, tcx))

Diff for: compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4973,13 +4973,13 @@ fn hint_missing_borrow<'tcx>(
49734973
}
49744974

49754975
let found_args = match found.kind() {
4976-
ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
4976+
ty::FnPtr(sig_tys, _) => infcx.enter_forall(**sig_tys, |sig_tys| sig_tys.inputs().iter()),
49774977
kind => {
49784978
span_bug!(span, "found was converted to a FnPtr above but is now {:?}", kind)
49794979
}
49804980
};
49814981
let expected_args = match expected.kind() {
4982-
ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
4982+
ty::FnPtr(sig_tys, _) => infcx.enter_forall(**sig_tys, |sig_tys| sig_tys.inputs().iter()),
49834983
kind => {
49844984
span_bug!(span, "expected was converted to a FnPtr above but is now {:?}", kind)
49854985
}

Diff for: compiler/rustc_ty_utils/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ fn layout_of_uncached<'tcx>(
606606
}
607607

608608
ty::UnsafeBinder(bound_ty) => {
609-
let ty = tcx.instantiate_bound_regions_with_erased(bound_ty.into());
609+
let ty = tcx.instantiate_bound_regions_with_erased(*bound_ty);
610610
cx.layout_of(ty)?.layout
611611
}
612612

Diff for: compiler/rustc_ty_utils/src/needs_drop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ where
203203
}
204204

205205
ty::UnsafeBinder(bound_ty) => {
206-
let ty = self.tcx.instantiate_bound_regions_with_erased(bound_ty.into());
206+
let ty = self.tcx.instantiate_bound_regions_with_erased(*bound_ty);
207207
queue_type(self, ty);
208208
}
209209

Diff for: compiler/rustc_type_ir/src/binder.rs

+17-32
Original file line numberDiff line numberDiff line change
@@ -59,42 +59,27 @@ where
5959
}
6060

6161
#[cfg(feature = "nightly")]
62-
macro_rules! impl_binder_encode_decode {
63-
($($t:ty),+ $(,)?) => {
64-
$(
65-
impl<I: Interner, E: rustc_serialize::Encoder> rustc_serialize::Encodable<E> for ty::Binder<I, $t>
66-
where
67-
$t: rustc_serialize::Encodable<E>,
68-
I::BoundVarKinds: rustc_serialize::Encodable<E>,
69-
{
70-
fn encode(&self, e: &mut E) {
71-
self.bound_vars().encode(e);
72-
self.as_ref().skip_binder().encode(e);
73-
}
74-
}
75-
impl<I: Interner, D: rustc_serialize::Decoder> rustc_serialize::Decodable<D> for ty::Binder<I, $t>
76-
where
77-
$t: TypeVisitable<I> + rustc_serialize::Decodable<D>,
78-
I::BoundVarKinds: rustc_serialize::Decodable<D>,
79-
{
80-
fn decode(decoder: &mut D) -> Self {
81-
let bound_vars = rustc_serialize::Decodable::decode(decoder);
82-
ty::Binder::bind_with_vars(rustc_serialize::Decodable::decode(decoder), bound_vars)
83-
}
84-
}
85-
)*
62+
impl<I: Interner, E: rustc_serialize::Encoder, T> rustc_serialize::Encodable<E> for ty::Binder<I, T>
63+
where
64+
T: rustc_serialize::Encodable<E>,
65+
I::BoundVarKinds: rustc_serialize::Encodable<E>,
66+
{
67+
fn encode(&self, e: &mut E) {
68+
self.bound_vars().encode(e);
69+
self.as_ref().skip_binder().encode(e);
8670
}
8771
}
8872

8973
#[cfg(feature = "nightly")]
90-
impl_binder_encode_decode! {
91-
ty::FnSig<I>,
92-
ty::FnSigTys<I>,
93-
ty::TraitPredicate<I>,
94-
ty::ExistentialPredicate<I>,
95-
ty::TraitRef<I>,
96-
ty::ExistentialTraitRef<I>,
97-
ty::HostEffectPredicate<I>,
74+
impl<I: Interner, D: rustc_serialize::Decoder, T> rustc_serialize::Decodable<D> for ty::Binder<I, T>
75+
where
76+
T: TypeVisitable<I> + rustc_serialize::Decodable<D>,
77+
I::BoundVarKinds: rustc_serialize::Decodable<D>,
78+
{
79+
fn decode(decoder: &mut D) -> Self {
80+
let bound_vars = rustc_serialize::Decodable::decode(decoder);
81+
ty::Binder::bind_with_vars(rustc_serialize::Decodable::decode(decoder), bound_vars)
82+
}
9883
}
9984

10085
impl<I: Interner, T> Binder<I, T>

Diff for: compiler/rustc_type_ir/src/interner.rs

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ pub trait Interner:
4444
type BoundVarKinds: Copy + Debug + Hash + Eq + SliceLike<Item = Self::BoundVarKind> + Default;
4545
type BoundVarKind: Copy + Debug + Hash + Eq;
4646

47+
type SigBinderRef: Deref<Target = ty::Binder<Self, ty::FnSigTys<Self>>>
48+
+ Copy
49+
+ Debug
50+
+ Hash
51+
+ Eq;
52+
type TyBinderRef: Deref<Target = ty::Binder<Self, Self::Ty>> + Copy + Debug + Hash + Eq;
53+
4754
type PredefinedOpaques: Copy
4855
+ Debug
4956
+ Hash

0 commit comments

Comments
 (0)