Auto merge of #120834 - oli-obk:only_local_coherence, r=lcnr

A trait's local impls are trivially coherent if there are no impls.

This avoids creating a dependency edge on the hir or the specialization graph

This may resolve part of the performance issue of https://github.com/rust-lang/rust/pull/120558
This commit is contained in:
bors 2024-02-12 05:05:57 +00:00
commit de4d615e6b
1 changed files with 3 additions and 1 deletions

View File

@ -124,11 +124,13 @@ pub fn provide(providers: &mut Providers) {
}
fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Result<(), ErrorGuaranteed> {
// If there are no impls for the trait, then "all impls" are trivially coherent and we won't check anything
// anyway. Thus we bail out even before the specialization graph, avoiding the dep_graph edge.
let Some(impls) = tcx.all_local_trait_impls(()).get(&def_id) else { return Ok(()) };
// Trigger building the specialization graph for the trait. This will detect and report any
// overlap errors.
let mut res = tcx.ensure().specialization_graph_of(def_id);
let impls = tcx.hir().trait_impls(def_id);
for &impl_def_id in impls {
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity();