1 //! See `CompletionContext` structure.
5 use base_db::SourceDatabaseExt;
7 HasAttrs, Local, Name, PathResolution, ScopeDef, Semantics, SemanticsScope, Type, TypeInfo,
10 active_parameter::ActiveParameter,
11 base_db::{FilePosition, SourceDatabase},
12 famous_defs::FamousDefs,
13 FxHashMap, FxHashSet, RootDatabase,
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,
26 determine_location, is_in_loop_body, is_in_token_of_for_loop, previous_token,
32 const COMPLETION_MARKER: &str = "intellijRulezz";
34 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
35 pub(crate) enum PatternRefutability {
40 pub(crate) enum Visible {
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>,
54 pub(super) fn none(&self) -> bool {
55 self.unsafe_tok.is_none() && self.vis_node.is_none()
59 /// The state of the path we are currently completing.
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,
77 impl PathCompletionCtx {
78 pub(super) fn is_trivial_path(&self) -> bool {
82 has_call_parens: false,
83 has_macro_bang: false,
84 is_absolute_path: false,
94 /// The kind of path we are completing right now.
95 #[derive(Clone, Debug, PartialEq, Eq)]
96 pub(super) enum PathKind {
101 ref_expr_parent: Option<ast::RefExpr>,
102 is_func_update: Option<ast::RecordExpr>,
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>,
112 annotated_item_kind: Option<SyntaxKind>,
115 /// Path in item position, that is inside an (Assoc)ItemList
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>),
134 /// The kind of item list a [`PathKind::Item`] belongs to.
135 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
136 pub(super) enum ItemListKind {
145 /// The path qualifier state of the path we are completing.
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,
155 pub(crate) is_infer_qualifier: bool,
158 /// The state of the pattern we are completing.
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>,
171 /// The state of the lifetime we are completing.
173 pub(super) struct LifetimeContext {
174 pub(super) lifetime: Option<ast::Lifetime>,
175 pub(super) kind: LifetimeKind,
178 /// The kind of lifetime we are completing.
180 pub(super) enum LifetimeKind {
181 LifetimeParam { is_decl: bool, param: ast::LifetimeParam },
187 /// The state of the name we are completing.
189 pub(super) struct NameContext {
191 pub(super) name: Option<ast::Name>,
192 pub(super) kind: NameKind,
195 /// The kind of the name we are completing.
198 pub(super) enum NameKind {
220 /// The state of the NameRef we are completing.
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>,
228 /// The kind of the NameRef we are completing.
230 pub(super) enum NameRefKind {
231 Path(PathCompletionCtx),
232 DotAccess(DotAccess),
233 /// Position where we are only interested in keyword completions
235 /// The record expression this nameref is a field of
236 RecordExpr(ast::RecordExpr),
239 /// The identifier we are currently completing.
241 pub(super) enum IdentContext {
243 NameRef(NameRefContext),
244 Lifetime(LifetimeContext),
245 /// The string the cursor is currently inside
248 original: ast::String,
250 expanded: Option<ast::String>,
252 /// Set if we are currently completing in an unexpanded attribute, this usually implies a builtin attribute like `allow($0)`
254 fake_attribute_under_caret: Option<ast::Attr>,
258 /// Information about the field or method access we are completing.
260 pub(super) struct DotAccess {
261 pub(super) receiver: Option<ast::Expr>,
262 pub(super) receiver_ty: Option<TypeInfo>,
263 pub(super) kind: DotAccessKind,
267 pub(super) enum DotAccessKind {
269 /// True if the receiver is an integer and there is no ident in the original file after it yet
271 receiver_is_ambiguous_float_literal: bool,
278 #[derive(Clone, Debug, PartialEq, Eq)]
279 pub(crate) enum ParamKind {
281 Closure(ast::ClosureExpr),
284 /// `CompletionContext` is created early during completion to figure out, where
285 /// exactly is the cursor, syntax-wise.
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,
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,
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>,
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,
316 pub(super) completion_location: Option<ImmediateLocation>,
317 pub(super) previous_token: Option<SyntaxToken>,
319 pub(super) ident_ctx: IdentContext,
321 pub(super) pattern_ctx: Option<PatternContext>,
322 pub(super) qualifier_ctx: QualifierCtx,
324 pub(super) existing_derives: FxHashSet<hir::Macro>,
326 pub(super) locals: FxHashMap<Name, Local>,
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();
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))
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),
346 pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
347 self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
350 pub(crate) fn famous_defs(&self) -> FamousDefs {
351 FamousDefs(&self.sema, self.krate)
354 pub(super) fn nameref_ctx(&self) -> Option<&NameRefContext> {
355 match &self.ident_ctx {
356 IdentContext::NameRef(it) => Some(it),
361 pub(super) fn name_ctx(&self) -> Option<&NameContext> {
362 match &self.ident_ctx {
363 IdentContext::Name(it) => Some(it),
368 pub(super) fn lifetime_ctx(&self) -> Option<&LifetimeContext> {
369 match &self.ident_ctx {
370 IdentContext::Lifetime(it) => Some(it),
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, .. })),
380 }) => receiver.as_ref(),
385 pub(crate) fn has_dot_receiver(&self) -> bool {
386 self.dot_receiver().is_some()
389 // FIXME: This shouldn't exist
390 pub(crate) fn expects_generic_arg(&self) -> bool {
391 matches!(self.completion_location, Some(ImmediateLocation::GenericArgList(_)))
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),
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))
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
408 I: hir::HasVisibility + hir::HasAttrs + hir::HasCrate + Copy,
410 self.is_visible_impl(&item.visibility(self.db), &item.attrs(self.db), item.krate(self.db))
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);
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),
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()),
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());
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) {
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));
466 vis: &hir::Visibility,
468 defining_crate: hir::Crate,
470 if !vis.is_visible_from(self.db, self.module.into()) {
471 if !self.config.enable_private_editable {
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 };
481 if self.is_doc_hidden(attrs, defining_crate) {
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()
494 // CompletionContext construction
495 impl<'a> CompletionContext<'a> {
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);
504 let original_file = sema.parse(file_id);
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()
514 let fake_ident_token =
515 file_with_fake_ident.syntax().token_at_offset(offset).right_biased()?;
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();
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);
530 let mut ctx = CompletionContext {
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 },
550 qualifier_ctx: Default::default(),
551 existing_derives: Default::default(),
555 original_file.syntax().clone(),
556 file_with_fake_ident.syntax().clone(),
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.
568 mut original_file: SyntaxNode,
569 mut speculative_file: SyntaxNode,
570 mut offset: TextSize,
571 mut fake_ident_token: SyntaxToken,
573 let _p = profile::span("CompletionContext::expand_and_fill");
574 let mut derive_ctx = None;
578 |item: &ast::Item| item.syntax().ancestors().skip(1).find_map(ast::Item::cast);
579 let ancestor_items = iter::successors(
581 find_node_at_offset::<ast::Item>(&original_file, offset),
582 find_node_at_offset::<ast::Item>(&speculative_file, offset),
584 |(a, b)| parent_item(a).zip(parent_item(b)),
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 {
590 self.sema.expand_attr_macro(&actual_item),
591 self.sema.speculative_expand_attr_macro(
593 &item_with_fake_ident,
594 fake_ident_token.clone(),
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
607 original_file = actual_expansion;
608 speculative_file = fake_expansion;
609 fake_ident_token = fake_mapped_token;
613 // exactly one expansion failed, inconsistent state so stop expanding completely
614 _ => break 'expansion,
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) {
621 None => break 'expansion,
623 let spec_tt = match find_node_at_offset::<ast::TokenTree>(&speculative_file, offset) {
625 None => break 'expansion,
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()),
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(
638 fake_ident_token.clone(),
644 fake_mapped_token.text_range().start(),
648 // at this point we won't have any more successful expansions, so stop
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),
657 let mac_call_path0 = actual_macro_call.path().as_ref().map(|s| s.syntax().text());
659 macro_call_with_fake_ident.path().as_ref().map(|s| s.syntax().text());
661 // inconsistent state, stop expanding
662 if mac_call_path0 != mac_call_path1 {
665 let speculative_args = match macro_call_with_fake_ident.token_tree() {
667 None => break 'expansion,
671 self.sema.expand(&actual_macro_call),
672 self.sema.speculative_expand(
675 fake_ident_token.clone(),
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
686 original_file = actual_expansion;
687 speculative_file = fake_expansion;
688 fake_ident_token = fake_mapped_token;
692 // at least on expansion failed, we won't have anything to expand from this point
693 // onwards so break out
694 _ => break 'expansion,
698 // none of our states have changed so stop the loop
702 self.fill(&original_file, speculative_file, offset, derive_ctx)
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() {
709 None => return (None, None),
714 ast::LetStmt(it) => {
715 cov_mark::hit!(expected_type_let_with_leading_char);
716 cov_mark::hit!(expected_type_let_without_leading_char);
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,
728 ast::LetExpr(it) => {
729 cov_mark::hit!(expected_type_if_let_without_leading_char);
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);
737 cov_mark::hit!(expected_type_fn_param);
738 ActiveParameter::at_token(
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);
751 .unwrap_or((None, None))
753 ast::RecordExprFieldList(it) => {
754 // wouldn't try {} be nice...
756 if self.token.kind() == T![..]
757 || self.token.prev_token().map(|t| t.kind()) == Some(T![..])
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())?;
767 cov_mark::hit!(expected_type_struct_field_without_leading_char);
768 let expr_field = self.token.prev_sibling_or_token()?
770 .and_then(ast::RecordExprField::cast)?;
771 let (_, _, ty) = self.sema.resolve_record_field(&expr_field)?;
774 expr_field.field_name().map(NameOrNameRef::NameRef),
777 })().unwrap_or((None, None))
779 ast::RecordExprField(it) => {
780 if let Some(expr) = it.expr() {
781 cov_mark::hit!(expected_type_struct_field_with_leading_char);
783 self.sema.type_of_expr(&expr).map(TypeInfo::original),
784 it.field_name().map(NameOrNameRef::NameRef),
787 cov_mark::hit!(expected_type_struct_field_followed_by_comma);
788 let ty = self.sema.resolve_record_field(&it)
789 .map(|(_, _, ty)| ty);
792 it.field_name().map(NameOrNameRef::NameRef),
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())
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);
812 let ty = it.condition()
813 .and_then(|e| self.sema.type_of_expr(&e))
814 .map(TypeInfo::original);
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);
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)
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))
835 ast::ParamList(_) => (None, None),
836 ast::Stmt(_) => (None, None),
837 ast::Item(_) => (None, None),
839 match node.parent() {
844 None => (None, None),
852 /// Fill the completion context, this is what does semantic reasoning about the surrounding context
853 /// of the completion location.
856 original_file: &SyntaxNode,
857 file_with_fake_ident: SyntaxNode,
859 derive_ctx: Option<(SyntaxNode, SyntaxNode, TextSize, ast::Attr)>,
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()) {
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
872 self.previous_token = previous_token(syntax_element.clone());
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()
879 (self.expected_type, self.expected_name) = self.expected_type_and_name();
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
885 .resolve_derive_macro(&origin_attr)
891 if let Some(ast::NameLike::NameRef(name_ref)) =
892 find_node_at_offset(&file_with_fake_ident, offset)
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;
900 self.ident_ctx = IdentContext::NameRef(nameref_ctx);
906 let name_like = match find_node_at_offset(&file_with_fake_ident, offset) {
909 if let Some(original) = ast::String::cast(self.original_token.clone()) {
910 self.ident_ctx = IdentContext::String {
912 expanded: ast::String::cast(self.token.clone()),
915 // Fix up trailing whitespace problem
917 let token = if self.token.kind() == SyntaxKind::WHITESPACE {
918 self.previous_token.as_ref()?
922 let p = token.parent()?;
923 if p.kind() == SyntaxKind::TOKEN_TREE
924 && p.ancestors().any(|it| it.kind() == SyntaxKind::META)
926 self.ident_ctx = IdentContext::UnexpandedAttrTT {
927 fake_attribute_under_caret: syntax_element
929 .find_map(ast::Attr::cast),
938 self.completion_location =
939 determine_location(&self.sema, original_file, offset, &name_like);
942 .token_ancestors_with_macros(self.token.clone())
943 .take_while(|it| it.kind() != SOURCE_FILE)
944 .filter_map(ast::Item::cast)
946 .find_map(|it| match it {
947 ast::Item::Impl(impl_) => Some(impl_),
950 self.function_def = self
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)
956 .find_map(|it| match it {
957 ast::Item::Fn(fn_) => Some(fn_),
962 ast::NameLike::Lifetime(lifetime) => {
963 self.ident_ctx = IdentContext::Lifetime(Self::classify_lifetime(
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());
974 self.qualifier_ctx = qualifier_ctx;
975 self.ident_ctx = IdentContext::NameRef(nameref_ctx);
976 self.pattern_ctx = pat_ctx;
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);
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 {
997 let kind = match_ast! {
999 ast::LifetimeParam(param) => LifetimeKind::LifetimeParam {
1000 is_decl: param.lifetime().as_ref() == Some(&lifetime),
1003 ast::BreakExpr(_) => LifetimeKind::LabelRef,
1004 ast::ContinueExpr(_) => LifetimeKind::LabelRef,
1005 ast::Label(_) => LifetimeKind::LabelDef,
1006 _ => LifetimeKind::Lifetime,
1009 let lifetime = find_node_at_offset(&original_file, lifetime.syntax().text_range().start());
1011 Some(LifetimeContext { lifetime, kind })
1015 _sema: &Semantics<RootDatabase>,
1016 original_file: &SyntaxNode,
1018 ) -> Option<(NameContext, Option<PatternContext>)> {
1019 let parent = name.syntax().parent()?;
1020 let mut pat_ctx = None;
1021 let kind = match_ast! {
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) => {
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());
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,
1054 let name = find_node_at_offset(&original_file, name.syntax().text_range().start());
1055 Some((NameContext { name, kind }, pat_ctx))
1058 fn classify_name_ref(
1059 sema: &Semantics<RootDatabase>,
1060 original_file: &SyntaxNode,
1061 name_ref: ast::NameRef,
1063 ) -> (NameRefContext, Option<PatternContext>, QualifierCtx) {
1064 let nameref = find_node_at_offset(&original_file, name_ref.syntax().text_range().start());
1066 let mut res = (NameRefContext { nameref, kind: None }, None, QualifierCtx::default());
1067 let (nameref_ctx, pattern_ctx, qualifier_ctx) = &mut res;
1069 if let Some(record_field) = ast::RecordExprField::for_field_name(&name_ref) {
1071 find_node_in_file_compensated(original_file, &record_field.parent_record_lit())
1072 .map(NameRefKind::RecordExpr);
1075 if let Some(record_field) = ast::RecordPatField::for_field_name_ref(&name_ref) {
1076 *pattern_ctx = Some(PatternContext {
1078 has_type_ascription: false,
1081 record_pat: find_node_in_file_compensated(
1083 &record_field.parent_record_pat(),
1085 ..pattern_context_for(
1087 record_field.parent_record_pat().clone().into(),
1093 let segment = match_ast! {
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! {
1101 ast::LiteralKind::FloatNumber { .. } if l.syntax().last_token().map_or(false, |it| it.text().ends_with('.'))
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 },
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()) },
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,
1131 parent: path.parent_path(),
1132 kind: PathKind::Item { kind: ItemListKind::SourceFile },
1133 has_type_args: false,
1136 let is_in_block = |it: &SyntaxNode| {
1139 ast::ExprStmt::can_cast(node.kind()) || ast::StmtList::can_cast(node.kind())
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)
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()
1155 matches!(prev_expr, Some(ast::Expr::IfExpr(_)))
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)
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(),
1192 let fetch_ascription = |it: Option<SyntaxNode>| {
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()))
1201 ast::RetType(it) => {
1202 if it.thin_arrow_token().is_none() {
1205 let parent = match ast::Fn::cast(parent.parent()?) {
1206 Some(x) => x.param_list(),
1207 None => ast::ClosureExpr::cast(parent.parent()?)?.param_list(),
1210 let parent = find_in_original_file(parent, original_file)?.syntax().parent()?;
1211 Some(TypeAscriptionTarget::RetType(match_ast! {
1213 ast::ClosureExpr(it) => {
1217 it.body().map(ast::Expr::BlockExpr)
1224 if it.colon_token().is_none() {
1227 Some(TypeAscriptionTarget::FnParam(find_in_original_file(it.pat(), original_file)))
1229 ast::LetStmt(it) => {
1230 if it.colon_token().is_none() {
1233 Some(TypeAscriptionTarget::Let(find_in_original_file(it.pat(), original_file)))
1240 // Infer the path kind
1241 let kind = path.syntax().parent().and_then(|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())),
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));
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());
1269 Some(PathKind::Expr { in_block_expr, in_loop_body, after_if_expr, ref_expr_parent, is_func_update })
1271 ast::TupleStructPat(it) => {
1272 path_ctx.has_call_parens = true;
1273 *pattern_ctx = Some(pattern_context_for(original_file, it.into()));
1276 ast::RecordPat(it) => {
1277 path_ctx.has_call_parens = true;
1278 *pattern_ctx = Some(pattern_context_for(original_file, it.into()));
1281 ast::PathPat(it) => {
1282 *pattern_ctx = Some(pattern_context_for(original_file, it.into()));
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));
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! {
1300 ast::Trait(_) => ItemListKind::Trait,
1301 ast::Impl(it) => if it.trait_().is_some() {
1302 ItemListKind::TraitImpl
1309 None => return None,
1311 Some(SyntaxKind::EXTERN_ITEM_LIST) => Some(PathKind::Item { kind: ItemListKind::ExternBlock }),
1312 Some(SyntaxKind::SOURCE_FILE) => Some(PathKind::Item { kind: ItemListKind::SourceFile }),
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 }
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 {
1335 Some(attached.kind())
1337 Some(PathKind::Attr {
1339 annotated_item_kind,
1342 ast::Visibility(it) => Some(PathKind::Vis { has_in_token: it.in_token().is_some() }),
1343 ast::UseTree(_) => Some(PathKind::Use),
1350 Some(kind) => path_ctx.kind = kind,
1353 path_ctx.has_type_args = segment.generic_arg_list().is_some();
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());
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());
1371 let is_infer_qualifier = path.qualifier().is_none()
1373 path.segment().and_then(|it| it.kind()),
1374 Some(ast::PathSegmentKind::Type {
1375 type_ref: Some(ast::Type::InferType(_)),
1388 } else if let Some(segment) = path.segment() {
1389 if segment.coloncolon_token().is_some() {
1390 path_ctx.is_absolute_path = true;
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()) {
1402 } else if ast::ExprStmt::can_cast(parent.kind()) {
1409 PathKind::Item { .. } => {
1410 parent.ancestors().find(|it| ast::MacroCall::can_cast(it.kind()))
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)
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);
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
1433 .and_then(|t| syntax::algo::skip_trivia_token(t, Direction::Prev))
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
1447 nameref_ctx.kind = Some(NameRefKind::Path(path_ctx));
1452 fn pattern_context_for(original_file: &SyntaxNode, pat: ast::Pat) -> PatternContext {
1453 let mut is_param = None;
1454 let (refutability, has_type_ascription) =
1458 .skip_while(|it| ast::Pat::can_cast(it.kind()))
1460 .map_or((PatternRefutability::Irrefutable, false), |node| {
1461 let refutability = match_ast! {
1463 ast::LetStmt(let_) => return (PatternRefutability::Irrefutable, let_.ty().is_some()),
1464 ast::Param(param) => {
1465 let has_type_ascription = param.ty().is_some();
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_),
1477 Some((param_list, param, kind))
1479 return (PatternRefutability::Irrefutable, has_type_ascription)
1481 ast::MatchArm(_) => PatternRefutability::Refutable,
1482 ast::LetExpr(_) => PatternRefutability::Refutable,
1483 ast::ForExpr(_) => PatternRefutability::Irrefutable,
1484 _ => PatternRefutability::Irrefutable,
1487 (refutability, false)
1489 let (ref_token, mut_token) = match &pat {
1490 ast::Pat::IdentPat(it) => (it.ref_token(), it.mut_token()),
1495 param_ctx: is_param,
1496 has_type_ascription,
1497 parent_pat: pat.syntax().parent().and_then(ast::Pat::cast),
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)
1509 x.map(|e| e.syntax().text_range()).and_then(|r| find_node_with_range(original_file, r))
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)
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() {
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)
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));
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))
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() {
1551 None => return false,
1555 token.kind() == T![&]
1558 const OP_TRAIT_LANG_NAMES: &[&str] = &[
1593 use expect_test::{expect, Expect};
1594 use hir::HirDisplay;
1596 use crate::tests::{position, TEST_CONFIG};
1598 use super::CompletionContext;
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();
1605 let ty = completion_context
1607 .map(|t| t.display_test(&db).to_string())
1608 .unwrap_or("?".to_owned());
1610 let name = completion_context
1612 .map_or_else(|| "?".to_owned(), |name| name.to_string());
1614 expect.assert_eq(&format!("ty: {}, name: {}", ty, name));
1618 fn expected_type_let_without_leading_char() {
1619 cov_mark::check!(expected_type_let_without_leading_char);
1620 check_expected_type_and_name(
1626 expect![[r#"ty: u32, name: x"#]],
1631 fn expected_type_let_with_leading_char() {
1632 cov_mark::check!(expected_type_let_with_leading_char);
1633 check_expected_type_and_name(
1639 expect![[r#"ty: u32, name: x"#]],
1644 fn expected_type_let_pat() {
1645 check_expected_type_and_name(
1651 expect![[r#"ty: u32, name: ?"#]],
1653 check_expected_type_and_name(
1659 expect![[r#"ty: u32, name: ?"#]],
1664 fn expected_type_fn_param() {
1665 cov_mark::check!(expected_type_fn_param);
1666 check_expected_type_and_name(
1668 fn foo() { bar($0); }
1671 expect![[r#"ty: u32, name: x"#]],
1673 check_expected_type_and_name(
1675 fn foo() { bar(c$0); }
1678 expect![[r#"ty: u32, name: x"#]],
1683 fn expected_type_fn_param_ref() {
1684 cov_mark::check!(expected_type_fn_param_ref);
1685 check_expected_type_and_name(
1687 fn foo() { bar(&$0); }
1690 expect![[r#"ty: u32, name: x"#]],
1692 check_expected_type_and_name(
1694 fn foo() { bar(&mut $0); }
1695 fn bar(x: &mut u32) {}
1697 expect![[r#"ty: u32, name: x"#]],
1699 check_expected_type_and_name(
1701 fn foo() { bar(& c$0); }
1704 expect![[r#"ty: u32, name: x"#]],
1706 check_expected_type_and_name(
1708 fn foo() { bar(&mut c$0); }
1709 fn bar(x: &mut u32) {}
1711 expect![[r#"ty: u32, name: x"#]],
1713 check_expected_type_and_name(
1715 fn foo() { bar(&c$0); }
1718 expect![[r#"ty: u32, name: x"#]],
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(
1727 struct Foo { a: u32 }
1732 expect![[r#"ty: u32, name: a"#]],
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(
1741 struct Foo { a: u32 }
1746 expect![[r#"ty: u32, name: a"#]],
1751 fn expected_type_generic_struct_field() {
1752 check_expected_type_and_name(
1754 struct Foo<T> { a: T }
1755 fn foo() -> Foo<u32> {
1759 expect![[r#"ty: u32, name: a"#]],
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(
1768 struct Foo { a: u32 }
1773 expect![[r#"ty: u32, name: a"#]],
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(
1787 expect![[r#"ty: E, name: ?"#]],
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(
1801 expect![[r#"ty: E, name: ?"#]],
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(
1813 match E::X { E::X => $0 }
1816 expect![[r#"ty: Foo, name: ?"#]],
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(
1828 match E::X { E::X => c$0 }
1831 expect![[r#"ty: Foo, name: ?"#]],
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(
1840 enum Foo { Bar, Baz, Quux }
1847 expect![[r#"ty: Foo, name: ?"#]],
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(
1856 enum Foo { Bar, Baz, Quux }
1863 expect![[r#"ty: Foo, name: ?"#]],
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(
1876 expect![[r#"ty: u32, name: ?"#]],
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(
1889 expect![[r#"ty: u32, name: ?"#]],
1894 fn expected_type_fn_ret_fn_ref_fully_typed() {
1895 check_expected_type_and_name(
1901 expect![[r#"ty: u32, name: ?"#]],
1906 fn expected_type_closure_param_return() {
1907 // FIXME: make this work with `|| $0`
1908 check_expected_type_and_name(
1915 fn bar(f: impl FnOnce() -> u32) {}
1917 expect![[r#"ty: u32, name: ?"#]],
1922 fn expected_type_generic_function() {
1923 check_expected_type_and_name(
1931 expect![[r#"ty: u32, name: t"#]],
1936 fn expected_type_generic_method() {
1937 check_expected_type_and_name(
1945 fn bar(self, t: T) {}
1948 expect![[r#"ty: u32, name: t"#]],
1953 fn expected_type_functional_update() {
1954 cov_mark::check!(expected_type_struct_func_update);
1955 check_expected_type_and_name(
1957 struct Foo { field: u32 }
1964 expect![[r#"ty: Foo, name: ?"#]],
1969 fn expected_type_param_pat() {
1970 check_expected_type_and_name(
1972 struct Foo { field: u32 }
1975 expect![[r#"ty: Foo, name: ?"#]],
1977 check_expected_type_and_name(
1979 struct Foo { field: u32 }
1982 // FIXME make this work, currently fails due to pattern recovery eating the `:`
1983 expect![[r#"ty: ?, name: ?"#]],