Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 552a694

Browse files
committedJul 31, 2024·
Auto merge of rust-lang#128441 - Bryanskiy:delegation-perf, r=<try>
Delegation: second attempt to improve perf Possible perf fix for rust-lang#125929 r? `@petrochenkov`
2 parents 0b5eb7b + 9b097b2 commit 552a694

File tree

5 files changed

+33
-27
lines changed

5 files changed

+33
-27
lines changed
 

‎compiler/rustc_hir/src/hir.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2952,6 +2952,17 @@ pub struct FnDecl<'hir> {
29522952
pub lifetime_elision_allowed: bool,
29532953
}
29542954

2955+
impl<'hir> FnDecl<'hir> {
2956+
pub fn opt_delegation_sig_id(&self) -> Option<DefId> {
2957+
if let FnRetTy::Return(ty) = self.output
2958+
&& let TyKind::InferDelegation(sig_id, _) = ty.kind
2959+
{
2960+
return Some(sig_id);
2961+
}
2962+
None
2963+
}
2964+
}
2965+
29552966
/// Represents what type of implicit self a function has, if any.
29562967
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
29572968
pub enum ImplicitSelfKind {

‎compiler/rustc_hir_analysis/src/collect/generics_of.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
5454
};
5555
}
5656

57-
// For a delegation item inherit generics from callee.
58-
if let Some(sig_id) = tcx.hir().opt_delegation_sig_id(def_id)
59-
&& let Some(generics) = inherit_generics_for_delegation_item(tcx, def_id, sig_id)
60-
{
61-
return generics;
62-
}
63-
6457
let hir_id = tcx.local_def_id_to_hir_id(def_id);
6558

6659
let node = tcx.hir_node(hir_id);
@@ -234,6 +227,16 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
234227
// inherit the generics of the item.
235228
Some(parent.to_def_id())
236229
}
230+
ItemKind::Fn(sig, _, _) => {
231+
// For a delegation item inherit generics from callee.
232+
if let Some(sig_id) = sig.decl.opt_delegation_sig_id()
233+
&& let Some(generics) =
234+
inherit_generics_for_delegation_item(tcx, def_id, sig_id)
235+
{
236+
return generics;
237+
}
238+
None
239+
}
237240
_ => None,
238241
},
239242
_ => None,

‎compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,6 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
115115
None => {}
116116
}
117117

118-
// For a delegation item inherit predicates from callee.
119-
if let Some(sig_id) = tcx.hir().opt_delegation_sig_id(def_id)
120-
&& let Some(predicates) = inherit_predicates_for_delegation_item(tcx, def_id, sig_id)
121-
{
122-
return predicates;
123-
}
124-
125118
let hir_id = tcx.local_def_id_to_hir_id(def_id);
126119
let node = tcx.hir_node(hir_id);
127120

@@ -151,6 +144,16 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
151144
ItemKind::Trait(_, _, _, self_bounds, ..) | ItemKind::TraitAlias(_, self_bounds) => {
152145
is_trait = Some(self_bounds);
153146
}
147+
148+
ItemKind::Fn(sig, _, _) => {
149+
// For a delegation item inherit predicates from callee.
150+
if let Some(sig_id) = sig.decl.opt_delegation_sig_id()
151+
&& let Some(predicates) =
152+
inherit_predicates_for_delegation_item(tcx, def_id, sig_id)
153+
{
154+
return predicates;
155+
}
156+
}
154157
_ => {}
155158
}
156159
};

‎compiler/rustc_hir_analysis/src/delegation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub(crate) fn inherit_sig_for_delegation_item<'tcx>(
242242
tcx: TyCtxt<'tcx>,
243243
def_id: LocalDefId,
244244
) -> &'tcx [Ty<'tcx>] {
245-
let sig_id = tcx.hir().delegation_sig_id(def_id);
245+
let sig_id = tcx.hir().opt_delegation_sig_id(def_id).unwrap();
246246
let caller_sig = tcx.fn_sig(sig_id);
247247
if let Err(err) = check_constraints(tcx, def_id, sig_id) {
248248
let sig_len = caller_sig.instantiate_identity().skip_binder().inputs().len() + 1;

‎compiler/rustc_middle/src/hir/map/mod.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -747,18 +747,7 @@ impl<'hir> Map<'hir> {
747747
}
748748

749749
pub fn opt_delegation_sig_id(self, def_id: LocalDefId) -> Option<DefId> {
750-
if let Some(ret) = self.get_fn_output(def_id)
751-
&& let FnRetTy::Return(ty) = ret
752-
&& let TyKind::InferDelegation(sig_id, _) = ty.kind
753-
{
754-
return Some(sig_id);
755-
}
756-
None
757-
}
758-
759-
#[inline]
760-
pub fn delegation_sig_id(self, def_id: LocalDefId) -> DefId {
761-
self.opt_delegation_sig_id(def_id).unwrap()
750+
self.tcx.opt_hir_owner_node(def_id)?.fn_decl()?.opt_delegation_sig_id()
762751
}
763752

764753
#[inline]

0 commit comments

Comments
 (0)
Please sign in to comment.