Skip to main content

hir/term_search/
tactics.rs

1//! Tactics for term search
2//!
3//! All the tactics take following arguments
4//! * `ctx` - Context for the term search
5//! * `defs` - Set of items in scope at term search target location
6//! * `lookup` - Lookup table for types
7//! * `should_continue` - Function that indicates when to stop iterating
8//!
9//! And they return iterator that yields type trees that unify with the `goal` type.
10
11use std::iter;
12
13use hir_ide::db::HirDatabase;
14use itertools::Itertools;
15use rustc_hash::FxHashSet;
16
17use crate::{
18    Adt, AssocItem, BuiltinType, GenericDef, GenericParam, HasAttrs, HasVisibility, Impl,
19    ModuleDef, ScopeDef, Type, TypeParam, term_search::Expr,
20};
21
22use super::{LookupTable, NewTypesKey, TermSearchCtx};
23
24/// # Trivial tactic
25///
26/// Attempts to fulfill the goal by trying items in scope
27/// Also works as a starting point to move all items in scope to lookup table.
28///
29/// # Arguments
30/// * `ctx` - Context for the term search
31/// * `defs` - Set of items in scope at term search target location
32/// * `lookup` - Lookup table for types
33///
34/// Returns iterator that yields elements that unify with `goal`.
35///
36/// _Note that there is no use of calling this tactic in every iteration as the output does not
37/// depend on the current state of `lookup`_
38pub(super) fn trivial<'a, 'lt, 'db, DB: HirDatabase>(
39    ctx: &'a TermSearchCtx<'_, 'db, DB>,
40    defs: &'a FxHashSet<ScopeDef<'db>>,
41    lookup: &'lt mut LookupTable<'db>,
42) -> impl Iterator<Item = Expr<'db>> + use<'a, 'db, 'lt, DB> {
43    let db = ctx.sema.db;
44    defs.iter().filter_map(|def| {
45        let expr = match def {
46            ScopeDef::ModuleDef(ModuleDef::Const(it)) => Some(Expr::Const(*it)),
47            ScopeDef::ModuleDef(ModuleDef::Static(it)) => Some(Expr::Static(*it)),
48            ScopeDef::GenericParam(GenericParam::ConstParam(it)) => Some(Expr::ConstParam(*it)),
49            ScopeDef::Local(it) => Some(Expr::Local(*it)),
50            _ => None,
51        }?;
52
53        let ty = expr.ty(db);
54        if ty.contains_unknown() {
55            return None;
56        }
57
58        lookup.insert(ty.clone(), std::iter::once(expr.clone()));
59
60        ty.instantiate_with_errors(db).could_unify_with_deeply(db, &ctx.goal).then_some(expr)
61    })
62}
63
64/// # Associated constant tactic
65///
66/// Attempts to fulfill the goal by trying constants defined as associated items.
67/// Only considers them on types that are in scope.
68///
69/// # Arguments
70/// * `ctx` - Context for the term search
71/// * `defs` - Set of items in scope at term search target location
72/// * `lookup` - Lookup table for types
73///
74/// Returns iterator that yields elements that unify with `goal`.
75///
76/// _Note that there is no use of calling this tactic in every iteration as the output does not
77/// depend on the current state of `lookup`_
78pub(super) fn assoc_const<'a, 'lt, 'db, DB: HirDatabase>(
79    ctx: &'a TermSearchCtx<'_, 'db, DB>,
80    defs: &'a FxHashSet<ScopeDef<'db>>,
81    lookup: &'lt mut LookupTable<'db>,
82) -> impl Iterator<Item = Expr<'db>> + use<'a, 'db, 'lt, DB> {
83    let db = ctx.sema.db;
84    let module = ctx.scope.module();
85
86    defs.iter()
87        .filter_map(|def| match def {
88            ScopeDef::ModuleDef(ModuleDef::Adt(it)) => Some(it),
89            _ => None,
90        })
91        .flat_map(|it| Impl::all_for_type(db, it.ty(db)))
92        .filter(|it| !it.is_unsafe(db))
93        .flat_map(|it| it.items(db))
94        .filter(move |it| it.is_visible_from(db, module))
95        .filter_map(AssocItem::as_const)
96        .filter_map(|it| {
97            if it.attrs(db).is_unstable() {
98                return None;
99            }
100
101            let expr = Expr::Const(it);
102            let ty = it.ty(db);
103
104            if ty.contains_unknown() {
105                return None;
106            }
107
108            lookup.insert(ty.clone(), std::iter::once(expr.clone()));
109
110            ty.instantiate_with_errors(db).could_unify_with_deeply(db, &ctx.goal).then_some(expr)
111        })
112}
113
114/// # Data constructor tactic
115///
116/// Attempts different data constructors for enums and structs in scope
117///
118/// Updates lookup by new types reached and returns iterator that yields
119/// elements that unify with `goal`.
120///
121/// # Arguments
122/// * `ctx` - Context for the term search
123/// * `defs` - Set of items in scope at term search target location
124/// * `lookup` - Lookup table for types
125/// * `should_continue` - Function that indicates when to stop iterating
126pub(super) fn data_constructor<'a, 'lt, 'db, DB: HirDatabase>(
127    ctx: &'a TermSearchCtx<'_, 'db, DB>,
128    _defs: &'a FxHashSet<ScopeDef<'db>>,
129    lookup: &'lt mut LookupTable<'db>,
130    should_continue: &'a dyn std::ops::Fn() -> bool,
131) -> impl Iterator<Item = Expr<'db>> + use<'a, 'db, 'lt, DB> {
132    let db = ctx.sema.db;
133    let module = ctx.scope.module();
134    lookup
135        .types_wishlist()
136        .clone()
137        .into_iter()
138        .chain(iter::once(ctx.goal.clone()))
139        .filter_map(|ty| ty.as_adt().map(|adt| (adt, ty)))
140        .filter(|_| should_continue())
141        .filter_map(move |(adt, ty)| match adt {
142            Adt::Struct(strukt) => {
143                // Ignore unstable or not visible
144                if strukt.is_unstable(db) || !strukt.is_visible_from(db, module) {
145                    return None;
146                }
147
148                let generics = GenericDef::from(strukt);
149
150                // We currently do not check lifetime bounds so ignore all types that have something to do
151                // with them
152                if !generics.lifetime_params(db).is_empty() {
153                    return None;
154                }
155
156                if ty.contains_unknown() {
157                    return None;
158                }
159
160                let fields = strukt.fields(db);
161                // Check if all fields are visible, otherwise we cannot fill them
162                if fields.iter().any(|it| !it.is_visible_from(db, module)) {
163                    return None;
164                }
165
166                let generics: Vec<_> = ty.type_arguments().collect();
167
168                // Early exit if some param cannot be filled from lookup
169                let param_exprs: Vec<Vec<Expr<'_>>> = fields
170                    .into_iter()
171                    .map(|field| {
172                        lookup.find(&field.ty(db).instantiate(db, generics.iter().cloned()))
173                    })
174                    .collect::<Option<_>>()?;
175
176                // Note that we need special case for 0 param constructors because of multi cartesian
177                // product
178                let exprs: Vec<Expr<'_>> = if param_exprs.is_empty() {
179                    vec![Expr::Struct { strukt, generics, params: Vec::new() }]
180                } else {
181                    param_exprs
182                        .into_iter()
183                        .multi_cartesian_product()
184                        .map(|params| Expr::Struct { strukt, generics: generics.clone(), params })
185                        .collect()
186                };
187
188                lookup.insert(ty.clone(), exprs.iter().cloned());
189                Some((ty, exprs))
190            }
191            Adt::Enum(enum_) => {
192                // Ignore unstable or not visible
193                if enum_.is_unstable(db) || !enum_.is_visible_from(db, module) {
194                    return None;
195                }
196
197                let generics = GenericDef::from(enum_);
198                // We currently do not check lifetime bounds so ignore all types that have something to do
199                // with them
200                if !generics.lifetime_params(db).is_empty() {
201                    return None;
202                }
203
204                if ty.contains_unknown() {
205                    return None;
206                }
207
208                let generics: Vec<_> = ty.type_arguments().collect();
209                let exprs = enum_
210                    .variants(db)
211                    .into_iter()
212                    .filter_map(|variant| {
213                        // Early exit if some param cannot be filled from lookup
214                        let param_exprs: Vec<Vec<Expr<'_>>> = variant
215                            .fields(db)
216                            .into_iter()
217                            .map(|field| {
218                                lookup.find(&field.ty(db).instantiate(db, generics.iter().cloned()))
219                            })
220                            .collect::<Option<_>>()?;
221
222                        // Note that we need special case for 0 param constructors because of multi cartesian
223                        // product
224                        let variant_exprs: Vec<Expr<'_>> = if param_exprs.is_empty() {
225                            vec![Expr::Variant {
226                                variant,
227                                generics: generics.clone(),
228                                params: Vec::new(),
229                            }]
230                        } else {
231                            param_exprs
232                                .into_iter()
233                                .multi_cartesian_product()
234                                .map(|params| Expr::Variant {
235                                    variant,
236                                    generics: generics.clone(),
237                                    params,
238                                })
239                                .collect()
240                        };
241                        lookup.insert(ty.clone(), variant_exprs.iter().cloned());
242                        Some(variant_exprs)
243                    })
244                    .flatten()
245                    .collect();
246
247                Some((ty, exprs))
248            }
249            Adt::Union(_) => None,
250        })
251        .filter_map(|(ty, exprs)| {
252            ty.instantiate_with_errors(db).could_unify_with_deeply(db, &ctx.goal).then_some(exprs)
253        })
254        .flatten()
255}
256
257/// # Free function tactic
258///
259/// Attempts to call different functions in scope with parameters from lookup table.
260/// Functions that include generics are not used for performance reasons.
261///
262/// Updates lookup by new types reached and returns iterator that yields
263/// elements that unify with `goal`.
264///
265/// # Arguments
266/// * `ctx` - Context for the term search
267/// * `defs` - Set of items in scope at term search target location
268/// * `lookup` - Lookup table for types
269/// * `should_continue` - Function that indicates when to stop iterating
270pub(super) fn free_function<'a, 'lt, 'db, DB: HirDatabase>(
271    ctx: &'a TermSearchCtx<'_, 'db, DB>,
272    defs: &'a FxHashSet<ScopeDef<'db>>,
273    lookup: &'lt mut LookupTable<'db>,
274    should_continue: &'a dyn std::ops::Fn() -> bool,
275) -> impl Iterator<Item = Expr<'db>> + use<'a, 'db, 'lt, DB> {
276    let db = ctx.sema.db;
277    let module = ctx.scope.module();
278    defs.iter()
279        .filter_map(move |def| match def {
280            ScopeDef::ModuleDef(ModuleDef::Function(it)) => {
281                let generics = GenericDef::from(*it);
282
283                // Ignore const params for now
284                let type_params = generics
285                    .type_or_const_params(db)
286                    .into_iter()
287                    .map(|it| it.as_type_param(db))
288                    .collect::<Option<Vec<TypeParam>>>()?;
289
290                // Ignore lifetimes as we do not check them
291                if !generics.lifetime_params(db).is_empty() {
292                    return None;
293                }
294
295                // Only account for stable type parameters for now, unstable params can be default
296                // tho, for example in `Box<T, #[unstable] A: Allocator>`
297                if type_params.iter().any(|it| it.is_unstable(db) && it.default(db).is_none()) {
298                    return None;
299                }
300
301                let non_default_type_params_len =
302                    type_params.iter().filter(|it| it.default(db).is_none()).count();
303
304                // Ignore bigger number of generics for now as they kill the performance
305                if non_default_type_params_len > 0 {
306                    return None;
307                }
308
309                let generic_params = lookup
310                    .iter_types()
311                    .collect::<Vec<_>>() // Force take ownership
312                    .into_iter()
313                    .permutations(non_default_type_params_len);
314
315                let exprs: Vec<_> = generic_params
316                    .filter(|_| should_continue())
317                    .filter_map(|generics| {
318                        // Insert default type params
319                        let mut g = generics.into_iter();
320                        let generics: Vec<_> = type_params
321                            .iter()
322                            .map(|it| match it.default(db) {
323                                Some(ty) => Some(ty),
324                                None => {
325                                    let generic = g.next().expect("Missing type param");
326                                    // Filter out generics that do not unify due to trait bounds
327                                    it.ty(db).could_unify_with(db, &generic).then_some(generic)
328                                }
329                            })
330                            .collect::<Option<_>>()?;
331
332                        let ret_ty = it.ret_type(db).instantiate(db, generics.iter().cloned());
333                        // Filter out private and unsafe functions
334                        if !it.is_visible_from(db, module)
335                            || it.is_unsafe_to_call(
336                                db,
337                                None,
338                                crate::Crate::from(ctx.scope.resolver().krate()).edition(db),
339                            )
340                            || it.is_unstable(db)
341                            || ret_ty.is_raw_ptr()
342                        {
343                            return None;
344                        }
345
346                        // Early exit if some param cannot be filled from lookup
347                        let param_exprs: Vec<Vec<Expr<'_>>> = it
348                            .params_without_self(db)
349                            .into_iter()
350                            .map(|field| {
351                                let ty = &field.ty().instantiate(db, &generics);
352                                match ty.is_mutable_reference() {
353                                    true => None,
354                                    false => lookup.find_autoref(db, ty),
355                                }
356                            })
357                            .collect::<Option<_>>()?;
358
359                        // Note that we need special case for 0 param constructors because of multi cartesian
360                        // product
361                        let fn_exprs: Vec<Expr<'_>> = if param_exprs.is_empty() {
362                            vec![Expr::Function { func: *it, generics, params: Vec::new() }]
363                        } else {
364                            param_exprs
365                                .into_iter()
366                                .multi_cartesian_product()
367                                .map(|params| Expr::Function {
368                                    func: *it,
369                                    generics: generics.clone(),
370
371                                    params,
372                                })
373                                .collect()
374                        };
375
376                        lookup.insert(ret_ty.clone(), fn_exprs.iter().cloned());
377                        Some((ret_ty, fn_exprs))
378                    })
379                    .collect();
380                Some(exprs)
381            }
382            _ => None,
383        })
384        .flatten()
385        .filter_map(|(ty, exprs)| {
386            ty.instantiate_with_errors(db).could_unify_with_deeply(db, &ctx.goal).then_some(exprs)
387        })
388        .flatten()
389}
390
391/// # Impl method tactic
392///
393/// Attempts to call methods on types from lookup table.
394/// This includes both functions from direct impl blocks as well as functions from traits.
395/// Methods defined in impl blocks that are generic and methods that are themselves have
396/// generics are ignored for performance reasons.
397///
398/// Updates lookup by new types reached and returns iterator that yields
399/// elements that unify with `goal`.
400///
401/// # Arguments
402/// * `ctx` - Context for the term search
403/// * `defs` - Set of items in scope at term search target location
404/// * `lookup` - Lookup table for types
405/// * `should_continue` - Function that indicates when to stop iterating
406pub(super) fn impl_method<'a, 'lt, 'db, DB: HirDatabase>(
407    ctx: &'a TermSearchCtx<'_, 'db, DB>,
408    _defs: &'a FxHashSet<ScopeDef<'db>>,
409    lookup: &'lt mut LookupTable<'db>,
410    should_continue: &'a dyn std::ops::Fn() -> bool,
411) -> impl Iterator<Item = Expr<'db>> + use<'a, 'db, 'lt, DB> {
412    let db = ctx.sema.db;
413    let module = ctx.scope.module();
414    lookup
415        .new_types(NewTypesKey::ImplMethod)
416        .into_iter()
417        .filter(|ty| !ty.type_arguments().any(|it| it.contains_unknown()))
418        .filter(|_| should_continue())
419        .flat_map(|ty| {
420            Impl::all_for_type(db, ty.clone()).into_iter().map(move |imp| (ty.clone(), imp))
421        })
422        .flat_map(|(ty, imp)| imp.items(db).into_iter().map(move |item| (imp, ty.clone(), item)))
423        .filter_map(|(imp, ty, it)| match it {
424            AssocItem::Function(f) => Some((imp, ty, f)),
425            _ => None,
426        })
427        .filter(|_| should_continue())
428        .filter_map(move |(imp, ty, it)| {
429            let fn_generics = GenericDef::from(it);
430            let imp_generics = GenericDef::from(imp);
431
432            // Ignore all functions that have something to do with lifetimes as we don't check them
433            if !fn_generics.lifetime_params(db).is_empty()
434                || !imp_generics.lifetime_params(db).is_empty()
435            {
436                return None;
437            }
438
439            // Ignore functions without self param
440            if !it.has_self_param(db) {
441                return None;
442            }
443
444            // Filter out private and unsafe functions
445            if !it.is_visible_from(db, module)
446                || it.is_unsafe_to_call(
447                    db,
448                    None,
449                    crate::Crate::from(ctx.scope.resolver().krate()).edition(db),
450                )
451                || it.is_unstable(db)
452            {
453                return None;
454            }
455
456            // Ignore functions with generics for now as they kill the performance
457            // Also checking bounds for generics is problematic
458            if !fn_generics.type_or_const_params(db).is_empty() {
459                return None;
460            }
461
462            let ret_ty = it.ret_type(db).instantiate(db, ty.type_arguments());
463
464            // Ignore functions that do not change the type
465            if ty.instantiate_with_errors(db).could_unify_with_deeply(db, &ret_ty) {
466                return None;
467            }
468
469            let self_ty = it
470                .self_param(db)
471                .expect("No self param")
472                .ty(db)
473                .instantiate(db, ty.type_arguments());
474
475            // Ignore functions that have different self type
476            if !self_ty.autoderef(db).any(|s_ty| ty == s_ty) {
477                return None;
478            }
479
480            let target_type_exprs = lookup.find(&ty).expect("Type not in lookup");
481
482            // Early exit if some param cannot be filled from lookup
483            let param_exprs: Vec<Vec<Expr<'_>>> = it
484                .params_without_self(db)
485                .into_iter()
486                .map(|field| {
487                    lookup.find_autoref(db, &field.ty().instantiate(db, ty.type_arguments()))
488                })
489                .collect::<Option<_>>()?;
490
491            let generics: Vec<_> = ty.type_arguments().collect();
492            let fn_exprs: Vec<Expr<'_>> = std::iter::once(target_type_exprs)
493                .chain(param_exprs)
494                .multi_cartesian_product()
495                .map(|params| {
496                    let mut params = params.into_iter();
497                    let target = Box::new(params.next().unwrap());
498                    Expr::Method {
499                        func: it,
500                        generics: generics.clone(),
501                        target,
502                        params: params.collect(),
503                    }
504                })
505                .collect();
506
507            Some((ret_ty, fn_exprs))
508        })
509        .filter_map(|(ty, exprs)| {
510            ty.instantiate_with_errors(db).could_unify_with_deeply(db, &ctx.goal).then_some(exprs)
511        })
512        .flatten()
513}
514
515/// # Struct projection tactic
516///
517/// Attempts different struct fields (`foo.bar.baz`)
518///
519/// Updates lookup by new types reached and returns iterator that yields
520/// elements that unify with `goal`.
521///
522/// # Arguments
523/// * `ctx` - Context for the term search
524/// * `defs` - Set of items in scope at term search target location
525/// * `lookup` - Lookup table for types
526/// * `should_continue` - Function that indicates when to stop iterating
527pub(super) fn struct_projection<'a, 'lt, 'db, DB: HirDatabase>(
528    ctx: &'a TermSearchCtx<'_, 'db, DB>,
529    _defs: &'a FxHashSet<ScopeDef<'db>>,
530    lookup: &'lt mut LookupTable<'db>,
531    should_continue: &'a dyn std::ops::Fn() -> bool,
532) -> impl Iterator<Item = Expr<'db>> + use<'a, 'db, 'lt, DB> {
533    let db = ctx.sema.db;
534    let module = ctx.scope.module();
535    lookup
536        .new_types(NewTypesKey::StructProjection)
537        .into_iter()
538        .map(|ty| (ty.clone(), lookup.find(&ty).expect("Expr not in lookup")))
539        .filter(|_| should_continue())
540        .flat_map(move |(ty, targets)| {
541            ty.fields(db).into_iter().filter_map(move |(field, filed_ty)| {
542                if !field.is_visible_from(db, module) {
543                    return None;
544                }
545                let exprs = targets
546                    .clone()
547                    .into_iter()
548                    .map(move |target| Expr::Field { field, expr: Box::new(target) });
549                Some((filed_ty, exprs))
550            })
551        })
552        .filter_map(|(ty, exprs)| {
553            ty.instantiate_with_errors(db).could_unify_with_deeply(db, &ctx.goal).then_some(exprs)
554        })
555        .flatten()
556}
557
558/// # Famous types tactic
559///
560/// Attempts different values of well known types such as `true` or `false`.
561///
562/// Updates lookup by new types reached and returns iterator that yields
563/// elements that unify with `goal`.
564///
565/// _Note that there is no point of calling it iteratively as the output is always the same_
566///
567/// # Arguments
568/// * `ctx` - Context for the term search
569/// * `defs` - Set of items in scope at term search target location
570/// * `lookup` - Lookup table for types
571pub(super) fn famous_types<'a, 'lt, 'db, DB: HirDatabase>(
572    ctx: &'a TermSearchCtx<'_, 'db, DB>,
573    _defs: &'a FxHashSet<ScopeDef<'db>>,
574    lookup: &'lt mut LookupTable<'db>,
575) -> impl Iterator<Item = Expr<'db>> + use<'a, 'db, 'lt, DB> {
576    let db = ctx.sema.db;
577    let bool_ty = BuiltinType::bool().ty(db);
578    let unit_ty = Type::new_unit(db);
579    [
580        Expr::FamousType { ty: bool_ty.clone(), value: "true" },
581        Expr::FamousType { ty: bool_ty, value: "false" },
582        Expr::FamousType { ty: unit_ty, value: "()" },
583    ]
584    .into_iter()
585    .inspect(|exprs| {
586        lookup.insert(exprs.ty(db), std::iter::once(exprs.clone()));
587    })
588    .filter(|expr| expr.ty(db).instantiate_with_errors(db).could_unify_with_deeply(db, &ctx.goal))
589}
590
591/// # Impl static method (without self type) tactic
592///
593/// Attempts different functions from impl blocks that take no self parameter.
594///
595/// Updates lookup by new types reached and returns iterator that yields
596/// elements that unify with `goal`.
597///
598/// # Arguments
599/// * `ctx` - Context for the term search
600/// * `defs` - Set of items in scope at term search target location
601/// * `lookup` - Lookup table for types
602/// * `should_continue` - Function that indicates when to stop iterating
603pub(super) fn impl_static_method<'a, 'lt, 'db, DB: HirDatabase>(
604    ctx: &'a TermSearchCtx<'_, 'db, DB>,
605    _defs: &'a FxHashSet<ScopeDef<'db>>,
606    lookup: &'lt mut LookupTable<'db>,
607    should_continue: &'a dyn std::ops::Fn() -> bool,
608) -> impl Iterator<Item = Expr<'db>> + use<'a, 'db, 'lt, DB> {
609    let db = ctx.sema.db;
610    let module = ctx.scope.module();
611    lookup
612        .types_wishlist()
613        .clone()
614        .into_iter()
615        .chain(iter::once(ctx.goal.clone()))
616        .filter(|ty| !ty.type_arguments().any(|it| it.contains_unknown()))
617        .filter(|_| should_continue())
618        .flat_map(|ty| {
619            Impl::all_for_type(db, ty.clone()).into_iter().map(move |imp| (ty.clone(), imp))
620        })
621        .filter(|(_, imp)| !imp.is_unsafe(db))
622        .flat_map(|(ty, imp)| imp.items(db).into_iter().map(move |item| (imp, ty.clone(), item)))
623        .filter_map(|(imp, ty, it)| match it {
624            AssocItem::Function(f) => Some((imp, ty, f)),
625            _ => None,
626        })
627        .filter(|_| should_continue())
628        .filter_map(move |(imp, ty, it)| {
629            let fn_generics = GenericDef::from(it);
630            let imp_generics = GenericDef::from(imp);
631
632            // Ignore all functions that have something to do with lifetimes as we don't check them
633            if !fn_generics.lifetime_params(db).is_empty()
634                || !imp_generics.lifetime_params(db).is_empty()
635            {
636                return None;
637            }
638
639            // Ignore functions with self param
640            if it.has_self_param(db) {
641                return None;
642            }
643
644            // Filter out private and unsafe functions
645            if !it.is_visible_from(db, module)
646                || it.is_unsafe_to_call(
647                    db,
648                    None,
649                    crate::Crate::from(ctx.scope.resolver().krate()).edition(db),
650                )
651                || it.is_unstable(db)
652            {
653                return None;
654            }
655
656            // Ignore functions with generics for now as they kill the performance
657            // Also checking bounds for generics is problematic
658            if !fn_generics.type_or_const_params(db).is_empty() {
659                return None;
660            }
661
662            let ret_ty = it.ret_type(db).instantiate(db, ty.type_arguments());
663
664            // Early exit if some param cannot be filled from lookup
665            let param_exprs: Vec<Vec<Expr<'_>>> = it
666                .params_without_self(db)
667                .into_iter()
668                .map(|field| {
669                    lookup.find_autoref(db, &field.ty().instantiate(db, ty.type_arguments()))
670                })
671                .collect::<Option<_>>()?;
672
673            // Note that we need special case for 0 param constructors because of multi cartesian
674            // product
675            let generics = ty.type_arguments().collect();
676            let fn_exprs: Vec<Expr<'_>> = if param_exprs.is_empty() {
677                vec![Expr::Function { func: it, generics, params: Vec::new() }]
678            } else {
679                param_exprs
680                    .into_iter()
681                    .multi_cartesian_product()
682                    .map(|params| Expr::Function { func: it, generics: generics.clone(), params })
683                    .collect()
684            };
685
686            lookup.insert(ret_ty.clone(), fn_exprs.iter().cloned());
687
688            Some((ret_ty, fn_exprs))
689        })
690        .filter_map(|(ty, exprs)| {
691            ty.instantiate_with_errors(db).could_unify_with_deeply(db, &ctx.goal).then_some(exprs)
692        })
693        .flatten()
694}
695
696/// # Make tuple tactic
697///
698/// Attempts to create tuple types if any are listed in types wishlist
699///
700/// Updates lookup by new types reached and returns iterator that yields
701/// elements that unify with `goal`.
702///
703/// # Arguments
704/// * `ctx` - Context for the term search
705/// * `defs` - Set of items in scope at term search target location
706/// * `lookup` - Lookup table for types
707/// * `should_continue` - Function that indicates when to stop iterating
708pub(super) fn make_tuple<'a, 'lt, 'db, DB: HirDatabase>(
709    ctx: &'a TermSearchCtx<'_, 'db, DB>,
710    _defs: &'a FxHashSet<ScopeDef<'db>>,
711    lookup: &'lt mut LookupTable<'db>,
712    should_continue: &'a dyn std::ops::Fn() -> bool,
713) -> impl Iterator<Item = Expr<'db>> + use<'a, 'db, 'lt, DB> {
714    let db = ctx.sema.db;
715
716    lookup
717        .types_wishlist()
718        .clone()
719        .into_iter()
720        .filter(|_| should_continue())
721        .filter(|ty| ty.is_tuple())
722        .filter_map(move |ty| {
723            // Double check to not contain unknown
724            if ty.contains_unknown() {
725                return None;
726            }
727
728            // Early exit if some param cannot be filled from lookup
729            let param_exprs: Vec<Vec<Expr<'db>>> =
730                ty.type_arguments().map(|field| lookup.find(&field)).collect::<Option<_>>()?;
731
732            let exprs: Vec<Expr<'db>> = param_exprs
733                .into_iter()
734                .multi_cartesian_product()
735                .filter(|_| should_continue())
736                .map(|params| {
737                    let tys: Vec<Type<'_>> = params.iter().map(|it| it.ty(db)).collect();
738                    let tuple_ty = Type::new_tuple(db, &tys);
739
740                    let expr = Expr::Tuple { ty: tuple_ty.clone(), params };
741                    lookup.insert(tuple_ty, iter::once(expr.clone()));
742                    expr
743                })
744                .collect();
745
746            Some(exprs)
747        })
748        .flatten()
749        .filter_map(|expr| {
750            expr.ty(db)
751                .instantiate_with_errors(db)
752                .could_unify_with_deeply(db, &ctx.goal)
753                .then_some(expr)
754        })
755}