pub fn term_search<DB: HirDatabase>(ctx: &TermSearchCtx<'_, DB>) -> Vec<Expr>
Expand description

Search for terms (expressions) that unify with the goal type.

§Arguments

  • ctx - Context for term search

Internally this function uses Breadth First Search to find path to goal type. The general idea is following:

  1. Populate lookup (frontier for BFS) from values (local variables, statics, constants, etc) as well as from well knows values (such as true/false and ())
  2. Iteratively expand the frontier (or contents of the lookup) by trying different type transformation tactics. For example functions take as from set of types (arguments) to some type (return type). Other transformations include methods on type, type constructors and projections to struct fields (field access).
  3. Once we manage to find path to type we are interested in we continue for single round to see if we can find more paths that take us to the goal type.
  4. Return all the paths (type trees) that take us to the goal type.

Note that there are usually more ways we can get to the goal type but some are discarded to reduce the memory consumption. It is also unlikely anyone is willing ti browse through thousands of possible responses so we currently take first 10 from every tactic.