Skip to content

Commit 72bf112

Browse files
committed
Split trait_id_of_impl into impl(_opt)_trait_id
1 parent d73e30f commit 72bf112

File tree

13 files changed

+34
-36
lines changed

13 files changed

+34
-36
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
682682
}
683683
let my_def = self.body.source.def_id();
684684
let Some(td) =
685-
self.infcx.tcx.impl_of_assoc(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
685+
self.infcx.tcx.impl_of_assoc(my_def).and_then(|x| self.infcx.tcx.impl_opt_trait_id(x))
686686
else {
687687
return (false, false, None);
688688
};

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) => self.tcx.impl_opt_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
@@ -1127,9 +1127,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11271127
// things failed, so lets look at all traits, for diagnostic purposes now:
11281128
self.reset();
11291129

1130-
let span = self.span;
1131-
let tcx = self.tcx;
1132-
11331130
self.assemble_extension_candidates_for_all_traits();
11341131

11351132
let out_of_scope_traits = match self.pick_core(&mut Vec::new()) {
@@ -1138,10 +1135,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11381135
.into_iter()
11391136
.map(|source| match source {
11401137
CandidateSource::Trait(id) => id,
1141-
CandidateSource::Impl(impl_id) => match tcx.trait_id_of_impl(impl_id) {
1142-
Some(id) => id,
1143-
None => span_bug!(span, "found inherent method when looking at traits"),
1144-
},
1138+
CandidateSource::Impl(impl_id) => self.tcx.impl_trait_id(impl_id),
11451139
})
11461140
.collect(),
11471141
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
@@ -3862,7 +3862,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38623862
static_candidates.iter().all(|sc| match *sc {
38633863
CandidateSource::Trait(def_id) => def_id != info.def_id,
38643864
CandidateSource::Impl(def_id) => {
3865-
self.tcx.trait_id_of_impl(def_id) != Some(info.def_id)
3865+
self.tcx.impl_opt_trait_id(def_id) != Some(info.def_id)
38663866
}
38673867
})
38683868
})

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,12 +1982,6 @@ impl<'tcx> TyCtxt<'tcx> {
19821982
}
19831983
}
19841984

1985-
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
1986-
/// If it implements no trait, returns `None`.
1987-
pub fn trait_id_of_impl(self, def_id: DefId) -> Option<DefId> {
1988-
self.impl_trait_ref(def_id).map(|tr| tr.skip_binder().def_id)
1989-
}
1990-
19911985
/// If the given `DefId` is an associated item, returns the `DefId` of the parent trait or impl.
19921986
pub fn assoc_parent(self, def_id: DefId) -> Option<DefId> {
19931987
self.def_kind(def_id).is_assoc().then(|| self.parent(def_id))
@@ -2026,6 +2020,18 @@ impl<'tcx> TyCtxt<'tcx> {
20262020
Some(self.impl_trait_header(def_id)?.trait_ref)
20272021
}
20282022

2023+
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
2024+
pub fn impl_trait_id(self, def_id: DefId) -> DefId {
2025+
self.impl_opt_trait_id(def_id)
2026+
.unwrap_or_else(|| panic!("expected impl of trait for {def_id:?}"))
2027+
}
2028+
2029+
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
2030+
/// If it implements no trait, returns `None`.
2031+
pub fn impl_opt_trait_id(self, def_id: DefId) -> Option<DefId> {
2032+
self.impl_trait_ref(def_id).map(|tr| tr.skip_binder().def_id)
2033+
}
2034+
20292035
pub fn is_exportable(self, def_id: DefId) -> bool {
20302036
self.exportable_items(def_id.krate).contains(&def_id)
20312037
}

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -650,17 +650,18 @@ fn characteristic_def_id_of_mono_item<'tcx>(
650650
// its self-type. If the self-type does not provide a characteristic
651651
// DefId, we use the ___location of the impl after all.
652652

653-
if tcx.trait_of_assoc(def_id).is_some() {
653+
let parent_id = tcx.parent(def_id);
654+
let parent_def_kind = tcx.def_kind(parent_id);
655+
if parent_def_kind == DefKind::Trait {
654656
let self_ty = instance.args.type_at(0);
655657
// This is a default implementation of a trait method.
656658
return characteristic_def_id_of_type(self_ty).or(Some(def_id));
657659
}
658660

659-
if let Some(impl_def_id) = tcx.impl_of_assoc(def_id) {
660-
if tcx.sess.opts.incremental.is_some()
661-
&& tcx
662-
.trait_id_of_impl(impl_def_id)
663-
.is_some_and(|def_id| tcx.is_lang_item(def_id, LangItem::Drop))
661+
if let DefKind::Impl { of_trait } = parent_def_kind {
662+
if of_trait
663+
&& tcx.sess.opts.incremental.is_some()
664+
&& tcx.is_lang_item(tcx.impl_trait_id(parent_id), LangItem::Drop)
664665
{
665666
// Put `Drop::drop` into the same cgu as `drop_in_place`
666667
// since `drop_in_place` is the only thing that can
@@ -672,7 +673,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
672673
let impl_self_ty = tcx.instantiate_and_normalize_erasing_regions(
673674
instance.args,
674675
ty::TypingEnv::fully_monomorphized(),
675-
tcx.type_of(impl_def_id),
676+
tcx.type_of(parent_id),
676677
);
677678
if let Some(def_id) = characteristic_def_id_of_type(impl_self_ty) {
678679
return Some(def_id);

compiler/rustc_passes/src/dead.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,11 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
373373
return false;
374374
}
375375

376-
if let Some(trait_of) = self.tcx.trait_id_of_impl(impl_of)
376+
if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_of)
377+
&& let trait_ref = trait_ref.instantiate_identity()
378+
&& let trait_of = trait_ref.def_id
377379
&& self.tcx.has_attr(trait_of, sym::rustc_trivial_field_reads)
378380
{
379-
let trait_ref = self.tcx.impl_trait_ref(impl_of).unwrap().instantiate_identity();
380381
if let ty::Adt(adt_def, _) = trait_ref.self_ty().kind()
381382
&& let Some(adt_def_id) = adt_def.did().as_local()
382383
{

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
@@ -76,7 +76,7 @@ pub fn call_kind<'tcx>(
7676
let parent = tcx.opt_associated_item(method_did).and_then(|assoc| {
7777
let container_id = assoc.container_id(tcx);
7878
match assoc.container {
79-
AssocItemContainer::Impl => tcx.trait_id_of_impl(container_id),
79+
AssocItemContainer::Impl => tcx.impl_opt_trait_id(container_id),
8080
AssocItemContainer::Trait => Some(container_id),
8181
}
8282
});

0 commit comments

Comments
 (0)