]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/syntax_highlighting/highlight.rs
Split float literal tokens at the `.`
[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.ancestors().nth(1).map(|it| it.kind()) == Some(FIELD_EXPR) => {
31             SymbolKind::Field.into()
32         }
33         INT_NUMBER | FLOAT_NUMBER_PART => 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.module(db).map(hir::Module::krate).or_else(|| match def {
470         Definition::Module(module) => Some(module.krate()),
471         _ => None,
472     });
473     let is_from_other_crate = def_crate != Some(krate);
474     let is_from_builtin_crate = def_crate.map_or(false, |def_crate| def_crate.is_builtin(db));
475     let is_builtin_type = matches!(def, Definition::BuiltinType(_));
476     let is_public = def.visibility(db) == Some(hir::Visibility::Public);
477
478     match (is_from_other_crate, is_builtin_type, is_public) {
479         (true, false, _) => h |= HlMod::Library,
480         (false, _, true) => h |= HlMod::Public,
481         _ => {}
482     }
483
484     if is_from_builtin_crate {
485         h |= HlMod::DefaultLibrary;
486     }
487
488     h
489 }
490
491 fn highlight_method_call_by_name_ref(
492     sema: &Semantics<RootDatabase>,
493     krate: hir::Crate,
494     name_ref: &ast::NameRef,
495 ) -> Option<Highlight> {
496     let mc = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
497     highlight_method_call(sema, krate, &mc)
498 }
499
500 fn highlight_method_call(
501     sema: &Semantics<RootDatabase>,
502     krate: hir::Crate,
503     method_call: &ast::MethodCallExpr,
504 ) -> Option<Highlight> {
505     let func = sema.resolve_method_call(method_call)?;
506
507     let mut h = SymbolKind::Function.into();
508     h |= HlMod::Associated;
509
510     if func.is_unsafe_to_call(sema.db) || sema.is_unsafe_method_call(method_call) {
511         h |= HlMod::Unsafe;
512     }
513     if func.is_async(sema.db) {
514         h |= HlMod::Async;
515     }
516     if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() {
517         h |= HlMod::Trait;
518     }
519
520     let def_crate = func.module(sema.db).krate();
521     let is_from_other_crate = def_crate != krate;
522     let is_from_builtin_crate = def_crate.is_builtin(sema.db);
523     let is_public = func.visibility(sema.db) == hir::Visibility::Public;
524
525     if is_from_other_crate {
526         h |= HlMod::Library;
527     } else if is_public {
528         h |= HlMod::Public;
529     }
530
531     if is_from_builtin_crate {
532         h |= HlMod::DefaultLibrary;
533     }
534
535     if let Some(self_param) = func.self_param(sema.db) {
536         match self_param.access(sema.db) {
537             hir::Access::Shared => h |= HlMod::Reference,
538             hir::Access::Exclusive => {
539                 h |= HlMod::Mutable;
540                 h |= HlMod::Reference;
541             }
542             hir::Access::Owned => {
543                 if let Some(receiver_ty) =
544                     method_call.receiver().and_then(|it| sema.type_of_expr(&it))
545                 {
546                     if !receiver_ty.adjusted().is_copy(sema.db) {
547                         h |= HlMod::Consuming
548                     }
549                 }
550             }
551         }
552     }
553     Some(h)
554 }
555
556 fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
557     let default = HlTag::UnresolvedReference;
558
559     let parent = match name.syntax().parent() {
560         Some(it) => it,
561         _ => return default.into(),
562     };
563
564     let tag = match parent.kind() {
565         STRUCT => SymbolKind::Struct,
566         ENUM => SymbolKind::Enum,
567         VARIANT => SymbolKind::Variant,
568         UNION => SymbolKind::Union,
569         TRAIT => SymbolKind::Trait,
570         TYPE_ALIAS => SymbolKind::TypeAlias,
571         TYPE_PARAM => SymbolKind::TypeParam,
572         RECORD_FIELD => SymbolKind::Field,
573         MODULE => SymbolKind::Module,
574         FN => SymbolKind::Function,
575         CONST => SymbolKind::Const,
576         STATIC => SymbolKind::Static,
577         IDENT_PAT => SymbolKind::Local,
578         _ => return default.into(),
579     };
580
581     tag.into()
582 }
583
584 fn highlight_name_ref_by_syntax(
585     name: ast::NameRef,
586     sema: &Semantics<RootDatabase>,
587     krate: hir::Crate,
588 ) -> Highlight {
589     let default = HlTag::UnresolvedReference;
590
591     let parent = match name.syntax().parent() {
592         Some(it) => it,
593         _ => return default.into(),
594     };
595
596     match parent.kind() {
597         METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent)
598             .and_then(|it| highlight_method_call(sema, krate, &it))
599             .unwrap_or_else(|| SymbolKind::Function.into()),
600         FIELD_EXPR => {
601             let h = HlTag::Symbol(SymbolKind::Field);
602             let is_union = ast::FieldExpr::cast(parent)
603                 .and_then(|field_expr| sema.resolve_field(&field_expr))
604                 .map_or(false, |field| {
605                     matches!(field.parent_def(sema.db), hir::VariantDef::Union(_))
606                 });
607             if is_union {
608                 h | HlMod::Unsafe
609             } else {
610                 h.into()
611             }
612         }
613         PATH_SEGMENT => {
614             let name_based_fallback = || {
615                 if name.text().chars().next().unwrap_or_default().is_uppercase() {
616                     SymbolKind::Struct.into()
617                 } else {
618                     SymbolKind::Module.into()
619                 }
620             };
621             let path = match parent.parent().and_then(ast::Path::cast) {
622                 Some(it) => it,
623                 _ => return name_based_fallback(),
624             };
625             let expr = match path.syntax().parent() {
626                 Some(parent) => match_ast! {
627                     match parent {
628                         ast::PathExpr(path) => path,
629                         ast::MacroCall(_) => return SymbolKind::Macro.into(),
630                         _ => return name_based_fallback(),
631                     }
632                 },
633                 // within path, decide whether it is module or adt by checking for uppercase name
634                 None => return name_based_fallback(),
635             };
636             let parent = match expr.syntax().parent() {
637                 Some(it) => it,
638                 None => return default.into(),
639             };
640
641             match parent.kind() {
642                 CALL_EXPR => SymbolKind::Function.into(),
643                 _ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
644                     SymbolKind::Struct
645                 } else {
646                     SymbolKind::Const
647                 }
648                 .into(),
649             }
650         }
651         _ => default.into(),
652     }
653 }
654
655 fn is_consumed_lvalue(node: &SyntaxNode, local: &hir::Local, db: &RootDatabase) -> bool {
656     // When lvalues are passed as arguments and they're not Copy, then mark them as Consuming.
657     parents_match(node.clone().into(), &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST])
658         && !local.ty(db).is_copy(db)
659 }
660
661 /// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly.
662 fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[SyntaxKind]) -> bool {
663     while let (Some(parent), [kind, rest @ ..]) = (&node.parent(), kinds) {
664         if parent.kind() != *kind {
665             return false;
666         }
667
668         // FIXME: Would be nice to get parent out of the match, but binding by-move and by-value
669         // in the same pattern is unstable: rust-lang/rust#68354.
670         node = node.parent().unwrap().into();
671         kinds = rest;
672     }
673
674     // Only true if we matched all expected kinds
675     kinds.is_empty()
676 }
677
678 fn parent_matches<N: AstNode>(token: &SyntaxToken) -> bool {
679     token.parent().map_or(false, |it| N::can_cast(it.kind()))
680 }