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

@@ -565,6 +565,30 @@ fn highlight_element(
_ => h,
}
}
REF_EXPR => {
let ref_expr = element.into_node().and_then(ast::RefExpr::cast)?;
let expr = ref_expr.expr()?;
let field_expr = match expr {
ast::Expr::FieldExpr(fe) => fe,
_ => return None,
};
let expr = field_expr.expr()?;
let ty = match sema.type_of_expr(&expr) {
Some(ty) => ty,
None => {
println!("No type :(");
return None;
}
};
if !ty.is_packed(db) {
return None;
}
// FIXME account for alignment... somehow
Highlight::new(HighlightTag::Operator) | HighlightModifier::Unsafe
}
p if p.is_punct() => match p {
T![::] | T![->] | T![=>] | T![&] | T![..] | T![=] | T![@] => {
HighlightTag::Operator.into()