]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/syntax_highlighting/highlight.rs
Auto merge of #12490 - yue4u:fix/show-enum-in-fresh-use-tree, r=Veykril
[rust.git] / crates / ide / src / syntax_highlighting / highlight.rs
1 //! Computes color for a single element.
2
3 use hir::{AsAssocItem, HasVisibility, Semantics};
4 use ide_db::{
5     defs::{Definition, IdentClass, NameClass, NameRefClass},
6     FxHashMap, RootDatabase, SymbolKind,
7 };
8 use syntax::{
9     ast, match_ast, AstNode, AstToken, NodeOrToken,
10     SyntaxKind::{self, *},
11     SyntaxNode, SyntaxToken, T,
12 };
13
14 use crate::{
15     syntax_highlighting::tags::{HlOperator, HlPunct},
16     Highlight, HlMod, HlTag,
17 };
18
19 pub(super) fn token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Highlight> {
20     if let Some(comment) = ast::Comment::cast(token.clone()) {
21         let h = HlTag::Comment;
22         return Some(match comment.kind().doc {
23             Some(_) => h | HlMod::Documentation,
24             None => h.into(),
25         });
26     }
27
28     let highlight: Highlight = match token.kind() {
29         STRING | BYTE_STRING => HlTag::StringLiteral.into(),
30         INT_NUMBER if token.parent_ancestors().nth(1).map(|it| it.kind()) == Some(FIELD_EXPR) => {
31             SymbolKind::Field.into()
32         }
33         INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
34         BYTE => HlTag::ByteLiteral.into(),
35         CHAR => HlTag::CharLiteral.into(),
36         IDENT if token.parent().and_then(ast::TokenTree::cast).is_some() => {
37             // from this point on we are inside a token tree, this only happens for identifiers
38             // that were not mapped down into macro invocations
39             HlTag::None.into()
40         }
41         p if p.is_punct() => punctuation(sema, token, p),
42         k if k.is_keyword() => keyword(sema, token, k)?,
43         _ => return None,
44     };
45     Some(highlight)
46 }
47
48 pub(super) fn name_like(
49     sema: &Semantics<RootDatabase>,
50     krate: hir::Crate,
51     bindings_shadow_count: &mut FxHashMap<hir::Name, u32>,
52     syntactic_name_ref_highlighting: bool,
53     name_like: ast::NameLike,
54 ) -> Option<(Highlight, Option<u64>)> {
55     let mut binding_hash = None;
56     let highlight = match name_like {
57         ast::NameLike::NameRef(name_ref) => highlight_name_ref(
58             sema,
59             krate,
60             bindings_shadow_count,
61             &mut binding_hash,
62             syntactic_name_ref_highlighting,
63             name_ref,
64         ),
65         ast::NameLike::Name(name) => {
66             highlight_name(sema, bindings_shadow_count, &mut binding_hash, krate, name)
67         }
68         ast::NameLike::Lifetime(lifetime) => match IdentClass::classify_lifetime(sema, &lifetime) {
69             Some(IdentClass::NameClass(NameClass::Definition(def))) => {
70                 highlight_def(sema, krate, def) | HlMod::Definition
71             }
72             Some(IdentClass::NameRefClass(NameRefClass::Definition(def))) => {
73                 highlight_def(sema, krate, def)
74             }
75             // FIXME: Fallback for 'static and '_, as we do not resolve these yet
76             _ => SymbolKind::LifetimeParam.into(),
77         },
78     };
79     Some((highlight, binding_hash))
80 }
81
82 fn punctuation(sema: &Semantics<RootDatabase>, token: SyntaxToken, kind: SyntaxKind) -> Highlight {
83     let parent = token.parent();
84     let parent_kind = parent.as_ref().map_or(EOF, SyntaxNode::kind);
85     match (kind, parent_kind) {
86         (T![?], _) => HlTag::Operator(HlOperator::Other) | HlMod::ControlFlow,
87         (T![&], BIN_EXPR) => HlOperator::Bitwise.into(),
88         (T![&], _) => {
89             let h = HlTag::Operator(HlOperator::Other).into();
90             let is_unsafe = parent
91                 .and_then(ast::RefExpr::cast)
92                 .map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr));
93             if let Some(true) = is_unsafe {
94                 h | HlMod::Unsafe
95             } else {
96                 h
97             }
98         }
99         (T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.], _) => HlOperator::Other.into(),
100         (T![!], MACRO_CALL | MACRO_RULES) => HlPunct::MacroBang.into(),
101         (T![!], NEVER_TYPE) => HlTag::BuiltinType.into(),
102         (T![!], PREFIX_EXPR) => HlOperator::Logical.into(),
103         (T![*], PTR_TYPE) => HlTag::Keyword.into(),
104         (T![*], PREFIX_EXPR) => {
105             let is_raw_ptr = (|| {
106                 let prefix_expr = parent.and_then(ast::PrefixExpr::cast)?;
107                 let expr = prefix_expr.expr()?;
108                 sema.type_of_expr(&expr)?.original.is_raw_ptr().then(|| ())
109             })();
110             if let Some(()) = is_raw_ptr {
111                 HlTag::Operator(HlOperator::Other) | HlMod::Unsafe
112             } else {
113                 HlOperator::Other.into()
114             }
115         }
116         (T![-], PREFIX_EXPR) => {
117             let prefix_expr = parent.and_then(ast::PrefixExpr::cast).and_then(|e| e.expr());
118             match prefix_expr {
119                 Some(ast::Expr::Literal(_)) => HlTag::NumericLiteral,
120                 _ => HlTag::Operator(HlOperator::Other),
121             }
122             .into()
123         }
124         (T![+] | T![-] | T![*] | T![/] | T![%], BIN_EXPR) => HlOperator::Arithmetic.into(),
125         (T![+=] | T![-=] | T![*=] | T![/=] | T![%=], BIN_EXPR) => {
126             Highlight::from(HlOperator::Arithmetic) | HlMod::Mutable
127         }
128         (T![|] | T![&] | T![!] | T![^] | T![>>] | T![<<], BIN_EXPR) => HlOperator::Bitwise.into(),
129         (T![|=] | T![&=] | T![^=] | T![>>=] | T![<<=], BIN_EXPR) => {
130             Highlight::from(HlOperator::Bitwise) | HlMod::Mutable
131         }
132         (T![&&] | T![||], BIN_EXPR) => HlOperator::Logical.into(),
133         (T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=], BIN_EXPR) => {
134             HlOperator::Comparison.into()
135         }
136         (_, PREFIX_EXPR | BIN_EXPR | RANGE_EXPR | RANGE_PAT | REST_PAT) => HlOperator::Other.into(),
137         (_, ATTR) => HlTag::AttributeBracket.into(),
138         (kind, _) => match kind {
139             T!['['] | T![']'] => HlPunct::Bracket,
140             T!['{'] | T!['}'] => HlPunct::Brace,
141             T!['('] | T![')'] => HlPunct::Parenthesis,
142             T![<] | T![>] => HlPunct::Angle,
143             T![,] => HlPunct::Comma,
144             T![:] => HlPunct::Colon,
145             T![;] => HlPunct::Semi,
146             T![.] => HlPunct::Dot,
147             _ => HlPunct::Other,
148         }
149         .into(),
150     }
151 }
152
153 fn keyword(
154     sema: &Semantics<RootDatabase>,
155     token: SyntaxToken,
156     kind: SyntaxKind,
157 ) -> Option<Highlight> {
158     let h = Highlight::new(HlTag::Keyword);
159     let h = match kind {
160         T![await] => h | HlMod::Async | HlMod::ControlFlow,
161         T![async] => h | HlMod::Async,
162         T![break]
163         | T![continue]
164         | T![else]
165         | T![if]
166         | T![in]
167         | T![loop]
168         | T![match]
169         | T![return]
170         | T![while]
171         | T![yield] => h | HlMod::ControlFlow,
172         T![for] if parent_matches::<ast::ForExpr>(&token) => h | HlMod::ControlFlow,
173         T![unsafe] => h | HlMod::Unsafe,
174         T![true] | T![false] => HlTag::BoolLiteral.into(),
175         // crate is handled just as a token if it's in an `extern crate`
176         T![crate] if parent_matches::<ast::ExternCrate>(&token) => h,
177         // self, crate, super and `Self` are handled as either a Name or NameRef already, unless they
178         // are inside unmapped token trees
179         T![self] | T![crate] | T![super] | T![Self] if parent_matches::<ast::NameRef>(&token) => {
180             return None
181         }
182         T![self] if parent_matches::<ast::Name>(&token) => return None,
183         T![ref] => match token.parent().and_then(ast::IdentPat::cast) {
184             Some(ident) if sema.is_unsafe_ident_pat(&ident) => h | HlMod::Unsafe,
185             _ => h,
186         },
187         _ => h,
188     };
189     Some(h)
190 }
191
192 fn highlight_name_ref(
193     sema: &Semantics<RootDatabase>,
194     krate: hir::Crate,
195     bindings_shadow_count: &mut FxHashMap<hir::Name, u32>,
196     binding_hash: &mut Option<u64>,
197     syntactic_name_ref_highlighting: bool,
198     name_ref: ast::NameRef,
199 ) -> Highlight {
200     let db = sema.db;
201     if let Some(res) = highlight_method_call_by_name_ref(sema, krate, &name_ref) {
202         return res;
203     }
204
205     let name_class = match NameRefClass::classify(sema, &name_ref) {
206         Some(name_kind) => name_kind,
207         None if syntactic_name_ref_highlighting => {
208             return highlight_name_ref_by_syntax(name_ref, sema, krate)
209         }
210         // FIXME: This is required for helper attributes used by proc-macros, as those do not map down
211         // to anything when used.
212         // We can fix this for derive attributes since derive helpers are recorded, but not for
213         // general attributes.
214         None if name_ref.syntax().ancestors().any(|it| it.kind() == ATTR) => {
215             return HlTag::Symbol(SymbolKind::Attribute).into();
216         }
217         None => return HlTag::UnresolvedReference.into(),
218     };
219     let mut h = match name_class {
220         NameRefClass::Definition(def) => {
221             if let Definition::Local(local) = &def {
222                 let name = local.name(db);
223                 let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
224                 *binding_hash = Some(calc_binding_hash(&name, *shadow_count))
225             };
226
227             let mut h = highlight_def(sema, krate, def);
228
229             match def {
230                 Definition::Local(local) if is_consumed_lvalue(name_ref.syntax(), &local, db) => {
231                     h |= HlMod::Consuming;
232                 }
233                 Definition::Trait(trait_) if trait_.is_unsafe(db) => {
234                     if ast::Impl::for_trait_name_ref(&name_ref)
235                         .map_or(false, |impl_| impl_.unsafe_token().is_some())
236                     {
237                         h |= HlMod::Unsafe;
238                     }
239                 }
240                 Definition::Field(field) => {
241                     if let Some(parent) = name_ref.syntax().parent() {
242                         if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
243                             if let hir::VariantDef::Union(_) = field.parent_def(db) {
244                                 h |= HlMod::Unsafe;
245                             }
246                         }
247                     }
248                 }
249                 Definition::Macro(_) => {
250                     if let Some(macro_call) =
251                         ide_db::syntax_helpers::node_ext::full_path_of_name_ref(&name_ref)
252                             .and_then(|it| it.syntax().parent().and_then(ast::MacroCall::cast))
253                     {
254                         if sema.is_unsafe_macro_call(&macro_call) {
255                             h |= HlMod::Unsafe;
256                         }
257                     }
258                 }
259                 _ => (),
260             }
261
262             h
263         }
264         NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
265     };
266
267     h.tag = match name_ref.token_kind() {
268         T![Self] => HlTag::Symbol(SymbolKind::SelfType),
269         T![self] => HlTag::Symbol(SymbolKind::SelfParam),
270         T![super] | T![crate] => HlTag::Keyword,
271         _ => h.tag,
272     };
273     h
274 }
275
276 fn highlight_name(
277     sema: &Semantics<RootDatabase>,
278     bindings_shadow_count: &mut FxHashMap<hir::Name, u32>,
279     binding_hash: &mut Option<u64>,
280     krate: hir::Crate,
281     name: ast::Name,
282 ) -> Highlight {
283     let name_kind = NameClass::classify(sema, &name);
284     if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
285         let name = local.name(sema.db);
286         let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
287         *shadow_count += 1;
288         *binding_hash = Some(calc_binding_hash(&name, *shadow_count))
289     };
290     match name_kind {
291         Some(NameClass::Definition(def)) => {
292             let mut h = highlight_def(sema, krate, def) | HlMod::Definition;
293             if let Definition::Trait(trait_) = &def {
294                 if trait_.is_unsafe(sema.db) {
295                     h |= HlMod::Unsafe;
296                 }
297             }
298             h
299         }
300         Some(NameClass::ConstReference(def)) => highlight_def(sema, krate, def),
301         Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
302             let mut h = HlTag::Symbol(SymbolKind::Field).into();
303             if let hir::VariantDef::Union(_) = field_ref.parent_def(sema.db) {
304                 h |= HlMod::Unsafe;
305             }
306             h
307         }
308         None => highlight_name_by_syntax(name) | HlMod::Definition,
309     }
310 }
311
312 fn calc_binding_hash(name: &hir::Name, shadow_count: u32) -> u64 {
313     fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
314         use std::{collections::hash_map::DefaultHasher, hash::Hasher};
315
316         let mut hasher = DefaultHasher::new();
317         x.hash(&mut hasher);
318         hasher.finish()
319     }
320
321     hash((name, shadow_count))
322 }
323
324 fn highlight_def(sema: &Semantics<RootDatabase>, krate: hir::Crate, def: Definition) -> Highlight {
325     let db = sema.db;
326     let mut h = match def {
327         Definition::Macro(m) => Highlight::new(HlTag::Symbol(m.kind(sema.db).into())),
328         Definition::Field(_) => Highlight::new(HlTag::Symbol(SymbolKind::Field)),
329         Definition::Module(module) => {
330             let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module));
331             if module.parent(db).is_none() {
332                 h |= HlMod::CrateRoot
333             }
334             h
335         }
336         Definition::Function(func) => {
337             let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function));
338             if let Some(item) = func.as_assoc_item(db) {
339                 h |= HlMod::Associated;
340                 match func.self_param(db) {
341                     Some(sp) => match sp.access(db) {
342                         hir::Access::Exclusive => {
343                             h |= HlMod::Mutable;
344                             h |= HlMod::Reference;
345                         }
346                         hir::Access::Shared => h |= HlMod::Reference,
347                         hir::Access::Owned => h |= HlMod::Consuming,
348                     },
349                     None => h |= HlMod::Static,
350                 }
351
352                 match item.container(db) {
353                     hir::AssocItemContainer::Impl(i) => {
354                         if i.trait_(db).is_some() {
355                             h |= HlMod::Trait;
356                         }
357                     }
358                     hir::AssocItemContainer::Trait(_t) => {
359                         h |= HlMod::Trait;
360                     }
361                 }
362             }
363
364             if func.is_unsafe_to_call(db) {
365                 h |= HlMod::Unsafe;
366             }
367             if func.is_async(db) {
368                 h |= HlMod::Async;
369             }
370
371             h
372         }
373         Definition::Adt(adt) => {
374             let h = match adt {
375                 hir::Adt::Struct(_) => HlTag::Symbol(SymbolKind::Struct),
376                 hir::Adt::Enum(_) => HlTag::Symbol(SymbolKind::Enum),
377                 hir::Adt::Union(_) => HlTag::Symbol(SymbolKind::Union),
378             };
379
380             Highlight::new(h)
381         }
382         Definition::Variant(_) => Highlight::new(HlTag::Symbol(SymbolKind::Variant)),
383         Definition::Const(konst) => {
384             let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const));
385
386             if let Some(item) = konst.as_assoc_item(db) {
387                 h |= HlMod::Associated;
388                 match item.container(db) {
389                     hir::AssocItemContainer::Impl(i) => {
390                         if i.trait_(db).is_some() {
391                             h |= HlMod::Trait;
392                         }
393                     }
394                     hir::AssocItemContainer::Trait(_t) => {
395                         h |= HlMod::Trait;
396                     }
397                 }
398             }
399
400             h
401         }
402         Definition::Trait(_) => Highlight::new(HlTag::Symbol(SymbolKind::Trait)),
403         Definition::TypeAlias(type_) => {
404             let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
405
406             if let Some(item) = type_.as_assoc_item(db) {
407                 h |= HlMod::Associated;
408                 match item.container(db) {
409                     hir::AssocItemContainer::Impl(i) => {
410                         if i.trait_(db).is_some() {
411                             h |= HlMod::Trait;
412                         }
413                     }
414                     hir::AssocItemContainer::Trait(_t) => {
415                         h |= HlMod::Trait;
416                     }
417                 }
418             }
419
420             h
421         }
422         Definition::BuiltinType(_) => Highlight::new(HlTag::BuiltinType),
423         Definition::Static(s) => {
424             let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static));
425
426             if s.is_mut(db) {
427                 h |= HlMod::Mutable;
428                 h |= HlMod::Unsafe;
429             }
430
431             h
432         }
433         Definition::SelfType(_) => Highlight::new(HlTag::Symbol(SymbolKind::Impl)),
434         Definition::GenericParam(it) => match it {
435             hir::GenericParam::TypeParam(_) => Highlight::new(HlTag::Symbol(SymbolKind::TypeParam)),
436             hir::GenericParam::ConstParam(_) => {
437                 Highlight::new(HlTag::Symbol(SymbolKind::ConstParam))
438             }
439             hir::GenericParam::LifetimeParam(_) => {
440                 Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam))
441             }
442         },
443         Definition::Local(local) => {
444             let tag = if local.is_self(db) {
445                 HlTag::Symbol(SymbolKind::SelfParam)
446             } else if local.is_param(db) {
447                 HlTag::Symbol(SymbolKind::ValueParam)
448             } else {
449                 HlTag::Symbol(SymbolKind::Local)
450             };
451             let mut h = Highlight::new(tag);
452             let ty = local.ty(db);
453             if local.is_mut(db) || ty.is_mutable_reference() {
454                 h |= HlMod::Mutable;
455             }
456             if local.is_ref(db) || ty.is_reference() {
457                 h |= HlMod::Reference;
458             }
459             if ty.as_callable(db).is_some() || ty.impls_fnonce(db) {
460                 h |= HlMod::Callable;
461             }
462             h
463         }
464         Definition::Label(_) => Highlight::new(HlTag::Symbol(SymbolKind::Label)),
465         Definition::BuiltinAttr(_) => Highlight::new(HlTag::Symbol(SymbolKind::BuiltinAttr)),
466         Definition::ToolModule(_) => Highlight::new(HlTag::Symbol(SymbolKind::ToolModule)),
467     };
468
469     let def_crate = def.krate(db);
470     let is_from_other_crate = def_crate != Some(krate);
471     let is_from_builtin_crate = def_crate.map_or(false, |def_crate| def_crate.is_builtin(db));
472     let is_builtin_type = matches!(def, Definition::BuiltinType(_));
473     let is_public = def.visibility(db) == Some(hir::Visibility::Public);
474
475     match (is_from_other_crate, is_builtin_type, is_public) {
476         (true, false, _) => h |= HlMod::Library,
477         (false, _, true) => h |= HlMod::Public,
478         _ => {}
479     }
480
481     if is_from_builtin_crate {
482         h |= HlMod::DefaultLibrary;
483     }
484
485     h
486 }
487
488 fn highlight_method_call_by_name_ref(
489     sema: &Semantics<RootDatabase>,
490     krate: hir::Crate,
491     name_ref: &ast::NameRef,
492 ) -> Option<Highlight> {
493     let mc = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
494     highlight_method_call(sema, krate, &mc)
495 }
496
497 fn highlight_method_call(
498     sema: &Semantics<RootDatabase>,
499     krate: hir::Crate,
500     method_call: &ast::MethodCallExpr,
501 ) -> Option<Highlight> {
502     let func = sema.resolve_method_call(method_call)?;
503
504     let mut h = SymbolKind::Function.into();
505     h |= HlMod::Associated;
506
507     if func.is_unsafe_to_call(sema.db) || sema.is_unsafe_method_call(method_call) {
508         h |= HlMod::Unsafe;
509     }
510     if func.is_async(sema.db) {
511         h |= HlMod::Async;
512     }
513     if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() {
514         h |= HlMod::Trait;
515     }
516
517     let def_crate = func.module(sema.db).krate();
518     let is_from_other_crate = def_crate != krate;
519     let is_from_builtin_crate = def_crate.is_builtin(sema.db);
520     let is_public = func.visibility(sema.db) == hir::Visibility::Public;
521
522     if is_from_other_crate {
523         h |= HlMod::Library;
524     } else if is_public {
525         h |= HlMod::Public;
526     }
527
528     if is_from_builtin_crate {
529         h |= HlMod::DefaultLibrary;
530     }
531
532     if let Some(self_param) = func.self_param(sema.db) {
533         match self_param.access(sema.db) {
534             hir::Access::Shared => h |= HlMod::Reference,
535             hir::Access::Exclusive => {
536                 h |= HlMod::Mutable;
537                 h |= HlMod::Reference;
538             }
539             hir::Access::Owned => {
540                 if let Some(receiver_ty) =
541                     method_call.receiver().and_then(|it| sema.type_of_expr(&it))
542                 {
543                     if !receiver_ty.adjusted().is_copy(sema.db) {
544                         h |= HlMod::Consuming
545                     }
546                 }
547             }
548         }
549     }
550     Some(h)
551 }
552
553 fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
554     let default = HlTag::UnresolvedReference;
555
556     let parent = match name.syntax().parent() {
557         Some(it) => it,
558         _ => return default.into(),
559     };
560
561     let tag = match parent.kind() {
562         STRUCT => SymbolKind::Struct,
563         ENUM => SymbolKind::Enum,
564         VARIANT => SymbolKind::Variant,
565         UNION => SymbolKind::Union,
566         TRAIT => SymbolKind::Trait,
567         TYPE_ALIAS => SymbolKind::TypeAlias,
568         TYPE_PARAM => SymbolKind::TypeParam,
569         RECORD_FIELD => SymbolKind::Field,
570         MODULE => SymbolKind::Module,
571         FN => SymbolKind::Function,
572         CONST => SymbolKind::Const,
573         STATIC => SymbolKind::Static,
574         IDENT_PAT => SymbolKind::Local,
575         _ => return default.into(),
576     };
577
578     tag.into()
579 }
580
581 fn highlight_name_ref_by_syntax(
582     name: ast::NameRef,
583     sema: &Semantics<RootDatabase>,
584     krate: hir::Crate,
585 ) -> Highlight {
586     let default = HlTag::UnresolvedReference;
587
588     let parent = match name.syntax().parent() {
589         Some(it) => it,
590         _ => return default.into(),
591     };
592
593     match parent.kind() {
594         METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent)
595             .and_then(|it| highlight_method_call(sema, krate, &it))
596             .unwrap_or_else(|| SymbolKind::Function.into()),
597         FIELD_EXPR => {
598             let h = HlTag::Symbol(SymbolKind::Field);
599             let is_union = ast::FieldExpr::cast(parent)
600                 .and_then(|field_expr| sema.resolve_field(&field_expr))
601                 .map_or(false, |field| {
602                     matches!(field.parent_def(sema.db), hir::VariantDef::Union(_))
603                 });
604             if is_union {
605                 h | HlMod::Unsafe
606             } else {
607                 h.into()
608             }
609         }
610         PATH_SEGMENT => {
611             let name_based_fallback = || {
612                 if name.text().chars().next().unwrap_or_default().is_uppercase() {
613                     SymbolKind::Struct.into()
614                 } else {
615                     SymbolKind::Module.into()
616                 }
617             };
618             let path = match parent.parent().and_then(ast::Path::cast) {
619                 Some(it) => it,
620                 _ => return name_based_fallback(),
621             };
622             let expr = match path.syntax().parent() {
623                 Some(parent) => match_ast! {
624                     match parent {
625                         ast::PathExpr(path) => path,
626                         ast::MacroCall(_) => return SymbolKind::Macro.into(),
627                         _ => return name_based_fallback(),
628                     }
629                 },
630                 // within path, decide whether it is module or adt by checking for uppercase name
631                 None => return name_based_fallback(),
632             };
633             let parent = match expr.syntax().parent() {
634                 Some(it) => it,
635                 None => return default.into(),
636             };
637
638             match parent.kind() {
639                 CALL_EXPR => SymbolKind::Function.into(),
640                 _ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
641                     SymbolKind::Struct
642                 } else {
643                     SymbolKind::Const
644                 }
645                 .into(),
646             }
647         }
648         _ => default.into(),
649     }
650 }
651
652 fn is_consumed_lvalue(node: &SyntaxNode, local: &hir::Local, db: &RootDatabase) -> bool {
653     // When lvalues are passed as arguments and they're not Copy, then mark them as Consuming.
654     parents_match(node.clone().into(), &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST])
655         && !local.ty(db).is_copy(db)
656 }
657
658 /// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly.
659 fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[SyntaxKind]) -> bool {
660     while let (Some(parent), [kind, rest @ ..]) = (&node.parent(), kinds) {
661         if parent.kind() != *kind {
662             return false;
663         }
664
665         // FIXME: Would be nice to get parent out of the match, but binding by-move and by-value
666         // in the same pattern is unstable: rust-lang/rust#68354.
667         node = node.parent().unwrap().into();
668         kinds = rest;
669     }
670
671     // Only true if we matched all expected kinds
672     kinds.is_empty()
673 }
674
675 fn parent_matches<N: AstNode>(token: &SyntaxToken) -> bool {
676     token.parent().map_or(false, |it| N::can_cast(it.kind()))
677 }