Skip to main content

ide_completion/context/
analysis.rs

1//! Module responsible for analyzing the code surrounding the cursor for completion.
2use std::iter;
3
4use hir::{EnumVariant, ExpandResult, InFile, Semantics, Type, TypeInfo};
5use ide_db::{
6    RootDatabase, active_parameter::ActiveParameter, syntax_helpers::node_ext::find_loops,
7};
8use itertools::{Either, Itertools};
9use stdx::always;
10use syntax::{
11    AstNode, AstToken, Direction, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken,
12    T, TextRange, TextSize,
13    algo::{
14        self, ancestors_at_offset, find_node_at_offset, non_trivia_sibling,
15        previous_non_trivia_token,
16    },
17    ast::{
18        self, AttrKind, HasArgList, HasGenericArgs, HasGenericParams, HasLoopBody, HasName,
19        NameOrNameRef,
20    },
21    match_ast,
22};
23
24use crate::{
25    completions::postfix::{is_in_condition, is_in_value},
26    context::{
27        AttrCtx, BreakableKind, COMPLETION_MARKER, CompletionAnalysis, DotAccess, DotAccessExprCtx,
28        DotAccessKind, ItemListKind, LifetimeContext, LifetimeKind, NameContext, NameKind,
29        NameRefContext, NameRefKind, ParamContext, ParamKind, PathCompletionCtx, PathExprCtx,
30        PathKind, PatternContext, PatternRefutability, Qualified, QualifierCtx,
31        TypeAscriptionTarget, TypeLocation,
32    },
33};
34
35#[derive(Debug)]
36struct ExpansionResult {
37    original_file: SyntaxNode,
38    speculative_file: SyntaxNode,
39    /// The offset in the original file.
40    original_offset: TextSize,
41    /// The offset in the speculatively expanded file.
42    speculative_offset: TextSize,
43    fake_ident_token: SyntaxToken,
44    derive_ctx: Option<(SyntaxNode, SyntaxNode, TextSize, ast::Attr)>,
45}
46
47pub(super) struct AnalysisResult<'db> {
48    pub(super) analysis: CompletionAnalysis<'db>,
49    pub(super) expected: (Option<Type<'db>>, Option<ast::NameOrNameRef>),
50    pub(super) qualifier_ctx: QualifierCtx,
51    /// the original token of the expanded file
52    pub(super) token: SyntaxToken,
53    /// The offset in the original file.
54    pub(super) original_offset: TextSize,
55}
56
57pub(super) fn expand_and_analyze<'db>(
58    sema: &Semantics<'db, RootDatabase>,
59    original_file: InFile<SyntaxNode>,
60    speculative_file: SyntaxNode,
61    offset: TextSize,
62    original_token: &SyntaxToken,
63) -> Option<AnalysisResult<'db>> {
64    // as we insert after the offset, right biased will *always* pick the identifier no matter
65    // if there is an ident already typed or not
66    let fake_ident_token = speculative_file.token_at_offset(offset).right_biased()?;
67    // the relative offset between the cursor and the *identifier* token we are completing on
68    let relative_offset = offset - fake_ident_token.text_range().start();
69    // make the offset point to the start of the original token, as that is what the
70    // intermediate offsets calculated in expansion always points to
71    let offset = offset - relative_offset;
72    let expansion = expand_maybe_stop(
73        sema,
74        original_file.clone(),
75        speculative_file.clone(),
76        offset,
77        fake_ident_token.clone(),
78        relative_offset,
79    )
80    .unwrap_or(ExpansionResult {
81        original_file: original_file.value,
82        speculative_file,
83        original_offset: offset,
84        speculative_offset: fake_ident_token.text_range().start(),
85        fake_ident_token,
86        derive_ctx: None,
87    });
88
89    // add the relative offset back, so that left_biased finds the proper token
90    let original_offset = expansion.original_offset + relative_offset;
91    let token = expansion.original_file.token_at_offset(original_offset).left_biased()?;
92
93    analyze(sema, expansion, original_token, &token).map(|(analysis, expected, qualifier_ctx)| {
94        AnalysisResult { analysis, expected, qualifier_ctx, token, original_offset }
95    })
96}
97
98fn token_at_offset_ignore_whitespace(file: &SyntaxNode, offset: TextSize) -> Option<SyntaxToken> {
99    let token = file.token_at_offset(offset).left_biased()?;
100    algo::skip_whitespace_token(token, Direction::Prev)
101}
102
103/// Expand attributes and macro calls at the current cursor position for both the original file
104/// and fake file repeatedly. As soon as one of the two expansions fail we stop so the original
105/// and speculative states stay in sync.
106///
107/// We do this by recursively expanding all macros and picking the best possible match. We cannot just
108/// choose the first expansion each time because macros can expand to something that does not include
109/// our completion marker, e.g.:
110///
111/// ```ignore
112/// macro_rules! helper { ($v:ident) => {} }
113/// macro_rules! my_macro {
114///     ($v:ident) => {
115///         helper!($v);
116///         $v
117///     };
118/// }
119///
120/// my_macro!(complete_me_here);
121/// ```
122/// If we would expand the first thing we encounter only (which in fact this method used to do), we would
123/// be unable to complete here, because we would be walking directly into the void. So we instead try
124/// *every* possible path.
125///
126/// This can also creates discrepancies between the speculative and real expansions: because we insert
127/// tokens, we insert characters, which means if we try the second occurrence it may not be at the same
128/// position in the original and speculative file. We take an educated guess here, and for each token
129/// that we check, we subtract `COMPLETION_MARKER.len()`. This may not be accurate because proc macros
130/// can insert the text of the completion marker in other places while removing the span, but this is
131/// the best we can do.
132fn expand_maybe_stop(
133    sema: &Semantics<'_, RootDatabase>,
134    original_file: InFile<SyntaxNode>,
135    speculative_file: SyntaxNode,
136    original_offset: TextSize,
137    fake_ident_token: SyntaxToken,
138    relative_offset: TextSize,
139) -> Option<ExpansionResult> {
140    if let result @ Some(_) = expand(
141        sema,
142        original_file.clone(),
143        speculative_file.clone(),
144        original_offset,
145        fake_ident_token.clone(),
146        relative_offset,
147    ) {
148        return result;
149    }
150
151    // We can't check whether the fake expansion is inside macro call, because that requires semantic info.
152    // But hopefully checking just the real one should be enough.
153    if token_at_offset_ignore_whitespace(&original_file.value, original_offset + relative_offset)
154        .is_some_and(|original_token| {
155            !sema.is_inside_macro_call(original_file.with_value(&original_token))
156        })
157    {
158        // Recursion base case.
159        Some(ExpansionResult {
160            original_file: original_file.value,
161            speculative_file,
162            original_offset,
163            speculative_offset: fake_ident_token.text_range().start(),
164            fake_ident_token,
165            derive_ctx: None,
166        })
167    } else {
168        None
169    }
170}
171
172fn expand(
173    sema: &Semantics<'_, RootDatabase>,
174    original_file: InFile<SyntaxNode>,
175    speculative_file: SyntaxNode,
176    original_offset: TextSize,
177    fake_ident_token: SyntaxToken,
178    relative_offset: TextSize,
179) -> Option<ExpansionResult> {
180    let _p = tracing::info_span!("CompletionContext::expand").entered();
181
182    let parent_item =
183        |item: &ast::Item| item.syntax().ancestors().skip(1).find_map(ast::Item::cast);
184    let original_node = token_at_offset_ignore_whitespace(&original_file.value, original_offset)
185        .and_then(|token| token.parent_ancestors().find_map(ast::Item::cast));
186    let ancestor_items = iter::successors(
187        Option::zip(
188            original_node,
189            find_node_at_offset::<ast::Item>(
190                &speculative_file,
191                fake_ident_token.text_range().start(),
192            ),
193        ),
194        |(a, b)| parent_item(a).zip(parent_item(b)),
195    );
196
197    // first try to expand attributes as these are always the outermost macro calls
198    'ancestors: for (actual_item, item_with_fake_ident) in ancestor_items {
199        match (
200            sema.expand_attr_macro(&actual_item),
201            sema.speculative_expand_attr_macro(
202                &actual_item,
203                &item_with_fake_ident,
204                fake_ident_token.clone(),
205            ),
206        ) {
207            // maybe parent items have attributes, so continue walking the ancestors
208            (None, None) => continue 'ancestors,
209            // successful expansions
210            (
211                Some(ExpandResult { value: actual_expansion, err: _ }),
212                Some((fake_expansion, fake_mapped_tokens)),
213            ) => {
214                let mut accumulated_offset_from_fake_tokens = 0;
215                let actual_range = actual_expansion.text_range().end();
216                let result = fake_mapped_tokens
217                    .into_iter()
218                    .filter_map(|(fake_mapped_token, rank)| {
219                        let accumulated_offset = accumulated_offset_from_fake_tokens;
220                        if !fake_mapped_token.text().contains(COMPLETION_MARKER) {
221                            // Proc macros can make the same span with different text, we don't
222                            // want them to participate in completion because the macro author probably
223                            // didn't intend them to.
224                            return None;
225                        }
226                        accumulated_offset_from_fake_tokens += COMPLETION_MARKER.len();
227
228                        let new_offset = fake_mapped_token.text_range().start()
229                            - TextSize::new(accumulated_offset as u32);
230                        if new_offset + relative_offset > actual_range {
231                            // offset outside of bounds from the original expansion,
232                            // stop here to prevent problems from happening
233                            return None;
234                        }
235                        let result = expand_maybe_stop(
236                            sema,
237                            actual_expansion.clone(),
238                            fake_expansion.clone(),
239                            new_offset,
240                            fake_mapped_token,
241                            relative_offset,
242                        )?;
243                        Some((result, rank))
244                    })
245                    .min_by_key(|(_, rank)| *rank)
246                    .map(|(result, _)| result);
247                if result.is_some() {
248                    return result;
249                }
250            }
251            // exactly one expansion failed, inconsistent state so stop expanding completely
252            _ => break 'ancestors,
253        }
254    }
255
256    // No attributes have been expanded, so look for macro_call! token trees or derive token trees
257    let orig_tt = ancestors_at_offset(&original_file.value, original_offset)
258        .map_while(Either::<ast::TokenTree, ast::Meta>::cast)
259        .last()?;
260    let spec_tt = ancestors_at_offset(&speculative_file, fake_ident_token.text_range().start())
261        .map_while(Either::<ast::TokenTree, ast::Meta>::cast)
262        .last()?;
263
264    let (tts, attrs) = match (orig_tt, spec_tt) {
265        (Either::Left(orig_tt), Either::Left(spec_tt)) => {
266            let attrs = orig_tt
267                .syntax()
268                .parent()
269                .and_then(ast::Meta::cast)
270                .and_then(|it| it.parent_attr())
271                .zip(
272                    spec_tt
273                        .syntax()
274                        .parent()
275                        .and_then(ast::Meta::cast)
276                        .and_then(|it| it.parent_attr()),
277                );
278            (Some((orig_tt, spec_tt)), attrs)
279        }
280        (Either::Right(orig_path), Either::Right(spec_path)) => {
281            (None, orig_path.parent_attr().zip(spec_path.parent_attr()))
282        }
283        _ => return None,
284    };
285
286    // Expand pseudo-derive expansion aka `derive(Debug$0)`
287    if let Some((orig_attr, spec_attr)) = attrs
288        && let Some(orig_meta) = orig_attr.meta()
289    {
290        // FIXME: Support speculative expansion with `cfg_attr`.
291        if let (Some(actual_expansion), Some((fake_expansion, fake_mapped_tokens))) = (
292            sema.expand_derive_as_pseudo_attr_macro(&orig_meta),
293            sema.speculative_expand_derive_as_pseudo_attr_macro(
294                &orig_attr,
295                &spec_attr,
296                fake_ident_token.clone(),
297            ),
298        ) && let Some((fake_mapped_token, _)) =
299            fake_mapped_tokens.into_iter().min_by_key(|(_, rank)| *rank)
300        {
301            return Some(ExpansionResult {
302                original_file: original_file.value,
303                speculative_file,
304                original_offset,
305                speculative_offset: fake_ident_token.text_range().start(),
306                fake_ident_token,
307                derive_ctx: Some((
308                    actual_expansion,
309                    fake_expansion,
310                    fake_mapped_token.text_range().start(),
311                    orig_attr,
312                )),
313            });
314        }
315
316        if let Some(spec_adt) =
317            spec_attr.syntax().ancestors().find_map(ast::Item::cast).and_then(|it| match it {
318                ast::Item::Struct(it) => Some(ast::Adt::Struct(it)),
319                ast::Item::Enum(it) => Some(ast::Adt::Enum(it)),
320                ast::Item::Union(it) => Some(ast::Adt::Union(it)),
321                _ => None,
322            })
323        {
324            // might be the path of derive helper or a token tree inside of one
325            if let Some(helpers) = sema.derive_helper(&orig_attr) {
326                for (_mac, file) in helpers {
327                    if let Some((fake_expansion, fake_mapped_tokens)) = sema.speculative_expand_raw(
328                        file,
329                        spec_adt.syntax(),
330                        fake_ident_token.clone(),
331                    ) {
332                        // we are inside a derive helper token tree, treat this as being inside
333                        // the derive expansion
334                        let actual_expansion = sema.parse_or_expand(file.into());
335                        let mut accumulated_offset_from_fake_tokens = 0;
336                        let actual_range = actual_expansion.text_range().end();
337                        let result = fake_mapped_tokens
338                            .into_iter()
339                            .filter_map(|(fake_mapped_token, rank)| {
340                                let accumulated_offset = accumulated_offset_from_fake_tokens;
341                                if !fake_mapped_token.text().contains(COMPLETION_MARKER) {
342                                    // Proc macros can make the same span with different text, we don't
343                                    // want them to participate in completion because the macro author probably
344                                    // didn't intend them to.
345                                    return None;
346                                }
347                                accumulated_offset_from_fake_tokens += COMPLETION_MARKER.len();
348
349                                let new_offset = fake_mapped_token.text_range().start()
350                                    - TextSize::new(accumulated_offset as u32);
351                                if new_offset + relative_offset > actual_range {
352                                    // offset outside of bounds from the original expansion,
353                                    // stop here to prevent problems from happening
354                                    return None;
355                                }
356                                let result = expand_maybe_stop(
357                                    sema,
358                                    InFile::new(file.into(), actual_expansion.clone()),
359                                    fake_expansion.clone(),
360                                    new_offset,
361                                    fake_mapped_token,
362                                    relative_offset,
363                                )?;
364                                Some((result, rank))
365                            })
366                            .min_by_key(|(_, rank)| *rank)
367                            .map(|(result, _)| result);
368                        if result.is_some() {
369                            return result;
370                        }
371                    }
372                }
373            }
374        }
375        // at this point we won't have any more successful expansions, so stop
376        return None;
377    }
378
379    // Expand fn-like macro calls
380    let (orig_tt, spec_tt) = tts?;
381    let (actual_macro_call, macro_call_with_fake_ident) = (
382        orig_tt.syntax().parent().and_then(ast::MacroCall::cast)?,
383        spec_tt.syntax().parent().and_then(ast::MacroCall::cast)?,
384    );
385    let mac_call_path0 = actual_macro_call.path().as_ref().map(|s| s.syntax().text());
386    let mac_call_path1 = macro_call_with_fake_ident.path().as_ref().map(|s| s.syntax().text());
387
388    // inconsistent state, stop expanding
389    if mac_call_path0 != mac_call_path1 {
390        return None;
391    }
392    let speculative_args = macro_call_with_fake_ident.token_tree()?;
393
394    match (
395        sema.expand_macro_call(&actual_macro_call),
396        sema.speculative_expand_macro_call(&actual_macro_call, &speculative_args, fake_ident_token),
397    ) {
398        // successful expansions
399        (Some(actual_expansion), Some((fake_expansion, fake_mapped_tokens))) => {
400            let mut accumulated_offset_from_fake_tokens = 0;
401            let actual_range = actual_expansion.text_range().end();
402            fake_mapped_tokens
403                .into_iter()
404                .filter_map(|(fake_mapped_token, rank)| {
405                    let accumulated_offset = accumulated_offset_from_fake_tokens;
406                    if !fake_mapped_token.text().contains(COMPLETION_MARKER) {
407                        // Proc macros can make the same span with different text, we don't
408                        // want them to participate in completion because the macro author probably
409                        // didn't intend them to.
410                        return None;
411                    }
412                    accumulated_offset_from_fake_tokens += COMPLETION_MARKER.len();
413
414                    let new_offset = fake_mapped_token.text_range().start()
415                        - TextSize::new(accumulated_offset as u32);
416                    if new_offset + relative_offset > actual_range {
417                        // offset outside of bounds from the original expansion,
418                        // stop here to prevent problems from happening
419                        return None;
420                    }
421                    let result = expand_maybe_stop(
422                        sema,
423                        actual_expansion.clone(),
424                        fake_expansion.clone(),
425                        new_offset,
426                        fake_mapped_token,
427                        relative_offset,
428                    )?;
429                    Some((result, rank))
430                })
431                .min_by_key(|(_, rank)| *rank)
432                .map(|(result, _)| result)
433        }
434        // at least one expansion failed, we won't have anything to expand from this point
435        // onwards so break out
436        _ => None,
437    }
438}
439
440/// Fill the completion context, this is what does semantic reasoning about the surrounding context
441/// of the completion location.
442fn analyze<'db>(
443    sema: &Semantics<'db, RootDatabase>,
444    expansion_result: ExpansionResult,
445    original_token: &SyntaxToken,
446    self_token: &SyntaxToken,
447) -> Option<(CompletionAnalysis<'db>, (Option<Type<'db>>, Option<ast::NameOrNameRef>), QualifierCtx)>
448{
449    let _p = tracing::info_span!("CompletionContext::analyze").entered();
450    let ExpansionResult {
451        original_file,
452        speculative_file,
453        original_offset: _,
454        speculative_offset,
455        fake_ident_token,
456        derive_ctx,
457    } = expansion_result;
458
459    if original_token.kind() != self_token.kind()
460        // FIXME: This check can be removed once we use speculative database forking for completions
461        && !(original_token.kind().is_punct() || original_token.kind().is_trivia())
462        && !(SyntaxKind::is_any_identifier(original_token.kind())
463            && SyntaxKind::is_any_identifier(self_token.kind()))
464    {
465        return None;
466    }
467
468    // Overwrite the path kind for derives
469    if let Some((original_file, file_with_fake_ident, offset, origin_attr)) = derive_ctx
470        && let Some(origin_meta) = origin_attr.meta()
471    {
472        if let Some(ast::NameLike::NameRef(name_ref)) =
473            find_node_at_offset(&file_with_fake_ident, offset)
474        {
475            let parent = name_ref.syntax().parent()?;
476            let (mut nameref_ctx, _) =
477                classify_name_ref(sema, &original_file, name_ref, offset, parent)?;
478            if let NameRefKind::Path(path_ctx) = &mut nameref_ctx.kind {
479                path_ctx.kind = PathKind::Derive {
480                    existing_derives: sema
481                        .resolve_derive_macro(&origin_meta)
482                        .into_iter()
483                        .flatten()
484                        .flatten()
485                        .collect(),
486                };
487            }
488            return Some((
489                CompletionAnalysis::NameRef(nameref_ctx),
490                (None, None),
491                QualifierCtx::default(),
492            ));
493        }
494        return None;
495    }
496
497    let Some(name_like) = find_node_at_offset(&speculative_file, speculative_offset) else {
498        let analysis = if let Some(original) = ast::String::cast(original_token.clone()) {
499            CompletionAnalysis::String { original, expanded: ast::String::cast(self_token.clone()) }
500        } else {
501            // Fix up trailing whitespace problem
502            // #[attr(foo = $0
503            let token = syntax::algo::skip_trivia_token(self_token.clone(), Direction::Prev)?;
504            let p = token.parent()?;
505            if p.kind() == SyntaxKind::TOKEN_TREE
506                && p.ancestors().any(|it| it.kind() == SyntaxKind::TOKEN_TREE_META)
507            {
508                let colon_prefix = previous_non_trivia_token(self_token.clone())
509                    .is_some_and(|it| T![:] == it.kind());
510
511                CompletionAnalysis::UnexpandedAttrTT {
512                    fake_attribute_under_caret: fake_ident_token
513                        .parent_ancestors()
514                        .find_map(ast::TokenTreeMeta::cast),
515                    colon_prefix,
516                    extern_crate: p.ancestors().find_map(ast::ExternCrate::cast),
517                }
518            } else if p.kind() == SyntaxKind::TOKEN_TREE
519                && p.ancestors().any(|it| ast::Macro::can_cast(it.kind()))
520            {
521                if let Some([_ident, colon, _name, dollar]) = fake_ident_token
522                    .siblings_with_tokens(Direction::Prev)
523                    .filter(|it| !it.kind().is_trivia())
524                    .take(4)
525                    .collect_array()
526                    && dollar.kind() == T![$]
527                    && colon.kind() == T![:]
528                {
529                    CompletionAnalysis::MacroSegment
530                } else {
531                    return None;
532                }
533            } else if find_node_at_offset::<ast::CfgPredicate>(
534                &speculative_file,
535                speculative_offset,
536            )
537            .is_some()
538            {
539                CompletionAnalysis::CfgPredicate
540            } else {
541                return None;
542            }
543        };
544        return Some((analysis, (None, None), QualifierCtx::default()));
545    };
546
547    let expected = expected_type_and_name(sema, self_token, &name_like);
548    let mut qual_ctx = QualifierCtx::default();
549    let analysis = match name_like {
550        ast::NameLike::Lifetime(lifetime) => {
551            CompletionAnalysis::Lifetime(classify_lifetime(sema, &original_file, lifetime)?)
552        }
553        ast::NameLike::NameRef(name_ref) => {
554            let parent = name_ref.syntax().parent()?;
555            let (nameref_ctx, qualifier_ctx) = classify_name_ref(
556                sema,
557                &original_file,
558                name_ref,
559                expansion_result.original_offset,
560                parent,
561            )?;
562
563            if let NameRefContext {
564                kind:
565                    NameRefKind::Path(PathCompletionCtx { kind: PathKind::Expr { .. }, path, .. }, ..),
566                ..
567            } = &nameref_ctx
568                && is_in_token_of_for_loop(path)
569            {
570                // for pat $0
571                // there is nothing to complete here except `in` keyword
572                // don't bother populating the context
573                // Ideally this special casing wouldn't be needed, but the parser recovers
574                return None;
575            }
576
577            qual_ctx = qualifier_ctx;
578            CompletionAnalysis::NameRef(nameref_ctx)
579        }
580        ast::NameLike::Name(name) => {
581            let name_ctx = classify_name(sema, &original_file, name)?;
582            CompletionAnalysis::Name(name_ctx)
583        }
584    };
585    Some((analysis, expected, qual_ctx))
586}
587
588/// Calculate the expected type and name of the cursor position.
589fn expected_type_and_name<'db>(
590    sema: &Semantics<'db, RootDatabase>,
591    self_token: &SyntaxToken,
592    name_like: &ast::NameLike,
593) -> (Option<Type<'db>>, Option<NameOrNameRef>) {
594    let token = prev_special_biased_token_at_trivia(self_token.clone());
595    let mut node = match token.parent() {
596        Some(it) => it,
597        None => return (None, None),
598    };
599
600    let strip_refs = |mut ty: Type<'db>| match name_like {
601        ast::NameLike::NameRef(n) => {
602            let p = match n.syntax().parent() {
603                Some(it) => it,
604                None => return ty,
605            };
606            let top_syn = match_ast! {
607                match p {
608                    ast::FieldExpr(e) => e
609                        .syntax()
610                        .ancestors()
611                        .take_while(|it| ast::FieldExpr::can_cast(it.kind()))
612                        .last(),
613                    ast::PathSegment(e) => e
614                        .syntax()
615                        .ancestors()
616                        .skip(1)
617                        .take_while(|it| ast::Path::can_cast(it.kind()) || ast::PathExpr::can_cast(it.kind()))
618                        .find(|it| ast::PathExpr::can_cast(it.kind())),
619                    _ => None
620                }
621            };
622            let top_syn = match top_syn {
623                Some(it) => it,
624                None => return ty,
625            };
626            let refs_level = top_syn
627                .ancestors()
628                .skip(1)
629                .map_while(Either::<ast::RefExpr, ast::PrefixExpr>::cast)
630                .take_while(|it| match it {
631                    Either::Left(_) => true,
632                    Either::Right(prefix) => prefix.op_kind() == Some(ast::UnaryOp::Deref),
633                })
634                .fold(0i32, |level, expr| match expr {
635                    Either::Left(_) => level + 1,
636                    Either::Right(_) => level - 1,
637                });
638            for _ in 0..refs_level {
639                cov_mark::hit!(expected_type_fn_param_ref);
640                ty = ty.strip_reference();
641            }
642            for _ in refs_level..0 {
643                cov_mark::hit!(expected_type_fn_param_deref);
644                ty = ty.add_reference(hir::Mutability::Shared);
645            }
646            ty
647        }
648        _ => ty,
649    };
650
651    let (ty, name) = loop {
652        break match_ast! {
653            match node {
654                ast::LetStmt(it) => {
655                    cov_mark::hit!(expected_type_let_with_leading_char);
656                    cov_mark::hit!(expected_type_let_without_leading_char);
657                    let ty = it.pat()
658                        .and_then(|pat| sema.type_of_pat(&pat))
659                        .or_else(|| it.initializer().and_then(|it| sema.type_of_expr(&it)))
660                        .map(TypeInfo::original)
661                        .filter(|ty| {
662                            // don't infer the let type if the expr is a function,
663                            // preventing parenthesis from vanishing
664                            it.ty().is_some() || !ty.is_fn()
665                        });
666                    let name = match it.pat() {
667                        Some(ast::Pat::IdentPat(ident)) => ident.name().map(NameOrNameRef::Name),
668                        Some(_) | None => None,
669                    };
670
671                    (ty, name)
672                },
673                ast::LetExpr(it) => {
674                    cov_mark::hit!(expected_type_if_let_without_leading_char);
675                    let ty = it.pat()
676                        .and_then(|pat| sema.type_of_pat(&pat))
677                        .or_else(|| it.expr().and_then(|it| sema.type_of_expr(&it)))
678                        .map(TypeInfo::original);
679                    (ty, None)
680                },
681                ast::BinExpr(it) => {
682                    if let Some(ast::BinaryOp::Assignment { op: None }) = it.op_kind() {
683                        let ty = it.lhs()
684                            .and_then(|lhs| sema.type_of_expr(&lhs))
685                            .or_else(|| it.rhs().and_then(|rhs| sema.type_of_expr(&rhs)))
686                            .map(TypeInfo::original);
687                        (ty, None)
688                    } else if let Some(ast::BinaryOp::LogicOp(_)) = it.op_kind() {
689                        let ty = sema.type_of_expr(&it.clone().into()).map(TypeInfo::original);
690                        (ty, None)
691                    } else {
692                        (None, None)
693                    }
694                },
695                ast::ArgList(_) => {
696                    cov_mark::hit!(expected_type_fn_param);
697                    ActiveParameter::at_token(
698                        sema,
699                        token.clone(),
700                    ).map(|ap| {
701                        let name = ap.ident().map(NameOrNameRef::Name);
702                        (Some(ap.ty), name)
703                    })
704                    .unwrap_or((None, None))
705                },
706                ast::RecordExprFieldList(it) => {
707                    // wouldn't try {} be nice...
708                    (|| {
709                        if token.kind() == T![..]
710                            ||token.prev_token().map(|t| t.kind()) == Some(T![..])
711                        {
712                            cov_mark::hit!(expected_type_struct_func_update);
713                            let record_expr = it.syntax().parent().and_then(ast::RecordExpr::cast)?;
714                            let ty = sema.type_of_expr(&record_expr.into())?;
715                            Some((
716                                Some(ty.original),
717                                None
718                            ))
719                        } else {
720                            cov_mark::hit!(expected_type_struct_field_without_leading_char);
721                            cov_mark::hit!(expected_type_struct_field_followed_by_comma);
722                            let expr_field = previous_non_trivia_token(token.clone())?.parent().and_then(ast::RecordExprField::cast)?;
723                            let (_, _, ty) = sema.resolve_record_field(&expr_field)?;
724                            Some((
725                                Some(ty),
726                                expr_field.field_name().map(NameOrNameRef::NameRef),
727                            ))
728                        }
729                    })().unwrap_or((None, None))
730                },
731                ast::RecordExprField(it) => {
732                    let field_ty = sema.resolve_record_field(&it).map(|(_, _, ty)| ty);
733                    let field_name = it.field_name().map(NameOrNameRef::NameRef);
734                    if let Some(expr) = it.expr() {
735                        cov_mark::hit!(expected_type_struct_field_with_leading_char);
736                        let ty = field_ty
737                            .or_else(|| sema.type_of_expr(&expr).map(TypeInfo::original));
738                        (ty, field_name)
739                    } else {
740                        (field_ty, field_name)
741                    }
742                },
743                // match foo { $0 }
744                // match foo { ..., pat => $0 }
745                ast::MatchExpr(it) => {
746                    let on_arrow = previous_non_trivia_token(token.clone()).is_some_and(|it| T![=>] == it.kind());
747
748                    let ty = if on_arrow {
749                        // match foo { ..., pat => $0 }
750                        cov_mark::hit!(expected_type_match_arm_body_without_leading_char);
751                        cov_mark::hit!(expected_type_match_arm_body_with_leading_char);
752                        sema.type_of_expr(&it.into())
753                    } else {
754                        // match foo { $0 }
755                        cov_mark::hit!(expected_type_match_arm_without_leading_char);
756                        it.expr().and_then(|e| sema.type_of_expr(&e))
757                    }.map(TypeInfo::original);
758                    (ty, None)
759                },
760                ast::MatchArm(it) => {
761                    let on_arrow = previous_non_trivia_token(token.clone()).is_some_and(|it| T![=>] == it.kind());
762                    let in_body = it.expr().is_some_and(|it| it.syntax().text_range().contains_range(token.text_range()));
763                    let match_expr = it.parent_match();
764
765                    let ty = if on_arrow || in_body {
766                        // match foo { ..., pat => $0 }
767                        cov_mark::hit!(expected_type_match_arm_body_without_leading_char);
768                        cov_mark::hit!(expected_type_match_arm_body_with_leading_char);
769                        sema.type_of_expr(&match_expr.into())
770                    } else {
771                        // match foo { $0 }
772                        cov_mark::hit!(expected_type_match_arm_without_leading_char);
773                        match_expr.expr().and_then(|e| sema.type_of_expr(&e))
774                    }.map(TypeInfo::original);
775                    (ty, None)
776                },
777                ast::IfExpr(it) => {
778                    let ty = if let Some(body) = it.then_branch()
779                        && token.text_range().end() > body.syntax().text_range().start()
780                    {
781                        sema.type_of_expr(&body.into())
782                    } else {
783                        it.condition().and_then(|e| sema.type_of_expr(&e))
784                    }.map(TypeInfo::original);
785                    (ty, None)
786                },
787                ast::IdentPat(it) => {
788                    cov_mark::hit!(expected_type_if_let_with_leading_char);
789                    cov_mark::hit!(expected_type_match_arm_with_leading_char);
790                    let ty = sema.type_of_pat(&ast::Pat::from(it)).map(TypeInfo::original);
791                    (ty, None)
792                },
793                ast::TupleStructPat(it) => {
794                    let fields = it.path().and_then(|path| match sema.resolve_path(&path)? {
795                        hir::PathResolution::Def(hir::ModuleDef::Adt(adt)) => Some(adt.as_struct()?.fields(sema.db)),
796                        hir::PathResolution::Def(hir::ModuleDef::EnumVariant(variant)) => Some(variant.fields(sema.db)),
797                        _ => None,
798                    });
799                    let nr = it.fields().take_while(|it| it.syntax().text_range().end() <= token.text_range().start()).count();
800                    let ty = fields.and_then(|fields| Some(fields.get(nr)?.ty(sema.db).to_type(sema.db)));
801                    (ty, None)
802                },
803                ast::Fn(it) => {
804                    cov_mark::hit!(expected_type_fn_ret_with_leading_char);
805                    cov_mark::hit!(expected_type_fn_ret_without_leading_char);
806                    let def = sema.to_def(&it);
807                    (def.map(|def| def.ret_type(sema.db)), None)
808                },
809                ast::ReturnExpr(it) => {
810                    let fn_ = sema.ancestors_with_macros(it.syntax().clone())
811                        .find_map(Either::<ast::Fn, ast::ClosureExpr>::cast);
812                    let ty = fn_.and_then(|f| match f {
813                        Either::Left(f) => Some(sema.to_def(&f)?.ret_type(sema.db)),
814                        Either::Right(f) => {
815                            let ty = sema.type_of_expr(&f.into())?.original.as_callable(sema.db)?;
816                            Some(ty.return_type())
817                        },
818                    });
819                    (ty, None)
820                },
821                ast::BreakExpr(it) => {
822                    let ty = it.break_token()
823                        .and_then(|it| find_loops(sema, &it)?.next())
824                        .and_then(|expr| sema.type_of_expr(&expr));
825                    (ty.map(TypeInfo::original), None)
826                },
827                ast::ClosureExpr(it) => {
828                    let ty = sema.type_of_expr(&it.into());
829                    ty.and_then(|ty| ty.original.as_callable(sema.db))
830                        .map(|c| (Some(c.return_type()), None))
831                        .unwrap_or((None, None))
832                },
833                ast::ParamList(it) => {
834                    let closure = it.syntax().parent().and_then(ast::ClosureExpr::cast);
835                    let ty = closure
836                        .filter(|_| it.syntax().text_range().end() <= self_token.text_range().start())
837                        .and_then(|it| sema.type_of_expr(&it.into()));
838                    ty.and_then(|ty| ty.original.as_callable(sema.db))
839                        .map(|c| (Some(c.return_type()), None))
840                        .unwrap_or((None, None))
841                },
842                ast::Variant(it) => {
843                    let is_simple_field = |field: ast::TupleField| {
844                        let Some(ty) = field.ty() else { return true };
845                        matches!(ty, ast::Type::PathType(_)) && ty.generic_arg_list().is_none()
846                    };
847                    let is_simple_variant = matches!(
848                        it.field_list(),
849                        Some(ast::FieldList::TupleFieldList(list))
850                        if list.syntax().children_with_tokens().all(|it| it.kind() != T![,])
851                            && list.fields().next().is_none_or(is_simple_field)
852                    );
853                    (None, it.name().filter(|_| is_simple_variant).map(NameOrNameRef::Name))
854                },
855                ast::Stmt(_) => (None, None),
856                ast::Item(_) => (None, None),
857                _ => {
858                    match node.parent() {
859                        Some(n) => {
860                            node = n;
861                            continue;
862                        },
863                        None => (None, None),
864                    }
865                },
866            }
867        };
868    };
869    (ty.map(strip_refs), name)
870}
871
872fn classify_lifetime(
873    sema: &Semantics<'_, RootDatabase>,
874    original_file: &SyntaxNode,
875    lifetime: ast::Lifetime,
876) -> Option<LifetimeContext> {
877    let parent = lifetime.syntax().parent()?;
878    if parent.kind() == SyntaxKind::ERROR {
879        return None;
880    }
881
882    let lifetime =
883        find_node_at_offset::<ast::Lifetime>(original_file, lifetime.syntax().text_range().start());
884    let kind = match_ast! {
885        match parent {
886            ast::LifetimeParam(_) => LifetimeKind::LifetimeParam,
887            ast::BreakExpr(_) => LifetimeKind::LabelRef,
888            ast::ContinueExpr(_) => LifetimeKind::LabelRef,
889            ast::Label(_) => LifetimeKind::LabelDef,
890            _ => {
891                let def = lifetime.as_ref().and_then(|lt| sema.scope(lt.syntax())?.generic_def());
892                LifetimeKind::Lifetime { in_lifetime_param_bound: ast::TypeBound::can_cast(parent.kind()), def }
893            },
894        }
895    };
896
897    Some(LifetimeContext { kind })
898}
899
900fn classify_name(
901    sema: &Semantics<'_, RootDatabase>,
902    original_file: &SyntaxNode,
903    name: ast::Name,
904) -> Option<NameContext> {
905    let parent = name.syntax().parent()?;
906    let kind = match_ast! {
907        match parent {
908            ast::Const(_) => NameKind::Const,
909            ast::ConstParam(_) => NameKind::ConstParam,
910            ast::Enum(_) => NameKind::Enum,
911            ast::Fn(_) => NameKind::Function,
912            ast::IdentPat(bind_pat) => {
913                let mut pat_ctx = pattern_context_for(sema, original_file, bind_pat.into());
914                if let Some(record_field) = ast::RecordPatField::for_field_name(&name) {
915                    pat_ctx.record_pat = find_node_in_file_compensated(sema, original_file, &record_field.parent_record_pat());
916                }
917
918                NameKind::IdentPat(pat_ctx)
919            },
920            ast::MacroDef(_) => NameKind::MacroDef,
921            ast::MacroRules(_) => NameKind::MacroRules,
922            ast::Module(module) => NameKind::Module(module),
923            ast::RecordField(_) => NameKind::RecordField,
924            ast::Rename(_) => NameKind::Rename,
925            ast::SelfParam(_) => NameKind::SelfParam,
926            ast::Static(_) => NameKind::Static,
927            ast::Struct(_) => NameKind::Struct,
928            ast::Trait(_) => NameKind::Trait,
929            ast::TypeAlias(_) => NameKind::TypeAlias,
930            ast::TypeParam(_) => NameKind::TypeParam,
931            ast::Union(_) => NameKind::Union,
932            ast::Variant(_) => NameKind::Variant,
933            _ => return None,
934        }
935    };
936    let name = find_node_at_offset(original_file, name.syntax().text_range().start());
937    Some(NameContext { name, kind })
938}
939
940fn classify_name_ref<'db>(
941    sema: &Semantics<'db, RootDatabase>,
942    original_file: &SyntaxNode,
943    name_ref: ast::NameRef,
944    original_offset: TextSize,
945    parent: SyntaxNode,
946) -> Option<(NameRefContext<'db>, QualifierCtx)> {
947    let nameref = find_node_at_offset(original_file, original_offset);
948
949    let make_res = |kind| (NameRefContext { nameref: nameref.clone(), kind }, Default::default());
950
951    if let Some(record_field) = ast::RecordExprField::for_field_name(&name_ref) {
952        let dot_prefix = previous_non_trivia_token(name_ref.syntax().clone())
953            .is_some_and(|it| T![.] == it.kind());
954
955        return find_node_in_file_compensated(
956            sema,
957            original_file,
958            &record_field.parent_record_lit(),
959        )
960        .map(|expr| NameRefKind::RecordExpr { expr, dot_prefix })
961        .map(make_res);
962    }
963    if let Some(record_field) = ast::RecordPatField::for_field_name_ref(&name_ref) {
964        let kind = NameRefKind::Pattern(PatternContext {
965            param_ctx: None,
966            has_type_ascription: false,
967            ref_token: None,
968            mut_token: None,
969            record_pat: find_node_in_file_compensated(
970                sema,
971                original_file,
972                &record_field.parent_record_pat(),
973            ),
974            ..pattern_context_for(sema, original_file, record_field.parent_record_pat().into())
975        });
976        return Some(make_res(kind));
977    }
978
979    let field_expr_handle = |receiver, node| {
980        let receiver = find_opt_node_in_file(original_file, receiver);
981        let receiver_is_ambiguous_float_literal = match &receiver {
982            Some(ast::Expr::Literal(l)) => {
983                matches!(l.kind(), ast::LiteralKind::FloatNumber { .. })
984                    && l.syntax().last_token().is_some_and(|it| it.text().ends_with('.'))
985            }
986            _ => false,
987        };
988
989        let receiver_is_part_of_indivisible_expression = match &receiver {
990            Some(ast::Expr::IfExpr(_)) => {
991                let next_token_kind =
992                    next_non_trivia_token(name_ref.syntax().clone()).map(|t| t.kind());
993                next_token_kind == Some(SyntaxKind::ELSE_KW)
994            }
995            _ => false,
996        };
997        if receiver_is_part_of_indivisible_expression {
998            return None;
999        }
1000
1001        let mut receiver_ty = receiver.as_ref().and_then(|it| sema.type_of_expr(it));
1002        if receiver_is_ambiguous_float_literal {
1003            // `123.|` is parsed as a float but should actually be an integer.
1004            always!(receiver_ty.as_ref().is_none_or(|receiver_ty| receiver_ty.original.is_float()));
1005            receiver_ty =
1006                Some(TypeInfo { original: hir::BuiltinType::i32().ty(sema.db), adjusted: None });
1007        }
1008
1009        let kind = NameRefKind::DotAccess(DotAccess {
1010            receiver_ty,
1011            kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal },
1012            receiver,
1013            ctx: DotAccessExprCtx {
1014                in_block_expr: is_in_block(node),
1015                in_breakable: is_in_breakable(node).unzip().0,
1016            },
1017        });
1018        Some(make_res(kind))
1019    };
1020
1021    let segment = match_ast! {
1022        match parent {
1023            ast::PathSegment(segment) => segment,
1024            ast::FieldExpr(field) => {
1025                return field_expr_handle(field.expr(), field.syntax());
1026            },
1027            ast::ExternCrate(_) => {
1028                let kind = NameRefKind::ExternCrate;
1029                return Some(make_res(kind));
1030            },
1031            ast::MethodCallExpr(method) => {
1032                let receiver = find_opt_node_in_file(original_file, method.receiver());
1033                let has_parens = has_parens(&method);
1034                if !has_parens && let Some(res) = field_expr_handle(method.receiver(), method.syntax()) {
1035                    return Some(res)
1036                }
1037                let kind = NameRefKind::DotAccess(DotAccess {
1038                    receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
1039                    kind: DotAccessKind::Method,
1040                    receiver,
1041                    ctx: DotAccessExprCtx { in_block_expr: is_in_block(method.syntax()), in_breakable: is_in_breakable(method.syntax()).unzip().0 }
1042                });
1043                return Some(make_res(kind));
1044            },
1045            _ => return None,
1046        }
1047    };
1048
1049    let path = segment.parent_path();
1050    let original_path = find_node_in_file_compensated(sema, original_file, &path);
1051
1052    let mut path_ctx = PathCompletionCtx {
1053        has_call_parens: false,
1054        has_macro_bang: false,
1055        qualified: Qualified::No,
1056        parent: None,
1057        path: path.clone(),
1058        original_path,
1059        kind: PathKind::Item { kind: ItemListKind::SourceFile },
1060        has_type_args: false,
1061        use_tree_parent: false,
1062    };
1063
1064    let func_update_record = |syn: &SyntaxNode| {
1065        if let Some(record_expr) = syn.ancestors().nth(2).and_then(ast::RecordExpr::cast) {
1066            find_node_in_file_compensated(sema, original_file, &record_expr)
1067        } else {
1068            None
1069        }
1070    };
1071    let prev_expr = |node: SyntaxNode| {
1072        let node = match node.parent().and_then(ast::ExprStmt::cast) {
1073            Some(stmt) => stmt.syntax().clone(),
1074            None => node,
1075        };
1076        let prev_sibling = non_trivia_sibling(node.into(), Direction::Prev)?.into_node()?;
1077
1078        match_ast! {
1079            match prev_sibling {
1080                ast::ExprStmt(stmt) => stmt.expr().filter(|_| stmt.semicolon_token().is_none()),
1081                ast::LetStmt(stmt) => stmt.initializer().filter(|_| stmt.semicolon_token().is_none()),
1082                ast::Expr(expr) => Some(expr),
1083                _ => None,
1084            }
1085        }
1086    };
1087    let after_incomplete_let = |node: SyntaxNode| {
1088        prev_expr(node).and_then(|it| it.syntax().parent()).and_then(ast::LetStmt::cast)
1089    };
1090    let before_else_kw = |node: &SyntaxNode| {
1091        node.parent()
1092            .and_then(ast::ExprStmt::cast)
1093            .filter(|stmt| stmt.semicolon_token().is_none())
1094            .and_then(|stmt| non_trivia_sibling(stmt.syntax().clone().into(), Direction::Next))
1095            .and_then(NodeOrToken::into_node)
1096            .filter(|next| next.kind() == SyntaxKind::ERROR)
1097            .and_then(|next| next.first_token())
1098            .is_some_and(|token| token.kind() == SyntaxKind::ELSE_KW)
1099    };
1100
1101    // We do not want to generate path completions when we are sandwiched between an item decl signature and its body.
1102    // ex. trait Foo $0 {}
1103    // in these cases parser recovery usually kicks in for our inserted identifier, causing it
1104    // to either be parsed as an ExprStmt or a ItemRecovery, depending on whether it is in a block
1105    // expression or an item list.
1106    // The following code checks if the body is missing, if it is we either cut off the body
1107    // from the item or it was missing in the first place
1108    let inbetween_body_and_decl_check = |node: SyntaxNode| {
1109        if let Some(NodeOrToken::Node(n)) =
1110            syntax::algo::non_trivia_sibling(node.into(), syntax::Direction::Prev)
1111            && let Some(item) = ast::Item::cast(n)
1112        {
1113            let is_inbetween = match &item {
1114                ast::Item::Const(it) => it.body().is_none() && it.semicolon_token().is_none(),
1115                ast::Item::Enum(it) => it.variant_list().is_none(),
1116                ast::Item::ExternBlock(it) => it.extern_item_list().is_none(),
1117                ast::Item::Fn(it) => it.body().is_none() && it.semicolon_token().is_none(),
1118                ast::Item::Impl(it) => it.assoc_item_list().is_none(),
1119                ast::Item::Module(it) => it.item_list().is_none() && it.semicolon_token().is_none(),
1120                ast::Item::Static(it) => it.body().is_none(),
1121                ast::Item::Struct(it) => {
1122                    it.field_list().is_none() && it.semicolon_token().is_none()
1123                }
1124                ast::Item::Trait(it) => it.assoc_item_list().is_none(),
1125                ast::Item::TypeAlias(it) => it.ty().is_none() && it.semicolon_token().is_none(),
1126                ast::Item::Union(it) => it.record_field_list().is_none(),
1127                _ => false,
1128            };
1129            if is_inbetween {
1130                return Some(item);
1131            }
1132        }
1133        None
1134    };
1135
1136    let generic_arg_location = |arg: ast::GenericArg| {
1137        let mut override_location = None;
1138        let location = find_opt_node_in_file_compensated(
1139            sema,
1140            original_file,
1141            arg.syntax().parent().and_then(ast::GenericArgList::cast),
1142        )
1143        .map(|args| {
1144            let mut in_trait = None;
1145            let param = (|| {
1146                let parent = args.syntax().parent()?;
1147                let params = match_ast! {
1148                    match parent {
1149                        ast::PathSegment(segment) => {
1150                            match sema.resolve_path(&segment.parent_path().top_path())? {
1151                                hir::PathResolution::Def(def) => match def {
1152                                    hir::ModuleDef::Function(func) => {
1153                                         sema.source(func)?.value.generic_param_list()
1154                                    }
1155                                    hir::ModuleDef::Adt(adt) => {
1156                                        sema.source(adt)?.value.generic_param_list()
1157                                    }
1158                                    hir::ModuleDef::EnumVariant(variant) => {
1159                                        sema.source(variant.parent_enum(sema.db))?.value.generic_param_list()
1160                                    }
1161                                    hir::ModuleDef::Trait(trait_) => {
1162                                        if let ast::GenericArg::AssocTypeArg(arg) = &arg {
1163                                            let arg_name = arg.name_ref()?;
1164                                            let arg_name = arg_name.text();
1165                                            for item in trait_.items_with_supertraits(sema.db) {
1166                                                match item {
1167                                                    hir::AssocItem::TypeAlias(assoc_ty)
1168                                                        if assoc_ty.name(sema.db).as_str() == arg_name => {
1169                                                            override_location = Some(TypeLocation::AssocTypeEq);
1170                                                            return None;
1171                                                        },
1172                                                    hir::AssocItem::Const(const_)
1173                                                        if const_.name(sema.db)?.as_str() == arg_name => {
1174                                                            override_location =  Some(TypeLocation::AssocConstEq);
1175                                                            return None;
1176                                                        },
1177                                                    _ => (),
1178                                                }
1179                                            }
1180                                            return None;
1181                                        } else {
1182                                            in_trait = Some(trait_);
1183                                            sema.source(trait_)?.value.generic_param_list()
1184                                        }
1185                                    }
1186                                    hir::ModuleDef::TypeAlias(ty_) => {
1187                                        sema.source(ty_)?.value.generic_param_list()
1188                                    }
1189                                    _ => None,
1190                                },
1191                                _ => None,
1192                            }
1193                        },
1194                        ast::MethodCallExpr(call) => {
1195                            let func = sema.resolve_method_call(&call)?;
1196                            sema.source(func)?.value.generic_param_list()
1197                        },
1198                        ast::AssocTypeArg(arg) => {
1199                            let trait_ = ast::PathSegment::cast(arg.syntax().parent()?.parent()?)?;
1200                            match sema.resolve_path(&trait_.parent_path().top_path())? {
1201                                hir::PathResolution::Def(hir::ModuleDef::Trait(trait_)) =>  {
1202                                        let arg_name = arg.name_ref()?;
1203                                        let arg_name = arg_name.text();
1204                                        let trait_items = trait_.items_with_supertraits(sema.db);
1205                                        let assoc_ty = trait_items.iter().find_map(|item| match item {
1206                                            hir::AssocItem::TypeAlias(assoc_ty) => {
1207                                                (assoc_ty.name(sema.db).as_str() == arg_name)
1208                                                    .then_some(assoc_ty)
1209                                            },
1210                                            _ => None,
1211                                        })?;
1212                                        sema.source(*assoc_ty)?.value.generic_param_list()
1213                                    }
1214                                _ => None,
1215                            }
1216                        },
1217                        _ => None,
1218                    }
1219                }?;
1220                // Determine the index of the argument in the `GenericArgList` and match it with
1221                // the corresponding parameter in the `GenericParamList`. Since lifetime parameters
1222                // are often omitted, ignore them for the purposes of matching the argument with
1223                // its parameter unless a lifetime argument is provided explicitly. That is, for
1224                // `struct S<'a, 'b, T>`, match `S::<$0>` to `T` and `S::<'a, $0, _>` to `'b`.
1225                // FIXME: This operates on the syntax tree and will produce incorrect results when
1226                // generic parameters are disabled by `#[cfg]` directives. It should operate on the
1227                // HIR, but the functionality necessary to do so is not exposed at the moment.
1228                let mut explicit_lifetime_arg = false;
1229                let arg_idx = arg
1230                    .syntax()
1231                    .siblings(Direction::Prev)
1232                    // Skip the node itself
1233                    .skip(1)
1234                    .map(|arg| if ast::LifetimeArg::can_cast(arg.kind()) { explicit_lifetime_arg = true })
1235                    .count();
1236                let param_idx = if explicit_lifetime_arg {
1237                    arg_idx
1238                } else {
1239                    // Lifetimes parameters always precede type and generic parameters,
1240                    // so offset the argument index by the total number of lifetime params
1241                    arg_idx + params.lifetime_params().count()
1242                };
1243                params.generic_params().nth(param_idx)
1244            })();
1245            (args, in_trait, param)
1246        });
1247        let (arg_list, of_trait, corresponding_param) = match location {
1248            Some((arg_list, of_trait, param)) => (Some(arg_list), of_trait, param),
1249            _ => (None, None, None),
1250        };
1251        override_location.unwrap_or(TypeLocation::GenericArg {
1252            args: arg_list,
1253            of_trait,
1254            corresponding_param,
1255        })
1256    };
1257
1258    let type_location = |node: &SyntaxNode| {
1259        let parent = node.parent()?;
1260        let res = match_ast! {
1261            match parent {
1262                ast::Const(it) => {
1263                    let name = find_opt_node_in_file(original_file, it.name())?;
1264                    let original = ast::Const::cast(name.syntax().parent()?)?;
1265                    TypeLocation::TypeAscription(TypeAscriptionTarget::Const(original.body()))
1266                },
1267                ast::Static(it) => {
1268                    let name = find_opt_node_in_file(original_file, it.name())?;
1269                    let original = ast::Static::cast(name.syntax().parent()?)?;
1270                    TypeLocation::TypeAscription(TypeAscriptionTarget::Const(original.body()))
1271                },
1272                ast::RetType(_) => {
1273                    let parent = match ast::Fn::cast(parent.parent()?) {
1274                        Some(it) => it.param_list(),
1275                        None => ast::ClosureExpr::cast(parent.parent()?)?.param_list(),
1276                    };
1277
1278                    let parent = find_opt_node_in_file(original_file, parent)?.syntax().parent()?;
1279                    let body = match_ast! {
1280                        match parent {
1281                            ast::ClosureExpr(it) => {
1282                                it.body()
1283                            },
1284                            ast::Fn(it) => {
1285                                it.body().map(ast::Expr::BlockExpr)
1286                            },
1287                            _ => return None,
1288                        }
1289                    };
1290                    let item = ast::Fn::cast(parent);
1291                    TypeLocation::TypeAscription(TypeAscriptionTarget::RetType { body, item })
1292                },
1293                ast::Param(it) => {
1294                    it.colon_token()?;
1295                    TypeLocation::TypeAscription(TypeAscriptionTarget::FnParam(find_opt_node_in_file(original_file, it.pat())))
1296                },
1297                ast::LetStmt(it) => {
1298                    it.colon_token()?;
1299                    TypeLocation::TypeAscription(TypeAscriptionTarget::Let(find_opt_node_in_file(original_file, it.pat())))
1300                },
1301                ast::Impl(it) => {
1302                    match it.trait_() {
1303                        Some(t) if t.syntax() == node => TypeLocation::ImplTrait,
1304                        _ => match it.self_ty() {
1305                            Some(t) if t.syntax() == node => TypeLocation::ImplTarget,
1306                            _ => return None,
1307                        },
1308                    }
1309                },
1310                ast::TypeBound(_) => TypeLocation::TypeBound,
1311                // is this case needed?
1312                ast::TypeBoundList(_) => TypeLocation::TypeBound,
1313                ast::GenericArg(it) => generic_arg_location(it),
1314                // is this case needed?
1315                ast::GenericArgList(it) => {
1316                    let args = find_opt_node_in_file_compensated(sema, original_file, Some(it));
1317                    TypeLocation::GenericArg { args, of_trait: None, corresponding_param: None }
1318                },
1319                ast::TupleField(_) => TypeLocation::TupleField,
1320                _ => return None,
1321            }
1322        };
1323        Some(res)
1324    };
1325
1326    let make_path_kind_expr = |expr: ast::Expr| {
1327        let it = expr.syntax();
1328        let prev_token = iter::successors(it.first_token(), |it| it.prev_token())
1329            .skip(1)
1330            .find(|it| !it.kind().is_trivia());
1331        let in_block_expr = is_in_block(it);
1332        let (in_loop_body, innermost_breakable) = is_in_breakable(it).unzip();
1333        let after_if_expr = is_after_if_expr(it.clone());
1334        let after_amp = prev_token.as_ref().is_some_and(|it| it.kind() == SyntaxKind::AMP);
1335        let ref_expr_parent = prev_token.and_then(|it| it.parent()).and_then(ast::RefExpr::cast);
1336        let (innermost_ret_ty, self_param) = {
1337            let find_ret_ty = |it: SyntaxNode| {
1338                if let Some(item) = ast::Item::cast(it.clone()) {
1339                    match item {
1340                        ast::Item::Fn(f) => Some(sema.to_def(&f).map(|it| it.ret_type(sema.db))),
1341                        ast::Item::MacroCall(_) => None,
1342                        _ => Some(None),
1343                    }
1344                } else {
1345                    let expr = ast::Expr::cast(it)?;
1346                    let callable = match expr {
1347                        // FIXME
1348                        // ast::Expr::BlockExpr(b) if b.async_token().is_some() || b.try_token().is_some() => sema.type_of_expr(b),
1349                        ast::Expr::ClosureExpr(_) => sema.type_of_expr(&expr),
1350                        _ => return None,
1351                    };
1352                    Some(
1353                        callable
1354                            .and_then(|c| c.adjusted().as_callable(sema.db))
1355                            .map(|it| it.return_type()),
1356                    )
1357                }
1358            };
1359            let fn_self_param =
1360                |fn_: ast::Fn| sema.to_def(&fn_).and_then(|it| it.self_param(sema.db));
1361            let closure_this_param = |closure: ast::ClosureExpr| {
1362                if closure.param_list()?.params().next()?.pat()?.syntax().text() != "this" {
1363                    return None;
1364                }
1365                sema.type_of_expr(&closure.into())
1366                    .and_then(|it| it.original.as_callable(sema.db))
1367                    .and_then(|it| it.params().into_iter().next())
1368            };
1369            let find_fn_self_param = |it: SyntaxNode| {
1370                match_ast! {
1371                    match it {
1372                        ast::Fn(fn_) => Some(fn_self_param(fn_).map(Either::Left)),
1373                        ast::ClosureExpr(f) => closure_this_param(f).map(Either::Right).map(Some),
1374                        ast::MacroCall(_) => None,
1375                        ast::Item(_) => Some(None),
1376                        _ => None,
1377                    }
1378                }
1379            };
1380
1381            match find_node_in_file_compensated(sema, original_file, &expr) {
1382                Some(it) => {
1383                    // buggy
1384                    let innermost_ret_ty = sema
1385                        .ancestors_with_macros(it.syntax().clone())
1386                        .find_map(find_ret_ty)
1387                        .flatten();
1388
1389                    let self_param = sema
1390                        .ancestors_with_macros(it.syntax().clone())
1391                        .find_map(find_fn_self_param)
1392                        .flatten();
1393                    (innermost_ret_ty, self_param)
1394                }
1395                None => (None, None),
1396            }
1397        };
1398        let innermost_breakable_ty = innermost_breakable
1399            .and_then(ast::Expr::cast)
1400            .and_then(|expr| find_node_in_file_compensated(sema, original_file, &expr))
1401            .and_then(|expr| sema.type_of_expr(&expr))
1402            .map(|ty| if ty.original.is_never() { ty.adjusted() } else { ty.original() });
1403        let is_func_update = func_update_record(it);
1404        let in_condition = is_in_condition(&expr);
1405        let after_incomplete_let = after_incomplete_let(it.clone()).is_some();
1406        let incomplete_expr_stmt =
1407            it.parent().and_then(ast::ExprStmt::cast).map(|it| it.semicolon_token().is_none());
1408        let before_else_kw = before_else_kw(it);
1409        let incomplete_let = left_ancestors(it.parent())
1410            .find_map(ast::LetStmt::cast)
1411            .is_some_and(|it| it.semicolon_token().is_none())
1412            || after_incomplete_let && incomplete_expr_stmt.unwrap_or(true) && !before_else_kw;
1413        let in_value = is_in_value(&expr);
1414        let impl_ = fetch_immediate_impl_or_trait(sema, original_file, expr.syntax())
1415            .and_then(Either::left);
1416
1417        let in_match_guard = match it.parent().and_then(ast::MatchArm::cast) {
1418            Some(arm) => arm
1419                .fat_arrow_token()
1420                .is_none_or(|arrow| it.text_range().start() < arrow.text_range().start()),
1421            None => false,
1422        };
1423
1424        PathKind::Expr {
1425            expr_ctx: PathExprCtx {
1426                in_block_expr,
1427                in_breakable: in_loop_body,
1428                after_if_expr,
1429                before_else_kw,
1430                in_condition,
1431                ref_expr_parent,
1432                after_amp,
1433                is_func_update,
1434                innermost_ret_ty,
1435                innermost_breakable_ty,
1436                self_param,
1437                in_value,
1438                incomplete_let,
1439                after_incomplete_let,
1440                impl_,
1441                in_match_guard,
1442            },
1443        }
1444    };
1445    let make_path_kind_type = |ty: ast::Type| {
1446        let location = type_location(ty.syntax());
1447        PathKind::Type { location: location.unwrap_or(TypeLocation::Other) }
1448    };
1449
1450    let kind_item = |it: &SyntaxNode| {
1451        let parent = it.parent()?;
1452        let kind = match_ast! {
1453            match parent {
1454                ast::ItemList(_) => PathKind::Item { kind: ItemListKind::Module },
1455                ast::AssocItemList(_) => PathKind::Item { kind: match parent.parent() {
1456                    Some(it) => match_ast! {
1457                        match it {
1458                            ast::Trait(_) => ItemListKind::Trait,
1459                            ast::Impl(it) => if it.trait_().is_some() {
1460                                ItemListKind::TraitImpl(find_node_in_file_compensated(sema, original_file, &it))
1461                            } else {
1462                                ItemListKind::Impl
1463                            },
1464                            _ => return None
1465                        }
1466                    },
1467                    None => return None,
1468                } },
1469                ast::ExternItemList(it) => {
1470                    let exn_blk = it.syntax().parent().and_then(ast::ExternBlock::cast);
1471                    PathKind::Item {
1472                        kind: ItemListKind::ExternBlock {
1473                            is_unsafe: exn_blk.and_then(|it| it.unsafe_token()).is_some(),
1474                        }
1475                    }
1476                },
1477                ast::SourceFile(_) => PathKind::Item { kind: ItemListKind::SourceFile },
1478                _ => return None,
1479            }
1480        };
1481        Some(kind)
1482    };
1483
1484    let mut kind_macro_call = |it: ast::MacroCall| {
1485        path_ctx.has_macro_bang = it.excl_token().is_some();
1486        let parent = it.syntax().parent()?;
1487        if let Some(kind) = kind_item(it.syntax()) {
1488            return Some(kind);
1489        }
1490        let kind = match_ast! {
1491            match parent {
1492                ast::MacroExpr(expr) => make_path_kind_expr(expr.into()),
1493                ast::MacroPat(it) => PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into())},
1494                ast::MacroType(ty) => make_path_kind_type(ty.into()),
1495                _ => return None,
1496            }
1497        };
1498        Some(kind)
1499    };
1500    let make_path_kind_attr = |meta: ast::Meta| {
1501        let attr = meta.parent_attr()?;
1502        let kind = attr.kind();
1503        let attached = attr.syntax().parent()?;
1504        let is_trailing_outer_attr = kind != AttrKind::Inner
1505            && non_trivia_sibling(attr.syntax().clone().into(), syntax::Direction::Next).is_none();
1506        let annotated_item_kind = if is_trailing_outer_attr { None } else { Some(attached.kind()) };
1507        let derive_helpers = annotated_item_kind
1508            .filter(|kind| {
1509                matches!(
1510                    kind,
1511                    SyntaxKind::STRUCT
1512                        | SyntaxKind::ENUM
1513                        | SyntaxKind::UNION
1514                        | SyntaxKind::VARIANT
1515                        | SyntaxKind::TUPLE_FIELD
1516                        | SyntaxKind::RECORD_FIELD
1517                )
1518            })
1519            .and_then(|_| find_node_at_offset::<ast::Adt>(original_file, original_offset))
1520            .and_then(|adt| sema.derive_helpers_in_scope(&adt))
1521            .unwrap_or_default();
1522        Some(PathKind::Attr { attr_ctx: AttrCtx { kind, annotated_item_kind, derive_helpers } })
1523    };
1524
1525    // Infer the path kind
1526    let parent = path.syntax().parent()?;
1527    let kind = 'find_kind: {
1528        if parent.kind() == SyntaxKind::ERROR {
1529            if let Some(kind) = inbetween_body_and_decl_check(parent.clone()) {
1530                return Some(make_res(NameRefKind::Keyword(kind)));
1531            }
1532
1533            break 'find_kind kind_item(&parent)?;
1534        }
1535        match_ast! {
1536            match parent {
1537                ast::PathType(it) => make_path_kind_type(it.into()),
1538                ast::PathExpr(it) => {
1539                    if let Some(p) = it.syntax().parent() {
1540                        let p_kind = p.kind();
1541                        // The syntax node of interest, for which we want to check whether
1542                        // it is sandwiched between an item decl signature and its body.
1543                        let probe = if ast::ExprStmt::can_cast(p_kind) {
1544                            Some(p)
1545                        } else if ast::StmtList::can_cast(p_kind) {
1546                            Some(it.syntax().clone())
1547                        } else {
1548                            None
1549                        };
1550                        if let Some(kind) = probe.and_then(inbetween_body_and_decl_check) {
1551                            return Some(make_res(NameRefKind::Keyword(kind)));
1552                        }
1553                    }
1554
1555                    path_ctx.has_call_parens = it.syntax().parent().is_some_and(|it| ast::CallExpr::cast(it).is_some_and(|it| has_parens(&it)));
1556
1557                    make_path_kind_expr(it.into())
1558                },
1559                ast::TupleStructPat(it) => {
1560                    path_ctx.has_call_parens = true;
1561                    PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into()) }
1562                },
1563                ast::RecordPat(it) => {
1564                    path_ctx.has_call_parens = true;
1565                    PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into()) }
1566                },
1567                ast::PathPat(it) => {
1568                    PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into())}
1569                },
1570                ast::MacroCall(it) => {
1571                    kind_macro_call(it)?
1572                },
1573                ast::Meta(meta) => make_path_kind_attr(meta)?,
1574                ast::VisibilityInner(it) => PathKind::Vis { has_in_token: it.in_token().is_some() },
1575                ast::UseTree(_) => PathKind::Use,
1576                // completing inside a qualifier
1577                ast::Path(parent) => {
1578                    path_ctx.parent = Some(parent.clone());
1579                    let parent = iter::successors(Some(parent), |it| it.parent_path()).last()?.syntax().parent()?;
1580                    match_ast! {
1581                        match parent {
1582                            ast::PathType(it) => make_path_kind_type(it.into()),
1583                            ast::PathExpr(it) => {
1584                                path_ctx.has_call_parens = it.syntax().parent().is_some_and(|it| ast::CallExpr::cast(it).is_some_and(|it| has_parens(&it)));
1585
1586                                make_path_kind_expr(it.into())
1587                            },
1588                            ast::TupleStructPat(it) => {
1589                                path_ctx.has_call_parens = true;
1590                                PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into()) }
1591                            },
1592                            ast::RecordPat(it) => {
1593                                path_ctx.has_call_parens = true;
1594                                PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into()) }
1595                            },
1596                            ast::PathPat(it) => {
1597                                PathKind::Pat { pat_ctx: pattern_context_for(sema, original_file, it.into())}
1598                            },
1599                            ast::MacroCall(it) => {
1600                                kind_macro_call(it)?
1601                            },
1602                            ast::Meta(meta) => make_path_kind_attr(meta)?,
1603                            ast::VisibilityInner(it) => PathKind::Vis { has_in_token: it.in_token().is_some() },
1604                            ast::UseTree(_) => PathKind::Use,
1605                            ast::RecordExpr(it) => make_path_kind_expr(it.into()),
1606                            _ => return None,
1607                        }
1608                    }
1609                },
1610                ast::RecordExpr(it) => {
1611                    // A record expression in this position is usually a result of parsing recovery, so check that
1612                    if let Some(kind) = inbetween_body_and_decl_check(it.syntax().clone()) {
1613                        return Some(make_res(NameRefKind::Keyword(kind)));
1614                    }
1615                    make_path_kind_expr(it.into())
1616                },
1617                _ => return None,
1618            }
1619        }
1620    };
1621
1622    path_ctx.kind = kind;
1623    path_ctx.has_type_args = segment.generic_arg_list().is_some();
1624
1625    // calculate the qualifier context
1626    if let Some((qualifier, use_tree_parent)) = path_or_use_tree_qualifier(&path) {
1627        path_ctx.use_tree_parent = use_tree_parent;
1628        if !use_tree_parent && segment.coloncolon_token().is_some() {
1629            path_ctx.qualified = Qualified::Absolute;
1630        } else {
1631            let qualifier = qualifier
1632                .segment()
1633                .and_then(|it| find_node_in_file(original_file, &it))
1634                .map(|it| it.parent_path());
1635            if let Some(qualifier) = qualifier {
1636                let type_anchor = match qualifier.segment().and_then(|it| it.kind()) {
1637                    Some(ast::PathSegmentKind::Type { type_ref: Some(type_ref), trait_ref })
1638                        if qualifier.qualifier().is_none() =>
1639                    {
1640                        Some((type_ref, trait_ref))
1641                    }
1642                    _ => None,
1643                };
1644
1645                path_ctx.qualified = if let Some((ty, trait_ref)) = type_anchor {
1646                    let ty = match ty {
1647                        ast::Type::InferType(_) => None,
1648                        ty => sema.resolve_type(&ty),
1649                    };
1650                    let trait_ = trait_ref.and_then(|it| sema.resolve_trait(&it.path()?));
1651                    Qualified::TypeAnchor { ty, trait_ }
1652                } else {
1653                    let res = sema.resolve_path(&qualifier);
1654
1655                    // For understanding how and why super_chain_len is calculated the way it
1656                    // is check the documentation at it's definition
1657                    let mut segment_count = 0;
1658                    let super_count = iter::successors(Some(qualifier.clone()), |p| p.qualifier())
1659                        .take_while(|p| {
1660                            p.segment()
1661                                .and_then(|s| {
1662                                    segment_count += 1;
1663                                    s.super_token()
1664                                })
1665                                .is_some()
1666                        })
1667                        .count();
1668
1669                    let super_chain_len =
1670                        if segment_count > super_count { None } else { Some(super_count) };
1671
1672                    Qualified::With { path: qualifier, resolution: res, super_chain_len }
1673                }
1674            };
1675        }
1676    } else if let Some(segment) = path.segment()
1677        && segment.coloncolon_token().is_some()
1678    {
1679        path_ctx.qualified = Qualified::Absolute;
1680    }
1681
1682    let mut qualifier_ctx = QualifierCtx::default();
1683    if path_ctx.is_trivial_path() {
1684        // fetch the full expression that may have qualifiers attached to it
1685        let top_node = match path_ctx.kind {
1686            PathKind::Expr { expr_ctx: PathExprCtx { in_block_expr: true, .. } } => {
1687                parent.ancestors().find(|it| ast::PathExpr::can_cast(it.kind())).and_then(|p| {
1688                    let parent = p.parent()?;
1689                    if ast::StmtList::can_cast(parent.kind()) {
1690                        Some(p)
1691                    } else if ast::ExprStmt::can_cast(parent.kind()) {
1692                        Some(parent)
1693                    } else {
1694                        None
1695                    }
1696                })
1697            }
1698            PathKind::Item { .. } => parent.ancestors().find(|it| it.kind() == SyntaxKind::ERROR),
1699            _ => None,
1700        };
1701        if let Some(top) = top_node {
1702            if let Some(NodeOrToken::Node(error_node)) =
1703                syntax::algo::non_trivia_sibling(top.clone().into(), syntax::Direction::Prev)
1704                && error_node.kind() == SyntaxKind::ERROR
1705            {
1706                for token in error_node.children_with_tokens().filter_map(NodeOrToken::into_token) {
1707                    match token.kind() {
1708                        SyntaxKind::UNSAFE_KW => qualifier_ctx.unsafe_tok = Some(token),
1709                        SyntaxKind::ASYNC_KW => qualifier_ctx.async_tok = Some(token),
1710                        SyntaxKind::SAFE_KW => qualifier_ctx.safe_tok = Some(token),
1711                        _ => {}
1712                    }
1713                }
1714                qualifier_ctx.vis_node = error_node.children().find_map(ast::Visibility::cast);
1715                qualifier_ctx.abi_node = error_node.children().find_map(ast::Abi::cast);
1716            }
1717
1718            if let PathKind::Item { .. } = path_ctx.kind
1719                && qualifier_ctx.none()
1720                && let Some(t) = top.first_token()
1721                && let Some(prev) =
1722                    t.prev_token().and_then(|t| syntax::algo::skip_trivia_token(t, Direction::Prev))
1723                && ![T![;], T!['}'], T!['{'], T![']']].contains(&prev.kind())
1724            {
1725                // This was inferred to be an item position path, but it seems
1726                // to be part of some other broken node which leaked into an item
1727                // list
1728                return None;
1729            }
1730        }
1731    }
1732    Some((NameRefContext { nameref, kind: NameRefKind::Path(path_ctx) }, qualifier_ctx))
1733}
1734
1735/// When writing in the middle of some code the following situation commonly occurs (`|` denotes the cursor):
1736/// ```ignore
1737/// value.method|
1738/// (1, 2, 3)
1739/// ```
1740/// Here, we want to complete the method parentheses & arguments (if the corresponding settings are on),
1741/// but the thing is parsed as a method call with parentheses. Therefore we use heuristics: if the parentheses
1742/// are on the next line, consider them non-existent.
1743fn has_parens(node: &dyn HasArgList) -> bool {
1744    let Some(arg_list) = node.arg_list() else { return false };
1745    if arg_list.l_paren_token().is_none() {
1746        return false;
1747    }
1748    let prev_siblings = iter::successors(arg_list.syntax().prev_sibling_or_token(), |it| {
1749        it.prev_sibling_or_token()
1750    });
1751    prev_siblings
1752        .take_while(|syntax| syntax.kind().is_trivia())
1753        .filter_map(|syntax| {
1754            syntax.into_token().filter(|token| token.kind() == SyntaxKind::WHITESPACE)
1755        })
1756        .all(|whitespace| !whitespace.text().contains('\n'))
1757}
1758
1759fn pattern_context_for(
1760    sema: &Semantics<'_, RootDatabase>,
1761    original_file: &SyntaxNode,
1762    pat: ast::Pat,
1763) -> PatternContext {
1764    let mut param_ctx = None;
1765
1766    let mut missing_variants = vec![];
1767    let is_pat_like = |kind| {
1768        ast::Pat::can_cast(kind)
1769            || ast::RecordPatField::can_cast(kind)
1770            || ast::RecordPatFieldList::can_cast(kind)
1771    };
1772
1773    let (refutability, has_type_ascription) = pat
1774        .syntax()
1775        .ancestors()
1776        .find(|it| !is_pat_like(it.kind()))
1777        .map_or((PatternRefutability::Irrefutable, false), |node| {
1778            let refutability = match_ast! {
1779                match node {
1780                    ast::LetStmt(let_) => return (PatternRefutability::Refutable, let_.ty().is_some()),
1781                    ast::Param(param) => {
1782                        let has_type_ascription = param.ty().is_some();
1783                        param_ctx = (|| {
1784                            let fake_param_list = param.syntax().parent().and_then(ast::ParamList::cast)?;
1785                            let param_list = find_node_in_file_compensated(sema, original_file, &fake_param_list)?;
1786                            let param_list_owner = param_list.syntax().parent()?;
1787                            let kind = match_ast! {
1788                                match param_list_owner {
1789                                    ast::ClosureExpr(closure) => ParamKind::Closure(closure),
1790                                    ast::Fn(fn_) => ParamKind::Function(fn_),
1791                                    _ => return None,
1792                                }
1793                            };
1794                            Some(ParamContext {
1795                                param_list, param, kind
1796                            })
1797                        })();
1798                        return (PatternRefutability::Irrefutable, has_type_ascription)
1799                    },
1800                    ast::MatchArm(match_arm) => {
1801                       let missing_variants_opt = match_arm
1802                            .syntax()
1803                            .parent()
1804                            .and_then(ast::MatchArmList::cast)
1805                            .and_then(|match_arm_list| {
1806                                match_arm_list
1807                                .syntax()
1808                                .parent()
1809                                .and_then(ast::MatchExpr::cast)
1810                                .and_then(|match_expr| {
1811                                    let expr_opt = find_opt_node_in_file(original_file, match_expr.expr());
1812
1813                                    expr_opt.and_then(|expr| {
1814                                        sema.type_of_expr(&expr)?
1815                                        .adjusted()
1816                                        .autoderef(sema.db)
1817                                        .find_map(|ty| match ty.as_adt() {
1818                                            Some(hir::Adt::Enum(e)) => Some(e),
1819                                            _ => None,
1820                                        }).map(|enum_| enum_.variants(sema.db))
1821                                    })
1822                                }).map(|variants| variants.iter().filter_map(|variant| {
1823                                        let variant_name = variant.name(sema.db);
1824
1825                                        let variant_already_present = match_arm_list.arms().any(|arm| {
1826                                            arm.pat().and_then(|pat| {
1827                                                let pat_already_present = pat.syntax().to_string().contains(variant_name.as_str());
1828                                                pat_already_present.then_some(pat_already_present)
1829                                            }).is_some()
1830                                        });
1831
1832                                        (!variant_already_present).then_some(*variant)
1833                                    }).collect::<Vec<EnumVariant>>())
1834                        });
1835
1836                        if let Some(missing_variants_) = missing_variants_opt {
1837                            missing_variants = missing_variants_;
1838                        };
1839
1840                        PatternRefutability::Refutable
1841                    },
1842                    ast::LetExpr(_) => PatternRefutability::Refutable,
1843                    ast::ForExpr(_) => PatternRefutability::Irrefutable,
1844                    _ => PatternRefutability::Irrefutable,
1845                }
1846            };
1847            (refutability, false)
1848        });
1849    let (ref_token, mut_token) = match &pat {
1850        ast::Pat::IdentPat(it) => (it.ref_token(), it.mut_token()),
1851        _ => (None, None),
1852    };
1853
1854    // Only suggest name in let-stmt or fn param
1855    let should_suggest_name = matches!(
1856            &pat,
1857            ast::Pat::IdentPat(it)
1858                if it.syntax()
1859                .parent().is_some_and(|node| {
1860                    let kind = node.kind();
1861                    ast::LetStmt::can_cast(kind) || ast::Param::can_cast(kind)
1862                })
1863    );
1864
1865    PatternContext {
1866        refutability,
1867        param_ctx,
1868        has_type_ascription,
1869        should_suggest_name,
1870        after_if_expr: is_after_if_expr(pat.syntax().clone()),
1871        parent_pat: pat.syntax().parent().and_then(ast::Pat::cast),
1872        mut_token,
1873        ref_token,
1874        record_pat: None,
1875        impl_or_trait: fetch_immediate_impl_or_trait(sema, original_file, pat.syntax()),
1876        missing_variants,
1877    }
1878}
1879
1880fn fetch_immediate_impl_or_trait(
1881    sema: &Semantics<'_, RootDatabase>,
1882    original_file: &SyntaxNode,
1883    node: &SyntaxNode,
1884) -> Option<Either<ast::Impl, ast::Trait>> {
1885    let mut ancestors = ancestors_in_file_compensated(sema, original_file, node)?
1886        .filter_map(ast::Item::cast)
1887        .filter(|it| !matches!(it, ast::Item::MacroCall(_)));
1888
1889    match ancestors.next()? {
1890        ast::Item::Const(_) | ast::Item::Fn(_) | ast::Item::TypeAlias(_) => (),
1891        ast::Item::Impl(it) => return Some(Either::Left(it)),
1892        ast::Item::Trait(it) => return Some(Either::Right(it)),
1893        _ => return None,
1894    }
1895    match ancestors.next()? {
1896        ast::Item::Impl(it) => Some(Either::Left(it)),
1897        ast::Item::Trait(it) => Some(Either::Right(it)),
1898        _ => None,
1899    }
1900}
1901
1902/// Attempts to find `node` inside `syntax` via `node`'s text range.
1903/// If the fake identifier has been inserted after this node or inside of this node use the `_compensated` version instead.
1904fn find_opt_node_in_file<N: AstNode>(syntax: &SyntaxNode, node: Option<N>) -> Option<N> {
1905    find_node_in_file(syntax, &node?)
1906}
1907
1908/// Attempts to find `node` inside `syntax` via `node`'s text range.
1909/// If the fake identifier has been inserted after this node or inside of this node use the `_compensated` version instead.
1910fn find_node_in_file<N: AstNode>(syntax: &SyntaxNode, node: &N) -> Option<N> {
1911    let syntax_range = syntax.text_range();
1912    let range = node.syntax().text_range();
1913    let intersection = range.intersect(syntax_range)?;
1914    syntax.covering_element(intersection).ancestors().find_map(N::cast)
1915}
1916
1917/// Attempts to find `node` inside `syntax` via `node`'s text range while compensating
1918/// for the offset introduced by the fake ident.
1919/// This is wrong if `node` comes before the insertion point! Use `find_node_in_file` instead.
1920fn find_node_in_file_compensated<N: AstNode>(
1921    sema: &Semantics<'_, RootDatabase>,
1922    in_file: &SyntaxNode,
1923    node: &N,
1924) -> Option<N> {
1925    ancestors_in_file_compensated(sema, in_file, node.syntax())?.find_map(N::cast)
1926}
1927
1928fn ancestors_in_file_compensated<'sema>(
1929    sema: &'sema Semantics<'_, RootDatabase>,
1930    in_file: &SyntaxNode,
1931    node: &SyntaxNode,
1932) -> Option<impl Iterator<Item = SyntaxNode> + 'sema> {
1933    let syntax_range = in_file.text_range();
1934    let range = node.text_range();
1935    let end = range.end().checked_sub(TextSize::try_from(COMPLETION_MARKER.len()).ok()?)?;
1936    if end < range.start() {
1937        return None;
1938    }
1939    let range = TextRange::new(range.start(), end);
1940    // our inserted ident could cause `range` to go outside of the original syntax, so cap it
1941    let intersection = range.intersect(syntax_range)?;
1942    let node = match in_file.covering_element(intersection) {
1943        NodeOrToken::Node(node) => node,
1944        NodeOrToken::Token(tok) => tok.parent()?,
1945    };
1946    Some(sema.ancestors_with_macros(node))
1947}
1948
1949/// Attempts to find `node` inside `syntax` via `node`'s text range while compensating
1950/// for the offset introduced by the fake ident..
1951/// This is wrong if `node` comes before the insertion point! Use `find_node_in_file` instead.
1952fn find_opt_node_in_file_compensated<N: AstNode>(
1953    sema: &Semantics<'_, RootDatabase>,
1954    syntax: &SyntaxNode,
1955    node: Option<N>,
1956) -> Option<N> {
1957    find_node_in_file_compensated(sema, syntax, &node?)
1958}
1959
1960fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<(ast::Path, bool)> {
1961    if let Some(qual) = path.qualifier() {
1962        return Some((qual, false));
1963    }
1964    let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
1965    let use_tree = use_tree_list.syntax().parent().and_then(ast::UseTree::cast)?;
1966    Some((use_tree.path()?, true))
1967}
1968
1969fn left_ancestors(node: Option<SyntaxNode>) -> impl Iterator<Item = SyntaxNode> {
1970    node.into_iter().flat_map(|node| {
1971        let end = node.text_range().end();
1972        node.ancestors().take_while(move |it| it.text_range().end() == end)
1973    })
1974}
1975
1976fn is_in_token_of_for_loop(path: &ast::Path) -> bool {
1977    // oh my ...
1978    (|| {
1979        let expr = path.syntax().parent().and_then(ast::PathExpr::cast)?;
1980        let for_expr = expr.syntax().parent().and_then(ast::ForExpr::cast)?;
1981        if for_expr.in_token().is_some() {
1982            return Some(false);
1983        }
1984        let pat = for_expr.pat()?;
1985        let next_sibl = next_non_trivia_sibling(pat.syntax().clone().into())?;
1986        Some(match next_sibl {
1987            syntax::NodeOrToken::Node(n) => {
1988                n.text_range().start() == path.syntax().text_range().start()
1989            }
1990            syntax::NodeOrToken::Token(t) => {
1991                t.text_range().start() == path.syntax().text_range().start()
1992            }
1993        })
1994    })()
1995    .unwrap_or(false)
1996}
1997
1998fn is_in_breakable(node: &SyntaxNode) -> Option<(BreakableKind, SyntaxNode)> {
1999    node.ancestors()
2000        .take_while(|it| it.kind() != SyntaxKind::FN && it.kind() != SyntaxKind::CLOSURE_EXPR)
2001        .find_map(|it| {
2002            let (breakable, loop_body) = match_ast! {
2003                match it {
2004                    ast::ForExpr(it) => (BreakableKind::For, it.loop_body()?),
2005                    ast::WhileExpr(it) => (BreakableKind::While, it.loop_body()?),
2006                    ast::LoopExpr(it) => (BreakableKind::Loop, it.loop_body()?),
2007                    ast::BlockExpr(it) => return it.label().map(|_| (BreakableKind::Block, it.syntax().clone())),
2008                    _ => return None,
2009                }
2010            };
2011            loop_body.syntax().text_range().contains_range(node.text_range())
2012                .then_some((breakable, it))
2013        })
2014}
2015
2016fn is_in_block(node: &SyntaxNode) -> bool {
2017    if has_in_newline_expr_first(node) {
2018        return true;
2019    };
2020    node.parent()
2021        .map(|node| ast::ExprStmt::can_cast(node.kind()) || ast::StmtList::can_cast(node.kind()))
2022        .unwrap_or(false)
2023}
2024
2025/// Similar to `has_parens`, heuristic sensing incomplete statement before ambiguous `Expr`
2026///
2027/// Heuristic:
2028///
2029/// If the `PathExpr` is left part of the `Expr` and there is a newline after the `PathExpr`,
2030/// it is considered that the `PathExpr` is not part of the `Expr`.
2031fn has_in_newline_expr_first(node: &SyntaxNode) -> bool {
2032    if ast::PathExpr::can_cast(node.kind())
2033        && let Some(NodeOrToken::Token(next)) = node.next_sibling_or_token()
2034        && next.kind() == SyntaxKind::WHITESPACE
2035        && next.text().contains('\n')
2036        && let Some(stmt_like) = node
2037            .ancestors()
2038            .take_while(|it| it.text_range().start() == node.text_range().start())
2039            .filter_map(Either::<ast::ExprStmt, ast::Expr>::cast)
2040            .last()
2041    {
2042        stmt_like.syntax().parent().and_then(ast::StmtList::cast).is_some()
2043    } else {
2044        false
2045    }
2046}
2047
2048fn is_after_if_expr(node: SyntaxNode) -> bool {
2049    let node = match node.parent().and_then(Either::<ast::ExprStmt, ast::MatchArm>::cast) {
2050        Some(stmt) => stmt.syntax().clone(),
2051        None => node,
2052    };
2053    let Some(prev_token) = previous_non_trivia_token(node) else { return false };
2054    prev_token
2055        .parent_ancestors()
2056        .take_while(|it| it.text_range().end() == prev_token.text_range().end())
2057        .find_map(ast::IfExpr::cast)
2058        .is_some()
2059}
2060
2061fn next_non_trivia_token(e: impl Into<SyntaxElement>) -> Option<SyntaxToken> {
2062    let mut token = match e.into() {
2063        SyntaxElement::Node(n) => n.last_token()?,
2064        SyntaxElement::Token(t) => t,
2065    }
2066    .next_token();
2067    while let Some(inner) = token {
2068        if !inner.kind().is_trivia() {
2069            return Some(inner);
2070        } else {
2071            token = inner.next_token();
2072        }
2073    }
2074    None
2075}
2076
2077fn next_non_trivia_sibling(ele: SyntaxElement) -> Option<SyntaxElement> {
2078    let mut e = ele;
2079    while let Some(next) = e.next_sibling_or_token() {
2080        if !next.kind().is_trivia() {
2081            return Some(next);
2082        } else {
2083            e = next;
2084        }
2085    }
2086    None
2087}
2088
2089fn prev_special_biased_token_at_trivia(mut token: SyntaxToken) -> SyntaxToken {
2090    while token.kind().is_trivia()
2091        && let Some(prev) = token.prev_token()
2092        && let T![=]
2093        | T![+=]
2094        | T![/=]
2095        | T![*=]
2096        | T![%=]
2097        | T![>>=]
2098        | T![<<=]
2099        | T![-=]
2100        | T![|=]
2101        | T![&=]
2102        | T![^=]
2103        | T![|]
2104        | T![return]
2105        | T![break]
2106        | T![continue]
2107        | T![lifetime_ident] = prev.kind()
2108    {
2109        token = prev
2110    }
2111    token
2112}