Skip to content

Commit c55b9cf

Browse files
committed
Introduce trait_item_of
1 parent 2e2642e commit c55b9cf

File tree

11 files changed

+29
-39
lines changed

11 files changed

+29
-39
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
688688
return (false, false, None);
689689
};
690690

691-
let implemented_trait_item = self.infcx.tcx.associated_item(my_def).trait_item_def_id;
691+
let implemented_trait_item = self.infcx.tcx.trait_item_of(my_def);
692692

693693
(
694694
true,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -576,26 +576,18 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
576576
codegen_fn_attrs
577577
}
578578

579-
/// If the provided DefId is a method in a trait impl, return the DefId of the method prototype.
580-
fn opt_trait_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
581-
let impl_item = tcx.opt_associated_item(def_id)?;
582-
match impl_item.container {
583-
ty::AssocItemContainer::Impl => impl_item.trait_item_def_id,
584-
_ => None,
585-
}
586-
}
587-
588579
/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
589580
/// applied to the method prototype.
590581
fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
591-
let Some(trait_item) = opt_trait_item(tcx, def_id) else { return false };
592-
tcx.codegen_fn_attrs(trait_item).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
582+
tcx.trait_item_of(def_id).is_some_and(|id| {
583+
tcx.codegen_fn_attrs(id).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
584+
})
593585
}
594586

595587
/// If the provided DefId is a method in a trait impl, return the value of the `#[align]`
596588
/// attribute on the method prototype (if any).
597589
fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> {
598-
tcx.codegen_fn_attrs(opt_trait_item(tcx, def_id)?).alignment
590+
tcx.codegen_fn_attrs(tcx.trait_item_of(def_id)?).alignment
599591
}
600592

601593
/// We now check the #\[rustc_autodiff\] attributes which we generated from the #[autodiff(...)]

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,8 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
111111
}
112112

