diff options
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/syntax_highlighting.rs | 41 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/test_data/highlighting.html | 13 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tests.rs | 13 |
3 files changed, 53 insertions, 14 deletions
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 25d6f7abd..4fa9e638a 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs | |||
@@ -13,8 +13,8 @@ use rustc_hash::FxHashMap; | |||
13 | use syntax::{ | 13 | use syntax::{ |
14 | ast::{self, HasFormatSpecifier}, | 14 | ast::{self, HasFormatSpecifier}, |
15 | AstNode, AstToken, Direction, NodeOrToken, SyntaxElement, | 15 | AstNode, AstToken, Direction, NodeOrToken, SyntaxElement, |
16 | SyntaxKind::*, | 16 | SyntaxKind::{self, *}, |
17 | TextRange, WalkEvent, T, | 17 | SyntaxNode, TextRange, WalkEvent, T, |
18 | }; | 18 | }; |
19 | 19 | ||
20 | use crate::FileId; | 20 | use crate::FileId; |
@@ -454,6 +454,23 @@ fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> { | |||
454 | Some(TextRange::new(range_start, range_end)) | 454 | Some(TextRange::new(range_start, range_end)) |
455 | } | 455 | } |
456 | 456 | ||
457 | /// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly. | ||
458 | fn parents_match(mut node: SyntaxNode, mut kinds: &[SyntaxKind]) -> bool { | ||
459 | while let (Some(parent), [kind, rest @ ..]) = (&node.parent(), kinds) { | ||
460 | if parent.kind() != *kind { | ||
461 | return false; | ||
462 | } | ||
463 | |||
464 | // FIXME: Would be nice to get parent out of the match, but binding by-move and by-value | ||
465 | // in the same pattern is unstable: rust-lang/rust#68354. | ||
466 | node = node.parent().unwrap(); | ||
467 | kinds = rest; | ||
468 | } | ||
469 | |||
470 | // Only true if we matched all expected kinds | ||
471 | kinds.len() == 0 | ||
472 | } | ||
473 | |||
457 | fn highlight_element( | 474 | fn highlight_element( |
458 | sema: &Semantics<RootDatabase>, | 475 | sema: &Semantics<RootDatabase>, |
459 | bindings_shadow_count: &mut FxHashMap<Name, u32>, | 476 | bindings_shadow_count: &mut FxHashMap<Name, u32>, |
@@ -522,6 +539,26 @@ fn highlight_element( | |||
522 | 539 | ||
523 | let mut h = highlight_def(db, def); | 540 | let mut h = highlight_def(db, def); |
524 | 541 | ||
542 | // When lvalues are passed as arguments and they're not Copy, then mark | ||
543 | // them as Consuming. | ||
544 | if parents_match( | ||
545 | name_ref.syntax().clone(), | ||
546 | &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST], | ||
547 | ) { | ||
548 | let lvalue_ty = if let Definition::Local(local) = &def { | ||
549 | Some(local.ty(db)) | ||
550 | } else if let Definition::SelfType(impl_def) = &def { | ||
551 | Some(impl_def.target_ty(db)) | ||
552 | } else { | ||
553 | None | ||
554 | }; | ||
555 | if let Some(lvalue_ty) = lvalue_ty { | ||
556 | if !lvalue_ty.is_copy(db) { | ||
557 | h |= HighlightModifier::Consuming; | ||
558 | } | ||
559 | } | ||
560 | } | ||
561 | |||
525 | if let Some(parent) = name_ref.syntax().parent() { | 562 | if let Some(parent) = name_ref.syntax().parent() { |
526 | if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) { | 563 | if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) { |
527 | if let Definition::Field(field) = def { | 564 | if let Definition::Field(field) = def { |
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index d0df2e0ec..cade46348 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html | |||
@@ -61,8 +61,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
61 | <span class="punctuation">}</span> | 61 | <span class="punctuation">}</span> |
62 | 62 | ||
63 | <span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span> | 63 | <span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span> |
64 | <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> | 64 | <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="value_param declaration">f</span><span class="punctuation">:</span> <span class="struct">Foo</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="builtin_type">i32</span> <span class="punctuation">{</span> |
65 | <span class="self_keyword">self</span><span class="punctuation">.</span><span class="field">x</span> | 65 | <span class="value_param">f</span><span class="punctuation">.</span><span class="function consuming">baz</span><span class="punctuation">(</span><span class="self_keyword">self</span><span class="punctuation">)</span> |
66 | <span class="punctuation">}</span> | 66 | <span class="punctuation">}</span> |
67 | 67 | ||
68 | <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> | 68 | <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> |
@@ -80,8 +80,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
80 | <span class="punctuation">}</span> | 80 | <span class="punctuation">}</span> |
81 | 81 | ||
82 | <span class="keyword">impl</span> <span class="struct">FooCopy</span> <span class="punctuation">{</span> | 82 | <span class="keyword">impl</span> <span class="struct">FooCopy</span> <span class="punctuation">{</span> |
83 | <span class="keyword">fn</span> <span class="function declaration">baz</span><span class="punctuation">(</span><span class="self_keyword">self</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="builtin_type">u32</span> <span class="punctuation">{</span> | 83 | <span class="keyword">fn</span> <span class="function declaration">baz</span><span class="punctuation">(</span><span class="self_keyword">self</span><span class="punctuation">,</span> <span class="value_param declaration">f</span><span class="punctuation">:</span> <span class="struct">FooCopy</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="builtin_type">u32</span> <span class="punctuation">{</span> |
84 | <span class="self_keyword">self</span><span class="punctuation">.</span><span class="field">x</span> | 84 | <span class="value_param">f</span><span class="punctuation">.</span><span class="function">baz</span><span class="punctuation">(</span><span class="self_keyword">self</span><span class="punctuation">)</span> |
85 | <span class="punctuation">}</span> | 85 | <span class="punctuation">}</span> |
86 | 86 | ||
87 | <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> | 87 | <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> |
@@ -144,14 +144,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
144 | <span class="variable">y</span><span class="punctuation">;</span> | 144 | <span class="variable">y</span><span class="punctuation">;</span> |
145 | 145 | ||
146 | <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> | 146 | <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> |
147 | <span class="keyword">let</span> <span class="variable declaration">foo2</span> <span class="operator">=</span> <span class="variable mutable">foo</span><span class="punctuation">.</span><span class="unresolved_reference">clone</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span> | ||
147 | <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> | 148 | <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> |
148 | <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> | 149 | <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> |
149 | <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> | 150 | <span class="variable mutable">foo</span><span class="punctuation">.</span><span class="function consuming">baz</span><span class="punctuation">(</span><span class="variable consuming">foo2</span><span class="punctuation">)</span><span class="punctuation">;</span> |
150 | 151 | ||
151 | <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">copy</span> <span class="operator">=</span> <span class="struct">FooCopy</span> <span class="punctuation">{</span> <span class="field">x</span> <span class="punctuation">}</span><span class="punctuation">;</span> | 152 | <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">copy</span> <span class="operator">=</span> <span class="struct">FooCopy</span> <span class="punctuation">{</span> <span class="field">x</span> <span class="punctuation">}</span><span class="punctuation">;</span> |
152 | <span class="variable mutable">copy</span><span class="punctuation">.</span><span class="function">quop</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span> | 153 | <span class="variable mutable">copy</span><span class="punctuation">.</span><span class="function">quop</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span> |
153 | <span class="variable mutable">copy</span><span class="punctuation">.</span><span class="function mutable">qux</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span> | 154 | <span class="variable mutable">copy</span><span class="punctuation">.</span><span class="function mutable">qux</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span> |
154 | <span class="variable mutable">copy</span><span class="punctuation">.</span><span class="function">baz</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span> | 155 | <span class="variable mutable">copy</span><span class="punctuation">.</span><span class="function">baz</span><span class="punctuation">(</span><span class="variable mutable">copy</span><span class="punctuation">)</span><span class="punctuation">;</span> |
155 | <span class="punctuation">}</span> | 156 | <span class="punctuation">}</span> |
156 | 157 | ||
157 | <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> | 158 | <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> |
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 6f72a29bd..57d4e1252 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs | |||
@@ -35,8 +35,8 @@ impl Bar for Foo { | |||
35 | } | 35 | } |
36 | 36 | ||
37 | impl Foo { | 37 | impl Foo { |
38 | fn baz(mut self) -> i32 { | 38 | fn baz(mut self, f: Foo) -> i32 { |
39 | self.x | 39 | f.baz(self) |
40 | } | 40 | } |
41 | 41 | ||
42 | fn qux(&mut self) { | 42 | fn qux(&mut self) { |
@@ -54,8 +54,8 @@ struct FooCopy { | |||
54 | } | 54 | } |
55 | 55 | ||
56 | impl FooCopy { | 56 | impl FooCopy { |
57 | fn baz(self) -> u32 { | 57 | fn baz(self, f: FooCopy) -> u32 { |
58 | self.x | 58 | f.baz(self) |
59 | } | 59 | } |
60 | 60 | ||
61 | fn qux(&mut self) { | 61 | fn qux(&mut self) { |
@@ -118,14 +118,15 @@ fn main() { | |||
118 | y; | 118 | y; |
119 | 119 | ||
120 | let mut foo = Foo { x, y: x }; | 120 | let mut foo = Foo { x, y: x }; |
121 | let foo2 = foo.clone(); | ||
121 | foo.quop(); | 122 | foo.quop(); |
122 | foo.qux(); | 123 | foo.qux(); |
123 | foo.baz(); | 124 | foo.baz(foo2); |
124 | 125 | ||
125 | let mut copy = FooCopy { x }; | 126 | let mut copy = FooCopy { x }; |
126 | copy.quop(); | 127 | copy.quop(); |
127 | copy.qux(); | 128 | copy.qux(); |
128 | copy.baz(); | 129 | copy.baz(copy); |
129 | } | 130 | } |
130 | 131 | ||
131 | enum Option<T> { | 132 | enum Option<T> { |