1
Fork 0

Auto merge of #126813 - compiler-errors:SliceLike, r=lcnr

Add `SliceLike` to `rustc_type_ir`, use it in the generic solver code (+ some other changes)

First, we split out `TraitRef::new_from_args` which takes *just* `ty::GenericArgsRef` from `TraitRef::new` which takes `impl IntoIterator<Item: Into<GenericArg>>`. I will explain in a minute why.

Second, we introduce `SliceLike`, which allows us to be generic over `List<T>` and `[T]`. This trait has an `as_slice()` and `into_iter()` method, and some other convenience functions. However, importantly, since types like `I::GenericArgs` now implement `SliceLike` rather than `IntoIter<Item = I::GenericArg>`, we can't use `TraitRef::new` on this directly. That's where `new_from_args` comes in.

Finally, we adjust all the code to use these slice operators. Some things get simpler, some things get a bit more annoying since we need to use `as_slice()` in a few places. 🤷

r? lcnr
This commit is contained in:
bors 2024-06-25 00:33:49 +00:00
commit 5b270e1198
52 changed files with 378 additions and 254 deletions

View file

@ -240,7 +240,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
let trait_generics = self.generics_of(trait_def_id);
(
ty::TraitRef::new(self, trait_def_id, args.truncate_to(self, trait_generics)),
ty::TraitRef::new_from_args(self, trait_def_id, args.truncate_to(self, trait_generics)),
&args[trait_generics.count()..],
)
}
@ -261,12 +261,8 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
self.check_args_compatible(def_id, args)
}
fn check_and_mk_args(
self,
def_id: DefId,
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
) -> ty::GenericArgsRef<'tcx> {
self.check_and_mk_args(def_id, args)
fn debug_assert_args_compatible(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) {
self.debug_assert_args_compatible(def_id, args);
}
fn intern_canonical_goal_evaluation_step(

View file

@ -133,6 +133,20 @@ impl<H, T> RawList<H, T> {
}
}
impl<'a, H, T: Copy> rustc_type_ir::inherent::SliceLike for &'a RawList<H, T> {
type Item = T;
type IntoIter = iter::Copied<<&'a [T] as IntoIterator>::IntoIter>;
fn iter(self) -> Self::IntoIter {
(*self).iter()
}
fn as_slice(&self) -> &[Self::Item] {
(*self).as_slice()
}
}
macro_rules! impl_list_empty {
($header_ty:ty, $header_init:expr) => {
impl<T> RawList<$header_ty, T> {

View file

@ -499,7 +499,7 @@ impl<'tcx> Ty<'tcx> {
#[inline]
pub fn new_opaque(tcx: TyCtxt<'tcx>, def_id: DefId, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
Ty::new_alias(tcx, ty::Opaque, AliasTy::new(tcx, def_id, args))
Ty::new_alias(tcx, ty::Opaque, AliasTy::new_from_args(tcx, def_id, args))
}
/// Constructs a `TyKind::Error` type with current `ErrorGuaranteed`
@ -669,6 +669,15 @@ impl<'tcx> Ty<'tcx> {
Ty::new(tcx, Dynamic(obj, reg, repr))
}
#[inline]
pub fn new_projection_from_args(
tcx: TyCtxt<'tcx>,
item_def_id: DefId,
args: ty::GenericArgsRef<'tcx>,
) -> Ty<'tcx> {
Ty::new_alias(tcx, ty::Projection, AliasTy::new_from_args(tcx, item_def_id, args))
}
#[inline]
pub fn new_projection(
tcx: TyCtxt<'tcx>,
@ -1409,7 +1418,7 @@ impl<'tcx> Ty<'tcx> {
let assoc_items = tcx.associated_item_def_ids(
tcx.require_lang_item(hir::LangItem::DiscriminantKind, None),
);
Ty::new_projection(tcx, assoc_items[0], tcx.mk_args(&[self.into()]))
Ty::new_projection_from_args(tcx, assoc_items[0], tcx.mk_args(&[self.into()]))
}
ty::Pat(ty, _) => ty.discriminant_ty(tcx),