[−][src]Module rustc_typeck::check
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
demandmodule. Theinfermodule 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_typestable, 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 rather 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. When foo() is called, however, T will be
substituted for a fresh type variable N. This variable will
eventually be resolved to some concrete type (which might itself be
type parameter).
Re-exports
pub use self::Expectation::*; |
Modules
| _match | |
| autoderef | Some helper functions for |
| callee | |
| cast | Code for type-checking cast expressions. |
| check | |
| closure | Code for type-checking closure expressions. |
| coercion | Type Coercion |
| compare_method | |
| demand | |
| diverges | |
| dropck | |
| expectation | |
| expr | Type checking expressions. |
| fn_ctxt | |
| gather_locals | |
| generator_interior | 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 |
| inherited | |
| intrinsic | Type-checking for the rust-intrinsic and platform-intrinsic intrinsics that the compiler exposes. |
| method | 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 | |
| regionck | The region check is a final pass that runs over the AST after we have inferred the type constraints but before we have actually finalized the types. Its purpose is to embed a variety of region constraints. Inserting these constraints as a separate pass is good because (1) it localizes the code that has to do with region inference and (2) often we cannot know what constraints are needed until the basic types have been inferred. |
| upvar | Inferring borrow kinds for upvars |
| wfcheck | |
| writeback |
Structs
| BreakableCtxt | |
| CheckItemTypesVisitor | |
| EnclosingBreakables | |
| FnCtxt | |
| GeneratorTypes | When |
| Inherited | Closures defined within the function. For example: |
| InheritedBuilder | Helper type of a temporary returned by |
| LocalTy | The type of a local binding, including the revealed type for anon types. |
| MaybeInProgressTables | A wrapper for |
| UnsafetyState |
Enums
| Diverges | 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). |
| Expectation | When type-checking an expression, we propagate downward
whatever type hint we are able in the form of an |
| FallbackMode | Controls how we perform fallback for unconstrained type variables. |
| Needs | |
| PlaceOp | |
| TupleArgumentsFlag | Controls whether the arguments are tupled. This is used for the call operator. |
Functions
| adt_destructor | |
| bad_non_zero_sized_fields | Emit an error when encountering more or less than one non-zero-sized field in a transparent enum. |
| bad_variant_count | Emit an error when encountering more or less than one variant in a transparent enum. |
| binding_opaque_type_cycle_error | Emit an error for recursive opaque types in a |
| bounds_from_generic_predicates | Resugar |
| check_item_type | |
| check_wf_new | |
| diagnostic_only_typeck | Used only to get |
| fatally_break_rust | |
| fixup_opaque_types | Inspects the substs of opaque types, replacing any inference variables with proper generic parameter from the identity substs. |
| fn_maybe_err | |
| fn_sig_suggestion | Return placeholder code for the given function. |
| get_owner_return_paths | Given a |
| has_typeck_results | |
| maybe_check_static_with_link_section | |
| missing_items_err | |
| potentially_plural_count | |
| primary_body_of | If this |
| provide | |
| report_forbidden_specialization | |
| report_unexpected_variant_res | |
| suggestion_signature | Return placeholder code for the given associated item.
Similar to |
| typeck | |
| typeck_const_arg | |
| typeck_item_bodies | |
| typeck_with_fallback | |
| used_trait_imports |