Skip to content

Commit

Permalink
Auto merge of rust-lang#128408 - Bryanskiy:delegation-perf, r=<try>
Browse files Browse the repository at this point in the history
Delegation: querify `sig_id` getter

Possible perf fix for rust-lang#125929

r? `@petrochenkov`
  • Loading branch information
bors committed Jul 30, 2024
2 parents f8060d2 + 93a7133 commit d3a48a6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ 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)
if let Some(sig_id) = tcx.opt_delegation_sig_id(def_id)
&& let Some(generics) = inherit_generics_for_delegation_item(tcx, def_id, sig_id)
{
return generics;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
}

// For a delegation item inherit predicates from callee.
if let Some(sig_id) = tcx.hir().opt_delegation_sig_id(def_id)
if let Some(sig_id) = tcx.opt_delegation_sig_id(def_id)
&& let Some(predicates) = inherit_predicates_for_delegation_item(tcx, def_id, sig_id)
{
return predicates;
Expand Down
20 changes: 18 additions & 2 deletions compiler/rustc_hir_analysis/src/delegation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::query::Providers;
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::ErrorGuaranteed;
Expand Down Expand Up @@ -217,7 +219,7 @@ fn check_constraints<'tcx>(
}

if let Some(local_sig_id) = sig_id.as_local()
&& tcx.hir().opt_delegation_sig_id(local_sig_id).is_some()
&& tcx.opt_delegation_sig_id(local_sig_id).is_some()
{
emit("recursive delegation is not supported yet");
}
Expand All @@ -242,7 +244,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.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;
Expand All @@ -257,3 +259,17 @@ pub(crate) fn inherit_sig_for_delegation_item<'tcx>(
let sig_iter = sig.inputs().iter().cloned().chain(std::iter::once(sig.output()));
tcx.arena.alloc_from_iter(sig_iter)
}

pub(crate) fn opt_delegation_sig_id<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Option<DefId> {
if let Some(ret) = tcx.hir().get_fn_output(def_id)
&& let hir::FnRetTy::Return(ty) = ret
&& let hir::TyKind::InferDelegation(sig_id, _) = ty.kind
{
return Some(sig_id);
}
None
}

pub fn provide(providers: &mut Providers) {
*providers = Providers { inherit_sig_for_delegation_item, opt_delegation_sig_id, ..*providers };
}
5 changes: 1 addition & 4 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ pub fn provide(providers: &mut Providers) {
variance::provide(providers);
outlives::provide(providers);
hir_wf_check::provide(providers);
*providers = Providers {
inherit_sig_for_delegation_item: delegation::inherit_sig_for_delegation_item,
..*providers
};
delegation::provide(providers);
}

pub fn check_crate(tcx: TyCtxt<'_>) {
Expand Down
15 changes: 0 additions & 15 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,21 +746,6 @@ impl<'hir> Map<'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()
}

#[inline]
fn opt_ident(self, id: HirId) -> Option<Ident> {
match self.tcx.hir_node(id) {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,10 @@ rustc_queries! {
desc { |tcx| "getting the native library for `{}`", tcx.def_path_str(def_id) }
}

query opt_delegation_sig_id(def_id: LocalDefId) -> Option<DefId> {
desc { "getting the item from which the signature is inherited when delegating" }
}

query inherit_sig_for_delegation_item(def_id: LocalDefId) -> &'tcx [Ty<'tcx>] {
desc { "inheriting delegation signature" }
}
Expand Down

0 comments on commit d3a48a6

Please sign in to comment.