Skip to content

feat: Implement object safety and its hovering hint #17814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/hir-ty/src/chalk_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,9 @@ impl chalk_solve::RustIrDatabase<Interner> for ChalkContext<'_> {
TyKind::Error.intern(Interner)
}

fn is_object_safe(&self, _trait_id: chalk_ir::TraitId<Interner>) -> bool {
// FIXME: implement actual object safety
true
fn is_object_safe(&self, trait_id: chalk_ir::TraitId<Interner>) -> bool {
let trait_ = from_chalk_trait_id(trait_id);
crate::object_safety::object_safety(self.db, trait_).is_none()
}

fn closure_kind(
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-ty/src/consteval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ fn function_traits() {
);
check_number(
r#"
//- minicore: coerce_unsized, fn
//- minicore: coerce_unsized, fn, dispatch_from_dyn
fn add2(x: u8) -> u8 {
x + 2
}
Expand Down Expand Up @@ -2062,7 +2062,7 @@ fn function_traits() {
fn dyn_trait() {
check_number(
r#"
//- minicore: coerce_unsized, index, slice
//- minicore: coerce_unsized, index, slice, dispatch_from_dyn
trait Foo {
fn foo(&self) -> u8 { 10 }
}
Expand All @@ -2085,7 +2085,7 @@ fn dyn_trait() {
);
check_number(
r#"
//- minicore: coerce_unsized, index, slice
//- minicore: coerce_unsized, index, slice, dispatch_from_dyn
trait Foo {
fn foo(&self) -> i32 { 10 }
}
Expand All @@ -2109,7 +2109,7 @@ fn dyn_trait() {
);
check_number(
r#"
//- minicore: coerce_unsized, index, slice
//- minicore: coerce_unsized, index, slice, dispatch_from_dyn
trait A {
fn x(&self) -> i32;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/consteval/tests/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn size_of_val() {
);
check_number(
r#"
//- minicore: coerce_unsized, fmt, builtin_impls
//- minicore: coerce_unsized, fmt, builtin_impls, dispatch_from_dyn
extern "rust-intrinsic" {
pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
}
Expand Down
6 changes: 5 additions & 1 deletion crates/hir-ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use base_db::{
use hir_def::{
db::DefDatabase, hir::ExprId, layout::TargetDataLayout, AdtId, BlockId, CallableDefId,
ConstParamId, DefWithBodyId, EnumVariantId, FunctionId, GeneralConstId, GenericDefId, ImplId,
LifetimeParamId, LocalFieldId, StaticId, TypeAliasId, TypeOrConstParamId, VariantId,
LifetimeParamId, LocalFieldId, StaticId, TraitId, TypeAliasId, TypeOrConstParamId, VariantId,
};
use la_arena::ArenaMap;
use smallvec::SmallVec;
Expand All @@ -24,6 +24,7 @@ use crate::{
lower::{GenericDefaults, GenericPredicates},
method_resolution::{InherentImpls, TraitImpls, TyFingerprint},
mir::{BorrowckResult, MirBody, MirLowerError},
object_safety::ObjectSafetyViolation,
Binders, ClosureId, Const, FnDefId, ImplTraitId, ImplTraits, InferenceResult, Interner,
PolyFnSig, Substitution, TraitEnvironment, TraitRef, Ty, TyDefId, ValueTyDefId,
};
Expand Down Expand Up @@ -107,6 +108,9 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
#[salsa::invoke(crate::layout::target_data_layout_query)]
fn target_data_layout(&self, krate: CrateId) -> Result<Arc<TargetDataLayout>, Arc<str>>;

#[salsa::invoke(crate::object_safety::object_safety_of_trait_query)]
fn object_safety_of_trait(&self, trait_: TraitId) -> Option<ObjectSafetyViolation>;

#[salsa::invoke(crate::lower::ty_query)]
#[salsa::cycle(crate::lower::ty_recover)]
fn ty(&self, def: TyDefId) -> Binders<Ty>;
Expand Down
17 changes: 17 additions & 0 deletions crates/hir-ty/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ impl Generics {
}
}

pub(crate) fn trait_self_param_idx(db: &dyn DefDatabase, def: GenericDefId) -> Option<usize> {
match def {
GenericDefId::TraitId(_) | GenericDefId::TraitAliasId(_) => {
let params = db.generic_params(def);
params.trait_self_param().map(|idx| idx.into_raw().into_u32() as usize)
}
GenericDefId::ImplId(_) => None,
_ => {
let parent_def = parent_generic_def(db, def)?;
let parent_params = db.generic_params(parent_def);
let parent_self_idx = parent_params.trait_self_param()?.into_raw().into_u32() as usize;
let self_params = db.generic_params(def);
Some(self_params.len() + parent_self_idx)
}
}
}

fn parent_generic_def(db: &dyn DefDatabase, def: GenericDefId) -> Option<GenericDefId> {
let container = match def {
GenericDefId::FunctionId(it) => it.lookup(db).container,
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod lang_items;
pub mod layout;
pub mod method_resolution;
pub mod mir;
pub mod object_safety;
pub mod primitive;
pub mod traits;

Expand Down
18 changes: 2 additions & 16 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use crate::{
},
db::HirDatabase,
error_lifetime,
generics::{generics, Generics},
generics::{generics, trait_self_param_idx, Generics},
make_binders,
mapping::{from_chalk_trait_id, lt_to_placeholder_idx, ToChalk},
static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
Expand Down Expand Up @@ -1747,21 +1747,7 @@ fn implicitly_sized_clauses<'a, 'subst: 'a>(
.lang_item(resolver.krate(), LangItem::Sized)
.and_then(|lang_item| lang_item.as_trait().map(to_chalk_trait_id))?;

let get_trait_self_idx = |container: ItemContainerId| {
if matches!(container, ItemContainerId::TraitId(_)) {
let generics = generics(db.upcast(), def);
Some(generics.len_self())
} else {
None
}
};
let trait_self_idx = match def {
GenericDefId::TraitId(_) => Some(0),
GenericDefId::FunctionId(it) => get_trait_self_idx(it.lookup(db.upcast()).container),
GenericDefId::ConstId(it) => get_trait_self_idx(it.lookup(db.upcast()).container),
GenericDefId::TypeAliasId(it) => get_trait_self_idx(it.lookup(db.upcast()).container),
_ => None,
};
let trait_self_idx = trait_self_param_idx(db.upcast(), def);

Some(
substitution
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/eval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ fn main() {
fn regression_14966() {
check_pass(
r#"
//- minicore: fn, copy, coerce_unsized
//- minicore: fn, copy, coerce_unsized, dispatch_from_dyn
trait A<T> {
fn a(&self) {}
}
Expand Down
Loading