Skip to content

Commit c17b2dc

Browse files
committed
Split trait_id_of_impl into impl(_opt)_trait_id
1 parent dcf76b6 commit c17b2dc

File tree

12 files changed

+24
-29
lines changed

12 files changed

+24
-29
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
708708
return (false, false, None);
709709
}
710710
let my_def = self.body.source.def_id();
711-
let Some(td) =
712-
tcx.trait_impl_of_assoc(my_def).and_then(|id| self.infcx.tcx.trait_id_of_impl(id))
711+
let Some(td) = tcx.trait_impl_of_assoc(my_def).map(|id| self.infcx.tcx.impl_trait_id(id))
713712
else {
714713
return (false, false, None);
715714
};

compiler/rustc_hir_analysis/src/check/always_applicable.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ fn ensure_impl_params_and_item_params_correspond<'tcx>(
148148
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
149149
ty::ImplPolarity::Negative => "!",
150150
};
151-
let trait_name = tcx
152-
.item_name(tcx.trait_id_of_impl(impl_def_id.to_def_id()).expect("expected impl of trait"));
151+
let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
153152
let mut err = struct_span_code_err!(
154153
tcx.dcx(),
155154
impl_span,
@@ -187,8 +186,7 @@ fn ensure_impl_predicates_are_implied_by_item_defn<'tcx>(
187186
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
188187

189188
let impl_span = tcx.def_span(impl_def_id.to_def_id());
190-
let trait_name = tcx
191-
.item_name(tcx.trait_id_of_impl(impl_def_id.to_def_id()).expect("expected impl of trait"));
189+
let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
192190
let polarity = match tcx.impl_polarity(impl_def_id) {
193191
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
194192
ty::ImplPolarity::Negative => "!",

compiler/rustc_hir_typeck/src/method/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
242242
match *source {
243243
// Note: this cannot come from an inherent impl,
244244
// because the first probing succeeded.
245-
CandidateSource::Impl(def) => self.tcx.trait_id_of_impl(def),
245+
CandidateSource::Impl(def) => Some(self.tcx.impl_trait_id(def)),
246246
CandidateSource::Trait(_) => None,
247247
}
248248
})

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,9 +1176,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11761176
// things failed, so lets look at all traits, for diagnostic purposes now:
11771177
self.reset();
11781178

1179-
let span = self.span;
1180-
let tcx = self.tcx;
1181-
11821179
self.assemble_extension_candidates_for_all_traits();
11831180

11841181
let out_of_scope_traits = match self.pick_core(&mut Vec::new()) {
@@ -1187,10 +1184,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11871184
.into_iter()
11881185
.map(|source| match source {
11891186
CandidateSource::Trait(id) => id,
1190-
CandidateSource::Impl(impl_id) => match tcx.trait_id_of_impl(impl_id) {
1191-
Some(id) => id,
1192-
None => span_bug!(span, "found inherent method when looking at traits"),
1193-
},
1187+
CandidateSource::Impl(impl_id) => self.tcx.impl_trait_id(impl_id),
11941188
})
11951189
.collect(),
11961190
Some(Err(MethodError::NoMatch(NoMatchData {

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3720,7 +3720,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
37203720
static_candidates.iter().all(|sc| match *sc {
37213721
CandidateSource::Trait(def_id) => def_id != info.def_id,
37223722
CandidateSource::Impl(def_id) => {
3723-
self.tcx.trait_id_of_impl(def_id) != Some(info.def_id)
3723+
self.tcx.impl_opt_trait_id(def_id) != Some(info.def_id)
37243724
}
37253725
})
37263726
})

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,12 +1913,6 @@ impl<'tcx> TyCtxt<'tcx> {
19131913
}
19141914
}
19151915

1916-
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
1917-
/// If it implements no trait, returns `None`.
1918-
pub fn trait_id_of_impl(self, def_id: DefId) -> Option<DefId> {
1919-
self.impl_trait_ref(def_id).map(|tr| tr.skip_binder().def_id)
1920-
}
1921-
19221916
/// If the given `DefId` is an associated item, returns the `DefId` and `DefKind` of the parent trait or impl.
19231917
pub fn assoc_parent(self, def_id: DefId) -> Option<(DefId, DefKind)> {
19241918
if !self.def_kind(def_id).is_assoc() {
@@ -1983,6 +1977,18 @@ impl<'tcx> TyCtxt<'tcx> {
19831977
Some(self.impl_trait_header(def_id)?.trait_ref)
19841978
}
19851979

1980+
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
1981+
pub fn impl_trait_id(self, def_id: DefId) -> DefId {
1982+
self.impl_opt_trait_id(def_id)
1983+
.unwrap_or_else(|| panic!("expected impl of trait for {def_id:?}"))
1984+
}
1985+
1986+
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
1987+
/// If it implements no trait, returns `None`.
1988+
pub fn impl_opt_trait_id(self, def_id: DefId) -> Option<DefId> {
1989+
self.impl_trait_ref(def_id).map(|tr| tr.skip_binder().def_id)
1990+
}
1991+
19861992
pub fn is_exportable(self, def_id: DefId) -> bool {
19871993
self.exportable_items(def_id.krate).contains(&def_id)
19881994
}

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
649649
if let Some((impl_def_id, DefKind::Impl { of_trait })) = assoc_parent {
650650
if of_trait
651651
&& tcx.sess.opts.incremental.is_some()
652-
&& tcx.is_lang_item(tcx.trait_id_of_impl(impl_def_id).unwrap(), LangItem::Drop)
652+
&& tcx.is_lang_item(tcx.impl_trait_id(impl_def_id), LangItem::Drop)
653653
{
654654
// Put `Drop::drop` into the same cgu as `drop_in_place`
655655
// since `drop_in_place` is the only thing that can

compiler/rustc_passes/src/reachable.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,7 @@ fn check_item<'tcx>(
404404
let items = tcx.associated_item_def_ids(id.owner_id);
405405
worklist.extend(items.iter().map(|ii_ref| ii_ref.expect_local()));
406406

407-
let Some(trait_def_id) = tcx.trait_id_of_impl(id.owner_id.to_def_id()) else {
408-
unreachable!();
409-
};
407+
let trait_def_id = tcx.impl_trait_id(id.owner_id.to_def_id());
410408

411409
if !trait_def_id.is_local() {
412410
return;

compiler/rustc_trait_selection/src/error_reporting/traits/call_kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn call_kind<'tcx>(
7777
let container_id = assoc.container_id(tcx);
7878
match assoc.container {
7979
AssocContainer::InherentImpl => None,
80-
AssocContainer::TraitImpl(_) => tcx.trait_id_of_impl(container_id),
80+
AssocContainer::TraitImpl(_) => Some(tcx.impl_trait_id(container_id)),
8181
AssocContainer::Trait => Some(container_id),
8282
}
8383
});

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
19791979
let ImplSourceUserDefinedData { impl_def_id, args, mut nested } = impl_impl_source;
19801980

19811981
let assoc_item_id = obligation.predicate.def_id;
1982-
let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
1982+
let trait_def_id = tcx.impl_trait_id(impl_def_id);
19831983

19841984
let param_env = obligation.param_env;
19851985
let assoc_term = match specialization_graph::assoc_def(tcx, impl_def_id, assoc_item_id) {

0 commit comments

Comments
 (0)