Auto merge of #128441 - Bryanskiy:delegation-perf, r=petrochenkov

Delegation: second attempt to improve perf

Possible perf fix for https://github.com/rust-lang/rust/pull/125929

r? `@petrochenkov`
This commit is contained in:
bors 2024-08-03 23:45:22 +00:00
commit 8f63e9f873
5 changed files with 33 additions and 27 deletions

View File

@ -2952,6 +2952,17 @@ pub struct FnDecl<'hir> {
pub lifetime_elision_allowed: bool,
}
impl<'hir> FnDecl<'hir> {
pub fn opt_delegation_sig_id(&self) -> Option<DefId> {
if let FnRetTy::Return(ty) = self.output
&& let TyKind::InferDelegation(sig_id, _) = ty.kind
{
return Some(sig_id);
}
None
}
}
/// Represents what type of implicit self a function has, if any.
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
pub enum ImplicitSelfKind {

View File

@ -54,13 +54,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
};
}
// For a delegation item inherit generics from callee.
if let Some(sig_id) = tcx.hir().opt_delegation_sig_id(def_id)
&& let Some(generics) = inherit_generics_for_delegation_item(tcx, def_id, sig_id)
{
return generics;
}
let hir_id = tcx.local_def_id_to_hir_id(def_id);
let node = tcx.hir_node(hir_id);
@ -234,6 +227,16 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
// inherit the generics of the item.
Some(parent.to_def_id())
}
ItemKind::Fn(sig, _, _) => {
// For a delegation item inherit generics from callee.
if let Some(sig_id) = sig.decl.opt_delegation_sig_id()
&& let Some(generics) =
inherit_generics_for_delegation_item(tcx, def_id, sig_id)
{
return generics;
}
None
}
_ => None,
},
_ => None,

View File

@ -115,13 +115,6 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
None => {}
}
// For a delegation item inherit predicates from callee.
if let Some(sig_id) = tcx.hir().opt_delegation_sig_id(def_id)
&& let Some(predicates) = inherit_predicates_for_delegation_item(tcx, def_id, sig_id)
{
return predicates;
}
let hir_id = tcx.local_def_id_to_hir_id(def_id);
let node = tcx.hir_node(hir_id);
@ -151,6 +144,16 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
ItemKind::Trait(_, _, _, self_bounds, ..) | ItemKind::TraitAlias(_, self_bounds) => {
is_trait = Some(self_bounds);
}
ItemKind::Fn(sig, _, _) => {
// For a delegation item inherit predicates from callee.
if let Some(sig_id) = sig.decl.opt_delegation_sig_id()
&& let Some(predicates) =
inherit_predicates_for_delegation_item(tcx, def_id, sig_id)
{
return predicates;
}
}
_ => {}
}
};

View File

@ -242,7 +242,7 @@ pub(crate) fn inherit_sig_for_delegation_item<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
) -> &'tcx [Ty<'tcx>] {
let sig_id = tcx.hir().delegation_sig_id(def_id);
let sig_id = tcx.hir().opt_delegation_sig_id(def_id).unwrap();
let caller_sig = tcx.fn_sig(sig_id);
if let Err(err) = check_constraints(tcx, def_id, sig_id) {
let sig_len = caller_sig.instantiate_identity().skip_binder().inputs().len() + 1;

View File

@ -747,18 +747,7 @@ pub fn expect_expr(self, id: HirId) -> &'hir Expr<'hir> {
}
pub fn opt_delegation_sig_id(self, def_id: LocalDefId) -> Option<DefId> {
if let Some(ret) = self.get_fn_output(def_id)
&& let FnRetTy::Return(ty) = ret
&& let TyKind::InferDelegation(sig_id, _) = ty.kind
{
return Some(sig_id);
}
None
}
#[inline]
pub fn delegation_sig_id(self, def_id: LocalDefId) -> DefId {
self.opt_delegation_sig_id(def_id).unwrap()
self.tcx.opt_hir_owner_node(def_id)?.fn_decl()?.opt_delegation_sig_id()
}
#[inline]