Skip to content

Commit c6a4dbe

Browse files
committed
add Self type into whereclause
1 parent 6bb470e commit c6a4dbe

File tree

7 files changed

+24
-44
lines changed

7 files changed

+24
-44
lines changed

compiler/rustc_middle/src/ty/sty.rs

+15-26
Original file line numberDiff line numberDiff line change
@@ -963,20 +963,13 @@ pub struct ExistentialTraitRef<'tcx> {
963963
pub substs: SubstsRef<'tcx>,
964964
}
965965

966-
impl<'tcx> ExistentialTraitRef<'tcx> {
967-
pub fn erase_self_ty(
968-
tcx: TyCtxt<'tcx>,
969-
trait_ref: ty::TraitRef<'tcx>,
970-
) -> ty::ExistentialTraitRef<'tcx> {
971-
// Assert there is a Self.
972-
trait_ref.substs.type_at(0);
973-
974-
ty::ExistentialTraitRef {
975-
def_id: trait_ref.def_id,
976-
substs: tcx.intern_substs(&trait_ref.substs[1..]),
977-
}
966+
impl From<TraitRef<'tcx>> for ExistentialTraitRef<'tcx> {
967+
fn from(trait_ref: TraitRef<'tcx>) -> Self {
968+
Self { def_id: trait_ref.def_id, substs: trait_ref.substs }
978969
}
970+
}
979971

972+
impl<'tcx> ExistentialTraitRef<'tcx> {
980973
/// Object types don't have a self type specified. Therefore, when
981974
/// we convert the principal trait-ref into a normal trait-ref,
982975
/// you must give *some* self type. A common choice is `mk_err()`
@@ -1532,6 +1525,16 @@ pub struct ExistentialProjection<'tcx> {
15321525
pub ty: Ty<'tcx>,
15331526
}
15341527

1528+
impl From<ty::ProjectionPredicate<'tcx>> for ExistentialProjection<'tcx> {
1529+
fn from(projection_predicate: ty::ProjectionPredicate<'tcx>) -> Self {
1530+
Self {
1531+
item_def_id: projection_predicate.projection_ty.item_def_id,
1532+
substs: projection_predicate.projection_ty.substs,
1533+
ty: projection_predicate.ty,
1534+
}
1535+
}
1536+
}
1537+
15351538
pub type PolyExistentialProjection<'tcx> = Binder<'tcx, ExistentialProjection<'tcx>>;
15361539

15371540
impl<'tcx> ExistentialProjection<'tcx> {
@@ -1562,20 +1565,6 @@ impl<'tcx> ExistentialProjection<'tcx> {
15621565
ty: self.ty,
15631566
}
15641567
}
1565-
1566-
pub fn erase_self_ty(
1567-
tcx: TyCtxt<'tcx>,
1568-
projection_predicate: ty::ProjectionPredicate<'tcx>,
1569-
) -> Self {
1570-
// Assert there is a Self.
1571-
projection_predicate.projection_ty.substs.type_at(0);
1572-
1573-
Self {
1574-
item_def_id: projection_predicate.projection_ty.item_def_id,
1575-
substs: tcx.intern_substs(&projection_predicate.projection_ty.substs[1..]),
1576-
ty: projection_predicate.ty,
1577-
}
1578-
}
15791568
}
15801569

