diff options
author | Paul Daniel Faria <[email protected]> | 2020-07-30 16:07:13 +0100 |
---|---|---|
committer | Paul Daniel Faria <[email protected]> | 2020-08-16 15:22:51 +0100 |
commit | a044ff0138d6bff9406b94de89fde43e7672ee1b (patch) | |
tree | c90b7a41f15d86b6bf7b6bd4fd4c9c8496197ae3 /crates | |
parent | 7009d5ee2bff53b9beb555b1572c97ab3882cd98 (diff) |
Mark mutating functions with `mutable` modifier, and owning functions with `consuming`.
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir/src/code_model.rs | 15 | ||||
-rw-r--r-- | crates/hir/src/semantics.rs | 4 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting.rs | 42 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tests.rs | 9 | ||||
-rw-r--r-- | crates/ide/test_data/highlighting.html | 15 |
5 files changed, 70 insertions, 15 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index c442654dd..31d5276b0 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -670,6 +670,21 @@ impl Function { | |||
670 | db.function_data(self.id).has_self_param | 670 | db.function_data(self.id).has_self_param |
671 | } | 671 | } |
672 | 672 | ||
673 | pub fn mutability_of_self_param(self, db: &dyn HirDatabase) -> Option<Mutability> { | ||
674 | let func_data = db.function_data(self.id); | ||
675 | if !func_data.has_self_param { | ||
676 | return None; | ||
677 | } | ||
678 | |||
679 | func_data.params.first().and_then(|param| { | ||
680 | if let TypeRef::Reference(_, mutability) = param { | ||
681 | Some(*mutability) | ||
682 | } else { | ||
683 | None | ||
684 | } | ||
685 | }) | ||
686 | } | ||
687 | |||
673 | pub fn params(self, db: &dyn HirDatabase) -> Vec<TypeRef> { | 688 | pub fn params(self, db: &dyn HirDatabase) -> Vec<TypeRef> { |
674 | db.function_data(self.id).params.clone() | 689 | db.function_data(self.id).params.clone() |
675 | } | 690 | } |
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index c693176fa..9f23315c3 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs | |||
@@ -267,7 +267,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
267 | self.imp.assert_contains_node(node) | 267 | self.imp.assert_contains_node(node) |
268 | } | 268 | } |
269 | 269 | ||
270 | pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool { | 270 | pub fn is_unsafe_method_call(&self, method_call_expr: &ast::MethodCallExpr) -> bool { |
271 | self.imp.is_unsafe_method_call(method_call_expr) | 271 | self.imp.is_unsafe_method_call(method_call_expr) |
272 | } | 272 | } |
273 | 273 | ||
@@ -571,7 +571,7 @@ impl<'db> SemanticsImpl<'db> { | |||
571 | InFile::new(file_id, node) | 571 | InFile::new(file_id, node) |
572 | } | 572 | } |
573 | 573 | ||
574 | pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool { | 574 | pub fn is_unsafe_method_call(&self, method_call_expr: &ast::MethodCallExpr) -> bool { |
575 | method_call_expr | 575 | method_call_expr |
576 | .expr() | 576 | .expr() |
577 | .and_then(|expr| { | 577 | .and_then(|expr| { |
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 5d7c7e8d0..15a78a614 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs | |||
@@ -4,7 +4,7 @@ mod injection; | |||
4 | #[cfg(test)] | 4 | #[cfg(test)] |
5 | mod tests; | 5 | mod tests; |
6 | 6 | ||
7 | use hir::{Name, Semantics, VariantDef}; | 7 | use hir::{Mutability, Name, Semantics, VariantDef}; |
8 | use ide_db::{ | 8 | use ide_db::{ |
9 | defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, | 9 | defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, |
10 | RootDatabase, | 10 | RootDatabase, |
@@ -729,13 +729,23 @@ fn highlight_name( | |||
729 | let is_unsafe = name_ref | 729 | let is_unsafe = name_ref |
730 | .and_then(|name_ref| name_ref.syntax().parent()) | 730 | .and_then(|name_ref| name_ref.syntax().parent()) |
731 | .and_then(ast::MethodCallExpr::cast) | 731 | .and_then(ast::MethodCallExpr::cast) |
732 | .map(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)) | 732 | .map(|method_call_expr| sema.is_unsafe_method_call(&method_call_expr)) |
733 | .unwrap_or(false); | 733 | .unwrap_or(false); |
734 | if is_unsafe { | 734 | if is_unsafe { |
735 | h |= HighlightModifier::Unsafe; | 735 | h |= HighlightModifier::Unsafe; |
736 | } | 736 | } |
737 | } | 737 | } |
738 | return h; | 738 | return if func.has_self_param(db) { |
739 | match func.mutability_of_self_param(db) { | ||
740 | Some(mutability) => match mutability { | ||
741 | Mutability::Mut => h | HighlightModifier::Mutable, | ||
742 | Mutability::Shared => h, | ||
743 | }, | ||
744 | None => h | HighlightModifier::Consuming, | ||
745 | } | ||
746 | } else { | ||
747 | h | ||
748 | }; | ||
739 | } | 749 | } |
740 | hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct, | 750 | hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct, |
741 | hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Enum, | 751 | hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Enum, |
@@ -808,14 +818,26 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas | |||
808 | match parent.kind() { | 818 | match parent.kind() { |
809 | METHOD_CALL_EXPR => { | 819 | METHOD_CALL_EXPR => { |
810 | let mut h = Highlight::new(HighlightTag::Function); | 820 | let mut h = Highlight::new(HighlightTag::Function); |
811 | let is_unsafe = ast::MethodCallExpr::cast(parent) | 821 | ast::MethodCallExpr::cast(parent) |
812 | .map(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)) | 822 | .and_then(|method_call_expr| { |
813 | .unwrap_or(false); | 823 | if sema.is_unsafe_method_call(&method_call_expr) { |
814 | if is_unsafe { | 824 | h |= HighlightModifier::Unsafe; |
815 | h |= HighlightModifier::Unsafe; | 825 | } |
816 | } | 826 | |
827 | let func = sema.resolve_method_call(&method_call_expr)?; | ||
828 | if !func.has_self_param(sema.db) { | ||
829 | return Some(h); | ||
830 | } | ||
817 | 831 | ||
818 | h | 832 | Some(match func.mutability_of_self_param(sema.db) { |
833 | Some(mutability) => match mutability { | ||
834 | Mutability::Mut => h | HighlightModifier::Mutable, | ||
835 | Mutability::Shared => h, | ||
836 | }, | ||
837 | None => h | HighlightModifier::Consuming, | ||
838 | }) | ||
839 | }) | ||
840 | .unwrap_or_else(|| h) | ||
819 | } | 841 | } |
820 | FIELD_EXPR => { | 842 | FIELD_EXPR => { |
821 | let h = HighlightTag::Field; | 843 | let h = HighlightTag::Field; |
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 94f37d773..6cb955d29 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs | |||
@@ -36,6 +36,10 @@ impl Foo { | |||
36 | fn qux(&mut self) { | 36 | fn qux(&mut self) { |
37 | self.x = 0; | 37 | self.x = 0; |
38 | } | 38 | } |
39 | |||
40 | fn quop(&self) -> i32 { | ||
41 | self.x | ||
42 | } | ||
39 | } | 43 | } |
40 | 44 | ||
41 | static mut STATIC_MUT: i32 = 0; | 45 | static mut STATIC_MUT: i32 = 0; |
@@ -87,6 +91,11 @@ fn main() { | |||
87 | let Foo { x: z, y } = Foo { x: z, y }; | 91 | let Foo { x: z, y } = Foo { x: z, y }; |
88 | 92 | ||
89 | y; | 93 | y; |
94 | |||
95 | let mut foo = Foo { x, y: x }; | ||
96 | foo.quop(); | ||
97 | foo.qux(); | ||
98 | foo.baz(); | ||
90 | } | 99 | } |
91 | 100 | ||
92 | enum Option<T> { | 101 | enum Option<T> { |
diff --git a/crates/ide/test_data/highlighting.html b/crates/ide/test_data/highlighting.html index 8e0160eee..2aad06a92 100644 --- a/crates/ide/test_data/highlighting.html +++ b/crates/ide/test_data/highlighting.html | |||
@@ -55,13 +55,17 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
55 | <span class="punctuation">}</span> | 55 | <span class="punctuation">}</span> |
56 | 56 | ||
57 | <span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span> | 57 | <span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span> |
58 | <span class="keyword">fn</span> <span class="function declaration">baz</span><span class="punctuation">(</span><span class="keyword">mut</span> <span class="self_keyword mutable">self</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="builtin_type">i32</span> <span class="punctuation">{</span> | 58 | <span class="keyword">fn</span> <span class="function declaration consuming">baz</span><span class="punctuation">(</span><span class="keyword">mut</span> <span class="self_keyword mutable">self</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="builtin_type">i32</span> <span class="punctuation">{</span> |
59 | <span class="self_keyword">self</span><span class="punctuation">.</span><span class="field">x</span> | 59 | <span class="self_keyword">self</span><span class="punctuation">.</span><span class="field">x</span> |
60 | <span class="punctuation">}</span> | 60 | <span class="punctuation">}</span> |
61 | 61 | ||
62 | <span class="keyword">fn</span> <span class="function declaration">qux</span><span class="punctuation">(</span><span class="operator">&</span><span class="keyword">mut</span> <span class="self_keyword mutable">self</span><span class="punctuation">)</span> <span class="punctuation">{</span> | 62 | <span class="keyword">fn</span> <span class="function declaration mutable">qux</span><span class="punctuation">(</span><span class="operator">&</span><span class="keyword">mut</span> <span class="self_keyword mutable">self</span><span class="punctuation">)</span> <span class="punctuation">{</span> |
63 | <span class="self_keyword mutable">self</span><span class="punctuation">.</span><span class="field">x</span> <span class="operator">=</span> <span class="numeric_literal">0</span><span class="punctuation">;</span> | 63 | <span class="self_keyword mutable">self</span><span class="punctuation">.</span><span class="field">x</span> <span class="operator">=</span> <span class="numeric_literal">0</span><span class="punctuation">;</span> |
64 | <span class="punctuation">}</span> | 64 | <span class="punctuation">}</span> |
65 | |||
66 | <span class="keyword">fn</span> <span class="function declaration">quop</span><span class="punctuation">(</span><span class="operator">&</span><span class="self_keyword">self</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="builtin_type">i32</span> <span class="punctuation">{</span> | ||
67 | <span class="self_keyword">self</span><span class="punctuation">.</span><span class="field">x</span> | ||
68 | <span class="punctuation">}</span> | ||
65 | <span class="punctuation">}</span> | 69 | <span class="punctuation">}</span> |
66 | 70 | ||
67 | <span class="keyword">static</span> <span class="keyword">mut</span> <span class="static declaration mutable unsafe">STATIC_MUT</span><span class="punctuation">:</span> <span class="builtin_type">i32</span> <span class="operator">=</span> <span class="numeric_literal">0</span><span class="punctuation">;</span> | 71 | <span class="keyword">static</span> <span class="keyword">mut</span> <span class="static declaration mutable unsafe">STATIC_MUT</span><span class="punctuation">:</span> <span class="builtin_type">i32</span> <span class="operator">=</span> <span class="numeric_literal">0</span><span class="punctuation">;</span> |
@@ -113,6 +117,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
113 | <span class="keyword">let</span> <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="field">x</span><span class="punctuation">:</span> <span class="variable declaration">z</span><span class="punctuation">,</span> <span class="field">y</span> <span class="punctuation">}</span> <span class="operator">=</span> <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="field">x</span><span class="punctuation">:</span> <span class="variable">z</span><span class="punctuation">,</span> <span class="field">y</span> <span class="punctuation">}</span><span class="punctuation">;</span> | 117 | <span class="keyword">let</span> <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="field">x</span><span class="punctuation">:</span> <span class="variable declaration">z</span><span class="punctuation">,</span> <span class="field">y</span> <span class="punctuation">}</span> <span class="operator">=</span> <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="field">x</span><span class="punctuation">:</span> <span class="variable">z</span><span class="punctuation">,</span> <span class="field">y</span> <span class="punctuation">}</span><span class="punctuation">;</span> |
114 | 118 | ||
115 | <span class="variable">y</span><span class="punctuation">;</span> | 119 | <span class="variable">y</span><span class="punctuation">;</span> |
120 | |||
121 | <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">foo</span> <span class="operator">=</span> <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="field">x</span><span class="punctuation">,</span> <span class="field">y</span><span class="punctuation">:</span> <span class="variable mutable">x</span> <span class="punctuation">}</span><span class="punctuation">;</span> | ||
122 | <span class="variable mutable">foo</span><span class="punctuation">.</span><span class="function">quop</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span> | ||
123 | <span class="variable mutable">foo</span><span class="punctuation">.</span><span class="function mutable">qux</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span> | ||
124 | <span class="variable mutable">foo</span><span class="punctuation">.</span><span class="function consuming">baz</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span> | ||
116 | <span class="punctuation">}</span> | 125 | <span class="punctuation">}</span> |
117 | 126 | ||
118 | <span class="keyword">enum</span> <span class="enum declaration">Option</span><span class="punctuation"><</span><span class="type_param declaration">T</span><span class="punctuation">></span> <span class="punctuation">{</span> | 127 | <span class="keyword">enum</span> <span class="enum declaration">Option</span><span class="punctuation"><</span><span class="type_param declaration">T</span><span class="punctuation">></span> <span class="punctuation">{</span> |
@@ -122,7 +131,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
122 | <span class="keyword">use</span> <span class="enum">Option</span><span class="operator">::</span><span class="punctuation">*</span><span class="punctuation">;</span> | 131 | <span class="keyword">use</span> <span class="enum">Option</span><span class="operator">::</span><span class="punctuation">*</span><span class="punctuation">;</span> |
123 | 132 | ||
124 | <span class="keyword">impl</span><span class="punctuation"><</span><span class="type_param declaration">T</span><span class="punctuation">></span> <span class="enum">Option</span><span class="punctuation"><</span><span class="type_param">T</span><span class="punctuation">></span> <span class="punctuation">{</span> | 133 | <span class="keyword">impl</span><span class="punctuation"><</span><span class="type_param declaration">T</span><span class="punctuation">></span> <span class="enum">Option</span><span class="punctuation"><</span><span class="type_param">T</span><span class="punctuation">></span> <span class="punctuation">{</span> |
125 | <span class="keyword">fn</span> <span class="function declaration">and</span><span class="punctuation"><</span><span class="type_param declaration">U</span><span class="punctuation">></span><span class="punctuation">(</span><span class="self_keyword">self</span><span class="punctuation">,</span> <span class="value_param declaration">other</span><span class="punctuation">:</span> <span class="enum">Option</span><span class="punctuation"><</span><span class="type_param">U</span><span class="punctuation">></span><span class="punctuation">)</span> <span class="operator">-></span> <span class="enum">Option</span><span class="punctuation"><</span><span class="punctuation">(</span><span class="type_param">T</span><span class="punctuation">,</span> <span class="type_param">U</span><span class="punctuation">)</span><span class="punctuation">></span> <span class="punctuation">{</span> | 134 | <span class="keyword">fn</span> <span class="function declaration consuming">and</span><span class="punctuation"><</span><span class="type_param declaration">U</span><span class="punctuation">></span><span class="punctuation">(</span><span class="self_keyword">self</span><span class="punctuation">,</span> <span class="value_param declaration">other</span><span class="punctuation">:</span> <span class="enum">Option</span><span class="punctuation"><</span><span class="type_param">U</span><span class="punctuation">></span><span class="punctuation">)</span> <span class="operator">-></span> <span class="enum">Option</span><span class="punctuation"><</span><span class="punctuation">(</span><span class="type_param">T</span><span class="punctuation">,</span> <span class="type_param">U</span><span class="punctuation">)</span><span class="punctuation">></span> <span class="punctuation">{</span> |
126 | <span class="keyword control">match</span> <span class="value_param">other</span> <span class="punctuation">{</span> | 135 | <span class="keyword control">match</span> <span class="value_param">other</span> <span class="punctuation">{</span> |
127 | <span class="enum_variant">None</span> <span class="operator">=></span> <span class="macro">unimplemented!</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">,</span> | 136 | <span class="enum_variant">None</span> <span class="operator">=></span> <span class="macro">unimplemented!</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">,</span> |
128 | <span class="variable declaration">Nope</span> <span class="operator">=></span> <span class="variable">Nope</span><span class="punctuation">,</span> | 137 | <span class="variable declaration">Nope</span> <span class="operator">=></span> <span class="variable">Nope</span><span class="punctuation">,</span> |