113113
Some(ImplTraitInTraitData::Impl { fn_def_id }) => {
114-
let assoc_item = tcx.associated_item(def_id);
115-
let trait_assoc_predicates =
116-
tcx.explicit_predicates_of(assoc_item.trait_item_def_id.unwrap());
114+
let trait_item_def_id = tcx.trait_item_of(def_id).unwrap();
115+
let trait_assoc_predicates = tcx.explicit_predicates_of(trait_item_def_id);
117116

118117
let impl_assoc_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
119118
let impl_def_id = tcx.parent(fn_def_id);

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
125125
Some(ty::ImplTraitInTraitData::Impl { fn_def_id }) => {
126126
match tcx.collect_return_position_impl_trait_in_trait_tys(fn_def_id) {
127127
Ok(map) => {
128-
let assoc_item = tcx.associated_item(def_id);
129-
return map[&assoc_item.trait_item_def_id.unwrap()];
128+
let trait_item_def_id = tcx.trait_item_of(def_id).unwrap();
129+
return map[&trait_item_def_id];
130130
}
131131
Err(_) => {
132132
return ty::EarlyBinder::bind(Ty::new_error_with_message(

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,15 @@ impl<'tcx> TyCtxt<'tcx> {
19351935
Some((parent, def_kind))
19361936
}
19371937

1938+
/// Returns the trait item that is implemented by the given item `DefId`.
1939+
pub fn trait_item_of(self, def_id: impl IntoQueryParam<DefId>) -> Option<DefId> {
1940+
let assoc = self.opt_associated_item(def_id.into_query_param())?;
1941+
if assoc.container != AssocItemContainer::Impl {
1942+
return None;
1943+
}
1944+
assoc.trait_item_def_id
1945+
}
1946+
19381947
/// If the given `DefId` is an associated item of a trait,
19391948
/// returns the `DefId` of the trait; otherwise, returns `None`.
19401949
pub fn trait_of_assoc(self, def_id: DefId) -> Option<DefId> {

compiler/rustc_passes/src/dead.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,11 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
481481
fn check_impl_or_impl_item_live(&mut self, local_def_id: LocalDefId) -> bool {
482482
let (impl_block_id, trait_def_id) = match self.tcx.def_kind(local_def_id) {
483483
// assoc impl items of traits are live if the corresponding trait items are live
484-
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => (
485-
self.tcx.local_parent(local_def_id),
486-
self.tcx
487-
.associated_item(local_def_id)
488-
.trait_item_def_id
489-
.and_then(|def_id| def_id.as_local()),
490-
),
484+
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => {
485+
let trait_item_id =
486+
self.tcx.trait_item_of(local_def_id).and_then(|def_id| def_id.as_local());
487+
(self.tcx.local_parent(local_def_id), trait_item_id)
488+
}
491489
// impl items are live if the corresponding traits are live
492490
DefKind::Impl { of_trait: true } => (
493491
local_def_id,

compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,7 @@ fn implemented_method<'tcx>(
469469
let ancestor = if let Some(impl_id) = tcx.impl_of_assoc(instance.def_id()) {
470470
// Implementation in an `impl` block
471471
trait_ref = tcx.impl_trait_ref(impl_id)?;
472-
let impl_method = tcx.associated_item(instance.def_id());
473-
method_id = impl_method.trait_item_def_id?;
472+
method_id = tcx.trait_item_of(instance.def_id())?;
474473
trait_method = tcx.associated_item(method_id);
475474
trait_id = trait_ref.skip_binder().def_id;
476475
impl_id

compiler/rustc_ty_utils/src/implied_bounds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<'
107107
// the assumed wf types of the trait's RPITIT GAT.
108108
ty::ImplTraitInTraitData::Impl { .. } => {
109109
let impl_def_id = tcx.local_parent(def_id);
110-
let rpitit_def_id = tcx.associated_item(def_id).trait_item_def_id.unwrap();
110+
let rpitit_def_id = tcx.trait_item_of(def_id).unwrap();
111111
let args = ty::GenericArgs::identity_for_item(tcx, def_id).rebase_onto(
112112
tcx,
113113
impl_def_id.to_def_id(),

src/tools/clippy/clippy_lints/src/functions/renamed_function_params.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_errors::{Applicability, MultiSpan};
3-
use rustc_hir::def_id::{DefId, DefIdSet};
4-
use rustc_hir::hir_id::OwnerId;
3+
use rustc_hir::def_id::DefIdSet;
54
use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind, Node, TraitRef};
65
use rustc_lint::LateContext;
76
use rustc_span::Span;
@@ -18,7 +17,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, item: &ImplItem<'_>, ignored
1817
of_trait: Some(of_trait),
1918
..
2019
}) = &parent_item.kind
21-
&& let Some(did) = trait_item_def_id_of_impl(cx, item.owner_id)
20+
&& let Some(did) = cx.tcx.trait_item_of(item.owner_id)
2221
&& !is_from_ignored_trait(&of_trait.trait_ref, ignored_traits)
2322
{
2423
let mut param_idents_iter = cx.tcx.hir_body_param_idents(body_id);
@@ -91,11 +90,6 @@ impl RenamedFnArgs {
9190
}
9291
}
9392

94-
/// Get the [`trait_item_def_id`](ImplItemRef::trait_item_def_id) of a relevant impl item.
95-
fn trait_item_def_id_of_impl(cx: &LateContext<'_>, target: OwnerId) -> Option<DefId> {
96-
cx.tcx.associated_item(target).trait_item_def_id
97-
}
98-
9993
fn is_from_ignored_trait(of_trait: &TraitRef<'_>, ignored_traits: &DefIdSet) -> bool {
10094
let Some(trait_did) = of_trait.trait_def_id() else {
10195
return false;

src/tools/clippy/clippy_lints/src/only_used_in_recursion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<'tcx> LateLintPass<'tcx> for OnlyUsedInRecursion {
248248
.tcx
249249
.impl_trait_ref(item.owner_id)
250250
.map(EarlyBinder::instantiate_identity)
251-
&& let Some(trait_item_id) = cx.tcx.associated_item(owner_id).trait_item_def_id
251+
&& let Some(trait_item_id) = cx.tcx.trait_item_of(owner_id)
252252
{
253253
(
254254
trait_item_id,

0 commit comments

Comments
 (0)