1 //! See `CompletionContext` structure.
3 use base_db::SourceDatabaseExt;
4 use hir::{Local, Name, ScopeDef, Semantics, SemanticsScope, Type, TypeInfo};
6 base_db::{FilePosition, SourceDatabase},
7 call_info::ActiveParameter,
11 algo::find_node_at_offset,
12 ast::{self, NameOrNameRef, NameOwner},
13 match_ast, AstNode, NodeOrToken,
14 SyntaxKind::{self, *},
15 SyntaxNode, SyntaxToken, TextRange, TextSize, T,
21 determine_location, determine_prev_sibling, for_is_prev2, inside_impl_trait_block,
22 is_in_loop_body, previous_token, ImmediateLocation, ImmediatePrevSibling,
27 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
28 pub(crate) enum PatternRefutability {
34 pub(super) enum PathKind {
40 pub(crate) struct PathCompletionContext {
41 /// If this is a call with () already there
42 call_kind: Option<CallKind>,
43 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
44 pub(super) is_trivial_path: bool,
45 /// If not a trivial path, the prefix (qualifier).
46 pub(super) qualifier: Option<ast::Path>,
47 /// Whether the qualifier comes from a use tree parent or not
48 pub(super) use_tree_parent: bool,
49 pub(super) kind: Option<PathKind>,
50 /// Whether the path segment has type args or not.
51 pub(super) has_type_args: bool,
52 /// `true` if we are a statement or a last expr in the block.
53 pub(super) can_be_stmt: bool,
54 pub(super) in_loop_body: bool,
57 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
58 pub(crate) enum CallKind {
63 /// `CompletionContext` is created early during completion to figure out, where
64 /// exactly is the cursor, syntax-wise.
66 pub(crate) struct CompletionContext<'a> {
67 pub(super) sema: Semantics<'a, RootDatabase>,
68 pub(super) scope: SemanticsScope<'a>,
69 pub(super) db: &'a RootDatabase,
70 pub(super) config: &'a CompletionConfig,
71 pub(super) position: FilePosition,
72 /// The token before the cursor, in the original file.
73 pub(super) original_token: SyntaxToken,
74 /// The token before the cursor, in the macro-expanded file.
75 pub(super) token: SyntaxToken,
76 pub(super) krate: Option<hir::Crate>,
77 pub(super) expected_name: Option<NameOrNameRef>,
78 pub(super) expected_type: Option<Type>,
80 /// The parent function of the cursor position if it exists.
81 pub(super) function_def: Option<ast::Fn>,
82 /// The parent impl of the cursor position if it exists.
83 pub(super) impl_def: Option<ast::Impl>,
84 pub(super) name_ref_syntax: Option<ast::NameRef>,
86 // potentially set if we are completing a lifetime
87 pub(super) lifetime_syntax: Option<ast::Lifetime>,
88 pub(super) lifetime_param_syntax: Option<ast::LifetimeParam>,
89 pub(super) lifetime_allowed: bool,
90 pub(super) is_label_ref: bool,
92 // potentially set if we are completing a name
93 pub(super) is_pat_or_const: Option<PatternRefutability>,
94 pub(super) is_param: bool,
96 pub(super) completion_location: Option<ImmediateLocation>,
97 pub(super) prev_sibling: Option<ImmediatePrevSibling>,
98 pub(super) attribute_under_caret: Option<ast::Attr>,
99 pub(super) previous_token: Option<SyntaxToken>,
101 pub(super) path_context: Option<PathCompletionContext>,
102 pub(super) active_parameter: Option<ActiveParameter>,
103 pub(super) locals: Vec<(String, Local)>,
105 pub(super) incomplete_let: bool,
107 no_completion_required: bool,
110 impl<'a> CompletionContext<'a> {
112 db: &'a RootDatabase,
113 position: FilePosition,
114 config: &'a CompletionConfig,
115 ) -> Option<CompletionContext<'a>> {
116 let sema = Semantics::new(db);
118 let original_file = sema.parse(position.file_id);
120 // Insert a fake ident to get a valid parse tree. We will use this file
121 // to determine context, though the original_file will be used for
122 // actual completion.
123 let file_with_fake_ident = {
124 let parse = db.parse(position.file_id);
125 let edit = Indel::insert(position.offset, "intellijRulezz".to_string());
126 parse.reparse(&edit).tree()
128 let fake_ident_token =
129 file_with_fake_ident.syntax().token_at_offset(position.offset).right_biased().unwrap();
131 let krate = sema.to_module_def(position.file_id).map(|m| m.krate());
133 original_file.syntax().token_at_offset(position.offset).left_biased()?;
134 let token = sema.descend_into_macros(original_token.clone());
135 let scope = sema.scope_at_offset(&token, position.offset);
136 let mut locals = vec![];
137 scope.process_all_names(&mut |name, scope| {
138 if let ScopeDef::Local(local) = scope {
139 locals.push((name.to_string(), local));
142 let mut ctx = CompletionContext {
155 name_ref_syntax: None,
156 lifetime_syntax: None,
157 lifetime_param_syntax: None,
158 lifetime_allowed: false,
160 is_pat_or_const: None,
162 completion_location: None,
164 attribute_under_caret: None,
165 previous_token: None,
167 active_parameter: ActiveParameter::at(db, position),
169 incomplete_let: false,
170 no_completion_required: false,
173 let mut original_file = original_file.syntax().clone();
174 let mut speculative_file = file_with_fake_ident.syntax().clone();
175 let mut offset = position.offset;
176 let mut fake_ident_token = fake_ident_token;
178 // Are we inside a macro call?
179 while let (Some(actual_macro_call), Some(macro_call_with_fake_ident)) = (
180 find_node_at_offset::<ast::MacroCall>(&original_file, offset),
181 find_node_at_offset::<ast::MacroCall>(&speculative_file, offset),
183 if actual_macro_call.path().as_ref().map(|s| s.syntax().text())
184 != macro_call_with_fake_ident.path().as_ref().map(|s| s.syntax().text())
188 let speculative_args = match macro_call_with_fake_ident.token_tree() {
192 if let (Some(actual_expansion), Some(speculative_expansion)) = (
193 ctx.sema.expand(&actual_macro_call),
194 ctx.sema.speculative_expand(
200 let new_offset = speculative_expansion.1.text_range().start();
201 if new_offset > actual_expansion.text_range().end() {
204 original_file = actual_expansion;
205 speculative_file = speculative_expansion.0;
206 fake_ident_token = speculative_expansion.1;
212 ctx.fill(&original_file, speculative_file, offset);
216 /// Checks whether completions in that particular case don't make much sense.
218 /// - `fn $0` -- we expect function name, it's unlikely that "hint" will be helpful.
219 /// Exception for this case is `impl Trait for Foo`, where we would like to hint trait method names.
220 /// - `for _ i$0` -- obviously, it'll be "in" keyword.
221 pub(crate) fn no_completion_required(&self) -> bool {
222 self.no_completion_required
225 /// The range of the identifier that is being completed.
226 pub(crate) fn source_range(&self) -> TextRange {
227 // check kind of macro-expanded token, but use range of original token
228 let kind = self.token.kind();
229 if kind == IDENT || kind == LIFETIME_IDENT || kind == UNDERSCORE || kind.is_keyword() {
230 cov_mark::hit!(completes_if_prefix_is_keyword);
231 self.original_token.text_range()
232 } else if kind == CHAR {
233 // assume we are completing a lifetime but the user has only typed the '
234 cov_mark::hit!(completes_if_lifetime_without_idents);
235 TextRange::at(self.original_token.text_range().start(), TextSize::from(1))
237 TextRange::empty(self.position.offset)
241 pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
242 self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
245 pub(crate) fn expects_assoc_item(&self) -> bool {
246 matches!(self.completion_location, Some(ImmediateLocation::Trait | ImmediateLocation::Impl))
249 pub(crate) fn has_dot_receiver(&self) -> bool {
251 &self.completion_location,
252 Some(ImmediateLocation::FieldAccess { receiver, .. } | ImmediateLocation::MethodCall { receiver,.. })
253 if receiver.is_some()
257 pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
258 match &self.completion_location {
260 ImmediateLocation::MethodCall { receiver, .. }
261 | ImmediateLocation::FieldAccess { receiver, .. },
262 ) => receiver.as_ref(),
267 pub(crate) fn expects_non_trait_assoc_item(&self) -> bool {
268 matches!(self.completion_location, Some(ImmediateLocation::Impl))
271 pub(crate) fn expects_item(&self) -> bool {
272 matches!(self.completion_location, Some(ImmediateLocation::ItemList))
275 pub(crate) fn expects_generic_arg(&self) -> bool {
276 matches!(self.completion_location, Some(ImmediateLocation::GenericArgList(_)))
279 pub(crate) fn has_block_expr_parent(&self) -> bool {
280 matches!(self.completion_location, Some(ImmediateLocation::BlockExpr))
283 pub(crate) fn expects_ident_pat_or_ref_expr(&self) -> bool {
285 self.completion_location,
286 Some(ImmediateLocation::IdentPat | ImmediateLocation::RefExpr)
290 pub(crate) fn expect_field(&self) -> bool {
292 self.completion_location,
293 Some(ImmediateLocation::RecordField | ImmediateLocation::TupleField)
297 pub(crate) fn in_use_tree(&self) -> bool {
299 self.completion_location,
300 Some(ImmediateLocation::Use | ImmediateLocation::UseTree)
304 pub(crate) fn has_impl_or_trait_prev_sibling(&self) -> bool {
307 Some(ImmediatePrevSibling::ImplDefType | ImmediatePrevSibling::TraitDefName)
311 pub(crate) fn has_impl_prev_sibling(&self) -> bool {
312 matches!(self.prev_sibling, Some(ImmediatePrevSibling::ImplDefType))
315 pub(crate) fn has_visibility_prev_sibling(&self) -> bool {
316 matches!(self.prev_sibling, Some(ImmediatePrevSibling::Visibility))
319 pub(crate) fn after_if(&self) -> bool {
320 matches!(self.prev_sibling, Some(ImmediatePrevSibling::IfExpr))
323 pub(crate) fn is_path_disallowed(&self) -> bool {
324 self.attribute_under_caret.is_some()
325 || self.previous_token_is(T![unsafe])
328 Some(ImmediatePrevSibling::Attribute | ImmediatePrevSibling::Visibility)
331 self.completion_location,
333 ImmediateLocation::Attribute(_)
334 | ImmediateLocation::ModDeclaration(_)
335 | ImmediateLocation::RecordPat(_)
336 | ImmediateLocation::RecordExpr(_)
341 pub(crate) fn expects_expression(&self) -> bool {
342 matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Expr), .. }))
345 pub(crate) fn expects_type(&self) -> bool {
346 matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Type), .. }))
349 pub(crate) fn path_call_kind(&self) -> Option<CallKind> {
350 self.path_context.as_ref().and_then(|it| it.call_kind)
353 pub(crate) fn is_trivial_path(&self) -> bool {
354 matches!(self.path_context, Some(PathCompletionContext { is_trivial_path: true, .. }))
357 pub(crate) fn is_non_trivial_path(&self) -> bool {
358 matches!(self.path_context, Some(PathCompletionContext { is_trivial_path: false, .. }))
361 pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
362 self.path_context.as_ref().and_then(|it| it.qualifier.as_ref())
365 /// Checks if an item is visible and not `doc(hidden)` at the completion site.
366 pub(crate) fn is_visible<I>(&self, item: &I) -> bool
368 I: hir::HasVisibility + hir::HasAttrs + hir::HasCrate + Copy,
370 self.is_visible_impl(&item.visibility(self.db), &item.attrs(self.db), item.krate(self.db))
373 pub(crate) fn is_scope_def_hidden(&self, scope_def: &ScopeDef) -> bool {
374 if let (Some(attrs), Some(krate)) = (scope_def.attrs(self.db), scope_def.krate(self.db)) {
375 return self.is_doc_hidden(&attrs, krate);
381 pub(crate) fn is_item_hidden(&self, item: &hir::ItemInNs) -> bool {
382 let attrs = item.attrs(self.db);
383 let krate = item.krate(self.db);
384 match (attrs, krate) {
385 (Some(attrs), Some(krate)) => self.is_doc_hidden(&attrs, krate),
390 /// A version of [`SemanticsScope::process_all_names`] that filters out `#[doc(hidden)]` items.
391 pub(crate) fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
392 self.scope.process_all_names(&mut |name, def| {
393 if self.is_scope_def_hidden(&def) {
403 vis: &hir::Visibility,
405 defining_crate: hir::Crate,
407 let module = match self.scope.module() {
409 None => return false,
411 if !vis.is_visible_from(self.db, module.into()) {
412 // If the definition location is editable, also show private items
413 let root_file = defining_crate.root_file(self.db);
414 let source_root_id = self.db.file_source_root(root_file);
415 let is_editable = !self.db.source_root(source_root_id).is_library;
419 !self.is_doc_hidden(attrs, defining_crate)
422 fn is_doc_hidden(&self, attrs: &hir::Attrs, defining_crate: hir::Crate) -> bool {
423 let module = match self.scope.module() {
427 if module.krate() != defining_crate && attrs.has_doc_hidden() {
428 // `doc(hidden)` items are only completed within the defining crate.
435 fn fill_impl_def(&mut self) {
438 .token_ancestors_with_macros(self.token.clone())
439 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
440 .find_map(ast::Impl::cast);
443 fn expected_type_and_name(&self) -> (Option<Type>, Option<NameOrNameRef>) {
444 let mut node = match self.token.parent() {
446 None => return (None, None),
451 ast::LetStmt(it) => {
452 cov_mark::hit!(expected_type_let_with_leading_char);
453 cov_mark::hit!(expected_type_let_without_leading_char);
455 .and_then(|pat| self.sema.type_of_pat(&pat))
456 .or_else(|| it.initializer().and_then(|it| self.sema.type_of_expr(&it)))
457 .map(TypeInfo::original);
458 let name = if let Some(ast::Pat::IdentPat(ident)) = it.pat() {
459 ident.name().map(NameOrNameRef::Name)
466 ast::ArgList(_it) => {
467 cov_mark::hit!(expected_type_fn_param);
468 ActiveParameter::at_token(
472 let name = ap.ident().map(NameOrNameRef::Name);
473 let ty = if has_ref(&self.token) {
474 cov_mark::hit!(expected_type_fn_param_ref);
481 .unwrap_or((None, None))
483 ast::RecordExprFieldList(_it) => {
484 cov_mark::hit!(expected_type_struct_field_without_leading_char);
485 // wouldn't try {} be nice...
487 let expr_field = self.token.prev_sibling_or_token()?
489 .and_then(ast::RecordExprField::cast)?;
490 let (_, _, ty) = self.sema.resolve_record_field(&expr_field)?;
493 expr_field.field_name().map(NameOrNameRef::NameRef),
495 })().unwrap_or((None, None))
497 ast::RecordExprField(it) => {
498 cov_mark::hit!(expected_type_struct_field_with_leading_char);
500 it.expr().as_ref().and_then(|e| self.sema.type_of_expr(e)).map(TypeInfo::original),
501 it.field_name().map(NameOrNameRef::NameRef),
504 ast::MatchExpr(it) => {
505 cov_mark::hit!(expected_type_match_arm_without_leading_char);
506 let ty = it.expr().and_then(|e| self.sema.type_of_expr(&e)).map(TypeInfo::original);
510 cov_mark::hit!(expected_type_if_let_without_leading_char);
511 let ty = it.condition()
512 .and_then(|cond| cond.expr())
513 .and_then(|e| self.sema.type_of_expr(&e))
514 .map(TypeInfo::original);
517 ast::IdentPat(it) => {
518 cov_mark::hit!(expected_type_if_let_with_leading_char);
519 cov_mark::hit!(expected_type_match_arm_with_leading_char);
520 let ty = self.sema.type_of_pat(&ast::Pat::from(it)).map(TypeInfo::original);
524 cov_mark::hit!(expected_type_fn_ret_with_leading_char);
525 cov_mark::hit!(expected_type_fn_ret_without_leading_char);
526 let def = self.sema.to_def(&it);
527 (def.map(|def| def.ret_type(self.db)), None)
529 ast::ClosureExpr(it) => {
530 let ty = self.sema.type_of_expr(&it.into());
531 ty.and_then(|ty| ty.original.as_callable(self.db))
532 .map(|c| (Some(c.return_type()), None))
533 .unwrap_or((None, None))
535 ast::Stmt(_it) => (None, None),
537 match node.parent() {
542 None => (None, None),
552 original_file: &SyntaxNode,
553 file_with_fake_ident: SyntaxNode,
556 let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap();
557 let syntax_element = NodeOrToken::Token(fake_ident_token);
558 self.previous_token = previous_token(syntax_element.clone());
559 self.attribute_under_caret = syntax_element.ancestors().find_map(ast::Attr::cast);
560 self.no_completion_required = {
561 let inside_impl_trait_block = inside_impl_trait_block(syntax_element.clone());
562 let fn_is_prev = self.previous_token_is(T![fn]);
563 let for_is_prev2 = for_is_prev2(syntax_element.clone());
564 (fn_is_prev && !inside_impl_trait_block) || for_is_prev2
567 self.incomplete_let =
568 syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| {
569 it.syntax().text_range().end() == syntax_element.text_range().end()
572 let (expected_type, expected_name) = self.expected_type_and_name();
573 self.expected_type = expected_type;
574 self.expected_name = expected_name;
576 let name_like = match find_node_at_offset(&file_with_fake_ident, offset) {
580 self.completion_location =
581 determine_location(&self.sema, original_file, offset, &name_like);
582 self.prev_sibling = determine_prev_sibling(&name_like);
584 ast::NameLike::Lifetime(lifetime) => {
585 self.classify_lifetime(original_file, lifetime, offset);
587 ast::NameLike::NameRef(name_ref) => {
588 self.classify_name_ref(original_file, name_ref);
590 ast::NameLike::Name(name) => {
591 self.classify_name(name);
596 fn classify_lifetime(
598 original_file: &SyntaxNode,
599 lifetime: ast::Lifetime,
602 self.lifetime_syntax =
603 find_node_at_offset(original_file, lifetime.syntax().text_range().start());
604 if let Some(parent) = lifetime.syntax().parent() {
605 if parent.kind() == ERROR {
611 ast::LifetimeParam(_it) => {
612 self.lifetime_allowed = true;
613 self.lifetime_param_syntax =
614 self.sema.find_node_at_offset_with_macros(original_file, offset);
616 ast::BreakExpr(_it) => self.is_label_ref = true,
617 ast::ContinueExpr(_it) => self.is_label_ref = true,
618 ast::Label(_it) => (),
619 _ => self.lifetime_allowed = true,
625 fn classify_name(&mut self, name: ast::Name) {
626 if let Some(bind_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) {
627 self.is_pat_or_const = Some(PatternRefutability::Refutable);
628 if !bind_pat.is_simple_ident() {
629 self.is_pat_or_const = None;
631 let irrefutable_pat = bind_pat.syntax().ancestors().find_map(|node| {
634 ast::LetStmt(it) => Some(it.pat()),
635 ast::Param(it) => Some(it.pat()),
640 if let Some(Some(pat)) = irrefutable_pat {
641 // This check is here since we could be inside a pattern in the initializer expression of the let statement.
642 if pat.syntax().text_range().contains_range(bind_pat.syntax().text_range()) {
643 self.is_pat_or_const = Some(PatternRefutability::Irrefutable);
647 let is_name_in_field_pat = bind_pat
650 .and_then(ast::RecordPatField::cast)
651 .map_or(false, |pat_field| pat_field.name_ref().is_none());
652 if is_name_in_field_pat {
653 self.is_pat_or_const = None;
657 self.fill_impl_def();
660 self.is_param |= is_node::<ast::Param>(name.syntax());
663 fn classify_name_ref(&mut self, original_file: &SyntaxNode, name_ref: ast::NameRef) {
664 self.fill_impl_def();
666 self.name_ref_syntax =
667 find_node_at_offset(original_file, name_ref.syntax().text_range().start());
669 self.function_def = self
671 .token_ancestors_with_macros(self.token.clone())
672 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
673 .find_map(ast::Fn::cast);
675 let parent = match name_ref.syntax().parent() {
680 if let Some(segment) = ast::PathSegment::cast(parent) {
681 let path_ctx = self.path_context.get_or_insert(PathCompletionContext {
683 is_trivial_path: false,
685 has_type_args: false,
688 use_tree_parent: false,
691 path_ctx.in_loop_body = is_in_loop_body(name_ref.syntax());
692 let path = segment.parent_path();
694 if let Some(p) = path.syntax().parent() {
695 path_ctx.call_kind = match_ast! {
697 ast::PathExpr(it) => it.syntax().parent().and_then(ast::CallExpr::cast).map(|_| CallKind::Expr),
698 ast::MacroCall(it) => it.excl_token().and(Some(CallKind::Mac)),
699 ast::TupleStructPat(_it) => Some(CallKind::Pat),
705 if let Some(parent) = path.syntax().parent() {
706 path_ctx.kind = match_ast! {
708 ast::PathType(_it) => Some(PathKind::Type),
709 ast::PathExpr(_it) => Some(PathKind::Expr),
714 path_ctx.has_type_args = segment.generic_arg_list().is_some();
716 if let Some((path, use_tree_parent)) = path_or_use_tree_qualifier(&path) {
717 path_ctx.use_tree_parent = use_tree_parent;
718 path_ctx.qualifier = path
721 find_node_with_range::<ast::PathSegment>(
723 it.syntax().text_range(),
726 .map(|it| it.parent_path());
730 if let Some(segment) = path.segment() {
731 if segment.coloncolon_token().is_some() {
736 path_ctx.is_trivial_path = true;
738 // Find either enclosing expr statement (thing with `;`) or a
739 // block. If block, check that we are the last expr.
740 path_ctx.can_be_stmt = name_ref
744 if let Some(stmt) = ast::ExprStmt::cast(node.clone()) {
745 return Some(stmt.syntax().text_range() == name_ref.syntax().text_range());
747 if let Some(block) = ast::BlockExpr::cast(node) {
749 block.tail_expr().map(|e| e.syntax().text_range())
750 == Some(name_ref.syntax().text_range()),
760 fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> {
761 syntax.covering_element(range).ancestors().find_map(N::cast)
764 fn is_node<N: AstNode>(node: &SyntaxNode) -> bool {
765 match node.ancestors().find_map(N::cast) {
767 Some(n) => n.syntax().text_range() == node.text_range(),
771 fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<(ast::Path, bool)> {
772 if let Some(qual) = path.qualifier() {
773 return Some((qual, false));
775 let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
776 let use_tree = use_tree_list.syntax().parent().and_then(ast::UseTree::cast)?;
777 use_tree.path().zip(Some(true))
780 fn has_ref(token: &SyntaxToken) -> bool {
781 let mut token = token.clone();
782 for skip in [WHITESPACE, IDENT, T![mut]] {
783 if token.kind() == skip {
784 token = match token.prev_token() {
786 None => return false,
790 token.kind() == T![&]
795 use expect_test::{expect, Expect};
798 use crate::tests::{position, TEST_CONFIG};
800 use super::CompletionContext;
802 fn check_expected_type_and_name(ra_fixture: &str, expect: Expect) {
803 let (db, pos) = position(ra_fixture);
804 let completion_context = CompletionContext::new(&db, pos, &TEST_CONFIG).unwrap();
806 let ty = completion_context
808 .map(|t| t.display_test(&db).to_string())
809 .unwrap_or("?".to_owned());
811 let name = completion_context
813 .map_or_else(|| "?".to_owned(), |name| name.to_string());
815 expect.assert_eq(&format!("ty: {}, name: {}", ty, name));
819 fn expected_type_let_without_leading_char() {
820 cov_mark::check!(expected_type_let_without_leading_char);
821 check_expected_type_and_name(
827 expect![[r#"ty: u32, name: x"#]],
832 fn expected_type_let_with_leading_char() {
833 cov_mark::check!(expected_type_let_with_leading_char);
834 check_expected_type_and_name(
840 expect![[r#"ty: u32, name: x"#]],
845 fn expected_type_let_pat() {
846 check_expected_type_and_name(
852 expect![[r#"ty: u32, name: ?"#]],
854 check_expected_type_and_name(
860 expect![[r#"ty: u32, name: ?"#]],
865 fn expected_type_fn_param() {
866 cov_mark::check!(expected_type_fn_param);
867 check_expected_type_and_name(
869 fn foo() { bar($0); }
872 expect![[r#"ty: u32, name: x"#]],
874 check_expected_type_and_name(
876 fn foo() { bar(c$0); }
879 expect![[r#"ty: u32, name: x"#]],
884 fn expected_type_fn_param_ref() {
885 cov_mark::check!(expected_type_fn_param_ref);
886 check_expected_type_and_name(
888 fn foo() { bar(&$0); }
891 expect![[r#"ty: u32, name: x"#]],
893 check_expected_type_and_name(
895 fn foo() { bar(&mut $0); }
896 fn bar(x: &mut u32) {}
898 expect![[r#"ty: u32, name: x"#]],
900 check_expected_type_and_name(
902 fn foo() { bar(&c$0); }
905 expect![[r#"ty: u32, name: x"#]],
910 fn expected_type_struct_field_without_leading_char() {
911 cov_mark::check!(expected_type_struct_field_without_leading_char);
912 check_expected_type_and_name(
914 struct Foo { a: u32 }
919 expect![[r#"ty: u32, name: a"#]],
924 fn expected_type_generic_struct_field() {
925 check_expected_type_and_name(
927 struct Foo<T> { a: T }
928 fn foo() -> Foo<u32> {
932 expect![[r#"ty: u32, name: a"#]],
937 fn expected_type_struct_field_with_leading_char() {
938 cov_mark::check!(expected_type_struct_field_with_leading_char);
939 check_expected_type_and_name(
941 struct Foo { a: u32 }
946 expect![[r#"ty: u32, name: a"#]],
951 fn expected_type_match_arm_without_leading_char() {
952 cov_mark::check!(expected_type_match_arm_without_leading_char);
953 check_expected_type_and_name(
960 expect![[r#"ty: E, name: ?"#]],
965 fn expected_type_match_arm_with_leading_char() {
966 cov_mark::check!(expected_type_match_arm_with_leading_char);
967 check_expected_type_and_name(
974 expect![[r#"ty: E, name: ?"#]],
979 fn expected_type_if_let_without_leading_char() {
980 cov_mark::check!(expected_type_if_let_without_leading_char);
981 check_expected_type_and_name(
983 enum Foo { Bar, Baz, Quux }
990 expect![[r#"ty: Foo, name: ?"#]],
995 fn expected_type_if_let_with_leading_char() {
996 cov_mark::check!(expected_type_if_let_with_leading_char);
997 check_expected_type_and_name(
999 enum Foo { Bar, Baz, Quux }
1006 expect![[r#"ty: Foo, name: ?"#]],
1011 fn expected_type_fn_ret_without_leading_char() {
1012 cov_mark::check!(expected_type_fn_ret_without_leading_char);
1013 check_expected_type_and_name(
1019 expect![[r#"ty: u32, name: ?"#]],
1024 fn expected_type_fn_ret_with_leading_char() {
1025 cov_mark::check!(expected_type_fn_ret_with_leading_char);
1026 check_expected_type_and_name(
1032 expect![[r#"ty: u32, name: ?"#]],
1037 fn expected_type_fn_ret_fn_ref_fully_typed() {
1038 check_expected_type_and_name(
1044 expect![[r#"ty: u32, name: ?"#]],
1049 fn expected_type_closure_param_return() {
1050 // FIXME: make this work with `|| $0`
1051 check_expected_type_and_name(
1058 fn bar(f: impl FnOnce() -> u32) {}
1060 expect![[r#"ty: u32, name: ?"#]],
1065 fn expected_type_generic_function() {
1066 check_expected_type_and_name(
1074 expect![[r#"ty: u32, name: t"#]],
1079 fn expected_type_generic_method() {
1080 check_expected_type_and_name(
1088 fn bar(self, t: T) {}
1091 expect![[r#"ty: u32, name: t"#]],