Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Tactics for term search

use hir_def::generics::TypeOrConstParamData;
use hir_ty::db::HirDatabase;
use hir_ty::mir::BorrowKind;
use hir_ty::TyBuilder;
use itertools::Itertools;
use rustc_hash::FxHashSet;

use crate::{
    Adt, AssocItem, Enum, GenericParam, HasVisibility, Impl, Module, ModuleDef, ScopeDef, Type,
    Variant,
};

use crate::term_search::TypeTree;

use super::{LookupTable, NewTypesKey, MAX_VARIATIONS};

/// Trivial tactic
///
/// Attempts to fulfill the goal by trying items in scope
/// Also works as a starting point to move all items in scope to lookup table
pub(super) fn trivial<'a>(
    db: &'a dyn HirDatabase,
    defs: &'a FxHashSet<ScopeDef>,
    lookup: &'a mut LookupTable,
    goal: &'a Type,
) -> impl Iterator<Item = TypeTree> + 'a {
    defs.iter().filter_map(|def| {
        let tt = match def {
            ScopeDef::ModuleDef(ModuleDef::Const(it)) => Some(TypeTree::Const(*it)),
            ScopeDef::ModuleDef(ModuleDef::Static(it)) => Some(TypeTree::Static(*it)),
            ScopeDef::GenericParam(GenericParam::ConstParam(it)) => Some(TypeTree::ConstParam(*it)),
            ScopeDef::Local(it) => {
                let borrowck = db.borrowck(it.parent).ok()?;

                let invalid = borrowck.iter().any(|b| {
                    b.partially_moved.iter().any(|moved| {
                        Some(&moved.local) == b.mir_body.binding_locals.get(it.binding_id)
                    }) || b.borrow_regions.iter().any(|region| {
                        // Shared borrows are fine
                        Some(&region.local) == b.mir_body.binding_locals.get(it.binding_id)
                            && region.kind != BorrowKind::Shared
                    })
                });

                if invalid {
                    return None;
                }

                Some(TypeTree::Local(*it))
            }
            _ => None,
        }?;

        lookup.mark_exhausted(*def);

        let ty = tt.ty(db);
        lookup.insert(ty.clone(), std::iter::once(tt.clone()));

        // Don't suggest local references as they are not valid for return
        if matches!(tt, TypeTree::Local(_)) && ty.is_reference() {
            return None;
        }

        ty.could_unify_with_deeply(db, goal).then(|| tt)
    })
}

