Skip to content

Commit 633a0c6

Browse files
Uplift binder
1 parent 9fa07a4 commit 633a0c6

File tree

23 files changed

+685
-651
lines changed

23 files changed

+685
-651
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

+9
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ impl<I: rustc_type_ir::Interner> IntoDiagArg for rustc_type_ir::FnSig<I> {
118118
}
119119
}
120120

121+
impl<I: rustc_type_ir::Interner, T> IntoDiagArg for rustc_type_ir::Binder<I, T>
122+
where
123+
T: IntoDiagArg,
124+
{
125+
fn into_diag_arg(self) -> DiagArgValue {
126+
self.skip_binder().into_diag_arg()
127+
}
128+
}
129+
121130
into_diag_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
122131

123132
impl IntoDiagArg for bool {

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
1818
use rustc_infer::traits::FulfillmentError;
1919
use rustc_middle::bug;
2020
use rustc_middle::query::Key;
21-
use rustc_middle::ty::print::PrintTraitRefExt as _;
21+
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
2222
use rustc_middle::ty::GenericParamDefKind;
2323
use rustc_middle::ty::{self, suggest_constraining_type_param};
2424
use rustc_middle::ty::{AdtDef, Ty, TyCtxt, TypeVisitableExt};

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
4040
use rustc_infer::traits::ObligationCause;
4141
use rustc_middle::middle::stability::AllowUnstable;
4242
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
43+
use rustc_middle::ty::print::PrintPolyTraitRefExt as _;
4344
use rustc_middle::ty::{
4445
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, ParamEnv, Ty, TyCtxt,
4546
TypeVisitableExt,

compiler/rustc_middle/src/ty/codec.rs

+7-51
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,11 @@ impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for Ty<'tcx> {
115115
}
116116
}
117117

118-
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E>
119-
for ty::Binder<'tcx, ty::PredicateKind<'tcx>>
120-
{
121-
fn encode(&self, e: &mut E) {
122-
self.bound_vars().encode(e);
123-
encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands);
124-
}
125-
}
126-
127118
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Predicate<'tcx> {
128119
fn encode(&self, e: &mut E) {
129-
self.kind().encode(e);
120+
let kind = self.kind();
121+
kind.bound_vars().encode(e);
122+
encode_with_shorthand(e, &kind.skip_binder(), TyEncoder::predicate_shorthands);
130123
}
131124
}
132125

@@ -233,13 +226,11 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for Ty<'tcx> {
233226
}
234227
}
235228

236-
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D>
237-
for ty::Binder<'tcx, ty::PredicateKind<'tcx>>
238-
{
239-
fn decode(decoder: &mut D) -> ty::Binder<'tcx, ty::PredicateKind<'tcx>> {
229+
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Predicate<'tcx> {
230+
fn decode(decoder: &mut D) -> ty::Predicate<'tcx> {
240231
let bound_vars = Decodable::decode(decoder);
241232
// Handle shorthands first, if we have a usize > 0x80.
242-
ty::Binder::bind_with_vars(
233+
let predicate_kind = ty::Binder::bind_with_vars(
243234
if decoder.positioned_at_shorthand() {
244235
let pos = decoder.read_usize();
245236
assert!(pos >= SHORTHAND_OFFSET);
@@ -250,13 +241,7 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D>
250241
<ty::PredicateKind<'tcx> as Decodable<D>>::decode(decoder)
251242
},
252243
bound_vars,
253-
)
254-
}
255-
}
256-
257-
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Predicate<'tcx> {
258-
fn decode(decoder: &mut D) -> ty::Predicate<'tcx> {
259-
let predicate_kind = Decodable::decode(decoder);
244+
);
260245
decoder.interner().mk_predicate(predicate_kind)
261246
}
262247
}
@@ -599,32 +584,3 @@ macro_rules! implement_ty_decoder {
599584
}
600585
}
601586
}
602-
603-
macro_rules! impl_binder_encode_decode {
604-
($($t:ty),+ $(,)?) => {
605-
$(
606-
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Binder<'tcx, $t> {
607-
fn encode(&self, e: &mut E) {
608-
self.bound_vars().encode(e);
609-
self.as_ref().skip_binder().encode(e);
610-
}
611-
}
612-
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Binder<'tcx, $t> {
613-
fn decode(decoder: &mut D) -> Self {
614-
let bound_vars = Decodable::decode(decoder);
615-
ty::Binder::bind_with_vars(Decodable::decode(decoder), bound_vars)
616-
}
617-
}
618-
)*
619-
}
620-
}
621-
622-
impl_binder_encode_decode! {
623-
&'tcx ty::List<Ty<'tcx>>,
624-
ty::FnSig<'tcx>,
625-
ty::Predicate<'tcx>,
626-
ty::TraitPredicate<'tcx>,
627-
ty::ExistentialPredicate<'tcx>,
628-
ty::TraitRef<'tcx>,
629-
ty::ExistentialTraitRef<'tcx>,
630-
}

