Split trait_id_of_impl into impl(_opt)_trait_id

This commit is contained in:
Cameron Steffen 2025-08-09 16:39:04 -05:00
parent dcf76b6811
commit c17b2dc283
12 changed files with 24 additions and 29 deletions

View file

@ -708,8 +708,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
return (false, false, None);
}
let my_def = self.body.source.def_id();
let Some(td) =
tcx.trait_impl_of_assoc(my_def).and_then(|id| self.infcx.tcx.trait_id_of_impl(id))
let Some(td) = tcx.trait_impl_of_assoc(my_def).map(|id| self.infcx.tcx.impl_trait_id(id))
else {
return (false, false, None);
};

View file

@ -148,8 +148,7 @@ fn ensure_impl_params_and_item_params_correspond<'tcx>(
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
ty::ImplPolarity::Negative => "!",
};
let trait_name = tcx
.item_name(tcx.trait_id_of_impl(impl_def_id.to_def_id()).expect("expected impl of trait"));
let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
let mut err = struct_span_code_err!(
tcx.dcx(),
impl_span,
@ -187,8 +186,7 @@ fn ensure_impl_predicates_are_implied_by_item_defn<'tcx>(
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
let impl_span = tcx.def_span(impl_def_id.to_def_id());
let trait_name = tcx
.item_name(tcx.trait_id_of_impl(impl_def_id.to_def_id()).expect("expected impl of trait"));
let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
let polarity = match tcx.impl_polarity(impl_def_id) {
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
ty::ImplPolarity::Negative => "!",

View file

@ -242,7 +242,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match *source {
// Note: this cannot come from an inherent impl,
// because the first probing succeeded.
CandidateSource::Impl(def) => self.tcx.trait_id_of_impl(def),
CandidateSource::Impl(def) => Some(self.tcx.impl_trait_id(def)),
CandidateSource::Trait(_) => None,
}
})

View file

@ -1176,9 +1176,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
// things failed, so lets look at all traits, for diagnostic purposes now:
self.reset();
let span = self.span;
let tcx = self.tcx;
self.assemble_extension_candidates_for_all_traits();
let out_of_scope_traits = match self.pick_core(&mut Vec::new()) {
@ -1187,10 +1184,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
.into_iter()
.map(|source| match source {
CandidateSource::Trait(id) => id,
CandidateSource::Impl(impl_id) => match tcx.trait_id_of_impl(impl_id) {
Some(id) => id,
None => span_bug!(span, "found inherent method when looking at traits"),
},
CandidateSource::Impl(impl_id) => self.tcx.impl_trait_id(impl_id),
})
.collect(),
Some(Err(MethodError::NoMatch(NoMatchData {

View file

@ -3720,7 +3720,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
static_candidates.iter().all(|sc| match *sc {
CandidateSource::Trait(def_id) => def_id != info.def_id,
CandidateSource::Impl(def_id) => {
self.tcx.trait_id_of_impl(def_id) != Some(info.def_id)
self.tcx.impl_opt_trait_id(def_id) != Some(info.def_id)
}
})
})

View file

@ -1913,12 +1913,6 @@ impl<'tcx> TyCtxt<'tcx> {
}
}
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
/// If it implements no trait, returns `None`.
pub fn trait_id_of_impl(self, def_id: DefId) -> Option<DefId> {
self.impl_trait_ref(def_id).map(|tr| tr.skip_binder().def_id)
}
/// If the given `DefId` is an associated item, returns the `DefId` and `DefKind` of the parent trait or impl.
pub fn assoc_parent(self, def_id: DefId) -> Option<(DefId, DefKind)> {
if !self.def_kind(def_id).is_assoc() {
@ -1983,6 +1977,18 @@ impl<'tcx> TyCtxt<'tcx> {
Some(self.impl_trait_header(def_id)?.trait_ref)
}
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
pub fn impl_trait_id(self, def_id: DefId) -> DefId {
self.impl_opt_trait_id(def_id)
.unwrap_or_else(|| panic!("expected impl of trait for {def_id:?}"))
}
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
/// If it implements no trait, returns `None`.
pub fn impl_opt_trait_id(self, def_id: DefId) -> Option<DefId> {
self.impl_trait_ref(def_id).map(|tr| tr.skip_binder().def_id)
}
pub fn is_exportable(self, def_id: DefId) -> bool {
self.exportable_items(def_id.krate).contains(&def_id)
}

View file

@ -649,7 +649,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
if let Some((impl_def_id, DefKind::Impl { of_trait })) = assoc_parent {
if of_trait
&& tcx.sess.opts.incremental.is_some()
&& tcx.is_lang_item(tcx.trait_id_of_impl(impl_def_id).unwrap(), LangItem::Drop)
&& tcx.is_lang_item(tcx.impl_trait_id(impl_def_id), LangItem::Drop)
{
// Put `Drop::drop` into the same cgu as `drop_in_place`
// since `drop_in_place` is the only thing that can

View file

@ -404,9 +404,7 @@ fn check_item<'tcx>(
let items = tcx.associated_item_def_ids(id.owner_id);
worklist.extend(items.iter().map(|ii_ref| ii_ref.expect_local()));
let Some(trait_def_id) = tcx.trait_id_of_impl(id.owner_id.to_def_id()) else {
unreachable!();
};
let trait_def_id = tcx.impl_trait_id(id.owner_id.to_def_id());
if !trait_def_id.is_local() {
return;

View file

@ -77,7 +77,7 @@ pub fn call_kind<'tcx>(
let container_id = assoc.container_id(tcx);
match assoc.container {
AssocContainer::InherentImpl => None,
AssocContainer::TraitImpl(_) => tcx.trait_id_of_impl(container_id),
AssocContainer::TraitImpl(_) => Some(tcx.impl_trait_id(container_id)),
AssocContainer::Trait => Some(container_id),
}
});

View file

@ -1979,7 +1979,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
let ImplSourceUserDefinedData { impl_def_id, args, mut nested } = impl_impl_source;
let assoc_item_id = obligation.predicate.def_id;
let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
let trait_def_id = tcx.impl_trait_id(impl_def_id);
let param_env = obligation.param_env;
let assoc_term = match specialization_graph::assoc_def(tcx, impl_def_id, assoc_item_id) {

View file

@ -354,7 +354,7 @@ pub(crate) fn assoc_def(
impl_def_id: DefId,
assoc_def_id: DefId,
) -> Result<LeafDef, ErrorGuaranteed> {
let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
let trait_def_id = tcx.impl_trait_id(impl_def_id);
let trait_def = tcx.trait_def(trait_def_id);
// This function may be called while we are still building the

View file

@ -131,7 +131,7 @@ fn resolve_associated_item<'tcx>(
assert!(!rcvr_args.has_infer());
assert!(!trait_ref.has_infer());
let trait_def_id = tcx.trait_id_of_impl(impl_data.impl_def_id).unwrap();
let trait_def_id = tcx.impl_trait_id(impl_data.impl_def_id);
let trait_def = tcx.trait_def(trait_def_id);
let leaf_def = trait_def
.ancestors(tcx, impl_data.impl_def_id)?