Skip to content

Commit 510e4b4

Browse files
committed
Simplify
1 parent 610a94c commit 510e4b4

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

crates/hir-ty/src/chalk_ext.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use hir_def::{
1212
use crate::{
1313
db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id,
1414
from_placeholder_idx, to_chalk_trait_id, utils::generics, AdtId, AliasEq, AliasTy, Binders,
15-
CallableDefId, CallableSig, FnPointer, ImplTraitId, Interner, Lifetime, ProjectionTy,
15+
CallableDefId, CallableSig, DynTy, FnPointer, ImplTraitId, Interner, Lifetime, ProjectionTy,
1616
QuantifiedWhereClause, Substitution, TraitRef, Ty, TyBuilder, TyKind, TypeFlags, WhereClause,
1717
};
1818

@@ -378,6 +378,19 @@ impl ProjectionTyExt for ProjectionTy {
378378
}
379379
}
380380

381+
pub trait DynTyExt {
382+
fn principal(&self) -> Option<&TraitRef>;
383+
}
384+
385+
impl DynTyExt for DynTy {
386+
fn principal(&self) -> Option<&TraitRef> {
387+
self.bounds.skip_binders().interned().get(0).and_then(|b| match b.skip_binders() {
388+
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
389+
_ => None,
390+
})
391+
}
392+
}
393+
381394
pub trait TraitRefExt {
382395
fn hir_trait_id(&self) -> TraitId;
383396
}

crates/hir-ty/src/method_resolution.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use crate::{
2424
primitive::{FloatTy, IntTy, UintTy},
2525
static_lifetime, to_chalk_trait_id,
2626
utils::all_super_traits,
27-
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, ForeignDefId, InEnvironment, Interner,
28-
Scalar, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt,
27+
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, DynTyExt, ForeignDefId, InEnvironment,
28+
Interner, Scalar, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt,
2929
};
3030

3131
/// This is used as a key for indexing impls.
@@ -805,20 +805,9 @@ fn is_inherent_impl_coherent(
805805
| TyKind::Scalar(_) => def_map.is_rustc_coherence_is_core(),
806806

807807
&TyKind::Adt(AdtId(adt), _) => adt.module(db.upcast()).krate() == def_map.krate(),
808-
// FIXME: Factor out the principal trait fetching into a function
809-
TyKind::Dyn(it) => it
810-
.bounds
811-
.skip_binders()
812-
.interned()
813-
.get(0)
814-
.and_then(|b| match b.skip_binders() {
815-
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
816-
_ => None,
817-
})
818-
.map_or(false, |trait_ref| {
819-
from_chalk_trait_id(trait_ref.trait_id).module(db.upcast()).krate()
820-
== def_map.krate()
821-
}),
808+
TyKind::Dyn(it) => it.principal().map_or(false, |trait_ref| {
809+
from_chalk_trait_id(trait_ref.trait_id).module(db.upcast()).krate() == def_map.krate()
810+
}),
822811

823812
_ => true,
824813
};
@@ -843,20 +832,10 @@ fn is_inherent_impl_coherent(
843832
}
844833
hir_def::AdtId::EnumId(it) => db.enum_data(it).rustc_has_incoherent_inherent_impls,
845834
},
846-
// FIXME: Factor out the principal trait fetching into a function
847-
TyKind::Dyn(it) => it
848-
.bounds
849-
.skip_binders()
850-
.interned()
851-
.get(0)
852-
.and_then(|b| match b.skip_binders() {
853-
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
854-
_ => None,
855-
})
856-
.map_or(false, |trait_ref| {
857-
db.trait_data(from_chalk_trait_id(trait_ref.trait_id))
858-
.rustc_has_incoherent_inherent_impls
859-
}),
835+
TyKind::Dyn(it) => it.principal().map_or(false, |trait_ref| {
836+
db.trait_data(from_chalk_trait_id(trait_ref.trait_id))
837+
.rustc_has_incoherent_inherent_impls
838+
}),
860839

861840
_ => false,
862841
};

0 commit comments

Comments
 (0)