compiler/rustc_middle/src/ty/context.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ use crate::ty::{
3131
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Clauses, Const, ConstData,
3232
GenericParamDefKind, ImplPolarity, List, ListWithCachedTypeInfo, ParamConst, ParamTy, Pattern,
3333
PatternKind, PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, PredicatePolarity,
34-
Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid, TypeVisitable,
35-
Visibility,
34+
Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid, Visibility,
3635
};
3736
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
3837
use rustc_ast::{self as ast, attr};
@@ -96,9 +95,8 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9695
type GenericArg = ty::GenericArg<'tcx>;
9796
type Term = ty::Term<'tcx>;
9897

99-
type Binder<T: TypeVisitable<TyCtxt<'tcx>>> = Binder<'tcx, T>;
10098
type BoundVars = &'tcx List<ty::BoundVariableKind>;
101-
type BoundVar = ty::BoundVariableKind;
99+
type BoundVarKind = ty::BoundVariableKind;
102100

103101
type CanonicalVars = CanonicalVarInfos<'tcx>;
104102
type PredefinedOpaques = solve::PredefinedOpaques<'tcx>;
@@ -138,6 +136,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
138136

139137
type ParamEnv = ty::ParamEnv<'tcx>;
140138
type Predicate = Predicate<'tcx>;
139+
type Clause = Clause<'tcx>;
141140
type TraitPredicate = ty::TraitPredicate<'tcx>;
142141
type RegionOutlivesPredicate = ty::RegionOutlivesPredicate<'tcx>;
143142
type TypeOutlivesPredicate = ty::TypeOutlivesPredicate<'tcx>;
@@ -245,6 +244,10 @@ impl<'tcx> rustc_type_ir::inherent::Abi<TyCtxt<'tcx>> for abi::Abi {
245244
}
246245

247246
impl<'tcx> rustc_type_ir::inherent::Safety<TyCtxt<'tcx>> for hir::Safety {
247+
fn is_safe(self) -> bool {
248+
matches!(self, hir::Safety::Safe)
249+
}
250+
248251
fn prefix_str(self) -> &'static str {
249252
self.prefix_str()
250253
}

compiler/rustc_middle/src/ty/generic_args.rs

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ impl<'tcx> rustc_type_ir::inherent::GenericArgs<TyCtxt<'tcx>> for ty::GenericArg
5151
fn identity_for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::GenericArgsRef<'tcx> {
5252
GenericArgs::identity_for_item(tcx, def_id)
5353
}
54+
55+
fn extend_with_error(
56+
tcx: TyCtxt<'tcx>,
57+
def_id: DefId,
58+
original_args: &[ty::GenericArg<'tcx>],
59+
) -> ty::GenericArgsRef<'tcx> {
60+
ty::GenericArgs::extend_with_error(tcx, def_id, original_args)
61+
}
5462
}
5563