15811570
impl<'tcx> PolyExistentialProjection<'tcx> {

compiler/rustc_middle/src/ty/vtable.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ pub(super) fn vtable_allocation_provider<'tcx>(
7676
// No need to do any alignment checks on the memory accesses below, because we know the
7777
// allocation is correctly aligned as we created it above. Also we're only offsetting by
7878
// multiples of `ptr_align`, which means that it will stay aligned to `ptr_align`.
79-
8079
for (idx, entry) in vtable_entries.iter().enumerate() {
8180
let idx: u64 = u64::try_from(idx).unwrap();
8281
let scalar = match entry {
@@ -97,8 +96,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
9796
ScalarMaybeUninit::from_pointer(fn_ptr, &tcx)
9897
}
9998
VtblEntry::TraitVPtr(trait_ref) => {
100-
let super_trait_ref = trait_ref
101-
.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
99+
let super_trait_ref = trait_ref.map_bound(|trait_ref| trait_ref.into());
102100
let supertrait_alloc_id = tcx.vtable_allocation((ty, Some(super_trait_ref)));
103101
let vptr = Pointer::from(supertrait_alloc_id);
104102
ScalarMaybeUninit::from_pointer(vptr, &tcx)

compiler/rustc_trait_selection/src/traits/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,7 @@ fn vtable_entries<'tcx>(
675675
entries.extend(COMMON_VTABLE_ENTRIES);
676676
}
677677
VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
678-
let existential_trait_ref = trait_ref
679-
.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
678+
let existential_trait_ref = trait_ref.map_bound(|trait_ref| trait_ref.into());
680679

681680
// Lookup the shape of vtable for the trait.
682681
let own_existential_entries =

compiler/rustc_trait_selection/src/traits/object_safety.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,7 @@ fn object_ty_for_trait<'tcx>(
554554

555555
let trait_ref = ty::TraitRef::identity(tcx, trait_def_id);
556556

557-
let trait_predicate = trait_ref.map_bound(|trait_ref| {
558-
ty::WhereClause::Trait(ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref))
559-
});
557+
let trait_predicate = trait_ref.map_bound(|trait_ref| ty::WhereClause::Trait(trait_ref.into()));
560558

561559
let mut associated_types = traits::supertraits(tcx, trait_ref)
562560
.flat_map(|super_trait_ref| {

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
736736
source_trait_ref = principal_a.with_self_ty(tcx, source);
737737
upcast_trait_ref = util::supertraits(tcx, source_trait_ref).nth(idx).unwrap();
738738
assert_eq!(data_b.principal_def_id(), Some(upcast_trait_ref.def_id()));
739-
let existential_predicate = upcast_trait_ref.map_bound(|trait_ref| {
740-
ty::WhereClause::Trait(ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref))
741-
});
739+
let existential_predicate = upcast_trait_ref
740+
.map_bound(|trait_ref| ty::WhereClause::Trait(trait_ref.into()));
742741
let iter = Some(existential_predicate)
743742
.into_iter()
744743
.chain(

compiler/rustc_trait_selection/src/traits/util.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ pub fn upcast_choices(
285285
/// that come from `trait_ref`, excluding its supertraits. Used in
286286
/// computing the vtable base for an upcast trait of a trait object.
287287
pub fn count_own_vtable_entries(tcx: TyCtxt<'tcx>, trait_ref: ty::PolyTraitRef<'tcx>) -> usize {
288-
let existential_trait_ref =
289-
trait_ref.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
288+
let existential_trait_ref = trait_ref.map_bound(|trait_ref| trait_ref.into());
290289
let existential_trait_ref = tcx.erase_regions(existential_trait_ref);
291290
tcx.own_existential_vtable_entries(existential_trait_ref).len()
292291
}
@@ -299,9 +298,7 @@ pub fn get_vtable_index_of_object_method<N>(
299298
object: &super::ImplSourceObjectData<'tcx, N>,
300299
method_def_id: DefId,
301300
) -> usize {
302-
let existential_trait_ref = object
303-
.upcast_trait_ref
304-
.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
301+
let existential_trait_ref = object.upcast_trait_ref.map_bound(|trait_ref| trait_ref.into());
305302
let existential_trait_ref = tcx.erase_regions(existential_trait_ref);
306303
// Count number of methods preceding the one we are selecting and
307304
// add them to the total offset.

compiler/rustc_typeck/src/astconv/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14581458
),
14591459
);
14601460
}
1461-
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
1461+
trait_ref.into()
14621462
})
14631463
});
14641464
let existential_projections = bounds.projection_bounds.iter().map(|(bound, _)| {
@@ -1469,7 +1469,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14691469
&format!("trait_ref_to_existential called on {:?} with non-dummy Self", b),
14701470
);
14711471
}
1472-
ty::ExistentialProjection::erase_self_ty(tcx, b)
1472+
b.into()
14731473
})
14741474
});
14751475

0 commit comments

Comments
 (0)