]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/context.rs
Move more ImmediateLocation::TypeAnnotation into PathKind::Type
[rust.git] / crates / ide-completion / src / context.rs
1 //! See `CompletionContext` structure.
2
3 use std::iter;
4
5 use base_db::SourceDatabaseExt;
6 use hir::{
7     HasAttrs, Local, Name, PathResolution, ScopeDef, Semantics, SemanticsScope, Type, TypeInfo,
8 };
9 use ide_db::{
10     active_parameter::ActiveParameter,
11     base_db::{FilePosition, SourceDatabase},
12     famous_defs::FamousDefs,
13     FxHashMap, FxHashSet, RootDatabase,
14 };
15 use syntax::{
16     algo::{find_node_at_offset, non_trivia_sibling},
17     ast::{self, AttrKind, HasArgList, HasName, NameOrNameRef},
18     match_ast, AstNode, AstToken, Direction, NodeOrToken,
19     SyntaxKind::{self, *},
20     SyntaxNode, SyntaxToken, TextRange, TextSize, T,
21 };
22 use text_edit::Indel;
23
24 use crate::{
25     patterns::{
26         determine_location, is_in_loop_body, is_in_token_of_for_loop, previous_token,
27         ImmediateLocation,
28     },
29     CompletionConfig,
30 };
31
32 const COMPLETION_MARKER: &str = "intellijRulezz";
33
34 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
35 pub(crate) enum PatternRefutability {
36     Refutable,
37     Irrefutable,
38 }
39
40 pub(crate) enum Visible {
41     Yes,
42     Editable,
43     No,
44 }
45
46 /// Existing qualifiers for the thing we are currently completing.
47 #[derive(Debug, Default)]
48 pub(super) struct QualifierCtx {
49     pub(super) unsafe_tok: Option<SyntaxToken>,
50     pub(super) vis_node: Option<ast::Visibility>,
51 }
52
53 impl QualifierCtx {
54     pub(super) fn none(&self) -> bool {
55         self.unsafe_tok.is_none() && self.vis_node.is_none()
56     }
57 }
58
59 /// The state of the path we are currently completing.
60 #[derive(Debug)]
61 pub(crate) struct PathCompletionCtx {
62     /// If this is a call with () already there (or {} in case of record patterns)
63     pub(super) has_call_parens: bool,
64     /// If this has a macro call bang !
65     pub(super) has_macro_bang: bool,
66     /// Whether this path stars with a `::`.
67     pub(super) is_absolute_path: bool,
68     /// The qualifier of the current path if it exists.
69     pub(super) qualifier: Option<PathQualifierCtx>,
70     /// The parent of the path we are completing.
71     pub(super) parent: Option<ast::Path>,
72     pub(super) kind: PathKind,
73     /// Whether the path segment has type args or not.
74     pub(super) has_type_args: bool,
75 }
76
77 impl PathCompletionCtx {
78     pub(super) fn is_trivial_path(&self) -> bool {
79         matches!(
80             self,
81             PathCompletionCtx {
82                 has_call_parens: false,
83                 has_macro_bang: false,
84                 is_absolute_path: false,
85                 qualifier: None,
86                 parent: None,
87                 has_type_args: false,
88                 ..
89             }
90         )
91     }
92 }
93
94 /// The kind of path we are completing right now.
95 #[derive(Clone, Debug, PartialEq, Eq)]
96 pub(super) enum PathKind {
97     Expr {
98         in_block_expr: bool,
99         in_loop_body: bool,
100         after_if_expr: bool,
101         ref_expr_parent: Option<ast::RefExpr>,
102         is_func_update: Option<ast::RecordExpr>,
103     },
104     Type {
105         in_tuple_struct: bool,
106         /// Whether this type path is a type ascription or not
107         /// Original file ast node
108         ascription: Option<TypeAscriptionTarget>,
109     },
110     Attr {
111         kind: AttrKind,
112         annotated_item_kind: Option<SyntaxKind>,
113     },
114     Derive,
115     /// Path in item position, that is inside an (Assoc)ItemList
116     Item {
117         kind: ItemListKind,
118     },
119     Pat,
120     Vis {
121         has_in_token: bool,
122     },
123     Use,
124 }
125
126 #[derive(Clone, Debug, PartialEq, Eq)]
127 pub(crate) enum TypeAscriptionTarget {
128     Let(Option<ast::Pat>),
129     FnParam(Option<ast::Pat>),
130     RetType(Option<ast::Expr>),
131     Const(Option<ast::Expr>),
132 }
133
134 /// The kind of item list a [`PathKind::Item`] belongs to.
135 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
136 pub(super) enum ItemListKind {
137     SourceFile,
138     Module,
139     Impl,
140     TraitImpl,
141     Trait,
142     ExternBlock,
143 }
144
145 /// The path qualifier state of the path we are completing.
146 #[derive(Debug)]
147 pub(crate) struct PathQualifierCtx {
148     pub(crate) path: ast::Path,
149     pub(crate) resolution: Option<PathResolution>,
150     /// Whether this path consists solely of `super` segments
151     pub(crate) is_super_chain: bool,
152     /// Whether the qualifier comes from a use tree parent or not
153     pub(crate) use_tree_parent: bool,
154     /// <_>
155     pub(crate) is_infer_qualifier: bool,
156 }
157
158 /// The state of the pattern we are completing.
159 #[derive(Debug)]
160 pub(super) struct PatternContext {
161     pub(super) refutability: PatternRefutability,
162     pub(super) param_ctx: Option<(ast::ParamList, ast::Param, ParamKind)>,
163     pub(super) has_type_ascription: bool,
164     pub(super) parent_pat: Option<ast::Pat>,
165     pub(super) ref_token: Option<SyntaxToken>,
166     pub(super) mut_token: Option<SyntaxToken>,
167     /// The record pattern this name or ref is a field of
168     pub(super) record_pat: Option<ast::RecordPat>,
169 }
170
171 /// The state of the lifetime we are completing.
172 #[derive(Debug)]
173 pub(super) struct LifetimeContext {
174     pub(super) lifetime: Option<ast::Lifetime>,
175     pub(super) kind: LifetimeKind,
176 }
177
178 /// The kind of lifetime we are completing.
179 #[derive(Debug)]
180 pub(super) enum LifetimeKind {
181     LifetimeParam { is_decl: bool, param: ast::LifetimeParam },
182     Lifetime,
183     LabelRef,
184     LabelDef,
185 }
186
187 /// The state of the name we are completing.
188 #[derive(Debug)]
189 pub(super) struct NameContext {
190     #[allow(dead_code)]
191     pub(super) name: Option<ast::Name>,
192     pub(super) kind: NameKind,
193 }
194
195 /// The kind of the name we are completing.
196 #[derive(Debug)]
197 #[allow(dead_code)]
198 pub(super) enum NameKind {
199     Const,
200     ConstParam,
201     Enum,
202     Function,
203     IdentPat,
204     MacroDef,
205     MacroRules,
206     /// Fake node
207     Module(ast::Module),
208     RecordField,
209     Rename,
210     SelfParam,
211     Static,
212     Struct,
213     Trait,
214     TypeAlias,
215     TypeParam,
216     Union,
217     Variant,
218 }
219
220 /// The state of the NameRef we are completing.
221 #[derive(Debug)]
222 pub(super) struct NameRefContext {
223     /// NameRef syntax in the original file
224     pub(super) nameref: Option<ast::NameRef>,
225     pub(super) kind: Option<NameRefKind>,
226 }
227
228 /// The kind of the NameRef we are completing.
229 #[derive(Debug)]
230 pub(super) enum NameRefKind {
231     Path(PathCompletionCtx),
232     DotAccess(DotAccess),
233     /// Position where we are only interested in keyword completions
234     Keyword(ast::Item),
235     /// The record expression this nameref is a field of
236     RecordExpr(ast::RecordExpr),
237 }
238
239 /// The identifier we are currently completing.
240 #[derive(Debug)]
241 pub(super) enum IdentContext {
242     Name(NameContext),
243     NameRef(NameRefContext),
244     Lifetime(LifetimeContext),
245     /// The string the cursor is currently inside
246     String {
247         /// original token
248         original: ast::String,
249         /// fake token
250         expanded: Option<ast::String>,
251     },
252     /// Set if we are currently completing in an unexpanded attribute, this usually implies a builtin attribute like `allow($0)`
253     UnexpandedAttrTT {
254         fake_attribute_under_caret: Option<ast::Attr>,
255     },
256 }
257
258 /// Information about the field or method access we are completing.
259 #[derive(Debug)]
260 pub(super) struct DotAccess {
261     pub(super) receiver: Option<ast::Expr>,
262     pub(super) receiver_ty: Option<TypeInfo>,
263     pub(super) kind: DotAccessKind,
264 }
265
266 #[derive(Debug)]
267 pub(super) enum DotAccessKind {
268     Field {
269         /// True if the receiver is an integer and there is no ident in the original file after it yet
270         /// like `0.$0`
271         receiver_is_ambiguous_float_literal: bool,
272     },
273     Method {
274         has_parens: bool,
275     },
276 }
277
278 #[derive(Clone, Debug, PartialEq, Eq)]
279 pub(crate) enum ParamKind {
280     Function(ast::Fn),
281     Closure(ast::ClosureExpr),
282 }
283
284 /// `CompletionContext` is created early during completion to figure out, where
285 /// exactly is the cursor, syntax-wise.
286 #[derive(Debug)]
287 pub(crate) struct CompletionContext<'a> {
288     pub(super) sema: Semantics<'a, RootDatabase>,
289     pub(super) scope: SemanticsScope<'a>,
290     pub(super) db: &'a RootDatabase,
291     pub(super) config: &'a CompletionConfig,
292     pub(super) position: FilePosition,
293
294     /// The token before the cursor, in the original file.
295     pub(super) original_token: SyntaxToken,
296     /// The token before the cursor, in the macro-expanded file.
297     pub(super) token: SyntaxToken,
298     /// The crate of the current file.
299     pub(super) krate: hir::Crate,
300     /// The module of the `scope`.
301     pub(super) module: hir::Module,
302
303     /// The expected name of what we are completing.
304     /// This is usually the parameter name of the function argument we are completing.
305     pub(super) expected_name: Option<NameOrNameRef>,
306     /// The expected type of what we are completing.
307     pub(super) expected_type: Option<Type>,
308
309     /// The parent function of the cursor position if it exists.
310     pub(super) function_def: Option<ast::Fn>,
311     /// The parent impl of the cursor position if it exists.
312     pub(super) impl_def: Option<ast::Impl>,
313     /// Are we completing inside a let statement with a missing semicolon?
314     pub(super) incomplete_let: bool,
315
316     pub(super) completion_location: Option<ImmediateLocation>,
317     pub(super) previous_token: Option<SyntaxToken>,
318
319     pub(super) ident_ctx: IdentContext,
320
321     pub(super) pattern_ctx: Option<PatternContext>,
322     pub(super) qualifier_ctx: QualifierCtx,
323
324     pub(super) existing_derives: FxHashSet<hir::Macro>,
325
326     pub(super) locals: FxHashMap<Name, Local>,
327 }
328
329 impl<'a> CompletionContext<'a> {
330     /// The range of the identifier that is being completed.
331     pub(crate) fn source_range(&self) -> TextRange {
332         // check kind of macro-expanded token, but use range of original token
333         let kind = self.token.kind();
334         match kind {
335             CHAR => {
336                 // assume we are completing a lifetime but the user has only typed the '
337                 cov_mark::hit!(completes_if_lifetime_without_idents);
338                 TextRange::at(self.original_token.text_range().start(), TextSize::from(1))
339             }
340             IDENT | LIFETIME_IDENT | UNDERSCORE => self.original_token.text_range(),
341             _ if kind.is_keyword() => self.original_token.text_range(),
342             _ => TextRange::empty(self.position.offset),
343         }
344     }
345
346     pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
347         self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
348     }
349
350     pub(crate) fn famous_defs(&self) -> FamousDefs {
351         FamousDefs(&self.sema, self.krate)
352     }
353
354     pub(super) fn nameref_ctx(&self) -> Option<&NameRefContext> {
355         match &self.ident_ctx {
356             IdentContext::NameRef(it) => Some(it),
357             _ => None,
358         }
359     }
360
361     pub(super) fn name_ctx(&self) -> Option<&NameContext> {
362         match &self.ident_ctx {
363             IdentContext::Name(it) => Some(it),
364             _ => None,
365         }
366     }
367
368     pub(super) fn lifetime_ctx(&self) -> Option<&LifetimeContext> {
369         match &self.ident_ctx {
370             IdentContext::Lifetime(it) => Some(it),
371             _ => None,
372         }
373     }
374
375     pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
376         match self.nameref_ctx() {
377             Some(NameRefContext {
378                 kind: Some(NameRefKind::DotAccess(DotAccess { receiver, .. })),
379                 ..
380             }) => receiver.as_ref(),
381             _ => None,
382         }
383     }
384
385     pub(crate) fn has_dot_receiver(&self) -> bool {
386         self.dot_receiver().is_some()
387     }
388
389     // FIXME: This shouldn't exist
390     pub(crate) fn expects_generic_arg(&self) -> bool {
391         matches!(self.completion_location, Some(ImmediateLocation::GenericArgList(_)))
392     }
393
394     pub(crate) fn path_context(&self) -> Option<&PathCompletionCtx> {
395         self.nameref_ctx().and_then(|ctx| match &ctx.kind {
396             Some(NameRefKind::Path(path)) => Some(path),
397             _ => None,
398         })
399     }
400
401     pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
402         self.path_context().and_then(|it| it.qualifier.as_ref().map(|it| &it.path))
403     }
404
405     /// Checks if an item is visible and not `doc(hidden)` at the completion site.
406     pub(crate) fn is_visible<I>(&self, item: &I) -> Visible
407     where
408         I: hir::HasVisibility + hir::HasAttrs + hir::HasCrate + Copy,
409     {
410         self.is_visible_impl(&item.visibility(self.db), &item.attrs(self.db), item.krate(self.db))
411     }
412
413     pub(crate) fn is_scope_def_hidden(&self, scope_def: ScopeDef) -> bool {
414         if let (Some(attrs), Some(krate)) = (scope_def.attrs(self.db), scope_def.krate(self.db)) {
415             return self.is_doc_hidden(&attrs, krate);
416         }
417
418         false
419     }
420
421     /// Check if an item is `#[doc(hidden)]`.
422     pub(crate) fn is_item_hidden(&self, item: &hir::ItemInNs) -> bool {
423         let attrs = item.attrs(self.db);
424         let krate = item.krate(self.db);
425         match (attrs, krate) {
426             (Some(attrs), Some(krate)) => self.is_doc_hidden(&attrs, krate),
427             _ => false,
428         }
429     }
430     /// Whether the given trait is an operator trait or not.
431     pub(crate) fn is_ops_trait(&self, trait_: hir::Trait) -> bool {
432         match trait_.attrs(self.db).lang() {
433             Some(lang) => OP_TRAIT_LANG_NAMES.contains(&lang.as_str()),
434             None => false,
435         }
436     }
437
438     /// Returns the traits in scope, with the [`Drop`] trait removed.
439     pub(crate) fn traits_in_scope(&self) -> hir::VisibleTraits {
440         let mut traits_in_scope = self.scope.visible_traits();
441         if let Some(drop) = self.famous_defs().core_ops_Drop() {
442             traits_in_scope.0.remove(&drop.into());
443         }
444         traits_in_scope
445     }
446
447     /// A version of [`SemanticsScope::process_all_names`] that filters out `#[doc(hidden)]` items.
448     pub(crate) fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
449         let _p = profile::span("CompletionContext::process_all_names");
450         self.scope.process_all_names(&mut |name, def| {
451             if self.is_scope_def_hidden(def) {
452                 return;
453             }
454
455             f(name, def);
456         });
457     }
458
459     pub(crate) fn process_all_names_raw(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
460         let _p = profile::span("CompletionContext::process_all_names_raw");
461         self.scope.process_all_names(&mut |name, def| f(name, def));
462     }
463
464     fn is_visible_impl(
465         &self,
466         vis: &hir::Visibility,
467         attrs: &hir::Attrs,
468         defining_crate: hir::Crate,
469     ) -> Visible {
470         if !vis.is_visible_from(self.db, self.module.into()) {
471             if !self.config.enable_private_editable {
472                 return Visible::No;
473             }
474             // If the definition location is editable, also show private items
475             let root_file = defining_crate.root_file(self.db);
476             let source_root_id = self.db.file_source_root(root_file);
477             let is_editable = !self.db.source_root(source_root_id).is_library;
478             return if is_editable { Visible::Editable } else { Visible::No };
479         }
480
481         if self.is_doc_hidden(attrs, defining_crate) {
482             Visible::No
483         } else {
484             Visible::Yes
485         }
486     }
487
488     fn is_doc_hidden(&self, attrs: &hir::Attrs, defining_crate: hir::Crate) -> bool {
489         // `doc(hidden)` items are only completed within the defining crate.
490         self.krate != defining_crate && attrs.has_doc_hidden()
491     }
492 }
493
494 // CompletionContext construction
495 impl<'a> CompletionContext<'a> {
496     pub(super) fn new(
497         db: &'a RootDatabase,
498         position @ FilePosition { file_id, offset }: FilePosition,
499         config: &'a CompletionConfig,
500     ) -> Option<CompletionContext<'a>> {
501         let _p = profile::span("CompletionContext::new");
502         let sema = Semantics::new(db);
503
504         let original_file = sema.parse(file_id);
505
506         // Insert a fake ident to get a valid parse tree. We will use this file
507         // to determine context, though the original_file will be used for
508         // actual completion.
509         let file_with_fake_ident = {
510             let parse = db.parse(file_id);
511             let edit = Indel::insert(offset, COMPLETION_MARKER.to_string());
512             parse.reparse(&edit).tree()
513         };
514         let fake_ident_token =
515             file_with_fake_ident.syntax().token_at_offset(offset).right_biased()?;
516
517         let original_token = original_file.syntax().token_at_offset(offset).left_biased()?;
518         let token = sema.descend_into_macros_single(original_token.clone());
519         let scope = sema.scope_at_offset(&token.parent()?, offset)?;
520         let krate = scope.krate();
521         let module = scope.module();
522
523         let mut locals = FxHashMap::default();
524         scope.process_all_names(&mut |name, scope| {
525             if let ScopeDef::Local(local) = scope {
526                 locals.insert(name, local);
527             }
528         });
529
530         let mut ctx = CompletionContext {
531             sema,
532             scope,
533             db,
534             config,
535             position,
536             original_token,
537             token,
538             krate,
539             module,
540             expected_name: None,
541             expected_type: None,
542             function_def: None,
543             impl_def: None,
544             incomplete_let: false,
545             completion_location: None,
546             previous_token: None,
547             // dummy value, will be overwritten
548             ident_ctx: IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: None },
549             pattern_ctx: None,
550             qualifier_ctx: Default::default(),
551             existing_derives: Default::default(),
552             locals,
553         };
554         ctx.expand_and_fill(
555             original_file.syntax().clone(),
556             file_with_fake_ident.syntax().clone(),
557             offset,
558             fake_ident_token,
559         )?;
560         Some(ctx)
561     }
562
563     /// Expand attributes and macro calls at the current cursor position for both the original file
564     /// and fake file repeatedly. As soon as one of the two expansions fail we stop so the original
565     /// and speculative states stay in sync.
566     fn expand_and_fill(
567         &mut self,
568         mut original_file: SyntaxNode,
569         mut speculative_file: SyntaxNode,
570         mut offset: TextSize,
571         mut fake_ident_token: SyntaxToken,
572     ) -> Option<()> {
573         let _p = profile::span("CompletionContext::expand_and_fill");
574         let mut derive_ctx = None;
575
576         'expansion: loop {
577             let parent_item =
578                 |item: &ast::Item| item.syntax().ancestors().skip(1).find_map(ast::Item::cast);
579             let ancestor_items = iter::successors(
580                 Option::zip(
581                     find_node_at_offset::<ast::Item>(&original_file, offset),
582                     find_node_at_offset::<ast::Item>(&speculative_file, offset),
583                 ),
584                 |(a, b)| parent_item(a).zip(parent_item(b)),
585             );
586
587             // first try to expand attributes as these are always the outermost macro calls
588             'ancestors: for (actual_item, item_with_fake_ident) in ancestor_items {
589                 match (
590                     self.sema.expand_attr_macro(&actual_item),
591                     self.sema.speculative_expand_attr_macro(
592                         &actual_item,
593                         &item_with_fake_ident,
594                         fake_ident_token.clone(),
595                     ),
596                 ) {
597                     // maybe parent items have attributes, so continue walking the ancestors
598                     (None, None) => continue 'ancestors,
599                     // successful expansions
600                     (Some(actual_expansion), Some((fake_expansion, fake_mapped_token))) => {
601                         let new_offset = fake_mapped_token.text_range().start();
602                         if new_offset > actual_expansion.text_range().end() {
603                             // offset outside of bounds from the original expansion,
604                             // stop here to prevent problems from happening
605                             break 'expansion;
606                         }
607                         original_file = actual_expansion;
608                         speculative_file = fake_expansion;
609                         fake_ident_token = fake_mapped_token;
610                         offset = new_offset;
611                         continue 'expansion;
612                     }
613                     // exactly one expansion failed, inconsistent state so stop expanding completely
614                     _ => break 'expansion,
615                 }
616             }
617
618             // No attributes have been expanded, so look for macro_call! token trees or derive token trees
619             let orig_tt = match find_node_at_offset::<ast::TokenTree>(&original_file, offset) {
620                 Some(it) => it,
621                 None => break 'expansion,
622             };
623             let spec_tt = match find_node_at_offset::<ast::TokenTree>(&speculative_file, offset) {
624                 Some(it) => it,
625                 None => break 'expansion,
626             };
627
628             // Expand pseudo-derive expansion
629             if let (Some(orig_attr), Some(spec_attr)) = (
630                 orig_tt.syntax().parent().and_then(ast::Meta::cast).and_then(|it| it.parent_attr()),
631                 spec_tt.syntax().parent().and_then(ast::Meta::cast).and_then(|it| it.parent_attr()),
632             ) {
633                 if let (Some(actual_expansion), Some((fake_expansion, fake_mapped_token))) = (
634                     self.sema.expand_derive_as_pseudo_attr_macro(&orig_attr),
635                     self.sema.speculative_expand_derive_as_pseudo_attr_macro(
636                         &orig_attr,
637                         &spec_attr,
638                         fake_ident_token.clone(),
639                     ),
640                 ) {
641                     derive_ctx = Some((
642                         actual_expansion,
643                         fake_expansion,
644                         fake_mapped_token.text_range().start(),
645                         orig_attr,
646                     ));
647                 }
648                 // at this point we won't have any more successful expansions, so stop
649                 break 'expansion;
650             }
651
652             // Expand fn-like macro calls
653             if let (Some(actual_macro_call), Some(macro_call_with_fake_ident)) = (
654                 orig_tt.syntax().ancestors().find_map(ast::MacroCall::cast),
655                 spec_tt.syntax().ancestors().find_map(ast::MacroCall::cast),
656             ) {
657                 let mac_call_path0 = actual_macro_call.path().as_ref().map(|s| s.syntax().text());
658                 let mac_call_path1 =
659                     macro_call_with_fake_ident.path().as_ref().map(|s| s.syntax().text());
660
661                 // inconsistent state, stop expanding
662                 if mac_call_path0 != mac_call_path1 {
663                     break 'expansion;
664                 }
665                 let speculative_args = match macro_call_with_fake_ident.token_tree() {
666                     Some(tt) => tt,
667                     None => break 'expansion,
668                 };
669
670                 match (
671                     self.sema.expand(&actual_macro_call),
672                     self.sema.speculative_expand(
673                         &actual_macro_call,
674                         &speculative_args,
675                         fake_ident_token.clone(),
676                     ),
677                 ) {
678                     // successful expansions
679                     (Some(actual_expansion), Some((fake_expansion, fake_mapped_token))) => {
680                         let new_offset = fake_mapped_token.text_range().start();
681                         if new_offset > actual_expansion.text_range().end() {
682                             // offset outside of bounds from the original expansion,
683                             // stop here to prevent problems from happening
684                             break 'expansion;
685                         }
686                         original_file = actual_expansion;
687                         speculative_file = fake_expansion;
688                         fake_ident_token = fake_mapped_token;
689                         offset = new_offset;
690                         continue 'expansion;
691                     }
692                     // at least on expansion failed, we won't have anything to expand from this point
693                     // onwards so break out
694                     _ => break 'expansion,
695                 }
696             }
697
698             // none of our states have changed so stop the loop
699             break 'expansion;
700         }
701
702         self.fill(&original_file, speculative_file, offset, derive_ctx)
703     }
704
705     /// Calculate the expected type and name of the cursor position.
706     fn expected_type_and_name(&self) -> (Option<Type>, Option<NameOrNameRef>) {
707         let mut node = match self.token.parent() {
708             Some(it) => it,
709             None => return (None, None),
710         };
711         loop {
712             break match_ast! {
713                 match node {
714                     ast::LetStmt(it) => {
715                         cov_mark::hit!(expected_type_let_with_leading_char);
716                         cov_mark::hit!(expected_type_let_without_leading_char);
717                         let ty = it.pat()
718                             .and_then(|pat| self.sema.type_of_pat(&pat))
719                             .or_else(|| it.initializer().and_then(|it| self.sema.type_of_expr(&it)))
720                             .map(TypeInfo::original);
721                         let name = match it.pat() {
722                             Some(ast::Pat::IdentPat(ident)) => ident.name().map(NameOrNameRef::Name),
723                             Some(_) | None => None,
724                         };
725
726                         (ty, name)
727                     },
728                     ast::LetExpr(it) => {
729                         cov_mark::hit!(expected_type_if_let_without_leading_char);
730                         let ty = it.pat()
731                             .and_then(|pat| self.sema.type_of_pat(&pat))
732                             .or_else(|| it.expr().and_then(|it| self.sema.type_of_expr(&it)))
733                             .map(TypeInfo::original);
734                         (ty, None)
735                     },
736                     ast::ArgList(_) => {
737                         cov_mark::hit!(expected_type_fn_param);
738                         ActiveParameter::at_token(
739                             &self.sema,
740                             self.token.clone(),
741                         ).map(|ap| {
742                             let name = ap.ident().map(NameOrNameRef::Name);
743                             let ty = if has_ref(&self.token) {
744                                 cov_mark::hit!(expected_type_fn_param_ref);
745                                 ap.ty.remove_ref()
746                             } else {
747                                 Some(ap.ty)
748                             };
749                             (ty, name)
750                         })
751                         .unwrap_or((None, None))
752                     },
753                     ast::RecordExprFieldList(it) => {
754                         // wouldn't try {} be nice...
755                         (|| {
756                             if self.token.kind() == T![..]
757                                 || self.token.prev_token().map(|t| t.kind()) == Some(T![..])
758                             {
759                                 cov_mark::hit!(expected_type_struct_func_update);
760                                 let record_expr = it.syntax().parent().and_then(ast::RecordExpr::cast)?;
761                                 let ty = self.sema.type_of_expr(&record_expr.into())?;
762                                 Some((
763                                     Some(ty.original),
764                                     None
765                                 ))
766                             } else {
767                                 cov_mark::hit!(expected_type_struct_field_without_leading_char);
768                                 let expr_field = self.token.prev_sibling_or_token()?
769                                     .into_node()
770                                     .and_then(ast::RecordExprField::cast)?;
771                                 let (_, _, ty) = self.sema.resolve_record_field(&expr_field)?;
772                                 Some((
773                                     Some(ty),
774                                     expr_field.field_name().map(NameOrNameRef::NameRef),
775                                 ))
776                             }
777                         })().unwrap_or((None, None))
778                     },
779                     ast::RecordExprField(it) => {
780                         if let Some(expr) = it.expr() {
781                             cov_mark::hit!(expected_type_struct_field_with_leading_char);
782                             (
783                                 self.sema.type_of_expr(&expr).map(TypeInfo::original),
784                                 it.field_name().map(NameOrNameRef::NameRef),
785                             )
786                         } else {
787                             cov_mark::hit!(expected_type_struct_field_followed_by_comma);
788                             let ty = self.sema.resolve_record_field(&it)
789                                 .map(|(_, _, ty)| ty);
790                             (
791                                 ty,
792                                 it.field_name().map(NameOrNameRef::NameRef),
793                             )
794                         }
795                     },
796                     // match foo { $0 }
797                     // match foo { ..., pat => $0 }
798                     ast::MatchExpr(it) => {
799                         let ty = if self.previous_token_is(T![=>]) {
800                             // match foo { ..., pat => $0 }
801                             cov_mark::hit!(expected_type_match_arm_body_without_leading_char);
802                             cov_mark::hit!(expected_type_match_arm_body_with_leading_char);
803                             self.sema.type_of_expr(&it.into())
804                         } else {
805                             // match foo { $0 }
806                             cov_mark::hit!(expected_type_match_arm_without_leading_char);
807                             it.expr().and_then(|e| self.sema.type_of_expr(&e))
808                         }.map(TypeInfo::original);
809                         (ty, None)
810                     },
811                     ast::IfExpr(it) => {
812                         let ty = it.condition()
813                             .and_then(|e| self.sema.type_of_expr(&e))
814                             .map(TypeInfo::original);
815                         (ty, None)
816                     },
817                     ast::IdentPat(it) => {
818                         cov_mark::hit!(expected_type_if_let_with_leading_char);
819                         cov_mark::hit!(expected_type_match_arm_with_leading_char);
820                         let ty = self.sema.type_of_pat(&ast::Pat::from(it)).map(TypeInfo::original);
821                         (ty, None)
822                     },
823                     ast::Fn(it) => {
824                         cov_mark::hit!(expected_type_fn_ret_with_leading_char);
825                         cov_mark::hit!(expected_type_fn_ret_without_leading_char);
826                         let def = self.sema.to_def(&it);
827                         (def.map(|def| def.ret_type(self.db)), None)
828                     },
829                     ast::ClosureExpr(it) => {
830                         let ty = self.sema.type_of_expr(&it.into());
831                         ty.and_then(|ty| ty.original.as_callable(self.db))
832                             .map(|c| (Some(c.return_type()), None))
833                             .unwrap_or((None, None))
834                     },
835                     ast::ParamList(_) => (None, None),
836                     ast::Stmt(_) => (None, None),
837                     ast::Item(_) => (None, None),
838                     _ => {
839                         match node.parent() {
840                             Some(n) => {
841                                 node = n;
842                                 continue;
843                             },
844                             None => (None, None),
845                         }
846                     },
847                 }
848             };
849         }
850     }
851
852     /// Fill the completion context, this is what does semantic reasoning about the surrounding context
853     /// of the completion location.
854     fn fill(
855         &mut self,
856         original_file: &SyntaxNode,
857         file_with_fake_ident: SyntaxNode,
858         offset: TextSize,
859         derive_ctx: Option<(SyntaxNode, SyntaxNode, TextSize, ast::Attr)>,
860     ) -> Option<()> {
861         let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased()?;
862         let syntax_element = NodeOrToken::Token(fake_ident_token);
863         if is_in_token_of_for_loop(syntax_element.clone()) {
864             // for pat $0
865             // there is nothing to complete here except `in` keyword
866             // don't bother populating the context
867             // FIXME: the completion calculations should end up good enough
868             // such that this special case becomes unnecessary
869             return None;
870         }
871
872         self.previous_token = previous_token(syntax_element.clone());
873
874         self.incomplete_let =
875             syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| {
876                 it.syntax().text_range().end() == syntax_element.text_range().end()
877             });
878
879         (self.expected_type, self.expected_name) = self.expected_type_and_name();
880
881         // Overwrite the path kind for derives
882         if let Some((original_file, file_with_fake_ident, offset, origin_attr)) = derive_ctx {
883             self.existing_derives = self
884                 .sema
885                 .resolve_derive_macro(&origin_attr)
886                 .into_iter()
887                 .flatten()
888                 .flatten()
889                 .collect();
890
891             if let Some(ast::NameLike::NameRef(name_ref)) =
892                 find_node_at_offset(&file_with_fake_ident, offset)
893             {
894                 let parent = name_ref.syntax().parent()?;
895                 let (mut nameref_ctx, _, _) =
896                     Self::classify_name_ref(&self.sema, &original_file, name_ref, parent);
897                 if let Some(NameRefKind::Path(path_ctx)) = &mut nameref_ctx.kind {
898                     path_ctx.kind = PathKind::Derive;
899                 }
900                 self.ident_ctx = IdentContext::NameRef(nameref_ctx);
901                 return Some(());
902             }
903             return None;
904         }
905
906         let name_like = match find_node_at_offset(&file_with_fake_ident, offset) {
907             Some(it) => it,
908             None => {
909                 if let Some(original) = ast::String::cast(self.original_token.clone()) {
910                     self.ident_ctx = IdentContext::String {
911                         original,
912                         expanded: ast::String::cast(self.token.clone()),
913                     };
914                 } else {
915                     // Fix up trailing whitespace problem
916                     // #[attr(foo = $0
917                     let token = if self.token.kind() == SyntaxKind::WHITESPACE {
918                         self.previous_token.as_ref()?
919                     } else {
920                         &self.token
921                     };
922                     let p = token.parent()?;
923                     if p.kind() == SyntaxKind::TOKEN_TREE
924                         && p.ancestors().any(|it| it.kind() == SyntaxKind::META)
925                     {
926                         self.ident_ctx = IdentContext::UnexpandedAttrTT {
927                             fake_attribute_under_caret: syntax_element
928                                 .ancestors()
929                                 .find_map(ast::Attr::cast),
930                         };
931                     } else {
932                         return None;
933                     }
934                 }
935                 return Some(());
936             }
937         };
938         self.completion_location =
939             determine_location(&self.sema, original_file, offset, &name_like);
940         self.impl_def = self
941             .sema
942             .token_ancestors_with_macros(self.token.clone())
943             .take_while(|it| it.kind() != SOURCE_FILE)
944             .filter_map(ast::Item::cast)
945             .take(2)
946             .find_map(|it| match it {
947                 ast::Item::Impl(impl_) => Some(impl_),
948                 _ => None,
949             });
950         self.function_def = self
951             .sema
952             .token_ancestors_with_macros(self.token.clone())
953             .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
954             .filter_map(ast::Item::cast)
955             .take(2)
956             .find_map(|it| match it {
957                 ast::Item::Fn(fn_) => Some(fn_),
958                 _ => None,
959             });
960
961         match name_like {
962             ast::NameLike::Lifetime(lifetime) => {
963                 self.ident_ctx = IdentContext::Lifetime(Self::classify_lifetime(
964                     &self.sema,
965                     original_file,
966                     lifetime,
967                 )?);
968             }
969             ast::NameLike::NameRef(name_ref) => {
970                 let parent = name_ref.syntax().parent()?;
971                 let (nameref_ctx, pat_ctx, qualifier_ctx) =
972                     Self::classify_name_ref(&self.sema, &original_file, name_ref, parent.clone());
973
974                 self.qualifier_ctx = qualifier_ctx;
975                 self.ident_ctx = IdentContext::NameRef(nameref_ctx);
976                 self.pattern_ctx = pat_ctx;
977             }
978             ast::NameLike::Name(name) => {
979                 let (name_ctx, pat_ctx) = Self::classify_name(&self.sema, original_file, name)?;
980                 self.pattern_ctx = pat_ctx;
981                 self.ident_ctx = IdentContext::Name(name_ctx);
982             }
983         }
984         Some(())
985     }
986
987     fn classify_lifetime(
988         _sema: &Semantics<RootDatabase>,
989         original_file: &SyntaxNode,
990         lifetime: ast::Lifetime,
991     ) -> Option<LifetimeContext> {
992         let parent = lifetime.syntax().parent()?;
993         if parent.kind() == ERROR {
994             return None;
995         }
996
997         let kind = match_ast! {
998             match parent {
999                 ast::LifetimeParam(param) => LifetimeKind::LifetimeParam {
1000                     is_decl: param.lifetime().as_ref() == Some(&lifetime),
1001                     param
1002                 },
1003                 ast::BreakExpr(_) => LifetimeKind::LabelRef,
1004                 ast::ContinueExpr(_) => LifetimeKind::LabelRef,
1005                 ast::Label(_) => LifetimeKind::LabelDef,
1006                 _ => LifetimeKind::Lifetime,
1007             }
1008         };
1009         let lifetime = find_node_at_offset(&original_file, lifetime.syntax().text_range().start());
1010
1011         Some(LifetimeContext { lifetime, kind })
1012     }
1013
1014     fn classify_name(
1015         _sema: &Semantics<RootDatabase>,
1016         original_file: &SyntaxNode,
1017         name: ast::Name,
1018     ) -> Option<(NameContext, Option<PatternContext>)> {
1019         let parent = name.syntax().parent()?;
1020         let mut pat_ctx = None;
1021         let kind = match_ast! {
1022             match parent {
1023                 ast::Const(_) => NameKind::Const,
1024                 ast::ConstParam(_) => NameKind::ConstParam,
1025                 ast::Enum(_) => NameKind::Enum,
1026                 ast::Fn(_) => NameKind::Function,
1027                 ast::IdentPat(bind_pat) => {
1028                     pat_ctx = Some({
1029                         let mut pat_ctx = pattern_context_for(original_file, bind_pat.into());
1030                         if let Some(record_field) = ast::RecordPatField::for_field_name(&name) {
1031                             pat_ctx.record_pat = find_node_in_file_compensated(original_file, &record_field.parent_record_pat());
1032                         }
1033                         pat_ctx
1034                     });
1035
1036                     NameKind::IdentPat
1037                 },
1038                 ast::MacroDef(_) => NameKind::MacroDef,
1039                 ast::MacroRules(_) => NameKind::MacroRules,
1040                 ast::Module(module) => NameKind::Module(module),
1041                 ast::RecordField(_) => NameKind::RecordField,
1042                 ast::Rename(_) => NameKind::Rename,
1043                 ast::SelfParam(_) => NameKind::SelfParam,
1044                 ast::Static(_) => NameKind::Static,
1045                 ast::Struct(_) => NameKind::Struct,
1046                 ast::Trait(_) => NameKind::Trait,
1047                 ast::TypeAlias(_) => NameKind::TypeAlias,
1048                 ast::TypeParam(_) => NameKind::TypeParam,
1049                 ast::Union(_) => NameKind::Union,
1050                 ast::Variant(_) => NameKind::Variant,
1051                 _ => return None,
1052             }
1053         };
1054         let name = find_node_at_offset(&original_file, name.syntax().text_range().start());
1055         Some((NameContext { name, kind }, pat_ctx))
1056     }
1057
1058     fn classify_name_ref(
1059         sema: &Semantics<RootDatabase>,
1060         original_file: &SyntaxNode,
1061         name_ref: ast::NameRef,
1062         parent: SyntaxNode,
1063     ) -> (NameRefContext, Option<PatternContext>, QualifierCtx) {
1064         let nameref = find_node_at_offset(&original_file, name_ref.syntax().text_range().start());
1065
1066         let mut res = (NameRefContext { nameref, kind: None }, None, QualifierCtx::default());
1067         let (nameref_ctx, pattern_ctx, qualifier_ctx) = &mut res;
1068
1069         if let Some(record_field) = ast::RecordExprField::for_field_name(&name_ref) {
1070             nameref_ctx.kind =
1071                 find_node_in_file_compensated(original_file, &record_field.parent_record_lit())
1072                     .map(NameRefKind::RecordExpr);
1073             return res;
1074         }
1075         if let Some(record_field) = ast::RecordPatField::for_field_name_ref(&name_ref) {
1076             *pattern_ctx = Some(PatternContext {
1077                 param_ctx: None,
1078                 has_type_ascription: false,
1079                 ref_token: None,
1080                 mut_token: None,
1081                 record_pat: find_node_in_file_compensated(
1082                     original_file,
1083                     &record_field.parent_record_pat(),
1084                 ),
1085                 ..pattern_context_for(
1086                     original_file,
1087                     record_field.parent_record_pat().clone().into(),
1088                 )
1089             });
1090             return res;
1091         }
1092
1093         let segment = match_ast! {
1094             match parent {
1095                 ast::PathSegment(segment) => segment,
1096                 ast::FieldExpr(field) => {
1097                     let receiver = find_in_original_file(field.expr(), original_file);
1098                     let receiver_is_ambiguous_float_literal = match &receiver {
1099                         Some(ast::Expr::Literal(l)) => matches! {
1100                             l.kind(),
1101                             ast::LiteralKind::FloatNumber { .. } if l.syntax().last_token().map_or(false, |it| it.text().ends_with('.'))
1102                         },
1103                         _ => false,
1104                     };
1105                     nameref_ctx.kind = Some(NameRefKind::DotAccess(DotAccess {
1106                         receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
1107                         kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal },
1108                         receiver
1109                     }));
1110                     return res;
1111                 },
1112                 ast::MethodCallExpr(method) => {
1113                     let receiver = find_in_original_file(method.receiver(), original_file);
1114                     nameref_ctx.kind = Some(NameRefKind::DotAccess(DotAccess {
1115                         receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
1116                         kind: DotAccessKind::Method { has_parens: method.arg_list().map_or(false, |it| it.l_paren_token().is_some()) },
1117                         receiver
1118                     }));
1119                     return res;
1120                 },
1121                 _ => return res,
1122             }
1123         };
1124
1125         let path = segment.parent_path();
1126         let mut path_ctx = PathCompletionCtx {
1127             has_call_parens: false,
1128             has_macro_bang: false,
1129             is_absolute_path: false,
1130             qualifier: None,
1131             parent: path.parent_path(),
1132             kind: PathKind::Item { kind: ItemListKind::SourceFile },
1133             has_type_args: false,
1134         };
1135
1136         let is_in_block = |it: &SyntaxNode| {
1137             it.parent()
1138                 .map(|node| {
1139                     ast::ExprStmt::can_cast(node.kind()) || ast::StmtList::can_cast(node.kind())
1140                 })
1141                 .unwrap_or(false)
1142         };
1143         let func_update_record = |syn: &SyntaxNode| {
1144             if let Some(record_expr) = syn.ancestors().nth(2).and_then(ast::RecordExpr::cast) {
1145                 find_node_in_file_compensated(original_file, &record_expr)
1146             } else {
1147                 None
1148             }
1149         };
1150         let after_if_expr = |node: SyntaxNode| {
1151             let prev_expr = (|| {
1152                 let prev_sibling = non_trivia_sibling(node.into(), Direction::Prev)?.into_node()?;
1153                 ast::ExprStmt::cast(prev_sibling)?.expr()
1154             })();
1155             matches!(prev_expr, Some(ast::Expr::IfExpr(_)))
1156         };
1157
1158         // We do not want to generate path completions when we are sandwiched between an item decl signature and its body.
1159         // ex. trait Foo $0 {}
1160         // in these cases parser recovery usually kicks in for our inserted identifier, causing it
1161         // to either be parsed as an ExprStmt or a MacroCall, depending on whether it is in a block
1162         // expression or an item list.
1163         // The following code checks if the body is missing, if it is we either cut off the body
1164         // from the item or it was missing in the first place
1165         let inbetween_body_and_decl_check = |node: SyntaxNode| {
1166             if let Some(NodeOrToken::Node(n)) =
1167                 syntax::algo::non_trivia_sibling(node.into(), syntax::Direction::Prev)
1168             {
1169                 if let Some(item) = ast::Item::cast(n) {
1170                     let is_inbetween = match &item {
1171                         ast::Item::Const(it) => it.body().is_none(),
1172                         ast::Item::Enum(it) => it.variant_list().is_none(),
1173                         ast::Item::ExternBlock(it) => it.extern_item_list().is_none(),
1174                         ast::Item::Fn(it) => it.body().is_none(),
1175                         ast::Item::Impl(it) => it.assoc_item_list().is_none(),
1176                         ast::Item::Module(it) => it.item_list().is_none(),
1177                         ast::Item::Static(it) => it.body().is_none(),
1178                         ast::Item::Struct(it) => it.field_list().is_none(),
1179                         ast::Item::Trait(it) => it.assoc_item_list().is_none(),
1180                         ast::Item::TypeAlias(it) => it.ty().is_none(),
1181                         ast::Item::Union(it) => it.record_field_list().is_none(),
1182                         _ => false,
1183                     };
1184                     if is_inbetween {
1185                         return Some(item);
1186                     }
1187                 }
1188             }
1189             None
1190         };
1191
1192         let fetch_ascription = |it: Option<SyntaxNode>| {
1193             let parent = it?;
1194             match_ast! {
1195                 match parent {
1196                     ast::Const(it) => {
1197                         let name = find_in_original_file(it.name(), original_file)?;
1198                         let original = ast::Const::cast(name.syntax().parent()?)?;
1199                         Some(TypeAscriptionTarget::Const(original.body()))
1200                     },
1201                     ast::RetType(it) => {
1202                         if it.thin_arrow_token().is_none() {
1203                             return None;
1204                         }
1205                         let parent = match ast::Fn::cast(parent.parent()?) {
1206                             Some(x) => x.param_list(),
1207                             None => ast::ClosureExpr::cast(parent.parent()?)?.param_list(),
1208                         };
1209
1210                         let parent = find_in_original_file(parent, original_file)?.syntax().parent()?;
1211                         Some(TypeAscriptionTarget::RetType(match_ast! {
1212                             match parent {
1213                                 ast::ClosureExpr(it) => {
1214                                     it.body()
1215                                 },
1216                                 ast::Fn(it) => {
1217                                     it.body().map(ast::Expr::BlockExpr)
1218                                 },
1219                                 _ => return None,
1220                             }
1221                         }))
1222                     },
1223                     ast::Param(it) => {
1224                         if it.colon_token().is_none() {
1225                             return None;
1226                         }
1227                         Some(TypeAscriptionTarget::FnParam(find_in_original_file(it.pat(), original_file)))
1228                     },
1229                     ast::LetStmt(it) => {
1230                         if it.colon_token().is_none() {
1231                             return None;
1232                         }
1233                         Some(TypeAscriptionTarget::Let(find_in_original_file(it.pat(), original_file)))
1234                     },
1235                     _ => None,
1236                 }
1237             }
1238         };
1239
1240         // Infer the path kind
1241         let kind = path.syntax().parent().and_then(|it| {
1242             match_ast! {
1243                 match it {
1244                     ast::PathType(it) => {
1245                         let ascription = fetch_ascription(it.syntax().parent());
1246                         Some(PathKind::Type {
1247                             in_tuple_struct: it.syntax().parent().map_or(false, |it| ast::TupleField::can_cast(it.kind())),
1248                             ascription,
1249                         })
1250                     },
1251                     ast::PathExpr(it) => {
1252                         if let Some(p) = it.syntax().parent() {
1253                             if ast::ExprStmt::can_cast(p.kind()) {
1254                                 if let Some(kind) = inbetween_body_and_decl_check(p) {
1255                                     nameref_ctx.kind = Some(NameRefKind::Keyword(kind));
1256                                     return None;
1257                                 }
1258                             }
1259                         }
1260
1261                         path_ctx.has_call_parens = it.syntax().parent().map_or(false, |it| ast::CallExpr::can_cast(it.kind()));
1262                         let in_block_expr = is_in_block(it.syntax());
1263                         let in_loop_body = is_in_loop_body(it.syntax());
1264                         let after_if_expr = after_if_expr(it.syntax().clone());
1265                         let ref_expr_parent = path.as_single_name_ref()
1266                             .and_then(|_| it.syntax().parent()).and_then(ast::RefExpr::cast);
1267                         let is_func_update = func_update_record(it.syntax());
1268
1269                         Some(PathKind::Expr { in_block_expr, in_loop_body, after_if_expr, ref_expr_parent, is_func_update })
1270                     },
1271                     ast::TupleStructPat(it) => {
1272                         path_ctx.has_call_parens = true;
1273                         *pattern_ctx = Some(pattern_context_for(original_file, it.into()));
1274                         Some(PathKind::Pat)
1275                     },
1276                     ast::RecordPat(it) => {
1277                         path_ctx.has_call_parens = true;
1278                         *pattern_ctx = Some(pattern_context_for(original_file, it.into()));
1279                         Some(PathKind::Pat)
1280                     },
1281                     ast::PathPat(it) => {
1282                         *pattern_ctx = Some(pattern_context_for(original_file, it.into()));
1283                         Some(PathKind::Pat)
1284                     },
1285                     ast::MacroCall(it) => {
1286                         if let Some(kind) = inbetween_body_and_decl_check(it.syntax().clone()) {
1287                             nameref_ctx.kind = Some(NameRefKind::Keyword(kind));
1288                             return None;
1289                         }
1290
1291                         path_ctx.has_macro_bang = it.excl_token().is_some();
1292                         let parent = it.syntax().parent();
1293                         match parent.as_ref().map(|it| it.kind()) {
1294                             Some(SyntaxKind::MACRO_PAT) => Some(PathKind::Pat),
1295                             Some(SyntaxKind::MACRO_TYPE) => Some(PathKind::Type { in_tuple_struct: false, ascription: None }),
1296                             Some(SyntaxKind::ITEM_LIST) => Some(PathKind::Item { kind: ItemListKind::Module }),
1297                             Some(SyntaxKind::ASSOC_ITEM_LIST) => Some(PathKind::Item { kind: match parent.and_then(|it| it.parent()) {
1298                                 Some(it) => match_ast! {
1299                                     match it {
1300                                         ast::Trait(_) => ItemListKind::Trait,
1301                                         ast::Impl(it) => if it.trait_().is_some() {
1302                                             ItemListKind::TraitImpl
1303                                         } else {
1304                                             ItemListKind::Impl
1305                                         },
1306                                         _ => return None
1307                                     }
1308                                 },
1309                                 None => return None,
1310                             } }),
1311                             Some(SyntaxKind::EXTERN_ITEM_LIST) => Some(PathKind::Item { kind: ItemListKind::ExternBlock }),
1312                             Some(SyntaxKind::SOURCE_FILE) => Some(PathKind::Item { kind: ItemListKind::SourceFile }),
1313                             _ => {
1314                                return parent.and_then(ast::MacroExpr::cast).map(|it| {
1315                                     let in_loop_body = is_in_loop_body(it.syntax());
1316                                     let in_block_expr = is_in_block(it.syntax());
1317                                     let after_if_expr = after_if_expr(it.syntax().clone());
1318                                     let ref_expr_parent = path.as_single_name_ref()
1319                                         .and_then(|_| it.syntax().parent()).and_then(ast::RefExpr::cast);
1320                                     let is_func_update = func_update_record(it.syntax());
1321                                     PathKind::Expr { in_block_expr, in_loop_body, after_if_expr, ref_expr_parent, is_func_update }
1322                                 });
1323                             },
1324                         }
1325                     },
1326                     ast::Meta(meta) => (|| {
1327                         let attr = meta.parent_attr()?;
1328                         let kind = attr.kind();
1329                         let attached = attr.syntax().parent()?;
1330                         let is_trailing_outer_attr = kind != AttrKind::Inner
1331                             && non_trivia_sibling(attr.syntax().clone().into(), syntax::Direction::Next).is_none();
1332                         let annotated_item_kind = if is_trailing_outer_attr {
1333                             None
1334                         } else {
1335                             Some(attached.kind())
1336                         };
1337                         Some(PathKind::Attr {
1338                             kind,
1339                             annotated_item_kind,
1340                         })
1341                     })(),
1342                     ast::Visibility(it) => Some(PathKind::Vis { has_in_token: it.in_token().is_some() }),
1343                     ast::UseTree(_) => Some(PathKind::Use),
1344                     _ => return None,
1345                 }
1346             }
1347         });
1348
1349         match kind {
1350             Some(kind) => path_ctx.kind = kind,
1351             None => return res,
1352         }
1353         path_ctx.has_type_args = segment.generic_arg_list().is_some();
1354
1355         if let Some((path, use_tree_parent)) = path_or_use_tree_qualifier(&path) {
1356             if !use_tree_parent {
1357                 path_ctx.is_absolute_path =
1358                     path.top_path().segment().map_or(false, |it| it.coloncolon_token().is_some());
1359             }
1360
1361             let path = path
1362                 .segment()
1363                 .and_then(|it| find_node_in_file(original_file, &it))
1364                 .map(|it| it.parent_path());
1365             path_ctx.qualifier = path.map(|path| {
1366                 let res = sema.resolve_path(&path);
1367                 let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier())
1368                     .all(|p| p.segment().and_then(|s| s.super_token()).is_some());
1369
1370                 // `<_>::$0`
1371                 let is_infer_qualifier = path.qualifier().is_none()
1372                     && matches!(
1373                         path.segment().and_then(|it| it.kind()),
1374                         Some(ast::PathSegmentKind::Type {
1375                             type_ref: Some(ast::Type::InferType(_)),
1376                             trait_ref: None,
1377                         })
1378                     );
1379
1380                 PathQualifierCtx {
1381                     path,
1382                     resolution: res,
1383                     is_super_chain,
1384                     use_tree_parent,
1385                     is_infer_qualifier,
1386                 }
1387             });
1388         } else if let Some(segment) = path.segment() {
1389             if segment.coloncolon_token().is_some() {
1390                 path_ctx.is_absolute_path = true;
1391             }
1392         }
1393
1394         if path_ctx.is_trivial_path() {
1395             // fetch the full expression that may have qualifiers attached to it
1396             let top_node = match path_ctx.kind {
1397                 PathKind::Expr { in_block_expr: true, .. } => {
1398                     parent.ancestors().find(|it| ast::PathExpr::can_cast(it.kind())).and_then(|p| {
1399                         let parent = p.parent()?;
1400                         if ast::StmtList::can_cast(parent.kind()) {
1401                             Some(p)
1402                         } else if ast::ExprStmt::can_cast(parent.kind()) {
1403                             Some(parent)
1404                         } else {
1405                             None
1406                         }
1407                     })
1408                 }
1409                 PathKind::Item { .. } => {
1410                     parent.ancestors().find(|it| ast::MacroCall::can_cast(it.kind()))
1411                 }
1412                 _ => None,
1413             };
1414             if let Some(top) = top_node {
1415                 if let Some(NodeOrToken::Node(error_node)) =
1416                     syntax::algo::non_trivia_sibling(top.clone().into(), syntax::Direction::Prev)
1417                 {
1418                     if error_node.kind() == SyntaxKind::ERROR {
1419                         qualifier_ctx.unsafe_tok = error_node
1420                             .children_with_tokens()
1421                             .filter_map(NodeOrToken::into_token)
1422                             .find(|it| it.kind() == T![unsafe]);
1423                         qualifier_ctx.vis_node =
1424                             error_node.children().find_map(ast::Visibility::cast);
1425                     }
1426                 }
1427
1428                 if let PathKind::Item { .. } = path_ctx.kind {
1429                     if qualifier_ctx.none() {
1430                         if let Some(t) = top.first_token() {
1431                             if let Some(prev) = t
1432                                 .prev_token()
1433                                 .and_then(|t| syntax::algo::skip_trivia_token(t, Direction::Prev))
1434                             {
1435                                 if ![T![;], T!['}'], T!['{']].contains(&prev.kind()) {
1436                                     // This was inferred to be an item position path, but it seems
1437                                     // to be part of some other broken node which leaked into an item
1438                                     // list, so return without setting the path context
1439                                     return res;
1440                                 }
1441                             }
1442                         }
1443                     }
1444                 }
1445             }
1446         }
1447         nameref_ctx.kind = Some(NameRefKind::Path(path_ctx));
1448         res
1449     }
1450 }
1451
1452 fn pattern_context_for(original_file: &SyntaxNode, pat: ast::Pat) -> PatternContext {
1453     let mut is_param = None;
1454     let (refutability, has_type_ascription) =
1455     pat
1456         .syntax()
1457         .ancestors()
1458         .skip_while(|it| ast::Pat::can_cast(it.kind()))
1459         .next()
1460         .map_or((PatternRefutability::Irrefutable, false), |node| {
1461             let refutability = match_ast! {
1462                 match node {
1463                     ast::LetStmt(let_) => return (PatternRefutability::Irrefutable, let_.ty().is_some()),
1464                     ast::Param(param) => {
1465                         let has_type_ascription = param.ty().is_some();
1466                         is_param = (|| {
1467                             let fake_param_list = param.syntax().parent().and_then(ast::ParamList::cast)?;
1468                             let param_list = find_node_in_file_compensated(original_file, &fake_param_list)?;
1469                             let param_list_owner = param_list.syntax().parent()?;
1470                             let kind = match_ast! {
1471                                 match param_list_owner {
1472                                     ast::ClosureExpr(closure) => ParamKind::Closure(closure),
1473                                     ast::Fn(fn_) => ParamKind::Function(fn_),
1474                                     _ => return None,
1475                                 }
1476                             };
1477                             Some((param_list, param, kind))
1478                         })();
1479                         return (PatternRefutability::Irrefutable, has_type_ascription)
1480                     },
1481                     ast::MatchArm(_) => PatternRefutability::Refutable,
1482                     ast::LetExpr(_) => PatternRefutability::Refutable,
1483                     ast::ForExpr(_) => PatternRefutability::Irrefutable,
1484                     _ => PatternRefutability::Irrefutable,
1485                 }
1486             };
1487             (refutability, false)
1488         });
1489     let (ref_token, mut_token) = match &pat {
1490         ast::Pat::IdentPat(it) => (it.ref_token(), it.mut_token()),
1491         _ => (None, None),
1492     };
1493     PatternContext {
1494         refutability,
1495         param_ctx: is_param,
1496         has_type_ascription,
1497         parent_pat: pat.syntax().parent().and_then(ast::Pat::cast),
1498         mut_token,
1499         ref_token,
1500         record_pat: None,
1501     }
1502 }
1503
1504 fn find_in_original_file<N: AstNode>(x: Option<N>, original_file: &SyntaxNode) -> Option<N> {
1505     fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> {
1506         let range = syntax.text_range().intersect(range)?;
1507         syntax.covering_element(range).ancestors().find_map(N::cast)
1508     }
1509     x.map(|e| e.syntax().text_range()).and_then(|r| find_node_with_range(original_file, r))
1510 }
1511
1512 /// Attempts to find `node` inside `syntax` via `node`'s text range.
1513 fn find_node_in_file<N: AstNode>(syntax: &SyntaxNode, node: &N) -> Option<N> {
1514     let syntax_range = syntax.text_range();
1515     let range = node.syntax().text_range();
1516     let intersection = range.intersect(syntax_range)?;
1517     syntax.covering_element(intersection).ancestors().find_map(N::cast)
1518 }
1519
1520 /// Attempts to find `node` inside `syntax` via `node`'s text range while compensating
1521 /// for the offset introduced by the fake ident.
1522 /// This is wrong if `node` comes before the insertion point! Use `find_node_in_file` instead.
1523 fn find_node_in_file_compensated<N: AstNode>(syntax: &SyntaxNode, node: &N) -> Option<N> {
1524     let syntax_range = syntax.text_range();
1525     let range = node.syntax().text_range();
1526     let end = range.end().checked_sub(TextSize::try_from(COMPLETION_MARKER.len()).ok()?)?;
1527     if end < range.start() {
1528         return None;
1529     }
1530     let range = TextRange::new(range.start(), end);
1531     // our inserted ident could cause `range` to be go outside of the original syntax, so cap it
1532     let intersection = range.intersect(syntax_range)?;
1533     syntax.covering_element(intersection).ancestors().find_map(N::cast)
1534 }
1535
1536 fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<(ast::Path, bool)> {
1537     if let Some(qual) = path.qualifier() {
1538         return Some((qual, false));
1539     }
1540     let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
1541     let use_tree = use_tree_list.syntax().parent().and_then(ast::UseTree::cast)?;
1542     Some((use_tree.path()?, true))
1543 }
1544
1545 fn has_ref(token: &SyntaxToken) -> bool {
1546     let mut token = token.clone();
1547     for skip in [IDENT, WHITESPACE, T![mut]] {
1548         if token.kind() == skip {
1549             token = match token.prev_token() {
1550                 Some(it) => it,
1551                 None => return false,
1552             }
1553         }
1554     }
1555     token.kind() == T![&]
1556 }
1557
1558 const OP_TRAIT_LANG_NAMES: &[&str] = &[
1559     "add_assign",
1560     "add",
1561     "bitand_assign",
1562     "bitand",
1563     "bitor_assign",
1564     "bitor",
1565     "bitxor_assign",
1566     "bitxor",
1567     "deref_mut",
1568     "deref",
1569     "div_assign",
1570     "div",
1571     "eq",
1572     "fn_mut",
1573     "fn_once",
1574     "fn",
1575     "index_mut",
1576     "index",
1577     "mul_assign",
1578     "mul",
1579     "neg",
1580     "not",
1581     "partial_ord",
1582     "rem_assign",
1583     "rem",
1584     "shl_assign",
1585     "shl",
1586     "shr_assign",
1587     "shr",
1588     "sub",
1589 ];
1590
1591 #[cfg(test)]
1592 mod tests {
1593     use expect_test::{expect, Expect};
1594     use hir::HirDisplay;
1595
1596     use crate::tests::{position, TEST_CONFIG};
1597
1598     use super::CompletionContext;
1599
1600     fn check_expected_type_and_name(ra_fixture: &str, expect: Expect) {
1601         let (db, pos) = position(ra_fixture);
1602         let config = TEST_CONFIG;
1603         let completion_context = CompletionContext::new(&db, pos, &config).unwrap();
1604
1605         let ty = completion_context
1606             .expected_type
1607             .map(|t| t.display_test(&db).to_string())
1608             .unwrap_or("?".to_owned());
1609
1610         let name = completion_context
1611             .expected_name
1612             .map_or_else(|| "?".to_owned(), |name| name.to_string());
1613
1614         expect.assert_eq(&format!("ty: {}, name: {}", ty, name));
1615     }
1616
1617     #[test]
1618     fn expected_type_let_without_leading_char() {
1619         cov_mark::check!(expected_type_let_without_leading_char);
1620         check_expected_type_and_name(
1621             r#"
1622 fn foo() {
1623     let x: u32 = $0;
1624 }
1625 "#,
1626             expect![[r#"ty: u32, name: x"#]],
1627         );
1628     }
1629
1630     #[test]
1631     fn expected_type_let_with_leading_char() {
1632         cov_mark::check!(expected_type_let_with_leading_char);
1633         check_expected_type_and_name(
1634             r#"
1635 fn foo() {
1636     let x: u32 = c$0;
1637 }
1638 "#,
1639             expect![[r#"ty: u32, name: x"#]],
1640         );
1641     }
1642
1643     #[test]
1644     fn expected_type_let_pat() {
1645         check_expected_type_and_name(
1646             r#"
1647 fn foo() {
1648     let x$0 = 0u32;
1649 }
1650 "#,
1651             expect![[r#"ty: u32, name: ?"#]],
1652         );
1653         check_expected_type_and_name(
1654             r#"
1655 fn foo() {
1656     let $0 = 0u32;
1657 }
1658 "#,
1659             expect![[r#"ty: u32, name: ?"#]],
1660         );
1661     }
1662
1663     #[test]
1664     fn expected_type_fn_param() {
1665         cov_mark::check!(expected_type_fn_param);
1666         check_expected_type_and_name(
1667             r#"
1668 fn foo() { bar($0); }
1669 fn bar(x: u32) {}
1670 "#,
1671             expect![[r#"ty: u32, name: x"#]],
1672         );
1673         check_expected_type_and_name(
1674             r#"
1675 fn foo() { bar(c$0); }
1676 fn bar(x: u32) {}
1677 "#,
1678             expect![[r#"ty: u32, name: x"#]],
1679         );
1680     }
1681
1682     #[test]
1683     fn expected_type_fn_param_ref() {
1684         cov_mark::check!(expected_type_fn_param_ref);
1685         check_expected_type_and_name(
1686             r#"
1687 fn foo() { bar(&$0); }
1688 fn bar(x: &u32) {}
1689 "#,
1690             expect![[r#"ty: u32, name: x"#]],
1691         );
1692         check_expected_type_and_name(
1693             r#"
1694 fn foo() { bar(&mut $0); }
1695 fn bar(x: &mut u32) {}
1696 "#,
1697             expect![[r#"ty: u32, name: x"#]],
1698         );
1699         check_expected_type_and_name(
1700             r#"
1701 fn foo() { bar(& c$0); }
1702 fn bar(x: &u32) {}
1703         "#,
1704             expect![[r#"ty: u32, name: x"#]],
1705         );
1706         check_expected_type_and_name(
1707             r#"
1708 fn foo() { bar(&mut c$0); }
1709 fn bar(x: &mut u32) {}
1710 "#,
1711             expect![[r#"ty: u32, name: x"#]],
1712         );
1713         check_expected_type_and_name(
1714             r#"
1715 fn foo() { bar(&c$0); }
1716 fn bar(x: &u32) {}
1717         "#,
1718             expect![[r#"ty: u32, name: x"#]],
1719         );
1720     }
1721
1722     #[test]
1723     fn expected_type_struct_field_without_leading_char() {
1724         cov_mark::check!(expected_type_struct_field_without_leading_char);
1725         check_expected_type_and_name(
1726             r#"
1727 struct Foo { a: u32 }
1728 fn foo() {
1729     Foo { a: $0 };
1730 }
1731 "#,
1732             expect![[r#"ty: u32, name: a"#]],
1733         )
1734     }
1735
1736     #[test]
1737     fn expected_type_struct_field_followed_by_comma() {
1738         cov_mark::check!(expected_type_struct_field_followed_by_comma);
1739         check_expected_type_and_name(
1740             r#"
1741 struct Foo { a: u32 }
1742 fn foo() {
1743     Foo { a: $0, };
1744 }
1745 "#,
1746             expect![[r#"ty: u32, name: a"#]],
1747         )
1748     }
1749
1750     #[test]
1751     fn expected_type_generic_struct_field() {
1752         check_expected_type_and_name(
1753             r#"
1754 struct Foo<T> { a: T }
1755 fn foo() -> Foo<u32> {
1756     Foo { a: $0 }
1757 }
1758 "#,
1759             expect![[r#"ty: u32, name: a"#]],
1760         )
1761     }
1762
1763     #[test]
1764     fn expected_type_struct_field_with_leading_char() {
1765         cov_mark::check!(expected_type_struct_field_with_leading_char);
1766         check_expected_type_and_name(
1767             r#"
1768 struct Foo { a: u32 }
1769 fn foo() {
1770     Foo { a: c$0 };
1771 }
1772 "#,
1773             expect![[r#"ty: u32, name: a"#]],
1774         );
1775     }
1776
1777     #[test]
1778     fn expected_type_match_arm_without_leading_char() {
1779         cov_mark::check!(expected_type_match_arm_without_leading_char);
1780         check_expected_type_and_name(
1781             r#"
1782 enum E { X }
1783 fn foo() {
1784    match E::X { $0 }
1785 }
1786 "#,
1787             expect![[r#"ty: E, name: ?"#]],
1788         );
1789     }
1790
1791     #[test]
1792     fn expected_type_match_arm_with_leading_char() {
1793         cov_mark::check!(expected_type_match_arm_with_leading_char);
1794         check_expected_type_and_name(
1795             r#"
1796 enum E { X }
1797 fn foo() {
1798    match E::X { c$0 }
1799 }
1800 "#,
1801             expect![[r#"ty: E, name: ?"#]],
1802         );
1803     }
1804
1805     #[test]
1806     fn expected_type_match_arm_body_without_leading_char() {
1807         cov_mark::check!(expected_type_match_arm_body_without_leading_char);
1808         check_expected_type_and_name(
1809             r#"
1810 struct Foo;
1811 enum E { X }
1812 fn foo() -> Foo {
1813    match E::X { E::X => $0 }
1814 }
1815 "#,
1816             expect![[r#"ty: Foo, name: ?"#]],
1817         );
1818     }
1819
1820     #[test]
1821     fn expected_type_match_body_arm_with_leading_char() {
1822         cov_mark::check!(expected_type_match_arm_body_with_leading_char);
1823         check_expected_type_and_name(
1824             r#"
1825 struct Foo;
1826 enum E { X }
1827 fn foo() -> Foo {
1828    match E::X { E::X => c$0 }
1829 }
1830 "#,
1831             expect![[r#"ty: Foo, name: ?"#]],
1832         );
1833     }
1834
1835     #[test]
1836     fn expected_type_if_let_without_leading_char() {
1837         cov_mark::check!(expected_type_if_let_without_leading_char);
1838         check_expected_type_and_name(
1839             r#"
1840 enum Foo { Bar, Baz, Quux }
1841
1842 fn foo() {
1843     let f = Foo::Quux;
1844     if let $0 = f { }
1845 }
1846 "#,
1847             expect![[r#"ty: Foo, name: ?"#]],
1848         )
1849     }
1850
1851     #[test]
1852     fn expected_type_if_let_with_leading_char() {
1853         cov_mark::check!(expected_type_if_let_with_leading_char);
1854         check_expected_type_and_name(
1855             r#"
1856 enum Foo { Bar, Baz, Quux }
1857
1858 fn foo() {
1859     let f = Foo::Quux;
1860     if let c$0 = f { }
1861 }
1862 "#,
1863             expect![[r#"ty: Foo, name: ?"#]],
1864         )
1865     }
1866
1867     #[test]
1868     fn expected_type_fn_ret_without_leading_char() {
1869         cov_mark::check!(expected_type_fn_ret_without_leading_char);
1870         check_expected_type_and_name(
1871             r#"
1872 fn foo() -> u32 {
1873     $0
1874 }
1875 "#,
1876             expect![[r#"ty: u32, name: ?"#]],
1877         )
1878     }
1879
1880     #[test]
1881     fn expected_type_fn_ret_with_leading_char() {
1882         cov_mark::check!(expected_type_fn_ret_with_leading_char);
1883         check_expected_type_and_name(
1884             r#"
1885 fn foo() -> u32 {
1886     c$0
1887 }
1888 "#,
1889             expect![[r#"ty: u32, name: ?"#]],
1890         )
1891     }
1892
1893     #[test]
1894     fn expected_type_fn_ret_fn_ref_fully_typed() {
1895         check_expected_type_and_name(
1896             r#"
1897 fn foo() -> u32 {
1898     foo$0
1899 }
1900 "#,
1901             expect![[r#"ty: u32, name: ?"#]],
1902         )
1903     }
1904
1905     #[test]
1906     fn expected_type_closure_param_return() {
1907         // FIXME: make this work with `|| $0`
1908         check_expected_type_and_name(
1909             r#"
1910 //- minicore: fn
1911 fn foo() {
1912     bar(|| a$0);
1913 }
1914
1915 fn bar(f: impl FnOnce() -> u32) {}
1916 "#,
1917             expect![[r#"ty: u32, name: ?"#]],
1918         );
1919     }
1920
1921     #[test]
1922     fn expected_type_generic_function() {
1923         check_expected_type_and_name(
1924             r#"
1925 fn foo() {
1926     bar::<u32>($0);
1927 }
1928
1929 fn bar<T>(t: T) {}
1930 "#,
1931             expect![[r#"ty: u32, name: t"#]],
1932         );
1933     }
1934
1935     #[test]
1936     fn expected_type_generic_method() {
1937         check_expected_type_and_name(
1938             r#"
1939 fn foo() {
1940     S(1u32).bar($0);
1941 }
1942
1943 struct S<T>(T);
1944 impl<T> S<T> {
1945     fn bar(self, t: T) {}
1946 }
1947 "#,
1948             expect![[r#"ty: u32, name: t"#]],
1949         );
1950     }
1951
1952     #[test]
1953     fn expected_type_functional_update() {
1954         cov_mark::check!(expected_type_struct_func_update);
1955         check_expected_type_and_name(
1956             r#"
1957 struct Foo { field: u32 }
1958 fn foo() {
1959     Foo {
1960         ..$0
1961     }
1962 }
1963 "#,
1964             expect![[r#"ty: Foo, name: ?"#]],
1965         );
1966     }
1967
1968     #[test]
1969     fn expected_type_param_pat() {
1970         check_expected_type_and_name(
1971             r#"
1972 struct Foo { field: u32 }
1973 fn foo(a$0: Foo) {}
1974 "#,
1975             expect![[r#"ty: Foo, name: ?"#]],
1976         );
1977         check_expected_type_and_name(
1978             r#"
1979 struct Foo { field: u32 }
1980 fn foo($0: Foo) {}
1981 "#,
1982             // FIXME make this work, currently fails due to pattern recovery eating the `:`
1983             expect![[r#"ty: ?, name: ?"#]],
1984         );
1985     }
1986 }