/// Type constructor tactic
///
/// Attempts different type constructors for enums and structs in scope
///
/// # Arguments
/// * `db` - HIR database
/// * `module` - Module where the term search target location
/// * `defs` - Set of items in scope at term search target location
/// * `lookup` - Lookup table for types
/// * `goal` - Term search target type
pub(super) fn type_constructor<'a>(
    db: &'a dyn HirDatabase,
    module: &'a Module,
    defs: &'a FxHashSet<ScopeDef>,
    lookup: &'a mut LookupTable,
    goal: &'a Type,
) -> impl Iterator<Item = TypeTree> + 'a {
    fn variant_helper(
        db: &dyn HirDatabase,
        lookup: &mut LookupTable,
        parent_enum: Enum,
        variant: Variant,
        goal: &Type,
    ) -> Vec<(Type, Vec<TypeTree>)> {
        let generics = db.generic_params(variant.parent_enum(db).id.into());

        // Ignore enums with const generics
        if generics
            .type_or_consts
            .values()
            .any(|it| matches!(it, TypeOrConstParamData::ConstParamData(_)))
        {
            return Vec::new();
        }

        // We currently do not check lifetime bounds so ignore all types that have something to do
        // with them
        if !generics.lifetimes.is_empty() {
            return Vec::new();
        }

        let generic_params = lookup
            .iter_types()
            .collect::<Vec<_>>() // Force take ownership
            .into_iter()
            .permutations(generics.type_or_consts.len());

        generic_params
            .filter_map(|generics| {
                let enum_ty = parent_enum.ty_with_generics(db, generics.iter().cloned());

                if !generics.is_empty() && !enum_ty.could_unify_with_deeply(db, goal) {
                    return None;
                }

                // Early exit if some param cannot be filled from lookup
                let param_trees: Vec<Vec<TypeTree>> = variant
                    .fields(db)
                    .into_iter()
                    .map(|field| {
                        lookup.find(db, &field.ty_with_generics(db, generics.iter().cloned()))
                    })
                    .collect::<Option<_>>()?;

                // Note that we need special case for 0 param constructors because of multi cartesian
                // product
                let variant_trees: Vec<TypeTree> = if param_trees.is_empty() {
                    vec![TypeTree::Variant {
                        variant,
                        generics: generics.clone(),
                        params: Vec::new(),
                    }]
                } else {
                    param_trees
                        .into_iter()
                        .multi_cartesian_product()
                        .take(MAX_VARIATIONS)
                        .map(|params| TypeTree::Variant {
                            variant,
                            generics: generics.clone(),
                            params,
                        })
                        .collect()
                };
                lookup.insert(enum_ty.clone(), variant_trees.iter().cloned());

                Some((enum_ty, variant_trees))
            })
            .collect()
    }
    defs.iter()
        .filter_map(|def| match def {
            ScopeDef::ModuleDef(ModuleDef::Variant(it)) => {
                let variant_trees = variant_helper(db, lookup, it.parent_enum(db), *it, goal);
                if variant_trees.is_empty() {
                    return None;
                }
                lookup.mark_fulfilled(ScopeDef::ModuleDef(ModuleDef::Variant(*it)));
                Some(variant_trees)
            }
            ScopeDef::ModuleDef(ModuleDef::Adt(Adt::Enum(enum_))) => {
                let trees: Vec<(Type, Vec<TypeTree>)> = enum_
                    .variants(db)
                    .into_iter()
                    .flat_map(|it| variant_helper(db, lookup, enum_.clone(), it, goal))
                    .collect();

                if !trees.is_empty() {
                    lookup.mark_fulfilled(ScopeDef::ModuleDef(ModuleDef::Adt(Adt::Enum(*enum_))));
                }

                Some(trees)
            }
            ScopeDef::ModuleDef(ModuleDef::Adt(Adt::Struct(it))) => {
                let generics = db.generic_params(it.id.into());

                // Ignore enums with const generics
                if generics
                    .type_or_consts
                    .values()
                    .any(|it| matches!(it, TypeOrConstParamData::ConstParamData(_)))
                {
                    return None;
                }

                // We currently do not check lifetime bounds so ignore all types that have something to do
                // with them
                if !generics.lifetimes.is_empty() {
                    return None;
                }

                let generic_params = lookup
                    .iter_types()
                    .collect::<Vec<_>>() // Force take ownership
                    .into_iter()
                    .permutations(generics.type_or_consts.len());

                let trees = generic_params
                    .filter_map(|generics| {
                        let struct_ty = it.ty_with_generics(db, generics.iter().cloned());
                        if !generics.is_empty() && !struct_ty.could_unify_with_deeply(db, goal) {
                            return None;
                        }
                        let fileds = it.fields(db);
                        // Check if all fields are visible, otherwise we cannot fill them
                        if fileds.iter().any(|it| !it.is_visible_from(db, *module)) {
                            return None;
                        }

                        // Early exit if some param cannot be filled from lookup
                        let param_trees: Vec<Vec<TypeTree>> = fileds
                            .into_iter()
                            .map(|field| lookup.find(db, &field.ty(db)))
                            .collect::<Option<_>>()?;

                        // Note that we need special case for 0 param constructors because of multi cartesian
                        // product
                        let struct_trees: Vec<TypeTree> = if param_trees.is_empty() {
                            vec![TypeTree::Struct { strukt: *it, generics, params: Vec::new() }]
                        } else {
                            param_trees
                                .into_iter()
                                .multi_cartesian_product()
                                .take(MAX_VARIATIONS)
                                .map(|params| TypeTree::Struct {
                                    strukt: *it,
                                    generics: generics.clone(),
                                    params,
                                })
                                .collect()
                        };

                        lookup
                            .mark_fulfilled(ScopeDef::ModuleDef(ModuleDef::Adt(Adt::Struct(*it))));
                        lookup.insert(struct_ty.clone(), struct_trees.iter().cloned());

                        Some((struct_ty, struct_trees))
                    })
                    .collect();
                Some(trees)
            }
            _ => None,
        })
        .flatten()
        .filter_map(|(ty, trees)| ty.could_unify_with_deeply(db, goal).then(|| trees))
        .flatten()
}

