1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 typeck.rs, an introduction
15 The type checker is responsible for:
17 1. Determining the type of each expression
18 2. Resolving methods and traits
19 3. Guaranteeing that most type rules are met ("most?", you say, "why most?"
20 Well, dear reader, read on)
22 The main entry point is `check_crate()`. Type checking operates in
25 1. The collect phase first passes over all items and determines their
26 type, without examining their "innards".
28 2. Variance inference then runs to compute the variance of each parameter
30 3. Coherence checks for overlapping or orphaned impls
32 4. Finally, the check phase then checks function bodies and so forth.
33 Within the check phase, we check each function body one at a time
34 (bodies of function expressions are checked as part of the
35 containing function). Inference is used to supply types wherever
36 they are unknown. The actual checking of a function itself has
37 several phases (check, regionck, writeback), as discussed in the
38 documentation for the `check` module.
40 The type checker is defined into various submodules which are documented
43 - astconv: converts the AST representation of types
44 into the `ty` representation
46 - collect: computes the types of each top-level item and enters them into
47 the `tcx.types` table for later use
49 - coherence: enforces coherence rules, builds some tables
51 - variance: variance inference
53 - check: walks over function bodies and type checks them, inferring types for
54 local variables, type parameters, etc as necessary.
56 - infer: finds the types to use for each type variable such that
57 all subtyping and assignment constraints are met. In essence, the check
58 module specifies the constraints, and the infer module solves them.
62 This API is completely unstable and subject to change.
66 #![crate_name = "rustc_typeck"]
67 #![crate_type = "dylib"]
68 #![crate_type = "rlib"]
69 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
70 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
71 html_root_url = "https://doc.rust-lang.org/nightly/")]
74 #![allow(non_camel_case_types)]
76 #![feature(box_patterns)]
77 #![feature(box_syntax)]
78 #![feature(conservative_impl_trait)]
79 #![feature(loop_break_value)]
80 #![feature(never_type)]
82 #![feature(rustc_diagnostic_macros)]
84 #![cfg_attr(stage0, unstable(feature = "rustc_private", issue = "27812"))]
85 #![cfg_attr(stage0, feature(rustc_private))]
86 #![cfg_attr(stage0, feature(staged_api))]
88 #[macro_use] extern crate log;
89 #[macro_use] extern crate syntax;
90 extern crate syntax_pos;
93 extern crate fmt_macros;
94 #[macro_use] extern crate rustc;
95 extern crate rustc_platform_intrinsics as intrinsics;
96 extern crate rustc_back;
97 extern crate rustc_const_math;
98 extern crate rustc_data_structures;
99 extern crate rustc_errors as errors;
101 pub use rustc::dep_graph;
104 pub use rustc::middle;
105 pub use rustc::session;
108 use hir::map as hir_map;
109 use rustc::infer::InferOk;
110 use rustc::ty::subst::Substs;
111 use rustc::ty::{self, Ty, TyCtxt};
112 use rustc::ty::maps::Providers;
113 use rustc::traits::{FulfillmentContext, ObligationCause, ObligationCauseCode, Reveal};
115 use util::common::time;
118 use syntax::abi::Abi;
119 use syntax_pos::Span;
122 // NB: This module needs to be declared first so diagnostics are
123 // registered before they are used.
130 mod constrained_type_params;
135 pub struct TypeAndSubsts<'tcx> {
136 pub substs: &'tcx Substs<'tcx>,
140 fn require_c_abi_if_variadic(tcx: TyCtxt,
144 if decl.variadic && abi != Abi::C {
145 let mut err = struct_span_err!(tcx.sess, span, E0045,
146 "variadic function must have C calling convention");
147 err.span_label(span, "variadics require C calling conventions")
152 fn require_same_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
153 cause: &ObligationCause<'tcx>,
157 tcx.infer_ctxt((), Reveal::UserFacing).enter(|ref infcx| {
158 let mut fulfill_cx = FulfillmentContext::new();
159 match infcx.eq_types(false, &cause, expected, actual) {
160 Ok(InferOk { obligations, .. }) => {
161 fulfill_cx.register_predicate_obligations(infcx, obligations);
164 infcx.report_mismatched_types(cause, expected, actual, err).emit();
169 match fulfill_cx.select_all_or_error(infcx) {
172 infcx.report_fulfillment_errors(&errors);
179 fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
180 main_id: ast::NodeId,
182 let main_def_id = tcx.hir.local_def_id(main_id);
183 let main_t = tcx.type_of(main_def_id);
186 match tcx.hir.find(main_id) {
187 Some(hir_map::NodeItem(it)) => {
189 hir::ItemFn(.., ref generics, _) => {
190 if generics.is_parameterized() {
191 struct_span_err!(tcx.sess, generics.span, E0131,
192 "main function is not allowed to have type parameters")
193 .span_label(generics.span,
194 "main cannot have type parameters")
204 let substs = tcx.intern_substs(&[]);
205 let se_ty = tcx.mk_fn_def(main_def_id, substs,
206 ty::Binder(tcx.mk_fn_sig(
210 hir::Unsafety::Normal,
217 &ObligationCause::new(main_span, main_id, ObligationCauseCode::MainFunctionType),
223 "main has a non-function type: found `{}`",
229 fn check_start_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
230 start_id: ast::NodeId,
232 let start_def_id = tcx.hir.local_def_id(start_id);
233 let start_t = tcx.type_of(start_def_id);
236 match tcx.hir.find(start_id) {
237 Some(hir_map::NodeItem(it)) => {
239 hir::ItemFn(..,ref ps,_)
240 if ps.is_parameterized() => {
241 struct_span_err!(tcx.sess, ps.span, E0132,
242 "start function is not allowed to have type parameters")
244 "start function cannot have type parameters")
254 let substs = tcx.intern_substs(&[]);
255 let se_ty = tcx.mk_fn_def(start_def_id, substs,
256 ty::Binder(tcx.mk_fn_sig(
259 tcx.mk_imm_ptr(tcx.mk_imm_ptr(tcx.types.u8))
263 hir::Unsafety::Normal,
270 &ObligationCause::new(start_span, start_id, ObligationCauseCode::StartFunctionType),
275 span_bug!(start_span,
276 "start has a non-function type: found `{}`",
282 fn check_for_entry_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
283 if let Some((id, sp)) = *tcx.sess.entry_fn.borrow() {
284 match tcx.sess.entry_type.get() {
285 Some(config::EntryMain) => check_main_fn_ty(tcx, id, sp),
286 Some(config::EntryStart) => check_start_fn_ty(tcx, id, sp),
287 Some(config::EntryNone) => {}
288 None => bug!("entry function without a type")
293 pub fn provide(providers: &mut Providers) {
294 collect::provide(providers);
295 coherence::provide(providers);
296 check::provide(providers);
297 variance::provide(providers);
300 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
301 -> Result<(), usize> {
302 let time_passes = tcx.sess.time_passes();
304 // this ensures that later parts of type checking can assume that items
305 // have valid types and not error
306 tcx.sess.track_errors(|| {
307 time(time_passes, "type collecting", ||
308 collect::collect_item_types(tcx));
312 tcx.sess.track_errors(|| {
313 time(time_passes, "impl wf inference", ||
314 impl_wf_check::impl_wf_check(tcx));
317 tcx.sess.track_errors(|| {
318 time(time_passes, "coherence checking", ||
319 coherence::check_coherence(tcx));
322 tcx.sess.track_errors(|| {
323 time(time_passes, "variance testing", ||
324 variance::test::test_variance(tcx));
327 time(time_passes, "wf checking", || check::check_wf_new(tcx))?;
329 time(time_passes, "item-types checking", || check::check_item_types(tcx))?;
331 time(time_passes, "item-bodies checking", || check::check_item_bodies(tcx))?;
333 check_unused::check_crate(tcx);
334 check_for_entry_fn(tcx);
336 let err_count = tcx.sess.err_count();
344 /// A quasi-deprecated helper used in rustdoc and save-analysis to get
345 /// the type from a HIR node.
346 pub fn hir_ty_to_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, hir_ty: &hir::Ty) -> Ty<'tcx> {
347 // In case there are any projections etc, find the "environment"
348 // def-id that will be used to determine the traits/predicates in
349 // scope. This is derived from the enclosing item-like thing.
350 let env_node_id = tcx.hir.get_parent(hir_ty.id);
351 let env_def_id = tcx.hir.local_def_id(env_node_id);
352 let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id);
353 item_cx.to_ty(hir_ty)
356 __build_diagnostic_array! { librustc_typeck, DIAGNOSTICS }