This commit is contained in:
Lukas Wirth 2023-03-14 20:22:11 +01:00
parent 610a94c421
commit 510e4b4029
2 changed files with 23 additions and 31 deletions

View File

@ -12,7 +12,7 @@
use crate::{
db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id,
from_placeholder_idx, to_chalk_trait_id, utils::generics, AdtId, AliasEq, AliasTy, Binders,
CallableDefId, CallableSig, FnPointer, ImplTraitId, Interner, Lifetime, ProjectionTy,
CallableDefId, CallableSig, DynTy, FnPointer, ImplTraitId, Interner, Lifetime, ProjectionTy,
QuantifiedWhereClause, Substitution, TraitRef, Ty, TyBuilder, TyKind, TypeFlags, WhereClause,
};
@ -378,6 +378,19 @@ fn self_type_parameter(&self, db: &dyn HirDatabase) -> Ty {
}
}
pub trait DynTyExt {
fn principal(&self) -> Option<&TraitRef>;
}
impl DynTyExt for DynTy {
fn principal(&self) -> Option<&TraitRef> {
self.bounds.skip_binders().interned().get(0).and_then(|b| match b.skip_binders() {
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
_ => None,
})
}
}
pub trait TraitRefExt {
fn hir_trait_id(&self) -> TraitId;
}

View File

@ -24,8 +24,8 @@
primitive::{FloatTy, IntTy, UintTy},
static_lifetime, to_chalk_trait_id,
utils::all_super_traits,
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, ForeignDefId, InEnvironment, Interner,
Scalar, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt,
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, DynTyExt, ForeignDefId, InEnvironment,
Interner, Scalar, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt,
};
/// This is used as a key for indexing impls.
@ -805,19 +805,8 @@ fn is_inherent_impl_coherent(
| TyKind::Scalar(_) => def_map.is_rustc_coherence_is_core(),
&TyKind::Adt(AdtId(adt), _) => adt.module(db.upcast()).krate() == def_map.krate(),
// FIXME: Factor out the principal trait fetching into a function
TyKind::Dyn(it) => it
.bounds
.skip_binders()
.interned()
.get(0)
.and_then(|b| match b.skip_binders() {
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
_ => None,
})
.map_or(false, |trait_ref| {
from_chalk_trait_id(trait_ref.trait_id).module(db.upcast()).krate()
== def_map.krate()
TyKind::Dyn(it) => it.principal().map_or(false, |trait_ref| {
from_chalk_trait_id(trait_ref.trait_id).module(db.upcast()).krate() == def_map.krate()
}),
_ => true,
@ -843,17 +832,7 @@ fn is_inherent_impl_coherent(
}
hir_def::AdtId::EnumId(it) => db.enum_data(it).rustc_has_incoherent_inherent_impls,
},
// FIXME: Factor out the principal trait fetching into a function
TyKind::Dyn(it) => it
.bounds
.skip_binders()
.interned()
.get(0)
.and_then(|b| match b.skip_binders() {
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
_ => None,
})
.map_or(false, |trait_ref| {
TyKind::Dyn(it) => it.principal().map_or(false, |trait_ref| {
db.trait_data(from_chalk_trait_id(trait_ref.trait_id))
.rustc_has_incoherent_inherent_impls
}),