/// Free function tactic
///
/// Attempts to call different functions in scope with parameters from lookup table
///
/// # Arguments
/// * `db` - HIR database
/// * `module` - Module where the term search target location
/// * `defs` - Set of items in scope at term search target location
/// * `lookup` - Lookup table for types
/// * `goal` - Term search target type
pub(super) fn free_function<'a>(
    db: &'a dyn HirDatabase,
    module: &'a Module,
    defs: &'a FxHashSet<ScopeDef>,
    lookup: &'a mut LookupTable,
    goal: &'a Type,
) -> impl Iterator<Item = TypeTree> + 'a {
    defs.iter()
        .filter_map(|def| match def {
            ScopeDef::ModuleDef(ModuleDef::Function(it)) => {
                let generics = db.generic_params(it.id.into());

                // Skip functions that require const generics
                if generics
                    .type_or_consts
                    .values()
                    .any(|it| matches!(it, TypeOrConstParamData::ConstParamData(_)))
                {
                    return None;
                }

                // Ignore bigger number of generics for now as they kill the performance
                // Ignore lifetimes as we do not check them
                if generics.type_or_consts.len() > 0 || !generics.lifetimes.is_empty() {
                    return None;
                }

                let generic_params = lookup
                    .iter_types()
                    .collect::<Vec<_>>() // Force take ownership
                    .into_iter()
                    .permutations(generics.type_or_consts.len());

                let trees: Vec<_> = generic_params
                    .filter_map(|generics| {
                        let ret_ty = it.ret_type_with_generics(db, generics.iter().cloned());
                        // Filter out private and unsafe functions
                        if !it.is_visible_from(db, *module)
                            || it.is_unsafe_to_call(db)
                            || it.is_unstable(db)
                            || ret_ty.is_reference()
                            || ret_ty.is_raw_ptr()
                        {
                            return None;
                        }

                        // Early exit if some param cannot be filled from lookup
                        let param_trees: Vec<Vec<TypeTree>> = it
                            .params_without_self_with_generics(db, generics.iter().cloned())
                            .into_iter()
                            .map(|field| {
                                let ty = field.ty();
                                match ty.is_mutable_reference() {
                                    true => None,
                                    false => lookup.find_autoref(db, &ty),
                                }
                            })
                            .collect::<Option<_>>()?;

                        // Note that we need special case for 0 param constructors because of multi cartesian
                        // product
                        let fn_trees: Vec<TypeTree> = if param_trees.is_empty() {
                            vec![TypeTree::Function { func: *it, generics, params: Vec::new() }]
                        } else {
                            param_trees
                                .into_iter()
                                .multi_cartesian_product()
                                .take(MAX_VARIATIONS)
                                .map(|params| TypeTree::Function {
                                    func: *it,
                                    generics: generics.clone(),

                                    params,
                                })
                                .collect()
                        };

                        lookup.mark_fulfilled(ScopeDef::ModuleDef(ModuleDef::Function(*it)));
                        lookup.insert(ret_ty.clone(), fn_trees.iter().cloned());
                        Some((ret_ty, fn_trees))
                    })
                    .collect();
                Some(trees)
            }
            _ => None,
        })
        .flatten()
        .filter_map(|(ty, trees)| ty.could_unify_with_deeply(db, goal).then(|| trees))
        .flatten()
}

/// Impl method tactic
///
/// Attempts to to call methods on types from lookup table.
/// This includes both functions from direct impl blocks as well as functions from traits.
///
/// # Arguments
/// * `db` - HIR database
/// * `module` - Module where the term search target location
/// * `defs` - Set of items in scope at term search target location
/// * `lookup` - Lookup table for types
/// * `goal` - Term search target type
pub(super) fn impl_method<'a>(
    db: &'a dyn HirDatabase,
    module: &'a Module,
    _defs: &'a FxHashSet<ScopeDef>,
    lookup: &'a mut LookupTable,
    goal: &'a Type,
) -> impl Iterator<Item = TypeTree> + 'a {
    lookup
        .new_types(NewTypesKey::ImplMethod)
        .into_iter()
        .flat_map(|ty| {
            Impl::all_for_type(db, ty.clone()).into_iter().map(move |imp| (ty.clone(), imp))
        })
        .flat_map(|(ty, imp)| imp.items(db).into_iter().map(move |item| (imp, ty.clone(), item)))
        .filter_map(|(imp, ty, it)| match it {
            AssocItem::Function(f) => Some((imp, ty, f)),
            _ => None,
        })
        .filter_map(|(imp, ty, it)| {
            let fn_generics = db.generic_params(it.id.into());
            let imp_generics = db.generic_params(imp.id.into());

            // Ignore impl if it has const type arguments
            if fn_generics
                .type_or_consts
                .values()
                .any(|it| matches!(it, TypeOrConstParamData::ConstParamData(_)))
                || imp_generics
                    .type_or_consts
                    .values()
                    .any(|it| matches!(it, TypeOrConstParamData::ConstParamData(_)))
            {
                return None;
            }

            // Ignore all functions that have something to do with lifetimes as we don't check them
            if !fn_generics.lifetimes.is_empty() {
                return None;
            }

            // Ignore functions without self param
            if !it.has_self_param(db) {
                return None;
            }

            // Filter out private and unsafe functions
            if !it.is_visible_from(db, *module) || it.is_unsafe_to_call(db) || it.is_unstable(db) {
                return None;
            }

            // Ignore bigger number of generics for now as they kill the performance
            if imp_generics.type_or_consts.len() + fn_generics.type_or_consts.len() > 0 {
                return None;
            }

            let generic_params = lookup
                .iter_types()
                .collect::<Vec<_>>() // Force take ownership
                .into_iter()
                .permutations(imp_generics.type_or_consts.len() + fn_generics.type_or_consts.len());

            let trees: Vec<_> = generic_params
                .filter_map(|generics| {
                    let ret_ty = it.ret_type_with_generics(
                        db,
                        ty.type_arguments().chain(generics.iter().cloned()),
                    );
                    // Filter out functions that return references
                    if ret_ty.is_reference() || ret_ty.is_raw_ptr() {
                        return None;
                    }

                    // Ignore functions that do not change the type
                    if ty.could_unify_with_deeply(db, &ret_ty) {
                        return None;
                    }

                    let self_ty = it
                        .self_param(db)
                        .expect("No self param")
                        .ty_with_generics(db, ty.type_arguments().chain(generics.iter().cloned()));

                    // Ignore functions that have different self type
                    if !self_ty.autoderef(db).any(|s_ty| ty == s_ty) {
                        return None;
                    }

                    let target_type_trees = lookup.find(db, &ty).expect("Type not in lookup");

                    // Early exit if some param cannot be filled from lookup
                    let param_trees: Vec<Vec<TypeTree>> = it
                        .params_without_self_with_generics(
                            db,
                            ty.type_arguments().chain(generics.iter().cloned()),
                        )
                        .into_iter()
                        .map(|field| lookup.find_autoref(db, &field.ty()))
                        .collect::<Option<_>>()?;

                    let fn_trees: Vec<TypeTree> = std::iter::once(target_type_trees)
                        .chain(param_trees.into_iter())
                        .multi_cartesian_product()
                        .take(MAX_VARIATIONS)
                        .map(|params| TypeTree::Function { func: it, generics: Vec::new(), params })
                        .collect();

                    lookup.insert(ret_ty.clone(), fn_trees.iter().cloned());
                    Some((ret_ty, fn_trees))
                })
                .collect();
            Some(trees)
        })
        .flatten()
        .filter_map(|(ty, trees)| ty.could_unify_with_deeply(db, goal).then(|| trees))
        .flatten()
}

