Skip to content

Commit d3a48a6

Browse files
committed
Auto merge of #128408 - Bryanskiy:delegation-perf, r=<try>
Delegation: querify `sig_id` getter Possible perf fix for #125929 r? `@petrochenkov`
2 parents f8060d2 + 93a7133 commit d3a48a6

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
5555
}
5656

5757
// For a delegation item inherit generics from callee.
58-
if let Some(sig_id) = tcx.hir().opt_delegation_sig_id(def_id)
58+
if let Some(sig_id) = tcx.opt_delegation_sig_id(def_id)
5959
&& let Some(generics) = inherit_generics_for_delegation_item(tcx, def_id, sig_id)
6060
{
6161
return generics;

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
116116
}
117117

118118
// For a delegation item inherit predicates from callee.
119-
if let Some(sig_id) = tcx.hir().opt_delegation_sig_id(def_id)
119+
if let Some(sig_id) = tcx.opt_delegation_sig_id(def_id)
120120
&& let Some(predicates) = inherit_predicates_for_delegation_item(tcx, def_id, sig_id)
121121
{
122122
return predicates;

compiler/rustc_hir_analysis/src/delegation.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use rustc_data_structures::fx::FxHashMap;
2+
use rustc_hir as hir;
23
use rustc_hir::def::DefKind;
34
use rustc_hir::def_id::{DefId, LocalDefId};
5+
use rustc_middle::query::Providers;
46
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
57
use rustc_middle::ty::{self, Ty, TyCtxt};
68
use rustc_span::ErrorGuaranteed;
@@ -217,7 +219,7 @@ fn check_constraints<'tcx>(
217219
}
218220

219221
if let Some(local_sig_id) = sig_id.as_local()
220-
&& tcx.hir().opt_delegation_sig_id(local_sig_id).is_some()
222+
&& tcx.opt_delegation_sig_id(local_sig_id).is_some()
221223
{
222224
emit("recursive delegation is not supported yet");
223225
}
@@ -242,7 +244,7 @@ pub(crate) fn inherit_sig_for_delegation_item<'tcx>(
242244
tcx: TyCtxt<'tcx>,
243245
def_id: LocalDefId,
244246
) -> &'tcx [Ty<'tcx>] {
245-
let sig_id = tcx.hir().delegation_sig_id(def_id);
247+
let sig_id = tcx.opt_delegation_sig_id(def_id).unwrap();
246248
let caller_sig = tcx.fn_sig(sig_id);
247249
if let Err(err) = check_constraints(tcx, def_id, sig_id) {
248250
let sig_len = caller_sig.instantiate_identity().skip_binder().inputs().len() + 1;
@@ -257,3 +259,17 @@ pub(crate) fn inherit_sig_for_delegation_item<'tcx>(
257259
let sig_iter = sig.inputs().iter().cloned().chain(std::iter::once(sig.output()));
258260
tcx.arena.alloc_from_iter(sig_iter)
259261
}
262+
263+
pub(crate) fn opt_delegation_sig_id<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Option<DefId> {
264+
if let Some(ret) = tcx.hir().get_fn_output(def_id)
265+
&& let hir::FnRetTy::Return(ty) = ret
266+
&& let hir::TyKind::InferDelegation(sig_id, _) = ty.kind
267+
{
268+
return Some(sig_id);
269+
}
270+
None
271+
}
272+
273+
pub fn provide(providers: &mut Providers) {
274+
*providers = Providers { inherit_sig_for_delegation_item, opt_delegation_sig_id, ..*providers };
275+
}

compiler/rustc_hir_analysis/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ pub fn provide(providers: &mut Providers) {
147147
variance::provide(providers);
148148
outlives::provide(providers);
149149
hir_wf_check::provide(providers);
150-
*providers = Providers {
151-
inherit_sig_for_delegation_item: delegation::inherit_sig_for_delegation_item,
152-
..*providers
153-
};
150+
delegation::provide(providers);
154151
}
155152

156153
pub fn check_crate(tcx: TyCtxt<'_>) {

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

-15
Original file line numberDiff line numberDiff line change
@@ -746,21 +746,6 @@ impl<'hir> Map<'hir> {
746746
}
747747
}
748748

749-
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()
762-
}
763-
764749
#[inline]
765750
fn opt_ident(self, id: HirId) -> Option<Ident> {
766751
match self.tcx.hir_node(id) {

compiler/rustc_middle/src/query/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,10 @@ rustc_queries! {
17221722
desc { |tcx| "getting the native library for `{}`", tcx.def_path_str(def_id) }
17231723
}
17241724

1725+
query opt_delegation_sig_id(def_id: LocalDefId) -> Option<DefId> {
1726+
desc { "getting the item from which the signature is inherited when delegating" }
1727+
}
1728+
17251729
query inherit_sig_for_delegation_item(def_id: LocalDefId) -> &'tcx [Ty<'tcx>] {
17261730
desc { "inheriting delegation signature" }
17271731
}

0 commit comments

Comments
 (0)