Calculate drop glue and show it on hover

Also fix the `needs_drop()` intrinsic.

Unions also need this information (to err if they have a drop-needing field), but this will come in a follow-up PR.
This commit is contained in:
Chayim Refael Friedman 2025-01-20 19:48:55 +02:00
parent 2b485d7f23
commit 100e166bb1
13 changed files with 1002 additions and 6 deletions

View file

@ -152,7 +152,7 @@ pub use {
layout::LayoutError,
method_resolution::TyFingerprint,
mir::{MirEvalError, MirLowerError},
CastError, FnAbi, PointerCast, Safety, Variance,
CastError, DropGlue, FnAbi, PointerCast, Safety, Variance,
},
// FIXME: Properly encapsulate mir
hir_ty::{mir, Interner as ChalkTyInterner},
@ -1391,6 +1391,10 @@ impl Struct {
Type::from_def(db, self.id)
}
pub fn ty_placeholders(self, db: &dyn HirDatabase) -> Type {
Type::from_def_placeholders(db, self.id)
}
pub fn constructor_ty(self, db: &dyn HirDatabase) -> Type {
Type::from_value_def(db, self.id)
}
@ -1436,6 +1440,10 @@ impl Union {
Type::from_def(db, self.id)
}
pub fn ty_placeholders(self, db: &dyn HirDatabase) -> Type {
Type::from_def_placeholders(db, self.id)
}
pub fn constructor_ty(self, db: &dyn HirDatabase) -> Type {
Type::from_value_def(db, self.id)
}
@ -1490,6 +1498,10 @@ impl Enum {
Type::from_def(db, self.id)
}
pub fn ty_placeholders(self, db: &dyn HirDatabase) -> Type {
Type::from_def_placeholders(db, self.id)
}
/// The type of the enum variant bodies.
pub fn variant_body_ty(self, db: &dyn HirDatabase) -> Type {
Type::new_for_crate(
@ -2929,6 +2941,10 @@ impl TypeAlias {
Type::from_def(db, self.id)
}
pub fn ty_placeholders(self, db: &dyn HirDatabase) -> Type {
Type::from_def_placeholders(db, self.id)
}
pub fn name(self, db: &dyn HirDatabase) -> Name {
db.type_alias_data(self.id).name.clone()
}
@ -4708,6 +4724,19 @@ impl Type {
Type::new(db, def, ty.substitute(Interner, &substs))
}
fn from_def_placeholders(db: &dyn HirDatabase, def: impl Into<TyDefId> + HasResolver) -> Type {
let ty = db.ty(def.into());
let substs = TyBuilder::placeholder_subst(
db,
match def.into() {
TyDefId::AdtId(it) => GenericDefId::AdtId(it),
TyDefId::TypeAliasId(it) => GenericDefId::TypeAliasId(it),
TyDefId::BuiltinType(_) => return Type::new(db, def, ty.skip_binders().clone()),
},
);
Type::new(db, def, ty.substitute(Interner, &substs))
}
fn from_value_def(db: &dyn HirDatabase, def: impl Into<ValueTyDefId> + HasResolver) -> Type {
let Some(ty) = db.value_ty(def.into()) else {
return Type::new(db, def, TyKind::Error.intern(Interner));
@ -5737,6 +5766,10 @@ impl Type {
db.layout_of_ty(self.ty.clone(), self.env.clone())
.map(|layout| Layout(layout, db.target_data_layout(self.env.krate).unwrap()))
}
pub fn drop_glue(&self, db: &dyn HirDatabase) -> DropGlue {
db.has_drop_glue(self.ty.clone(), self.env.clone())
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]