aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
authorPaul Daniel Faria <[email protected]>2020-06-04 04:38:25 +0100
committerPaul Daniel Faria <[email protected]>2020-08-10 13:44:54 +0100
commit263f9a7f231a474dd56d02adbcd7c57d079e88fd (patch)
tree125391657a69cb05ac14fadee37e6138dc0daae7 /crates/ra_ide
parentf3336509e52187a7a70a8043557a7317872e3a2f (diff)
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.
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs24
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs11
2 files changed, 35 insertions, 0 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index 6b7874460..0cab684eb 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -565,6 +565,30 @@ fn highlight_element(
565 _ => h, 565 _ => h,
566 } 566 }
567 } 567 }
568 REF_EXPR => {
569 let ref_expr = element.into_node().and_then(ast::RefExpr::cast)?;
570 let expr = ref_expr.expr()?;
571 let field_expr = match expr {
572 ast::Expr::FieldExpr(fe) => fe,
573 _ => return None,
574 };
575
576 let expr = field_expr.expr()?;
577 let ty = match sema.type_of_expr(&expr) {
578 Some(ty) => ty,
579 None => {
580 println!("No type :(");
581 return None;
582 }
583 };
584 if !ty.is_packed(db) {
585 return None;
586 }
587
588 // FIXME account for alignment... somehow
589
590 Highlight::new(HighlightTag::Operator) | HighlightModifier::Unsafe
591 }
568 p if p.is_punct() => match p { 592 p if p.is_punct() => match p {
569 T![::] | T![->] | T![=>] | T![&] | T![..] | T![=] | T![@] => { 593 T![::] | T![->] | T![=>] | T![&] | T![..] | T![=] | T![@] => {
570 HighlightTag::Operator.into() 594 HighlightTag::Operator.into()
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs
index 09062c38e..f2c078d34 100644
--- a/crates/ra_ide/src/syntax_highlighting/tests.rs
+++ b/crates/ra_ide/src/syntax_highlighting/tests.rs
@@ -292,6 +292,13 @@ struct TypeForStaticMut {
292 292
293static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 }; 293static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 };
294 294
295#[repr(packed)]
296struct Packed {
297 a: u16,
298 b: u8,
299 c: u32,
300}
301
295fn main() { 302fn main() {
296 let x = &5 as *const usize; 303 let x = &5 as *const usize;
297 let u = Union { b: 0 }; 304 let u = Union { b: 0 };
@@ -306,6 +313,10 @@ fn main() {
306 let y = *(x); 313 let y = *(x);
307 let z = -x; 314 let z = -x;
308 let a = global_mut.a; 315 let a = global_mut.a;
316 let packed = Packed { a: 0, b: 0, c: 0 };
317 let a = &packed.a;
318 let b = &packed.b;
319 let c = &packed.c;
309 } 320 }
310} 321}
311"# 322"#