From 8e657f663d519771ac8ffcd4b52ded8cb381b918 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 6 Aug 2020 20:07:42 -0400 Subject: Mark static mutable names as unsafe --- crates/ra_ide/src/syntax_highlighting.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index a32ae0165..89efe71da 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -677,6 +677,7 @@ fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight { let mut h = Highlight::new(HighlightTag::Static); if s.is_mut(db) { h |= HighlightModifier::Mutable; + h |= HighlightModifier::Unsafe; } return h; } -- cgit v1.2.3 From be935b2b56dcbda5a5918d8c600552b0adbb3a96 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Fri, 7 Aug 2020 09:33:40 -0400 Subject: Apply unsafe semantic highlighting to union field access --- crates/ra_ide/src/syntax_highlighting.rs | 66 +++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 10 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index a32ae0165..bfe6143ca 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -4,7 +4,7 @@ mod injection; #[cfg(test)] mod tests; -use hir::{Name, Semantics}; +use hir::{Name, Semantics, VariantDef}; use ra_ide_db::{ defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, RootDatabase, @@ -455,6 +455,18 @@ fn macro_call_range(macro_call: &ast::MacroCall) -> Option { Some(TextRange::new(range_start, range_end)) } +fn is_possibly_unsafe(name_ref: &ast::NameRef) -> bool { + name_ref + .syntax() + .parent() + .and_then(|parent| { + ast::FieldExpr::cast(parent.clone()) + .map(|_| true) + .or_else(|| ast::RecordPatField::cast(parent).map(|_| true)) + }) + .unwrap_or(false) +} + fn highlight_element( sema: &Semantics, bindings_shadow_count: &mut FxHashMap, @@ -484,10 +496,19 @@ fn highlight_element( match name_kind { Some(NameClass::Definition(def)) => { - highlight_name(db, def) | HighlightModifier::Definition + highlight_name(db, def, false) | HighlightModifier::Definition + } + Some(NameClass::ConstReference(def)) => highlight_name(db, def, false), + Some(NameClass::FieldShorthand { field, .. }) => { + let mut h = HighlightTag::Field.into(); + if let Definition::Field(field) = field { + if let VariantDef::Union(_) = field.parent_def(db) { + h |= HighlightModifier::Unsafe; + } + } + + h } - Some(NameClass::ConstReference(def)) => highlight_name(db, def), - Some(NameClass::FieldShorthand { .. }) => HighlightTag::Field.into(), None => highlight_name_by_syntax(name) | HighlightModifier::Definition, } } @@ -498,6 +519,7 @@ fn highlight_element( } NAME_REF => { let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); + let possibly_unsafe = is_possibly_unsafe(&name_ref); match classify_name_ref(sema, &name_ref) { Some(name_kind) => match name_kind { NameRefClass::Definition(def) => { @@ -508,11 +530,13 @@ fn highlight_element( binding_hash = Some(calc_binding_hash(&name, *shadow_count)) } }; - highlight_name(db, def) + highlight_name(db, def, possibly_unsafe) } NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(), }, - None if syntactic_name_ref_highlighting => highlight_name_ref_by_syntax(name_ref), + None if syntactic_name_ref_highlighting => { + highlight_name_ref_by_syntax(name_ref, sema) + } None => HighlightTag::UnresolvedReference.into(), } } @@ -652,10 +676,19 @@ fn is_child_of_impl(element: &SyntaxElement) -> bool { } } -fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight { +fn highlight_name(db: &RootDatabase, def: Definition, possibly_unsafe: bool) -> Highlight { match def { Definition::Macro(_) => HighlightTag::Macro, - Definition::Field(_) => HighlightTag::Field, + Definition::Field(field) => { + let mut h = HighlightTag::Field.into(); + if possibly_unsafe { + if let VariantDef::Union(_) = field.parent_def(db) { + h |= HighlightModifier::Unsafe; + } + } + + return h; + } Definition::ModuleDef(def) => match def { hir::ModuleDef::Module(_) => HighlightTag::Module, hir::ModuleDef::Function(func) => { @@ -724,7 +757,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight { tag.into() } -fn highlight_name_ref_by_syntax(name: ast::NameRef) -> Highlight { +fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics) -> Highlight { let default = HighlightTag::UnresolvedReference; let parent = match name.syntax().parent() { @@ -734,7 +767,20 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef) -> Highlight { let tag = match parent.kind() { METHOD_CALL_EXPR => HighlightTag::Function, - FIELD_EXPR => HighlightTag::Field, + FIELD_EXPR => { + let h = HighlightTag::Field; + let is_union = ast::FieldExpr::cast(parent) + .and_then(|field_expr| { + let field = sema.resolve_field(&field_expr)?; + Some(if let VariantDef::Union(_) = field.parent_def(sema.db) { + true + } else { + false + }) + }) + .unwrap_or(false); + return if is_union { h | HighlightModifier::Unsafe } else { h.into() }; + } PATH_SEGMENT => { let path = match parent.parent().and_then(ast::Path::cast) { Some(it) => it, -- cgit v1.2.3 From 6cde0b1aa0f6b8623c6b81b2396f4a0345891233 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sat, 8 Aug 2020 14:14:18 -0400 Subject: Add support for extern crate This adds syntax highlighting, hover and goto def functionality for extern crate --- crates/ra_ide/src/syntax_highlighting.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 89efe71da..ec442bcd8 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -483,6 +483,7 @@ fn highlight_element( }; match name_kind { + Some(NameClass::ExternCrate(_)) => HighlightTag::Module.into(), Some(NameClass::Definition(def)) => { highlight_name(db, def) | HighlightModifier::Definition } @@ -500,6 +501,7 @@ fn highlight_element( let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); match classify_name_ref(sema, &name_ref) { Some(name_kind) => match name_kind { + NameRefClass::ExternCrate(_) => HighlightTag::Module.into(), NameRefClass::Definition(def) => { if let Definition::Local(local) = &def { if let Some(name) = local.name(db) { -- cgit v1.2.3 From 263f9a7f231a474dd56d02adbcd7c57d079e88fd Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Wed, 3 Jun 2020 23:38:25 -0400 Subject: 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. --- crates/ra_ide/src/syntax_highlighting.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') 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( _ => 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() -- cgit v1.2.3 From fd30134cf84b134259fe8140e513b152e37f3f88 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Mon, 15 Jun 2020 07:19:45 -0400 Subject: Remove token tree from ReprKind::Other variant, expose ReprKind higher, remove debug println. --- crates/ra_ide/src/syntax_highlighting.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 0cab684eb..b82b51efd 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -574,13 +574,7 @@ fn highlight_element( }; let expr = field_expr.expr()?; - let ty = match sema.type_of_expr(&expr) { - Some(ty) => ty, - None => { - println!("No type :("); - return None; - } - }; + let ty = sema.type_of_expr(&expr)?; if !ty.is_packed(db) { return None; } -- cgit v1.2.3 From 4a4b1f48efeff4ebe578eb92b7bb8338d0181a83 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Mon, 15 Jun 2020 07:41:13 -0400 Subject: Limit scope of unsafe to & instead of all ref exprs, add test showing missing support for autoref behavior --- crates/ra_ide/src/syntax_highlighting.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index b82b51efd..c5098189b 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -565,7 +565,7 @@ fn highlight_element( _ => h, } } - REF_EXPR => { + T![&] => { let ref_expr = element.into_node().and_then(ast::RefExpr::cast)?; let expr = ref_expr.expr()?; let field_expr = match expr { -- cgit v1.2.3 From c9e670b8754b8262b5071a96c32cbcd22ff968f4 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Mon, 15 Jun 2020 08:21:32 -0400 Subject: Update FIXME comment to be more useful --- crates/ra_ide/src/syntax_highlighting.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index c5098189b..9e8419c5f 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -579,7 +579,8 @@ fn highlight_element( return None; } - // FIXME account for alignment... somehow + // FIXME This needs layout computation to be correct. It will highlight + // more than it should with the current implementation. Highlight::new(HighlightTag::Operator) | HighlightModifier::Unsafe } -- cgit v1.2.3 From 38440d53d8329ac9f3f2013c6e32b3f69b069c72 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sat, 27 Jun 2020 14:42:42 -0400 Subject: Cleanup repr check, fix packed repr check and test --- crates/ra_ide/src/syntax_highlighting.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 9e8419c5f..a4a7aa228 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -565,7 +565,7 @@ fn highlight_element( _ => h, } } - T![&] => { + REF_EXPR => { let ref_expr = element.into_node().and_then(ast::RefExpr::cast)?; let expr = ref_expr.expr()?; let field_expr = match expr { @@ -582,7 +582,7 @@ fn highlight_element( // FIXME This needs layout computation to be correct. It will highlight // more than it should with the current implementation. - Highlight::new(HighlightTag::Operator) | HighlightModifier::Unsafe + HighlightTag::Operator | HighlightModifier::Unsafe } p if p.is_punct() => match p { T![::] | T![->] | T![=>] | T![&] | T![..] | T![=] | T![@] => { -- cgit v1.2.3 From d5f11e530dbf6edbdd0ca32d6cd5fafe634c8c4a Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sat, 27 Jun 2020 17:11:43 -0400 Subject: Unsafe borrow of packed fields: account for borrow through ref binding, auto ref function calls --- crates/ra_ide/src/syntax_highlighting.rs | 137 ++++++++++++++++++++++++++----- 1 file changed, 117 insertions(+), 20 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index a4a7aa228..454fef39c 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -497,9 +497,9 @@ fn highlight_element( match name_kind { Some(NameClass::ExternCrate(_)) => HighlightTag::Module.into(), Some(NameClass::Definition(def)) => { - highlight_name(db, def, false) | HighlightModifier::Definition + highlight_name(sema, db, def, None, false) | HighlightModifier::Definition } - Some(NameClass::ConstReference(def)) => highlight_name(db, def, false), + Some(NameClass::ConstReference(def)) => highlight_name(sema, db, def, None, false), Some(NameClass::FieldShorthand { field, .. }) => { let mut h = HighlightTag::Field.into(); if let Definition::Field(field) = field { @@ -532,7 +532,7 @@ fn highlight_element( binding_hash = Some(calc_binding_hash(&name, *shadow_count)) } }; - highlight_name(db, def, possibly_unsafe) + highlight_name(sema, db, def, Some(name_ref), possibly_unsafe) } NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(), }, @@ -565,8 +565,8 @@ fn highlight_element( _ => h, } } - REF_EXPR => { - let ref_expr = element.into_node().and_then(ast::RefExpr::cast)?; + T![&] => { + let ref_expr = element.parent().and_then(ast::RefExpr::cast)?; let expr = ref_expr.expr()?; let field_expr = match expr { ast::Expr::FieldExpr(fe) => fe, @@ -668,6 +668,52 @@ fn highlight_element( HighlightTag::SelfKeyword.into() } } + T![ref] => { + let modifier: Option = (|| { + let bind_pat = element.parent().and_then(ast::BindPat::cast)?; + let parent = bind_pat.syntax().parent()?; + + let ty = if let Some(pat_list) = + ast::RecordFieldPatList::cast(parent.clone()) + { + let record_pat = + pat_list.syntax().parent().and_then(ast::RecordPat::cast)?; + sema.type_of_pat(&ast::Pat::RecordPat(record_pat)) + } else if let Some(let_stmt) = ast::LetStmt::cast(parent.clone()) { + let field_expr = + if let ast::Expr::FieldExpr(field_expr) = let_stmt.initializer()? { + field_expr + } else { + return None; + }; + + sema.type_of_expr(&field_expr.expr()?) + } else if let Some(record_field_pat) = ast::RecordFieldPat::cast(parent) { + let record_pat = record_field_pat + .syntax() + .parent() + .and_then(ast::RecordFieldPatList::cast)? + .syntax() + .parent() + .and_then(ast::RecordPat::cast)?; + sema.type_of_pat(&ast::Pat::RecordPat(record_pat)) + } else { + None + }?; + + if !ty.is_packed(db) { + return None; + } + + Some(HighlightModifier::Unsafe) + })(); + + if let Some(modifier) = modifier { + h | modifier + } else { + h + } + } _ => h, } } @@ -697,7 +743,13 @@ fn is_child_of_impl(element: &SyntaxElement) -> bool { } } -fn highlight_name(db: &RootDatabase, def: Definition, possibly_unsafe: bool) -> Highlight { +fn highlight_name( + sema: &Semantics, + db: &RootDatabase, + def: Definition, + name_ref: Option, + possibly_unsafe: bool, +) -> Highlight { match def { Definition::Macro(_) => HighlightTag::Macro, Definition::Field(field) => { @@ -716,6 +768,29 @@ fn highlight_name(db: &RootDatabase, def: Definition, possibly_unsafe: bool) -> let mut h = HighlightTag::Function.into(); if func.is_unsafe(db) { h |= HighlightModifier::Unsafe; + } else { + (|| { + let method_call_expr = + name_ref?.syntax().parent().and_then(ast::MethodCallExpr::cast)?; + let expr = method_call_expr.expr()?; + let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr { + Some(field_expr) + } else { + None + }?; + let ty = sema.type_of_expr(&field_expr.expr()?)?; + if !ty.is_packed(db) { + return None; + } + + let func = sema.resolve_method_call(&method_call_expr)?; + if func.self_param(db)?.is_ref { + Some(HighlightModifier::Unsafe) + } else { + None + } + })() + .map(|modifier| h |= modifier); } return h; } @@ -787,8 +862,33 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics return default.into(), }; - let tag = match parent.kind() { - METHOD_CALL_EXPR => HighlightTag::Function, + match parent.kind() { + METHOD_CALL_EXPR => { + let mut h = Highlight::new(HighlightTag::Function); + let modifier: Option = (|| { + let method_call_expr = ast::MethodCallExpr::cast(parent)?; + let expr = method_call_expr.expr()?; + let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr { + field_expr + } else { + return None; + }; + + let expr = field_expr.expr()?; + let ty = sema.type_of_expr(&expr)?; + if ty.is_packed(sema.db) { + Some(HighlightModifier::Unsafe) + } else { + None + } + })(); + + if let Some(modifier) = modifier { + h |= modifier; + } + + h + } FIELD_EXPR => { let h = HighlightTag::Field; let is_union = ast::FieldExpr::cast(parent) @@ -801,7 +901,7 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics { let path = match parent.parent().and_then(ast::Path::cast) { @@ -826,18 +926,15 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics HighlightTag::Function, - _ => { - if name.text().chars().next().unwrap_or_default().is_uppercase() { - HighlightTag::Struct - } else { - HighlightTag::Constant - } + CALL_EXPR => HighlightTag::Function.into(), + _ => if name.text().chars().next().unwrap_or_default().is_uppercase() { + HighlightTag::Struct.into() + } else { + HighlightTag::Constant } + .into(), } } - _ => default, - }; - - tag.into() + _ => default.into(), + } } -- cgit v1.2.3 From aca3d6c57ec2c668cdb51eca34d6f7bc8fa7412b Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sat, 27 Jun 2020 17:28:07 -0400 Subject: Deduplicate unsafe method call into a single function --- crates/ra_ide/src/syntax_highlighting.rs | 72 ++++++++++++++------------------ 1 file changed, 31 insertions(+), 41 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 454fef39c..02b16b13c 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -743,6 +743,26 @@ fn is_child_of_impl(element: &SyntaxElement) -> bool { } } +fn is_method_call_unsafe( + sema: &Semantics, + method_call_expr: ast::MethodCallExpr, +) -> Option<()> { + let expr = method_call_expr.expr()?; + let field_expr = + if let ast::Expr::FieldExpr(field_expr) = expr { field_expr } else { return None }; + let ty = sema.type_of_expr(&field_expr.expr()?)?; + if !ty.is_packed(sema.db) { + return None; + } + + let func = sema.resolve_method_call(&method_call_expr)?; + if func.self_param(sema.db)?.is_ref { + Some(()) + } else { + None + } +} + fn highlight_name( sema: &Semantics, db: &RootDatabase, @@ -769,28 +789,13 @@ fn highlight_name( if func.is_unsafe(db) { h |= HighlightModifier::Unsafe; } else { - (|| { - let method_call_expr = - name_ref?.syntax().parent().and_then(ast::MethodCallExpr::cast)?; - let expr = method_call_expr.expr()?; - let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr { - Some(field_expr) - } else { - None - }?; - let ty = sema.type_of_expr(&field_expr.expr()?)?; - if !ty.is_packed(db) { - return None; - } - - let func = sema.resolve_method_call(&method_call_expr)?; - if func.self_param(db)?.is_ref { - Some(HighlightModifier::Unsafe) - } else { - None - } - })() - .map(|modifier| h |= modifier); + let is_unsafe = name_ref + .and_then(|name_ref| name_ref.syntax().parent()) + .and_then(ast::MethodCallExpr::cast) + .and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr)); + if is_unsafe.is_some() { + h |= HighlightModifier::Unsafe; + } } return h; } @@ -865,26 +870,11 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics { let mut h = Highlight::new(HighlightTag::Function); - let modifier: Option = (|| { - let method_call_expr = ast::MethodCallExpr::cast(parent)?; - let expr = method_call_expr.expr()?; - let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr { - field_expr - } else { - return None; - }; - - let expr = field_expr.expr()?; - let ty = sema.type_of_expr(&expr)?; - if ty.is_packed(sema.db) { - Some(HighlightModifier::Unsafe) - } else { - None - } - })(); + let is_unsafe = ast::MethodCallExpr::cast(parent) + .and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr)); - if let Some(modifier) = modifier { - h |= modifier; + if is_unsafe.is_some() { + h |= HighlightModifier::Unsafe; } h -- cgit v1.2.3 From c5cc24cb312c70159e63315ea49769b575e8cb65 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sun, 28 Jun 2020 16:04:00 -0400 Subject: Revert function structs back to using bool to track self param, use first param for self information in syntax highlighting instead --- crates/ra_ide/src/syntax_highlighting.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 02b16b13c..d5a5f69cc 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -4,7 +4,7 @@ mod injection; #[cfg(test)] mod tests; -use hir::{Name, Semantics, VariantDef}; +use hir::{Name, Semantics, TypeRef, VariantDef}; use ra_ide_db::{ defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, RootDatabase, @@ -756,8 +756,13 @@ fn is_method_call_unsafe( } let func = sema.resolve_method_call(&method_call_expr)?; - if func.self_param(sema.db)?.is_ref { - Some(()) + if func.has_self_param(sema.db) { + let params = func.params(sema.db); + if matches!(params.into_iter().next(), Some(TypeRef::Reference(..))) { + Some(()) + } else { + None + } } else { None } -- cgit v1.2.3 From 08182aa9fad4021e60cdc80ee0a578929507e115 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sun, 19 Jul 2020 11:45:46 -0400 Subject: Move unsafe packed ref logic to Semantics, use `Attrs::by_key` to simplify repr attr lookup --- crates/ra_ide/src/syntax_highlighting.rs | 34 ++------------------------------ 1 file changed, 2 insertions(+), 32 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index d5a5f69cc..cf93205b6 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -671,41 +671,11 @@ fn highlight_element( T![ref] => { let modifier: Option = (|| { let bind_pat = element.parent().and_then(ast::BindPat::cast)?; - let parent = bind_pat.syntax().parent()?; - - let ty = if let Some(pat_list) = - ast::RecordFieldPatList::cast(parent.clone()) - { - let record_pat = - pat_list.syntax().parent().and_then(ast::RecordPat::cast)?; - sema.type_of_pat(&ast::Pat::RecordPat(record_pat)) - } else if let Some(let_stmt) = ast::LetStmt::cast(parent.clone()) { - let field_expr = - if let ast::Expr::FieldExpr(field_expr) = let_stmt.initializer()? { - field_expr - } else { - return None; - }; - - sema.type_of_expr(&field_expr.expr()?) - } else if let Some(record_field_pat) = ast::RecordFieldPat::cast(parent) { - let record_pat = record_field_pat - .syntax() - .parent() - .and_then(ast::RecordFieldPatList::cast)? - .syntax() - .parent() - .and_then(ast::RecordPat::cast)?; - sema.type_of_pat(&ast::Pat::RecordPat(record_pat)) + if sema.is_unsafe_pat(&ast::Pat::BindPat(bind_pat)) { + Some(HighlightModifier::Unsafe) } else { None - }?; - - if !ty.is_packed(db) { - return None; } - - Some(HighlightModifier::Unsafe) })(); if let Some(modifier) = modifier { -- cgit v1.2.3 From a6af0272f7bf129a3063cdd7096f685fc58438e6 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 23 Jul 2020 10:11:37 -0400 Subject: Move semantic logic into Semantics, fix missing tag for safe amp operator, using functional methods rather than clunky inline closure --- crates/ra_ide/src/syntax_highlighting.rs | 89 ++++++++++---------------------- 1 file changed, 28 insertions(+), 61 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index cf93205b6..e29f65a78 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -565,29 +565,21 @@ fn highlight_element( _ => h, } } - T![&] => { - let ref_expr = element.parent().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 = sema.type_of_expr(&expr)?; - if !ty.is_packed(db) { - return None; - } - - // FIXME This needs layout computation to be correct. It will highlight - // more than it should with the current implementation. - - HighlightTag::Operator | HighlightModifier::Unsafe - } p if p.is_punct() => match p { - T![::] | T![->] | T![=>] | T![&] | T![..] | T![=] | T![@] => { - HighlightTag::Operator.into() + T![&] => { + let h = HighlightTag::Operator.into(); + let is_unsafe = element + .parent() + .and_then(ast::RefExpr::cast) + .map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr)) + .unwrap_or(false); + if is_unsafe { + h | HighlightModifier::Unsafe + } else { + h + } } + T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] => HighlightTag::Operator.into(), T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { HighlightTag::Macro.into() } @@ -668,22 +660,18 @@ fn highlight_element( HighlightTag::SelfKeyword.into() } } - T![ref] => { - let modifier: Option = (|| { - let bind_pat = element.parent().and_then(ast::BindPat::cast)?; - if sema.is_unsafe_pat(&ast::Pat::BindPat(bind_pat)) { + T![ref] => element + .parent() + .and_then(ast::BindPat::cast) + .and_then(|bind_pat| { + if sema.is_unsafe_bind_pat(&bind_pat) { Some(HighlightModifier::Unsafe) } else { None } - })(); - - if let Some(modifier) = modifier { - h | modifier - } else { - h - } - } + }) + .map(|modifier| h | modifier) + .unwrap_or(h), _ => h, } } @@ -713,31 +701,6 @@ fn is_child_of_impl(element: &SyntaxElement) -> bool { } } -fn is_method_call_unsafe( - sema: &Semantics, - method_call_expr: ast::MethodCallExpr, -) -> Option<()> { - let expr = method_call_expr.expr()?; - let field_expr = - if let ast::Expr::FieldExpr(field_expr) = expr { field_expr } else { return None }; - let ty = sema.type_of_expr(&field_expr.expr()?)?; - if !ty.is_packed(sema.db) { - return None; - } - - let func = sema.resolve_method_call(&method_call_expr)?; - if func.has_self_param(sema.db) { - let params = func.params(sema.db); - if matches!(params.into_iter().next(), Some(TypeRef::Reference(..))) { - Some(()) - } else { - None - } - } else { - None - } -} - fn highlight_name( sema: &Semantics, db: &RootDatabase, @@ -767,7 +730,7 @@ fn highlight_name( let is_unsafe = name_ref .and_then(|name_ref| name_ref.syntax().parent()) .and_then(ast::MethodCallExpr::cast) - .and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr)); + .and_then(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)); if is_unsafe.is_some() { h |= HighlightModifier::Unsafe; } @@ -846,7 +809,7 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics { let mut h = Highlight::new(HighlightTag::Function); let is_unsafe = ast::MethodCallExpr::cast(parent) - .and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr)); + .and_then(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)); if is_unsafe.is_some() { h |= HighlightModifier::Unsafe; @@ -866,7 +829,11 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics { let path = match parent.parent().and_then(ast::Path::cast) { -- cgit v1.2.3 From 39fdd41df4052cef5da4876067ae28615012476b Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 23 Jul 2020 18:31:28 -0400 Subject: Return bool from is_unsafe_method_call and cleanup usages --- crates/ra_ide/src/syntax_highlighting.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index e29f65a78..4527885e9 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -730,8 +730,9 @@ fn highlight_name( let is_unsafe = name_ref .and_then(|name_ref| name_ref.syntax().parent()) .and_then(ast::MethodCallExpr::cast) - .and_then(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)); - if is_unsafe.is_some() { + .map(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)) + .unwrap_or(false); + if is_unsafe { h |= HighlightModifier::Unsafe; } } @@ -809,9 +810,9 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics { let mut h = Highlight::new(HighlightTag::Function); let is_unsafe = ast::MethodCallExpr::cast(parent) - .and_then(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)); - - if is_unsafe.is_some() { + .map(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)) + .unwrap_or(false); + if is_unsafe { h |= HighlightModifier::Unsafe; } -- cgit v1.2.3 From 2199d0cda9c745ecb460dd987b8da982d02bc130 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Fri, 7 Aug 2020 10:40:09 -0400 Subject: Fix type names broken by rebase, redo expected test because of rebase --- crates/ra_ide/src/syntax_highlighting.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 4527885e9..c62bb3f1a 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -662,9 +662,9 @@ fn highlight_element( } T![ref] => element .parent() - .and_then(ast::BindPat::cast) - .and_then(|bind_pat| { - if sema.is_unsafe_bind_pat(&bind_pat) { + .and_then(ast::IdentPat::cast) + .and_then(|ident_pat| { + if sema.is_unsafe_ident_pat(&ident_pat) { Some(HighlightModifier::Unsafe) } else { None -- cgit v1.2.3 From 72baf1acdd544c645fd69c16967b91be9e75371b Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sun, 9 Aug 2020 18:54:04 -0400 Subject: Remove unused import left behind after rebasing --- crates/ra_ide/src/syntax_highlighting.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide/src/syntax_highlighting.rs') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index c62bb3f1a..c10e15db8 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -4,7 +4,7 @@ mod injection; #[cfg(test)] mod tests; -use hir::{Name, Semantics, TypeRef, VariantDef}; +use hir::{Name, Semantics, VariantDef}; use ra_ide_db::{ defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, RootDatabase, -- cgit v1.2.3