5664
impl<'tcx> rustc_type_ir::inherent::IntoKind for GenericArg<'tcx> {

compiler/rustc_middle/src/ty/predicate.rs

+4-123
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use rustc_type_ir as ir;
88
use std::cmp::Ordering;
99

1010
use crate::ty::{
11-
self, Binder, DebruijnIndex, EarlyBinder, PredicatePolarity, Term, Ty, TyCtxt, TypeFlags,
12-
Upcast, UpcastFrom, WithCachedTypeInfo,
11+
self, Binder, DebruijnIndex, EarlyBinder, PredicatePolarity, Ty, TyCtxt, TypeFlags, Upcast,
12+
UpcastFrom, WithCachedTypeInfo,
1313
};
1414

1515
pub type TraitRef<'tcx> = ir::TraitRef<TyCtxt<'tcx>>;
@@ -155,6 +155,8 @@ pub struct Clause<'tcx>(
155155
pub(super) Interned<'tcx, WithCachedTypeInfo<ty::Binder<'tcx, PredicateKind<'tcx>>>>,
156156
);
157157

158+
impl<'tcx> rustc_type_ir::inherent::Clause<TyCtxt<'tcx>> for Clause<'tcx> {}
159+
158160
impl<'tcx> Clause<'tcx> {
159161
pub fn as_predicate(self) -> Predicate<'tcx> {
160162
Predicate(self.0)
@@ -231,34 +233,6 @@ impl<'tcx> ExistentialPredicate<'tcx> {
231233

232234
pub type PolyExistentialPredicate<'tcx> = ty::Binder<'tcx, ExistentialPredicate<'tcx>>;
233235

234-
impl<'tcx> PolyExistentialPredicate<'tcx> {
235-
/// Given an existential predicate like `?Self: PartialEq<u32>` (e.g., derived from `dyn PartialEq<u32>`),
236-
/// and a concrete type `self_ty`, returns a full predicate where the existentially quantified variable `?Self`
237-
/// has been replaced with `self_ty` (e.g., `self_ty: PartialEq<u32>`, in our example).
238-
pub fn with_self_ty(&self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> ty::Clause<'tcx> {
239-
match self.skip_binder() {
240-
ExistentialPredicate::Trait(tr) => {
241-
self.rebind(tr).with_self_ty(tcx, self_ty).upcast(tcx)
242-
}
243-
ExistentialPredicate::Projection(p) => {
244-
self.rebind(p.with_self_ty(tcx, self_ty)).upcast(tcx)
245-
}
246-
ExistentialPredicate::AutoTrait(did) => {
247-
let generics = tcx.generics_of(did);
248-
let trait_ref = if generics.own_params.len() == 1 {
249-
ty::TraitRef::new(tcx, did, [self_ty])
250-
} else {
251-
// If this is an ill-formed auto trait, then synthesize
252-
// new error args for the missing generics.
253-
let err_args = ty::GenericArgs::extend_with_error(tcx, did, &[self_ty.into()]);
254-
ty::TraitRef::new(tcx, did, err_args)
255-
};
256-
self.rebind(trait_ref).upcast(tcx)
257-
}
258-
}
259-
}
260-
}
261-
262236
impl<'tcx> ty::List<ty::PolyExistentialPredicate<'tcx>> {
263237
/// Returns the "principal `DefId`" of this set of existential predicates.
264238
///
@@ -322,49 +296,9 @@ impl<'tcx> ty::List<ty::PolyExistentialPredicate<'tcx>> {
322296
}
323297

324298
pub type PolyTraitRef<'tcx> = ty::Binder<'tcx, TraitRef<'tcx>>;
325-
326-
impl<'tcx> PolyTraitRef<'tcx> {
327-
pub fn self_ty(&self) -> ty::Binder<'tcx, Ty<'tcx>> {
328-
self.map_bound_ref(|tr| tr.self_ty())
329-
}
330-
331-
pub fn def_id(&self) -> DefId {
332-
self.skip_binder().def_id
333-
}
334-
}
335-
336299
pub type PolyExistentialTraitRef<'tcx> = ty::Binder<'tcx, ExistentialTraitRef<'tcx>>;
337-
338-
impl<'tcx> PolyExistentialTraitRef<'tcx> {
339-
pub fn def_id(&self) -> DefId {
340-
self.skip_binder().def_id
341-
}
342-
343-
/// Object types don't have a self type specified. Therefore, when
344-
/// we convert the principal trait-ref into a normal trait-ref,
345-
/// you must give *some* self type. A common choice is `mk_err()`
346-
/// or some placeholder type.
347-
pub fn with_self_ty(&self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> ty::PolyTraitRef<'tcx> {
348-
self.map_bound(|trait_ref| trait_ref.with_self_ty(tcx, self_ty))
349-
}
350-
}
351-
352300
pub type PolyExistentialProjection<'tcx> = ty::Binder<'tcx, ExistentialProjection<'tcx>>;
353301

354-
impl<'tcx> PolyExistentialProjection<'tcx> {
355-
pub fn with_self_ty(
356-
&self,
357-
tcx: TyCtxt<'tcx>,
358-
self_ty: Ty<'tcx>,
359-
) -> ty::PolyProjectionPredicate<'tcx> {
360-
self.map_bound(|p| p.with_self_ty(tcx, self_ty))
361-
}
362-
363-
pub fn item_def_id(&self) -> DefId {
364-
self.skip_binder().def_id
365-
}
366-
}
367-
368302
impl<'tcx> Clause<'tcx> {
369303
/// Performs a instantiation suitable for going from a
370304
/// poly-trait-ref to supertraits that must hold if that
@@ -473,22 +407,6 @@ impl<'tcx> Clause<'tcx> {
473407

474408
pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>;
475409

476-
impl<'tcx> PolyTraitPredicate<'tcx> {
477-
pub fn def_id(self) -> DefId {
478-
// Ok to skip binder since trait `DefId` does not care about regions.
479-
self.skip_binder().def_id()
480-
}
481-
482-
pub fn self_ty(self) -> ty::Binder<'tcx, Ty<'tcx>> {
483-
self.map_bound(|trait_ref| trait_ref.self_ty())
484-
}
485-
486-
#[inline]
487-
pub fn polarity(self) -> PredicatePolarity {
488-
self.skip_binder().polarity
489-
}
490-
}
491-
492410
/// `A: B`
493411
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
494412
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
@@ -497,47 +415,10 @@ pub type RegionOutlivesPredicate<'tcx> = OutlivesPredicate<ty::Region<'tcx>, ty:
497415
pub type TypeOutlivesPredicate<'tcx> = OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>;
498416
pub type PolyRegionOutlivesPredicate<'tcx> = ty::Binder<'tcx, RegionOutlivesPredicate<'tcx>>;
499417
pub type PolyTypeOutlivesPredicate<'tcx> = ty::Binder<'tcx, TypeOutlivesPredicate<'tcx>>;
500-
501418
pub type PolySubtypePredicate<'tcx> = ty::Binder<'tcx, SubtypePredicate<'tcx>>;
502-
503419
pub type PolyCoercePredicate<'tcx> = ty::Binder<'tcx, CoercePredicate<'tcx>>;
504-
505420
pub type PolyProjectionPredicate<'tcx> = Binder<'tcx, ProjectionPredicate<'tcx>>;
506421

507-
impl<'tcx> PolyProjectionPredicate<'tcx> {
508-
/// Returns the `DefId` of the trait of the associated item being projected.
509-
#[inline]
510-
pub fn trait_def_id(&self, tcx: TyCtxt<'tcx>) -> DefId {
511-
self.skip_binder().projection_term.trait_def_id(tcx)
512-
}
513-
514-
/// Get the [PolyTraitRef] required for this projection to be well formed.
515-
/// Note that for generic associated types the predicates of the associated
516-
/// type also need to be checked.
517-
#[inline]
518-
pub fn required_poly_trait_ref(&self, tcx: TyCtxt<'tcx>) -> PolyTraitRef<'tcx> {
519-
// Note: unlike with `TraitRef::to_poly_trait_ref()`,
520-
// `self.0.trait_ref` is permitted to have escaping regions.
521-
// This is because here `self` has a `Binder` and so does our
522-
// return value, so we are preserving the number of binding
523-
// levels.
524-
self.map_bound(|predicate| predicate.projection_term.trait_ref(tcx))
525-
}
526-
527-
pub fn term(&self) -> Binder<'tcx, Term<'tcx>> {
528-
self.map_bound(|predicate| predicate.term)
529-
}
530-
531-
/// The `DefId` of the `TraitItem` for the associated type.
532-
///
533-
/// Note that this is not the `DefId` of the `TraitRef` containing this
534-
/// associated type, which is in `tcx.associated_item(projection_def_id()).container`.
535-
pub fn projection_def_id(&self) -> DefId {
536-
// Ok to skip binder since trait `DefId` does not care about regions.
537-
self.skip_binder().projection_term.def_id
538-
}
539-
}
540-
541422
pub trait ToPolyTraitRef<'tcx> {
542423
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
543424
}

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

+5-14
Original file line numberDiff line numberDiff line change
@@ -2934,12 +2934,13 @@ impl<'tcx> ty::TraitRef<'tcx> {
29342934
}
29352935
}
29362936

2937+
#[extension(pub trait PrintPolyTraitRefExt<'tcx>)]
29372938
impl<'tcx> ty::Binder<'tcx, ty::TraitRef<'tcx>> {
2938-
pub fn print_only_trait_path(self) -> ty::Binder<'tcx, TraitRefPrintOnlyTraitPath<'tcx>> {
2939+
fn print_only_trait_path(self) -> ty::Binder<'tcx, TraitRefPrintOnlyTraitPath<'tcx>> {
29392940
self.map_bound(|tr| tr.print_only_trait_path())
29402941
}
29412942

2942-
pub fn print_trait_sugared(self) -> ty::Binder<'tcx, TraitRefPrintSugared<'tcx>> {
2943+
fn print_trait_sugared(self) -> ty::Binder<'tcx, TraitRefPrintSugared<'tcx>> {
29432944
self.map_bound(|tr| tr.print_trait_sugared())
29442945
}
29452946
}
@@ -2960,8 +2961,9 @@ impl<'tcx> ty::TraitPredicate<'tcx> {
29602961
}
29612962
}
29622963

2964+
#[extension(pub trait PrintPolyTraitPredicateExt<'tcx>)]
29632965
impl<'tcx> ty::PolyTraitPredicate<'tcx> {
2964-
pub fn print_modifiers_and_trait_path(
2966+
fn print_modifiers_and_trait_path(
29652967
self,
29662968
) -> ty::Binder<'tcx, TraitPredPrintModifiersAndPath<'tcx>> {
29672969
self.map_bound(TraitPredPrintModifiersAndPath)
@@ -3016,17 +3018,6 @@ forward_display_to_print! {
30163018
&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
30173019
ty::Const<'tcx>,
30183020

3019-
// HACK(eddyb) these are exhaustive instead of generic,
3020-
// because `for<'tcx>` isn't possible yet.
3021-
ty::PolyExistentialProjection<'tcx>,
3022-
ty::PolyExistentialTraitRef<'tcx>,
3023-
ty::Binder<'tcx, ty::TraitRef<'tcx>>,
3024-
ty::Binder<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
3025-
ty::Binder<'tcx, TraitRefPrintSugared<'tcx>>,
3026-
ty::Binder<'tcx, ty::FnSig<'tcx>>,
3027-
ty::Binder<'tcx, ty::TraitPredicate<'tcx>>,
3028-
ty::Binder<'tcx, TraitPredPrintModifiersAndPath<'tcx>>,
3029-
ty::Binder<'tcx, ty::ProjectionPredicate<'tcx>>,
30303021
ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>,
30313022
ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>
30323023
}

0 commit comments

Comments
 (0)