/// Struct projection tactic
///
/// Attempts different struct fields
///
/// # Arguments
/// * `db` - HIR database
/// * `module` - Module where the term search target location
/// * `defs` - Set of items in scope at term search target location
/// * `lookup` - Lookup table for types
/// * `goal` - Term search target type
pub(super) fn struct_projection<'a>(
    db: &'a dyn HirDatabase,
    module: &'a Module,
    _defs: &'a FxHashSet<ScopeDef>,
    lookup: &'a mut LookupTable,
    goal: &'a Type,
) -> impl Iterator<Item = TypeTree> + 'a {
    lookup
        .new_types(NewTypesKey::StructProjection)
        .into_iter()
        .map(|ty| (ty.clone(), lookup.find(db, &ty).expect("TypeTree not in lookup")))
        .flat_map(move |(ty, targets)| {
            let module = module.clone();
            ty.fields(db).into_iter().filter_map(move |(field, filed_ty)| {
                if !field.is_visible_from(db, module) {
                    return None;
                }
                let trees = targets
                    .clone()
                    .into_iter()
                    .map(move |target| TypeTree::Field { field, type_tree: Box::new(target) });
                Some((filed_ty, trees))
            })
        })
        .filter_map(|(ty, trees)| ty.could_unify_with_deeply(db, goal).then(|| trees))
        .flatten()
}

/// Famous types tactic
///
/// Attempts different values of well known types such as `true` or `false`
///
/// # Arguments
/// * `db` - HIR database
/// * `module` - Module where the term search target location
/// * `defs` - Set of items in scope at term search target location
/// * `lookup` - Lookup table for types
/// * `goal` - Term search target type
pub(super) fn famous_types<'a>(
    db: &'a dyn HirDatabase,
    module: &'a Module,
    _defs: &'a FxHashSet<ScopeDef>,
    lookup: &'a mut LookupTable,
    goal: &'a Type,
) -> impl Iterator<Item = TypeTree> + 'a {
    [
        TypeTree::FamousType { ty: Type::new(db, module.id, TyBuilder::bool()), value: "true" },
        TypeTree::FamousType { ty: Type::new(db, module.id, TyBuilder::bool()), value: "false" },
        TypeTree::FamousType { ty: Type::new(db, module.id, TyBuilder::unit()), value: "()" },
    ]
    .into_iter()
    .map(|tt| {
        lookup.insert(tt.ty(db), std::iter::once(tt.clone()));
        tt
    })
    .filter(|tt| tt.ty(db).could_unify_with_deeply(db, goal))
}