Skip to content

Commit c7e6acc

Browse files
Auto merge of #142289 - fmease:maybe-perf-gen-args, r=<try>
[perf] Tweak `GenericArgs`-related functions
2 parents 40daf23 + 5ed5135 commit c7e6acc

File tree

6 files changed

+50
-17
lines changed

6 files changed

+50
-17
lines changed

compiler/rustc_hir_analysis/src/delegation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ fn build_predicates<'tcx>(
233233
ty::GenericPredicates { parent, predicates: tcx.arena.alloc_from_iter(preds) }
234234
}
235235

236+
// NOTE(fmease): Isn't this just a full reimplementation of `EarlyBinder::instantiate`? Investigate.
236237
fn build_generic_args<'tcx>(
237238
tcx: TyCtxt<'tcx>,
238239
sig_id: DefId,
@@ -276,8 +277,7 @@ fn create_generic_args<'tcx>(
276277
tcx.impl_trait_header(parent).unwrap().trait_ref.instantiate_identity().args;
277278

278279
let trait_args = ty::GenericArgs::identity_for_item(tcx, sig_id);
279-
let method_args =
280-
tcx.mk_args_from_iter(trait_args.iter().skip(callee_generics.parent_count));
280+
let method_args = tcx.mk_args(&trait_args[callee_generics.parent_count..]);
281281
let method_args = build_generic_args(tcx, sig_id, def_id, method_args);
282282

283283
tcx.mk_args_from_iter(parent_args.iter().chain(method_args))

compiler/rustc_middle/src/ty/context.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
pub mod tls;
66

7-
use std::assert_matches::{assert_matches, debug_assert_matches};
7+
use std::assert_matches::debug_assert_matches;
88
use std::borrow::Borrow;
99
use std::cmp::Ordering;
1010
use std::env::VarError;
@@ -263,14 +263,35 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
263263
}
264264
}
265265

266+
fn trait_ref_for_alias(
267+
self,
268+
def_id: DefId,
269+
args: ty::GenericArgsRef<'tcx>,
270+
) -> ty::TraitRef<'tcx> {
271+
debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
272+
let trait_def_id = self.parent(def_id);
273+
debug_assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
274+
let trait_generics = self.generics_of(trait_def_id);
275+
ty::TraitRef::new_from_args(self, trait_def_id, args.truncate_to(self, trait_generics))
276+
}
277+
278+
fn own_args_for_alias(
279+
self,
280+
def_id: DefId,
281+
args: ty::GenericArgsRef<'tcx>,
282+
) -> &'tcx [ty::GenericArg<'tcx>] {
283+
debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
284+
&args[self.generics_of(def_id).parent_count..]
285+
}
286+
266287
fn trait_ref_and_own_args_for_alias(
267288
self,
268289
def_id: DefId,
269290
args: ty::GenericArgsRef<'tcx>,
270291
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
271-
assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
292+
debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
272293
let trait_def_id = self.parent(def_id);
273-
assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
294+
debug_assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
274295
let trait_generics = self.generics_of(trait_def_id);
275296
(
276297
ty::TraitRef::new_from_args(self, trait_def_id, args.truncate_to(self, trait_generics)),

compiler/rustc_middle/src/ty/generic_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ impl<'tcx> GenericArgs<'tcx> {
588588
}
589589

590590
pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> GenericArgsRef<'tcx> {
591-
tcx.mk_args_from_iter(self.iter().take(generics.count()))
591+
tcx.mk_args(&self[..generics.count()])
592592
}
593593

594594
pub fn print_as_list(&self) -> String {

compiler/rustc_type_ir/src/interner.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ pub trait Interner:
176176

177177
fn alias_term_kind(self, alias: ty::AliasTerm<Self>) -> ty::AliasTermKind;
178178

179+
fn trait_ref_for_alias(
180+
self,
181+
def_id: Self::DefId,
182+
args: Self::GenericArgs,
183+
) -> ty::TraitRef<Self>;
184+
185+
fn own_args_for_alias(
186+
self,
187+
def_id: Self::DefId,
188+
args: Self::GenericArgs,
189+
) -> Self::GenericArgsSlice;
190+
179191
fn trait_ref_and_own_args_for_alias(
180192
self,
181193
def_id: Self::DefId,

compiler/rustc_type_ir/src/predicate.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -664,14 +664,6 @@ impl<I: Interner> AliasTerm<I> {
664664
interner.parent(self.def_id)
665665
}
666666

667-
/// Extracts the underlying trait reference and own args from this projection.
668-
/// For example, if this is a projection of `<T as StreamingIterator>::Item<'a>`,
669-
/// then this function would return a `T: StreamingIterator` trait reference and
670-
/// `['a]` as the own args.
671-
pub fn trait_ref_and_own_args(self, interner: I) -> (TraitRef<I>, I::GenericArgsSlice) {
672-
interner.trait_ref_and_own_args_for_alias(self.def_id, self.args)
673-
}
674-
675667
/// Extracts the underlying trait reference from this projection.
676668
/// For example, if this is a projection of `<T as Iterator>::Item`,
677669
/// then this function would return a `T: Iterator` trait reference.
@@ -680,14 +672,22 @@ impl<I: Interner> AliasTerm<I> {
680672
/// consider calling [Self::trait_ref_and_own_args] to get those
681673
/// as well.
682674
pub fn trait_ref(self, interner: I) -> TraitRef<I> {
683-
self.trait_ref_and_own_args(interner).0
675+
interner.trait_ref_for_alias(self.def_id, self.args)
684676
}
685677

686678
/// Extract the own args from this projection.
687679
/// For example, if this is a projection of `<T as StreamingIterator>::Item<'a>`,
688680
/// then this function would return the slice `['a]` as the own args.
689681
pub fn own_args(self, interner: I) -> I::GenericArgsSlice {
690-
self.trait_ref_and_own_args(interner).1
682+
interner.own_args_for_alias(self.def_id, self.args)
683+
}
684+
685+
/// Extracts the underlying trait reference and own args from this projection.
686+
/// For example, if this is a projection of `<T as StreamingIterator>::Item<'a>`,
687+
/// then this function would return a `T: StreamingIterator` trait reference and
688+
/// `['a]` as the own args.
689+
pub fn trait_ref_and_own_args(self, interner: I) -> (TraitRef<I>, I::GenericArgsSlice) {
690+
interner.trait_ref_and_own_args_for_alias(self.def_id, self.args)
691691
}
692692
}
693693

compiler/rustc_type_ir/src/ty_kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ impl<I: Interner> AliasTy<I> {
510510
/// consider calling [Self::trait_ref_and_own_args] to get those
511511
/// as well.
512512
pub fn trait_ref(self, interner: I) -> ty::TraitRef<I> {
513-
self.trait_ref_and_own_args(interner).0
513+
interner.trait_ref_for_alias(self.def_id, self.args)
514514
}
515515
}
516516

0 commit comments

Comments
 (0)