Expand description

typeck: check phase

Within the check phase of type check, we check each item one at a time (bodies of function expressions are checked as part of the containing function). Inference is used to supply types wherever they are unknown.

By far the most complex case is checking the body of a function. This can be broken down into several distinct phases:

  • gather: creates type variables to represent the type of each local variable and pattern binding.

  • main: the main pass does the lion’s share of the work: it determines the types of all expressions, resolves methods, checks for most invalid conditions, and so forth. In some cases, where a type is unknown, it may create a type or region variable and use that as the type of an expression.

    In the process of checking, various constraints will be placed on these type variables through the subtyping relationships requested through the demand module. The infer module is in charge of resolving those constraints.

  • regionck: after main is complete, the regionck pass goes over all types looking for regions and making sure that they did not escape into places they are not in scope. This may also influence the final assignments of the various region variables if there is some flexibility.

  • writeback: writes the final types within a function body, replacing type variables with their final inferred types. These final types are written into the tcx.node_types table, which should never contain any reference to a type variable.

Intermediate types

While type checking a function, the intermediate types for the expressions, blocks, and so forth contained within the function are stored in fcx.node_types and fcx.node_substs. These types may contain unresolved type variables. After type checking is complete, the functions in the writeback module are used to take the types from this table, resolve them, and then write them into their permanent home in the type context tcx.

This means that during inferencing you should use fcx.write_ty() and fcx.expr_ty() / fcx.node_ty() to write/obtain the types of nodes within the function.

The types of top-level items, which never contain unbound type variables, are stored directly into the tcx typeck_results.

N.B., a type variable is not the same thing as a type parameter. A type variable is an instance of a type parameter. That is, given a generic function fn foo<T>(t: T), while checking the function foo, the type ty_param(0) refers to the type T, which is treated in abstract. However, when foo() is called, T will be substituted for a fresh type variable N. This variable will eventually be resolved to some concrete type (which might itself be a type parameter).

Re-exports

pub use self::Expectation::*;

Modules

autoderef 🔒

Some helper functions for AutoDeref

callee 🔒

Code for type-checking cast expressions.

check 🔒
closure 🔒

Code for type-checking closure expressions.

Type Coercion

diverges 🔒
expr 🔒

Type checking expressions.

fallback 🔒
fn_ctxt 🔒

This calculates the types which has storage which lives across a suspension point in a generator from the perspective of typeck. The actual types used at runtime is calculated in rustc_mir_transform::generator and may be a subset of the types computed here.

inherited 🔒

Type-checking for the rust-intrinsic and platform-intrinsic intrinsics that the compiler exposes.

Method lookup: the secret sauce of Rust. See the rustc dev guide for more information.

op 🔒

Code related to processing overloaded binary and unary operators.

pat 🔒
place_op 🔒
region 🔒

This file builds up the ScopeTree, which describes the parent links in the region hierarchy.

regionck 🔒
upvar 🔒

Inferring borrow kinds for upvars

wfcheck 🔒

Structs

When check_fn is invoked on a generator (i.e., a body that includes yield), it returns back some information about the yield points.

Closures defined within the function. For example:

A temporary returned by Inherited::build(...). This is necessary for multiple InferCtxt to share the same in_progress_typeck_results without using Rc or something similar.

The type of a local binding, including the revealed type for anon types.

A wrapper for InferCtxt’s in_progress_typeck_results field.

Enums

Tracks whether executing a node may exit normally (versus return/break/panic, which “diverge”, leaving dead code in their wake). Tracked semi-automatically (through type variables marked as diverging), with some manual adjustments for control-flow primitives (approximating a CFG).

When type-checking an expression, we propagate downward whatever type hint we are able in the form of an Expectation.

Controls whether the arguments are tupled. This is used for the call operator.

Functions

Emit an error when encountering two or more non-zero-sized fields in a transparent enum.

Emit an error when encountering two or more variants in a transparent enum.

Re-sugar ty::GenericPredicates in a way suitable to be used in structured suggestions.

Used only to get TypeckResults for type inference during error recovery. Currently only used for type inference of statics and consts to avoid type cycle errors.

Return placeholder code for the given function.

Given a DefId for an opaque type in return position, find its parent item’s return expressions.

If this DefId is a “primary tables entry”, returns Some((body_id, body_ty, fn_sig)). Otherwise, returns None.

Suggests calling the constructor of a tuple struct or enum variant

Return placeholder code for the given associated item. Similar to ty::AssocItem::suggestion, but appropriate for use as the code snippet of a structured suggestion.

typeck 🔒