Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cfb8419

Browse files
Separate collection of crate-local inherent impls from error reporting
1 parent 11e760b commit cfb8419

File tree

22 files changed

+127
-125
lines changed

22 files changed

+127
-125
lines changed

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,38 @@ use crate::errors;
2222
pub(crate) fn crate_inherent_impls(
2323
tcx: TyCtxt<'_>,
2424
(): (),
25-
) -> Result<&'_ CrateInherentImpls, ErrorGuaranteed> {
25+
) -> (&'_ CrateInherentImpls, Result<(), ErrorGuaranteed>) {
2626
let mut collect = InherentCollect { tcx, impls_map: Default::default() };
27+
2728
let mut res = Ok(());
2829
for id in tcx.hir().items() {
2930
res = res.and(collect.check_item(id));
3031
}
31-
res?;
32-
Ok(tcx.arena.alloc(collect.impls_map))
32+
33+
(tcx.arena.alloc(collect.impls_map), res)
3334
}
3435

35-
pub(crate) fn crate_incoherent_impls(
36+
pub(crate) fn crate_inherent_impls_validity_check(
3637
tcx: TyCtxt<'_>,
37-
simp: SimplifiedType,
38-
) -> Result<&[DefId], ErrorGuaranteed> {
39-
let crate_map = tcx.crate_inherent_impls(())?;
40-
Ok(tcx.arena.alloc_from_iter(
38+
(): (),
39+
) -> Result<(), ErrorGuaranteed> {
40+
tcx.crate_inherent_impls(()).1
41+
}
42+
43+
pub(crate) fn crate_incoherent_impls(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
44+
let (crate_map, _) = tcx.crate_inherent_impls(());
45+
tcx.arena.alloc_from_iter(
4146
crate_map.incoherent_impls.get(&simp).unwrap_or(&Vec::new()).iter().map(|d| d.to_def_id()),
42-
))
47+
)
4348
}
4449

4550
/// On-demand query: yields a vector of the inherent impls for a specific type.
46-
pub(crate) fn inherent_impls(
47-
tcx: TyCtxt<'_>,
48-
ty_def_id: LocalDefId,
49-
) -> Result<&[DefId], ErrorGuaranteed> {
50-
let crate_map = tcx.crate_inherent_impls(())?;
51-
Ok(match crate_map.inherent_impls.get(&ty_def_id) {
51+
pub(crate) fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> &[DefId] {
52+
let (crate_map, _) = tcx.crate_inherent_impls(());
53+
match crate_map.inherent_impls.get(&ty_def_id) {
5254
Some(v) => &v[..],
5355
None => &[],
54-
})
56+
}
5557
}
5658

5759
struct InherentCollect<'tcx> {

compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
177177
return Ok(());
178178
}
179179

180-
let impls = self.tcx.inherent_impls(id.owner_id)?;
181-
180+
let impls = self.tcx.inherent_impls(id.owner_id);
182181
let overlap_mode = OverlapMode::get(self.tcx, id.owner_id.to_def_id());
183182

184183
let impls_items = impls

compiler/rustc_hir_analysis/src/coherence/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ fn enforce_empty_impls_for_marker_traits(
124124

125125
pub(crate) fn provide(providers: &mut Providers) {
126126
use self::builtin::coerce_unsized_info;
127-
use self::inherent_impls::{crate_incoherent_impls, crate_inherent_impls, inherent_impls};
127+
use self::inherent_impls::{
128+
crate_incoherent_impls, crate_inherent_impls, crate_inherent_impls_validity_check,
129+
inherent_impls,
130+
};
128131
use self::inherent_impls_overlap::crate_inherent_impls_overlap_check;
129132
use self::orphan::orphan_check_impl;
130133

@@ -133,6 +136,7 @@ pub(crate) fn provide(providers: &mut Providers) {
133136
crate_inherent_impls,
134137
crate_incoherent_impls,
135138
inherent_impls,
139+
crate_inherent_impls_validity_check,
136140
crate_inherent_impls_overlap_check,
137141
coerce_unsized_info,
138142
orphan_check_impl,

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
10281028
..
10291029
}) = node
10301030
&& let Some(ty_def_id) = qself_ty.ty_def_id()
1031-
&& let Ok([inherent_impl]) = tcx.inherent_impls(ty_def_id)
1031+
&& let [inherent_impl] = tcx.inherent_impls(ty_def_id)
10321032
&& let name = format!("{ident2}_{ident3}")
10331033
&& let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = tcx
10341034
.associated_items(inherent_impl)

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12721272
}
12731273

12741274
let candidates: Vec<_> = tcx
1275-
.inherent_impls(adt_did)?
1275+
.inherent_impls(adt_did)
12761276
.iter()
12771277
.filter_map(|&impl_| {
12781278
let (item, scope) =

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
170170
let _ = tcx.ensure().coherent_trait(trait_def_id);
171171
}
172172
// these queries are executed for side-effects (error reporting):
173-
let _ = tcx.ensure().crate_inherent_impls(());
173+
let _ = tcx.ensure().crate_inherent_impls_validity_check(());
174174
let _ = tcx.ensure().crate_inherent_impls_overlap_check(());
175175
});
176176

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21262126
.tcx
21272127
.inherent_impls(def_id)
21282128
.into_iter()
2129-
.flatten()
21302129
.flat_map(|i| self.tcx.associated_items(i).in_definition_order())
21312130
// Only assoc fn with no receivers.
21322131
.filter(|item| {

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,13 +728,13 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
728728
let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer) else {
729729
bug!("unexpected incoherent type: {:?}", self_ty)
730730
};
731-
for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter().flatten() {
731+
for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter() {
732732
self.assemble_inherent_impl_probe(impl_def_id);
733733
}
734734
}
735735

736736
fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: DefId) {
737-
let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id).into_iter().flatten();
737+
let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id).into_iter();
738738
for &impl_def_id in impl_def_ids {
739739
self.assemble_inherent_impl_probe(impl_def_id);
740740
}

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
680680
self.tcx
681681
.inherent_impls(adt_def.did())
682682
.into_iter()
683-
.flatten()
684683
.any(|def_id| self.associated_value(*def_id, item_name).is_some())
685684
} else {
686685
false
@@ -1438,7 +1437,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14381437
.tcx
14391438
.inherent_impls(adt.did())
14401439
.into_iter()
1441-
.flatten()
14421440
.copied()
14431441
.filter(|def_id| {
14441442
if let Some(assoc) = self.associated_value(*def_id, item_name) {
@@ -1900,7 +1898,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19001898
call_args: Option<Vec<Ty<'tcx>>>,
19011899
) -> Option<Symbol> {
19021900
if let ty::Adt(adt, adt_args) = rcvr_ty.kind() {
1903-
for inherent_impl_did in self.tcx.inherent_impls(adt.did()).into_iter().flatten() {
1901+
for inherent_impl_did in self.tcx.inherent_impls(adt.did()).into_iter() {
19041902
for inherent_method in
19051903
self.tcx.associated_items(inherent_impl_did).in_definition_order()
19061904
{
@@ -2114,9 +2112,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21142112
let ty::Adt(adt_def, _) = rcvr_ty.kind() else {
21152113
return;
21162114
};
2117-
// FIXME(oli-obk): try out bubbling this error up one level and cancelling the other error in that case.
2118-
let Ok(impls) = self.tcx.inherent_impls(adt_def.did()) else { return };
2119-
let mut items = impls
2115+
let mut items = self
2116+
.tcx
2117+
.inherent_impls(adt_def.did())
21202118
.iter()
21212119
.flat_map(|i| self.tcx.associated_items(i).in_definition_order())
21222120
// Only assoc fn with no receivers and only if
@@ -2495,7 +2493,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24952493
.and_then(|simp| {
24962494
tcx.incoherent_impls(simp)
24972495
.into_iter()
2498-
.flatten()
24992496
.find_map(|&id| self.associated_value(id, item_name))
25002497
})
25012498
.is_some()

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ provide! { tcx, def_id, other, cdata,
347347
tcx.arena.alloc_from_iter(cdata.get_associated_item_or_field_def_ids(def_id.index))
348348
}
349349
associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) }
350-
inherent_impls => { Ok(cdata.get_inherent_implementations_for_type(tcx, def_id.index)) }
350+
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
351351
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
352352
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
353353
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
@@ -393,7 +393,7 @@ provide! { tcx, def_id, other, cdata,
393393
traits => { tcx.arena.alloc_from_iter(cdata.get_traits()) }
394394
trait_impls_in_crate => { tcx.arena.alloc_from_iter(cdata.get_trait_impls()) }
395395
implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
396-
crate_incoherent_impls => { Ok(cdata.get_incoherent_impls(tcx, other)) }
396+
crate_incoherent_impls => { cdata.get_incoherent_impls(tcx, other) }
397397

398398
dep_kind => { cdata.dep_kind }
399399
module_children => {

0 commit comments

Comments
 (0)