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

Commit 873cf25

Browse files
committed
Add DynTyExt::principal_id
1 parent 73ae6a0 commit 873cf25

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/chalk_ext.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,25 @@ impl ProjectionTyExt for ProjectionTy {
443443
}
444444

445445
pub trait DynTyExt {
446-
fn principal(&self) -> Option<&TraitRef>;
446+
fn principal(&self) -> Option<Binders<Binders<&TraitRef>>>;
447+
fn principal_id(&self) -> Option<chalk_ir::TraitId<Interner>>;
447448
}
448449

449450
impl DynTyExt for DynTy {
450-
fn principal(&self) -> Option<&TraitRef> {
451+
fn principal(&self) -> Option<Binders<Binders<&TraitRef>>> {
452+
self.bounds.as_ref().filter_map(|bounds| {
453+
bounds.interned().first().and_then(|b| {
454+
b.as_ref().filter_map(|b| match b {
455+
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
456+
_ => None,
457+
})
458+
})
459+
})
460+
}
461+
462+
fn principal_id(&self) -> Option<chalk_ir::TraitId<Interner>> {
451463
self.bounds.skip_binders().interned().first().and_then(|b| match b.skip_binders() {
452-
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
464+
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref.trait_id),
453465
_ => None,
454466
})
455467
}

src/tools/rust-analyzer/crates/hir-ty/src/infer/closure.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ impl InferenceContext<'_> {
9696
.map(|b| b.into_value_and_skipped_binders().0);
9797
self.deduce_closure_kind_from_predicate_clauses(clauses)
9898
}
99-
TyKind::Dyn(dyn_ty) => dyn_ty.principal().and_then(|trait_ref| {
100-
self.fn_trait_kind_from_trait_id(from_chalk_trait_id(trait_ref.trait_id))
99+
TyKind::Dyn(dyn_ty) => dyn_ty.principal_id().and_then(|trait_id| {
100+
self.fn_trait_kind_from_trait_id(from_chalk_trait_id(trait_id))
101101
}),
102102
TyKind::InferenceVar(ty, chalk_ir::TyVariableKind::General) => {
103103
let clauses = self.clauses_for_self_ty(*ty);

src/tools/rust-analyzer/crates/hir-ty/src/lang_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn is_box(db: &dyn HirDatabase, adt: AdtId) -> bool {
1313

1414
pub fn is_unsafe_cell(db: &dyn HirDatabase, adt: AdtId) -> bool {
1515
let AdtId::StructId(id) = adt else { return false };
16+
1617
db.struct_data(id).flags.contains(StructFlags::IS_UNSAFE_CELL)
1718
}
1819

src/tools/rust-analyzer/crates/hir-ty/src/method_resolution.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,8 @@ 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-
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()
808+
TyKind::Dyn(it) => it.principal_id().map_or(false, |trait_id| {
809+
from_chalk_trait_id(trait_id).module(db.upcast()).krate() == def_map.krate()
810810
}),
811811

812812
_ => true,
@@ -834,9 +834,8 @@ fn is_inherent_impl_coherent(
834834
.contains(StructFlags::IS_RUSTC_HAS_INCOHERENT_INHERENT_IMPL),
835835
hir_def::AdtId::EnumId(it) => db.enum_data(it).rustc_has_incoherent_inherent_impls,
836836
},
837-
TyKind::Dyn(it) => it.principal().map_or(false, |trait_ref| {
838-
db.trait_data(from_chalk_trait_id(trait_ref.trait_id))
839-
.rustc_has_incoherent_inherent_impls
837+
TyKind::Dyn(it) => it.principal_id().map_or(false, |trait_id| {
838+
db.trait_data(from_chalk_trait_id(trait_id)).rustc_has_incoherent_inherent_impls
840839
}),
841840

842841
_ => false,
@@ -896,8 +895,8 @@ pub fn check_orphan_rules(db: &dyn HirDatabase, impl_: ImplId) -> bool {
896895
match unwrap_fundamental(ty).kind(Interner) {
897896
&TyKind::Adt(AdtId(id), _) => is_local(id.module(db.upcast()).krate()),
898897
TyKind::Error => true,
899-
TyKind::Dyn(it) => it.principal().map_or(false, |trait_ref| {
900-
is_local(from_chalk_trait_id(trait_ref.trait_id).module(db.upcast()).krate())
898+
TyKind::Dyn(it) => it.principal_id().map_or(false, |trait_id| {
899+
is_local(from_chalk_trait_id(trait_id).module(db.upcast()).krate())
901900
}),
902901
_ => false,
903902
}

0 commit comments

Comments
 (0)