Add tracking of packed repr, use it to highlight unsafe refs

Taking a reference to a misaligned field on a packed struct is an
unsafe operation. Highlight that behavior. Currently, the misaligned
part isn't tracked, so this highlight is a bit too aggressive.
This commit is contained in:
Paul Daniel Faria
2020-06-03 23:38:25 -04:00
parent f3336509e5
commit 263f9a7f23
4 changed files with 105 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ use std::{iter, sync::Arc};
use arrayvec::ArrayVec;
use either::Either;
use hir_def::{
adt::ReprKind,
adt::StructKind,
adt::VariantData,
builtin_type::BuiltinType,
@@ -431,6 +432,10 @@ impl Struct {
Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id)
}
pub fn is_packed(self, db: &dyn HirDatabase) -> bool {
matches!(db.struct_data(self.id).repr, Some(ReprKind::Packed))
}
fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
db.struct_data(self.id).variant_data.clone()
}
@@ -1253,6 +1258,19 @@ impl Type {
)
}
pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
let adt_id = match self.ty.value {
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_id), .. }) => adt_id,
_ => return false,
};
let adt = adt_id.into();
match adt {
Adt::Struct(s) => s.is_packed(db),
_ => false,
}
}
pub fn is_raw_ptr(&self) -> bool {
matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }))
}