1 // Copyright 2012-2015 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.
15 Within the check phase of type check, we check each item one at a time
16 (bodies of function expressions are checked as part of the containing
17 function). Inference is used to supply types wherever they are
20 By far the most complex case is checking the body of a function. This
21 can be broken down into several distinct phases:
23 - gather: creates type variables to represent the type of each local
24 variable and pattern binding.
26 - main: the main pass does the lion's share of the work: it
27 determines the types of all expressions, resolves
28 methods, checks for most invalid conditions, and so forth. In
29 some cases, where a type is unknown, it may create a type or region
30 variable and use that as the type of an expression.
32 In the process of checking, various constraints will be placed on
33 these type variables through the subtyping relationships requested
34 through the `demand` module. The `infer` module is in charge
35 of resolving those constraints.
37 - regionck: after main is complete, the regionck pass goes over all
38 types looking for regions and making sure that they did not escape
39 into places they are not in scope. This may also influence the
40 final assignments of the various region variables if there is some
43 - vtable: find and records the impls to use for each trait bound that
44 appears on a type parameter.
46 - writeback: writes the final types within a function body, replacing
47 type variables with their final inferred types. These final types
48 are written into the `tcx.node_types` table, which should *never* contain
49 any reference to a type variable.
53 While type checking a function, the intermediate types for the
54 expressions, blocks, and so forth contained within the function are
55 stored in `fcx.node_types` and `fcx.item_substs`. These types
56 may contain unresolved type variables. After type checking is
57 complete, the functions in the writeback module are used to take the
58 types from this table, resolve them, and then write them into their
59 permanent home in the type context `ccx.tcx`.
61 This means that during inferencing you should use `fcx.write_ty()`
62 and `fcx.expr_ty()` / `fcx.node_ty()` to write/obtain the types of
63 nodes within the function.
65 The types of top-level items, which never contain unbound type
66 variables, are stored directly into the `tcx` tables.
68 n.b.: A type variable is not the same thing as a type parameter. A
69 type variable is rather an "instance" of a type parameter: that is,
70 given a generic function `fn foo<T>(t: T)`: while checking the
71 function `foo`, the type `ty_param(0)` refers to the type `T`, which
72 is treated in abstract. When `foo()` is called, however, `T` will be
73 substituted for a fresh type variable `N`. This variable will
74 eventually be resolved to some concrete type (which might itself be
79 pub use self::Expectation::*;
80 pub use self::compare_method::{compare_impl_method, compare_const_impl};
81 use self::TupleArgumentsFlag::*;
83 use astconv::{AstConv, ast_region_to_region, PathParamMode};
84 use dep_graph::DepNode;
85 use fmt_macros::{Parser, Piece, Position};
86 use middle::cstore::LOCAL_CRATE;
87 use hir::def::{Def, PathResolution};
88 use hir::def_id::DefId;
90 use rustc::infer::{self, InferCtxt, InferOk, TypeOrigin, TypeTrace, type_variable};
91 use rustc::ty::subst::{Subst, Substs};
92 use rustc::traits::{self, Reveal};
93 use rustc::ty::{ParamTy, ParameterEnvironment};
94 use rustc::ty::{LvaluePreference, NoPreference, PreferMutLvalue};
95 use rustc::ty::{self, ToPolyTraitRef, Ty, TyCtxt, Visibility};
96 use rustc::ty::{MethodCall, MethodCallee};
97 use rustc::ty::adjustment;
98 use rustc::ty::fold::{BottomUpFolder, TypeFoldable};
99 use rustc::ty::util::{Representability, IntTypeExt};
100 use require_c_abi_if_variadic;
101 use rscope::{ElisionFailureInfo, RegionScope};
102 use session::{Session, CompileResult};
106 use util::common::{block_query, ErrorReported, indenter, loop_query};
107 use util::nodemap::{DefIdMap, FnvHashMap, NodeMap};
109 use std::cell::{Cell, Ref, RefCell};
110 use std::collections::{HashSet};
111 use std::mem::replace;
113 use syntax::abi::Abi;
116 use syntax::attr::AttrMetaMethods;
117 use syntax::codemap::{self, Spanned};
118 use syntax::feature_gate::{GateIssue, emit_feature_err};
119 use syntax::parse::token::{self, InternedString, keywords};
121 use syntax::util::lev_distance::find_best_match_for_name;
122 use syntax_pos::{self, Span};
123 use errors::DiagnosticBuilder;
125 use rustc::hir::intravisit::{self, Visitor};
126 use rustc::hir::{self, PatKind};
127 use rustc::hir::print as pprust;
128 use rustc_back::slice;
129 use rustc_const_eval::eval_length;
149 /// closures defined within the function. For example:
152 /// bar(move|| { ... })
155 /// Here, the function `foo()` and the closure passed to
156 /// `bar()` will each have their own `FnCtxt`, but they will
157 /// share the inherited fields.
158 pub struct Inherited<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
159 ccx: &'a CrateCtxt<'a, 'gcx>,
160 infcx: InferCtxt<'a, 'gcx, 'tcx>,
161 locals: RefCell<NodeMap<Ty<'tcx>>>,
163 fulfillment_cx: RefCell<traits::FulfillmentContext<'tcx>>,
165 // When we process a call like `c()` where `c` is a closure type,
166 // we may not have decided yet whether `c` is a `Fn`, `FnMut`, or
167 // `FnOnce` closure. In that case, we defer full resolution of the
168 // call until upvar inference can kick in and make the
169 // decision. We keep these deferred resolutions grouped by the
170 // def-id of the closure, so that once we decide, we can easily go
171 // back and process them.
172 deferred_call_resolutions: RefCell<DefIdMap<Vec<DeferredCallResolutionHandler<'gcx, 'tcx>>>>,
174 deferred_cast_checks: RefCell<Vec<cast::CastCheck<'tcx>>>,
176 // Anonymized types found in explicit return types and their
177 // associated fresh inference variable. Writeback resolves these
178 // variables to get the concrete type, which can be used to
179 // deanonymize TyAnon, after typeck is done with all functions.
180 anon_types: RefCell<DefIdMap<Ty<'tcx>>>,
182 // Obligations which will have to be checked at the end of
183 // type-checking, after all functions have been inferred.
184 deferred_obligations: RefCell<Vec<traits::DeferredObligation<'tcx>>>,
187 impl<'a, 'gcx, 'tcx> Deref for Inherited<'a, 'gcx, 'tcx> {
188 type Target = InferCtxt<'a, 'gcx, 'tcx>;
189 fn deref(&self) -> &Self::Target {
194 trait DeferredCallResolution<'gcx, 'tcx> {
195 fn resolve<'a>(&mut self, fcx: &FnCtxt<'a, 'gcx, 'tcx>);
198 type DeferredCallResolutionHandler<'gcx, 'tcx> = Box<DeferredCallResolution<'gcx, 'tcx>+'tcx>;
200 /// When type-checking an expression, we propagate downward
201 /// whatever type hint we are able in the form of an `Expectation`.
202 #[derive(Copy, Clone, Debug)]
203 pub enum Expectation<'tcx> {
204 /// We know nothing about what type this expression should have.
207 /// This expression should have the type given (or some subtype)
208 ExpectHasType(Ty<'tcx>),
210 /// This expression will be cast to the `Ty`
211 ExpectCastableToType(Ty<'tcx>),
213 /// This rvalue expression will be wrapped in `&` or `Box` and coerced
214 /// to `&Ty` or `Box<Ty>`, respectively. `Ty` is `[A]` or `Trait`.
215 ExpectRvalueLikeUnsized(Ty<'tcx>),
218 impl<'a, 'gcx, 'tcx> Expectation<'tcx> {
219 // Disregard "castable to" expectations because they
220 // can lead us astray. Consider for example `if cond
221 // {22} else {c} as u8` -- if we propagate the
222 // "castable to u8" constraint to 22, it will pick the
223 // type 22u8, which is overly constrained (c might not
224 // be a u8). In effect, the problem is that the
225 // "castable to" expectation is not the tightest thing
226 // we can say, so we want to drop it in this case.
227 // The tightest thing we can say is "must unify with
228 // else branch". Note that in the case of a "has type"
229 // constraint, this limitation does not hold.
231 // If the expected type is just a type variable, then don't use
232 // an expected type. Otherwise, we might write parts of the type
233 // when checking the 'then' block which are incompatible with the
235 fn adjust_for_branches(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Expectation<'tcx> {
237 ExpectHasType(ety) => {
238 let ety = fcx.shallow_resolve(ety);
239 if !ety.is_ty_var() {
245 ExpectRvalueLikeUnsized(ety) => {
246 ExpectRvalueLikeUnsized(ety)
252 /// Provide an expectation for an rvalue expression given an *optional*
253 /// hint, which is not required for type safety (the resulting type might
254 /// be checked higher up, as is the case with `&expr` and `box expr`), but
255 /// is useful in determining the concrete type.
257 /// The primary use case is where the expected type is a fat pointer,
258 /// like `&[isize]`. For example, consider the following statement:
260 /// let x: &[isize] = &[1, 2, 3];
262 /// In this case, the expected type for the `&[1, 2, 3]` expression is
263 /// `&[isize]`. If however we were to say that `[1, 2, 3]` has the
264 /// expectation `ExpectHasType([isize])`, that would be too strong --
265 /// `[1, 2, 3]` does not have the type `[isize]` but rather `[isize; 3]`.
266 /// It is only the `&[1, 2, 3]` expression as a whole that can be coerced
267 /// to the type `&[isize]`. Therefore, we propagate this more limited hint,
268 /// which still is useful, because it informs integer literals and the like.
269 /// See the test case `test/run-pass/coerce-expect-unsized.rs` and #20169
270 /// for examples of where this comes up,.
271 fn rvalue_hint(fcx: &FnCtxt<'a, 'gcx, 'tcx>, ty: Ty<'tcx>) -> Expectation<'tcx> {
272 match fcx.tcx.struct_tail(ty).sty {
273 ty::TySlice(_) | ty::TyStr | ty::TyTrait(..) => {
274 ExpectRvalueLikeUnsized(ty)
276 _ => ExpectHasType(ty)
280 // Resolves `expected` by a single level if it is a variable. If
281 // there is no expected type or resolution is not possible (e.g.,
282 // no constraints yet present), just returns `None`.
283 fn resolve(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Expectation<'tcx> {
288 ExpectCastableToType(t) => {
289 ExpectCastableToType(fcx.resolve_type_vars_if_possible(&t))
291 ExpectHasType(t) => {
292 ExpectHasType(fcx.resolve_type_vars_if_possible(&t))
294 ExpectRvalueLikeUnsized(t) => {
295 ExpectRvalueLikeUnsized(fcx.resolve_type_vars_if_possible(&t))
300 fn to_option(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
301 match self.resolve(fcx) {
302 NoExpectation => None,
303 ExpectCastableToType(ty) |
305 ExpectRvalueLikeUnsized(ty) => Some(ty),
309 fn only_has_type(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
310 match self.resolve(fcx) {
311 ExpectHasType(ty) => Some(ty),
317 #[derive(Copy, Clone)]
318 pub struct UnsafetyState {
319 pub def: ast::NodeId,
320 pub unsafety: hir::Unsafety,
321 pub unsafe_push_count: u32,
326 pub fn function(unsafety: hir::Unsafety, def: ast::NodeId) -> UnsafetyState {
327 UnsafetyState { def: def, unsafety: unsafety, unsafe_push_count: 0, from_fn: true }
330 pub fn recurse(&mut self, blk: &hir::Block) -> UnsafetyState {
331 match self.unsafety {
332 // If this unsafe, then if the outer function was already marked as
333 // unsafe we shouldn't attribute the unsafe'ness to the block. This
334 // way the block can be warned about instead of ignoring this
335 // extraneous block (functions are never warned about).
336 hir::Unsafety::Unsafe if self.from_fn => *self,
339 let (unsafety, def, count) = match blk.rules {
340 hir::PushUnsafeBlock(..) =>
341 (unsafety, blk.id, self.unsafe_push_count.checked_add(1).unwrap()),
342 hir::PopUnsafeBlock(..) =>
343 (unsafety, blk.id, self.unsafe_push_count.checked_sub(1).unwrap()),
344 hir::UnsafeBlock(..) =>
345 (hir::Unsafety::Unsafe, blk.id, self.unsafe_push_count),
346 hir::DefaultBlock | hir::PushUnstableBlock | hir:: PopUnstableBlock =>
347 (unsafety, self.def, self.unsafe_push_count),
349 UnsafetyState{ def: def,
351 unsafe_push_count: count,
359 pub struct FnCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
360 ast_ty_to_ty_cache: RefCell<NodeMap<Ty<'tcx>>>,
362 body_id: ast::NodeId,
364 // This flag is set to true if, during the writeback phase, we encounter
365 // a type error in this function.
366 writeback_errors: Cell<bool>,
368 // Number of errors that had been reported when we started
369 // checking this function. On exit, if we find that *more* errors
370 // have been reported, we will skip regionck and other work that
371 // expects the types within the function to be consistent.
372 err_count_on_creation: usize,
376 ps: RefCell<UnsafetyState>,
378 inh: &'a Inherited<'a, 'gcx, 'tcx>,
381 impl<'a, 'gcx, 'tcx> Deref for FnCtxt<'a, 'gcx, 'tcx> {
382 type Target = Inherited<'a, 'gcx, 'tcx>;
383 fn deref(&self) -> &Self::Target {
388 /// Helper type of a temporary returned by ccx.inherited(...).
389 /// Necessary because we can't write the following bound:
390 /// F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(Inherited<'b, 'gcx, 'tcx>).
391 pub struct InheritedBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
392 ccx: &'a CrateCtxt<'a, 'gcx>,
393 infcx: infer::InferCtxtBuilder<'a, 'gcx, 'tcx>
396 impl<'a, 'gcx, 'tcx> CrateCtxt<'a, 'gcx> {
397 pub fn inherited(&'a self, id: ast::NodeId)
398 -> InheritedBuilder<'a, 'gcx, 'tcx> {
399 let param_env = ParameterEnvironment::for_item(self.tcx, id);
402 infcx: self.tcx.infer_ctxt(Some(ty::Tables::empty()),
404 Reveal::NotSpecializable)
409 impl<'a, 'gcx, 'tcx> InheritedBuilder<'a, 'gcx, 'tcx> {
410 fn enter<F, R>(&'tcx mut self, f: F) -> R
411 where F: for<'b> FnOnce(Inherited<'b, 'gcx, 'tcx>) -> R
414 self.infcx.enter(|infcx| {
418 fulfillment_cx: RefCell::new(traits::FulfillmentContext::new()),
419 locals: RefCell::new(NodeMap()),
420 deferred_call_resolutions: RefCell::new(DefIdMap()),
421 deferred_cast_checks: RefCell::new(Vec::new()),
422 anon_types: RefCell::new(DefIdMap()),
423 deferred_obligations: RefCell::new(Vec::new()),
429 impl<'a, 'gcx, 'tcx> Inherited<'a, 'gcx, 'tcx> {
430 fn normalize_associated_types_in<T>(&self,
432 body_id: ast::NodeId,
435 where T : TypeFoldable<'tcx>
437 assoc::normalize_associated_types_in(self,
438 &mut self.fulfillment_cx.borrow_mut(),
446 struct CheckItemTypesVisitor<'a, 'tcx: 'a> { ccx: &'a CrateCtxt<'a, 'tcx> }
447 struct CheckItemBodiesVisitor<'a, 'tcx: 'a> { ccx: &'a CrateCtxt<'a, 'tcx> }
449 impl<'a, 'tcx> Visitor<'tcx> for CheckItemTypesVisitor<'a, 'tcx> {
450 fn visit_item(&mut self, i: &'tcx hir::Item) {
451 check_item_type(self.ccx, i);
452 intravisit::walk_item(self, i);
455 fn visit_ty(&mut self, t: &'tcx hir::Ty) {
457 hir::TyFixedLengthVec(_, ref expr) => {
458 check_const_with_type(self.ccx, &expr, self.ccx.tcx.types.usize, expr.id);
463 intravisit::walk_ty(self, t);
467 impl<'a, 'tcx> Visitor<'tcx> for CheckItemBodiesVisitor<'a, 'tcx> {
468 fn visit_item(&mut self, i: &'tcx hir::Item) {
469 check_item_body(self.ccx, i);
473 pub fn check_wf_new(ccx: &CrateCtxt) -> CompileResult {
474 ccx.tcx.sess.track_errors(|| {
475 let mut visit = wfcheck::CheckTypeWellFormedVisitor::new(ccx);
476 ccx.tcx.visit_all_items_in_krate(DepNode::WfCheck, &mut visit);
480 pub fn check_item_types(ccx: &CrateCtxt) -> CompileResult {
481 ccx.tcx.sess.track_errors(|| {
482 let mut visit = CheckItemTypesVisitor { ccx: ccx };
483 ccx.tcx.visit_all_items_in_krate(DepNode::TypeckItemType, &mut visit);
487 pub fn check_item_bodies(ccx: &CrateCtxt) -> CompileResult {
488 ccx.tcx.sess.track_errors(|| {
489 let mut visit = CheckItemBodiesVisitor { ccx: ccx };
490 ccx.tcx.visit_all_items_in_krate(DepNode::TypeckItemBody, &mut visit);
492 // Process deferred obligations, now that all functions
493 // bodies have been fully inferred.
494 for (&item_id, obligations) in ccx.deferred_obligations.borrow().iter() {
495 // Use the same DepNode as for the body of the original function/item.
496 let def_id = ccx.tcx.map.local_def_id(item_id);
497 let _task = ccx.tcx.dep_graph.in_task(DepNode::TypeckItemBody(def_id));
499 let param_env = ParameterEnvironment::for_item(ccx.tcx, item_id);
500 ccx.tcx.infer_ctxt(None, Some(param_env),
501 Reveal::NotSpecializable).enter(|infcx| {
502 let mut fulfillment_cx = traits::FulfillmentContext::new();
503 for obligation in obligations.iter().map(|o| o.to_obligation()) {
504 fulfillment_cx.register_predicate_obligation(&infcx, obligation);
507 if let Err(errors) = fulfillment_cx.select_all_or_error(&infcx) {
508 infcx.report_fulfillment_errors(&errors);
511 if let Err(errors) = fulfillment_cx.select_rfc1592_obligations(&infcx) {
512 infcx.report_fulfillment_errors_as_warnings(&errors, item_id);
519 pub fn check_drop_impls(ccx: &CrateCtxt) -> CompileResult {
520 ccx.tcx.sess.track_errors(|| {
521 let _task = ccx.tcx.dep_graph.in_task(DepNode::Dropck);
522 let drop_trait = match ccx.tcx.lang_items.drop_trait() {
523 Some(id) => ccx.tcx.lookup_trait_def(id), None => { return }
525 drop_trait.for_each_impl(ccx.tcx, |drop_impl_did| {
526 let _task = ccx.tcx.dep_graph.in_task(DepNode::DropckImpl(drop_impl_did));
527 if drop_impl_did.is_local() {
528 match dropck::check_drop_impl(ccx, drop_impl_did) {
531 assert!(ccx.tcx.sess.has_errors());
539 fn check_bare_fn<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
540 decl: &'tcx hir::FnDecl,
541 body: &'tcx hir::Block,
542 fn_id: ast::NodeId) {
543 let raw_fty = ccx.tcx.lookup_item_type(ccx.tcx.map.local_def_id(fn_id)).ty;
544 let fn_ty = match raw_fty.sty {
545 ty::TyFnDef(_, _, f) => f,
546 _ => span_bug!(body.span, "check_bare_fn: function type expected")
549 ccx.inherited(fn_id).enter(|inh| {
550 // Compute the fty from point of view of inside fn.
551 let fn_scope = inh.tcx.region_maps.call_site_extent(fn_id, body.id);
553 fn_ty.sig.subst(inh.tcx, &inh.parameter_environment.free_substs);
555 inh.tcx.liberate_late_bound_regions(fn_scope, &fn_sig);
557 inh.normalize_associated_types_in(body.span, body.id, &fn_sig);
559 let fcx = check_fn(&inh, fn_ty.unsafety, fn_id, &fn_sig, decl, fn_id, body);
561 fcx.select_all_obligations_and_apply_defaults();
562 fcx.closure_analyze_fn(body);
563 fcx.select_obligations_where_possible();
565 fcx.select_all_obligations_or_error(); // Casts can introduce new obligations.
567 fcx.regionck_fn(fn_id, decl, body);
568 fcx.resolve_type_vars_in_fn(decl, body, fn_id);
572 struct GatherLocalsVisitor<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
573 fcx: &'a FnCtxt<'a, 'gcx, 'tcx>
576 impl<'a, 'gcx, 'tcx> GatherLocalsVisitor<'a, 'gcx, 'tcx> {
577 fn assign(&mut self, _span: Span, nid: ast::NodeId, ty_opt: Option<Ty<'tcx>>) -> Ty<'tcx> {
580 // infer the variable's type
581 let var_ty = self.fcx.next_ty_var();
582 self.fcx.locals.borrow_mut().insert(nid, var_ty);
586 // take type that the user specified
587 self.fcx.locals.borrow_mut().insert(nid, typ);
594 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for GatherLocalsVisitor<'a, 'gcx, 'tcx> {
595 // Add explicitly-declared locals.
596 fn visit_local(&mut self, local: &'gcx hir::Local) {
597 let o_ty = match local.ty {
598 Some(ref ty) => Some(self.fcx.to_ty(&ty)),
601 self.assign(local.span, local.id, o_ty);
602 debug!("Local variable {:?} is assigned type {}",
604 self.fcx.ty_to_string(
605 self.fcx.locals.borrow().get(&local.id).unwrap().clone()));
606 intravisit::walk_local(self, local);
609 // Add pattern bindings.
610 fn visit_pat(&mut self, p: &'gcx hir::Pat) {
611 if let PatKind::Binding(_, ref path1, _) = p.node {
612 let var_ty = self.assign(p.span, p.id, None);
614 self.fcx.require_type_is_sized(var_ty, p.span,
615 traits::VariableType(p.id));
617 debug!("Pattern binding {} is assigned to {} with type {:?}",
619 self.fcx.ty_to_string(
620 self.fcx.locals.borrow().get(&p.id).unwrap().clone()),
623 intravisit::walk_pat(self, p);
626 fn visit_block(&mut self, b: &'gcx hir::Block) {
627 // non-obvious: the `blk` variable maps to region lb, so
628 // we have to keep this up-to-date. This
629 // is... unfortunate. It'd be nice to not need this.
630 intravisit::walk_block(self, b);
633 // Since an expr occurs as part of the type fixed size arrays we
634 // need to record the type for that node
635 fn visit_ty(&mut self, t: &'gcx hir::Ty) {
637 hir::TyFixedLengthVec(ref ty, ref count_expr) => {
639 self.fcx.check_expr_with_hint(&count_expr, self.fcx.tcx.types.usize);
641 hir::TyBareFn(ref function_declaration) => {
642 intravisit::walk_fn_decl_nopat(self, &function_declaration.decl);
643 walk_list!(self, visit_lifetime_def, &function_declaration.lifetimes);
645 _ => intravisit::walk_ty(self, t)
649 // Don't descend into the bodies of nested closures
650 fn visit_fn(&mut self, _: intravisit::FnKind<'gcx>, _: &'gcx hir::FnDecl,
651 _: &'gcx hir::Block, _: Span, _: ast::NodeId) { }
654 /// Helper used by check_bare_fn and check_expr_fn. Does the grungy work of checking a function
655 /// body and returns the function context used for that purpose, since in the case of a fn item
656 /// there is still a bit more to do.
659 /// * inherited: other fields inherited from the enclosing fn (if any)
660 fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
661 unsafety: hir::Unsafety,
662 unsafety_id: ast::NodeId,
663 fn_sig: &ty::FnSig<'tcx>,
664 decl: &'gcx hir::FnDecl,
666 body: &'gcx hir::Block)
667 -> FnCtxt<'a, 'gcx, 'tcx>
669 let mut fn_sig = fn_sig.clone();
671 debug!("check_fn(sig={:?}, fn_id={})", fn_sig, fn_id);
673 // Create the function context. This is either derived from scratch or,
674 // in the case of function expressions, based on the outer context.
675 let mut fcx = FnCtxt::new(inherited, fn_sig.output, body.id);
676 *fcx.ps.borrow_mut() = UnsafetyState::function(unsafety, unsafety_id);
678 fcx.require_type_is_sized(fcx.ret_ty, decl.output.span(), traits::ReturnType);
679 fcx.ret_ty = fcx.instantiate_anon_types(&fcx.ret_ty);
680 fn_sig.output = fcx.ret_ty;
683 let mut visit = GatherLocalsVisitor { fcx: &fcx, };
685 // Add formal parameters.
686 for (arg_ty, input) in fn_sig.inputs.iter().zip(&decl.inputs) {
687 // The type of the argument must be well-formed.
689 // NB -- this is now checked in wfcheck, but that
690 // currently only results in warnings, so we issue an
691 // old-style WF obligation here so that we still get the
692 // errors that we used to get.
693 fcx.register_old_wf_obligation(arg_ty, input.ty.span, traits::MiscObligation);
695 // Create type variables for each argument.
696 pat_util::pat_bindings(&input.pat, |_bm, pat_id, sp, _path| {
697 let var_ty = visit.assign(sp, pat_id, None);
698 fcx.require_type_is_sized(var_ty, sp, traits::VariableType(pat_id));
701 // Check the pattern.
702 fcx.check_pat(&input.pat, arg_ty);
703 fcx.write_ty(input.id, arg_ty);
706 visit.visit_block(body);
709 inherited.tables.borrow_mut().liberated_fn_sigs.insert(fn_id, fn_sig);
711 // FIXME(aburka) do we need this special case? and should it be is_uninhabited?
712 let expected = if fcx.ret_ty.is_never() {
715 ExpectHasType(fcx.ret_ty)
717 fcx.check_block_with_expected(body, expected);
722 pub fn check_struct(ccx: &CrateCtxt, id: ast::NodeId, span: Span) {
725 check_representable(tcx, span, id, "struct");
727 if tcx.lookup_simd(ccx.tcx.map.local_def_id(id)) {
728 check_simd(tcx, span, id);
732 pub fn check_item_type<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
733 debug!("check_item_type(it.id={}, it.name={})",
735 ccx.tcx.item_path_str(ccx.tcx.map.local_def_id(it.id)));
736 let _indenter = indenter();
738 // Consts can play a role in type-checking, so they are included here.
739 hir::ItemStatic(_, _, ref e) |
740 hir::ItemConst(_, ref e) => check_const(ccx, &e, it.id),
741 hir::ItemEnum(ref enum_definition, _) => {
742 check_enum_variants(ccx,
744 &enum_definition.variants,
747 hir::ItemFn(..) => {} // entirely within check_item_body
748 hir::ItemImpl(_, _, _, _, _, ref impl_items) => {
749 debug!("ItemImpl {} with id {}", it.name, it.id);
750 let impl_def_id = ccx.tcx.map.local_def_id(it.id);
751 match ccx.tcx.impl_trait_ref(impl_def_id) {
752 Some(impl_trait_ref) => {
753 check_impl_items_against_trait(ccx,
758 let trait_def_id = impl_trait_ref.def_id;
759 check_on_unimplemented(ccx, trait_def_id, it);
764 hir::ItemTrait(..) => {
765 let def_id = ccx.tcx.map.local_def_id(it.id);
766 check_on_unimplemented(ccx, def_id, it);
768 hir::ItemStruct(..) => {
769 check_struct(ccx, it.id, it.span);
771 hir::ItemTy(_, ref generics) => {
772 let pty_ty = ccx.tcx.node_id_to_type(it.id);
773 check_bounds_are_used(ccx, &generics.ty_params, pty_ty);
775 hir::ItemForeignMod(ref m) => {
776 if m.abi == Abi::RustIntrinsic {
777 for item in &m.items {
778 intrinsic::check_intrinsic_type(ccx, item);
780 } else if m.abi == Abi::PlatformIntrinsic {
781 for item in &m.items {
782 intrinsic::check_platform_intrinsic_type(ccx, item);
785 for item in &m.items {
786 let pty = ccx.tcx.lookup_item_type(ccx.tcx.map.local_def_id(item.id));
787 if !pty.generics.types.is_empty() {
788 let mut err = struct_span_err!(ccx.tcx.sess, item.span, E0044,
789 "foreign items may not have type parameters");
790 span_help!(&mut err, item.span,
791 "consider using specialization instead of \
796 if let hir::ForeignItemFn(ref fn_decl, _) = item.node {
797 require_c_abi_if_variadic(ccx.tcx, fn_decl, m.abi, item.span);
802 _ => {/* nothing to do */ }
806 pub fn check_item_body<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
807 debug!("check_item_body(it.id={}, it.name={})",
809 ccx.tcx.item_path_str(ccx.tcx.map.local_def_id(it.id)));
810 let _indenter = indenter();
812 hir::ItemFn(ref decl, _, _, _, _, ref body) => {
813 check_bare_fn(ccx, &decl, &body, it.id);
815 hir::ItemImpl(_, _, _, _, _, ref impl_items) => {
816 debug!("ItemImpl {} with id {}", it.name, it.id);
818 for impl_item in impl_items {
819 match impl_item.node {
820 hir::ImplItemKind::Const(_, ref expr) => {
821 check_const(ccx, &expr, impl_item.id)
823 hir::ImplItemKind::Method(ref sig, ref body) => {
824 check_bare_fn(ccx, &sig.decl, body, impl_item.id);
826 hir::ImplItemKind::Type(_) => {
827 // Nothing to do here.
832 hir::ItemTrait(_, _, _, ref trait_items) => {
833 for trait_item in trait_items {
834 match trait_item.node {
835 hir::ConstTraitItem(_, Some(ref expr)) => {
836 check_const(ccx, &expr, trait_item.id)
838 hir::MethodTraitItem(ref sig, Some(ref body)) => {
839 check_trait_fn_not_const(ccx, trait_item.span, sig.constness);
841 check_bare_fn(ccx, &sig.decl, body, trait_item.id);
843 hir::MethodTraitItem(ref sig, None) => {
844 check_trait_fn_not_const(ccx, trait_item.span, sig.constness);
846 hir::ConstTraitItem(_, None) |
847 hir::TypeTraitItem(..) => {
853 _ => {/* nothing to do */ }
857 fn check_trait_fn_not_const<'a,'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
859 constness: hir::Constness)
862 hir::Constness::NotConst => {
865 hir::Constness::Const => {
866 struct_span_err!(ccx.tcx.sess, span, E0379, "trait fns cannot be declared const")
867 .span_label(span, &format!("trait fns cannot be const"))
873 fn check_on_unimplemented<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
876 let generics = ccx.tcx.lookup_generics(def_id);
877 if let Some(ref attr) = item.attrs.iter().find(|a| {
878 a.check_name("rustc_on_unimplemented")
880 if let Some(ref istring) = attr.value_str() {
881 let parser = Parser::new(&istring);
882 let types = &generics.types;
883 for token in parser {
885 Piece::String(_) => (), // Normal string, no need to check it
886 Piece::NextArgument(a) => match a.position {
887 // `{Self}` is allowed
888 Position::ArgumentNamed(s) if s == "Self" => (),
889 // So is `{A}` if A is a type parameter
890 Position::ArgumentNamed(s) => match types.iter().find(|t| {
895 let name = ccx.tcx.item_name(def_id);
896 span_err!(ccx.tcx.sess, attr.span, E0230,
897 "there is no type parameter \
902 // `{:1}` and `{}` are not to be used
903 Position::ArgumentIs(_) => {
904 span_err!(ccx.tcx.sess, attr.span, E0231,
905 "only named substitution \
906 parameters are allowed");
913 ccx.tcx.sess, attr.span, E0232,
914 "this attribute must have a value")
915 .span_label(attr.span, &format!("attribute requires a value"))
916 .note(&format!("eg `#[rustc_on_unimplemented = \"foo\"]`"))
922 fn report_forbidden_specialization<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
923 impl_item: &hir::ImplItem,
926 let mut err = struct_span_err!(
927 tcx.sess, impl_item.span, E0520,
928 "item `{}` is provided by an `impl` that specializes \
929 another, but the item in the parent `impl` is not \
930 marked `default` and so it cannot be specialized.",
933 match tcx.span_of_impl(parent_impl) {
935 err.span_note(span, "parent implementation is here:");
938 err.note(&format!("parent implementation is in crate `{}`", cname));
945 fn check_specialization_validity<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
946 trait_def: &ty::TraitDef<'tcx>,
948 impl_item: &hir::ImplItem)
950 let ancestors = trait_def.ancestors(impl_id);
952 let parent = match impl_item.node {
953 hir::ImplItemKind::Const(..) => {
954 ancestors.const_defs(tcx, impl_item.name).skip(1).next()
955 .map(|node_item| node_item.map(|parent| parent.defaultness))
957 hir::ImplItemKind::Method(..) => {
958 ancestors.fn_defs(tcx, impl_item.name).skip(1).next()
959 .map(|node_item| node_item.map(|parent| parent.defaultness))
962 hir::ImplItemKind::Type(_) => {
963 ancestors.type_defs(tcx, impl_item.name).skip(1).next()
964 .map(|node_item| node_item.map(|parent| parent.defaultness))
968 if let Some(parent) = parent {
969 if parent.item.is_final() {
970 report_forbidden_specialization(tcx, impl_item, parent.node.def_id());
976 fn check_impl_items_against_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
979 impl_trait_ref: &ty::TraitRef<'tcx>,
980 impl_items: &[hir::ImplItem]) {
981 // If the trait reference itself is erroneous (so the compilation is going
982 // to fail), skip checking the items here -- the `impl_item` table in `tcx`
983 // isn't populated for such impls.
984 if impl_trait_ref.references_error() { return; }
986 // Locate trait definition and items
988 let trait_def = tcx.lookup_trait_def(impl_trait_ref.def_id);
989 let trait_items = tcx.trait_items(impl_trait_ref.def_id);
990 let mut overridden_associated_type = None;
992 // Check existing impl methods to see if they are both present in trait
993 // and compatible with trait signature
994 for impl_item in impl_items {
995 let ty_impl_item = tcx.impl_or_trait_item(tcx.map.local_def_id(impl_item.id));
996 let ty_trait_item = trait_items.iter()
997 .find(|ac| ac.name() == ty_impl_item.name());
999 // Check that impl definition matches trait definition
1000 if let Some(ty_trait_item) = ty_trait_item {
1001 match impl_item.node {
1002 hir::ImplItemKind::Const(..) => {
1003 let impl_const = match ty_impl_item {
1004 ty::ConstTraitItem(ref cti) => cti,
1005 _ => span_bug!(impl_item.span, "non-const impl-item for const")
1008 // Find associated const definition.
1009 if let &ty::ConstTraitItem(ref trait_const) = ty_trait_item {
1010 compare_const_impl(ccx,
1016 let mut err = struct_span_err!(tcx.sess, impl_item.span, E0323,
1017 "item `{}` is an associated const, \
1018 which doesn't match its trait `{:?}`",
1021 err.span_label(impl_item.span, &format!("does not match trait"));
1022 // We can only get the spans from local trait definition
1023 // Same for E0324 and E0325
1024 if let Some(trait_span) = tcx.map.span_if_local(ty_trait_item.def_id()) {
1025 err.span_label(trait_span, &format!("item in trait"));
1030 hir::ImplItemKind::Method(ref sig, ref body) => {
1031 check_trait_fn_not_const(ccx, impl_item.span, sig.constness);
1033 let impl_method = match ty_impl_item {
1034 ty::MethodTraitItem(ref mti) => mti,
1035 _ => span_bug!(impl_item.span, "non-method impl-item for method")
1038 if let &ty::MethodTraitItem(ref trait_method) = ty_trait_item {
1039 compare_impl_method(ccx,
1046 let mut err = struct_span_err!(tcx.sess, impl_item.span, E0324,
1047 "item `{}` is an associated method, \
1048 which doesn't match its trait `{:?}`",
1051 err.span_label(impl_item.span, &format!("does not match trait"));
1052 if let Some(trait_span) = tcx.map.span_if_local(ty_trait_item.def_id()) {
1053 err.span_label(trait_span, &format!("item in trait"));
1058 hir::ImplItemKind::Type(_) => {
1059 let impl_type = match ty_impl_item {
1060 ty::TypeTraitItem(ref tti) => tti,
1061 _ => span_bug!(impl_item.span, "non-type impl-item for type")
1064 if let &ty::TypeTraitItem(ref at) = ty_trait_item {
1065 if let Some(_) = at.ty {
1066 overridden_associated_type = Some(impl_item);
1069 let mut err = struct_span_err!(tcx.sess, impl_item.span, E0325,
1070 "item `{}` is an associated type, \
1071 which doesn't match its trait `{:?}`",
1074 err.span_label(impl_item.span, &format!("does not match trait"));
1075 if let Some(trait_span) = tcx.map.span_if_local(ty_trait_item.def_id()) {
1076 err.span_label(trait_span, &format!("item in trait"));
1084 check_specialization_validity(tcx, trait_def, impl_id, impl_item);
1087 // Check for missing items from trait
1088 let provided_methods = tcx.provided_trait_methods(impl_trait_ref.def_id);
1089 let mut missing_items = Vec::new();
1090 let mut invalidated_items = Vec::new();
1091 let associated_type_overridden = overridden_associated_type.is_some();
1092 for trait_item in trait_items.iter() {
1097 ty::ConstTraitItem(ref associated_const) => {
1098 is_provided = associated_const.has_value;
1099 is_implemented = impl_items.iter().any(|ii| {
1101 hir::ImplItemKind::Const(..) => {
1102 ii.name == associated_const.name
1108 ty::MethodTraitItem(ref trait_method) => {
1109 is_provided = provided_methods.iter().any(|m| m.name == trait_method.name);
1110 is_implemented = trait_def.ancestors(impl_id)
1111 .fn_defs(tcx, trait_method.name)
1113 .map(|node_item| !node_item.node.is_from_trait())
1116 ty::TypeTraitItem(ref trait_assoc_ty) => {
1117 is_provided = trait_assoc_ty.ty.is_some();
1118 is_implemented = trait_def.ancestors(impl_id)
1119 .type_defs(tcx, trait_assoc_ty.name)
1121 .map(|node_item| !node_item.node.is_from_trait())
1126 if !is_implemented {
1128 missing_items.push(trait_item.name());
1129 } else if associated_type_overridden {
1130 invalidated_items.push(trait_item.name());
1135 if !missing_items.is_empty() {
1136 struct_span_err!(tcx.sess, impl_span, E0046,
1137 "not all trait items implemented, missing: `{}`",
1138 missing_items.iter()
1139 .map(|name| name.to_string())
1140 .collect::<Vec<_>>().join("`, `"))
1141 .span_label(impl_span, &format!("missing `{}` in implementation",
1142 missing_items.iter()
1143 .map(|name| name.to_string())
1144 .collect::<Vec<_>>().join("`, `"))
1148 if !invalidated_items.is_empty() {
1149 let invalidator = overridden_associated_type.unwrap();
1150 span_err!(tcx.sess, invalidator.span, E0399,
1151 "the following trait items need to be reimplemented \
1152 as `{}` was overridden: `{}`",
1154 invalidated_items.iter()
1155 .map(|name| name.to_string())
1156 .collect::<Vec<_>>().join("`, `"))
1160 /// Checks a constant with a given type.
1161 fn check_const_with_type<'a, 'tcx>(ccx: &'a CrateCtxt<'a, 'tcx>,
1162 expr: &'tcx hir::Expr,
1163 expected_type: Ty<'tcx>,
1165 ccx.inherited(id).enter(|inh| {
1166 let fcx = FnCtxt::new(&inh, expected_type, expr.id);
1167 fcx.require_type_is_sized(expected_type, expr.span, traits::ConstSized);
1169 // Gather locals in statics (because of block expressions).
1170 // This is technically unnecessary because locals in static items are forbidden,
1171 // but prevents type checking from blowing up before const checking can properly
1173 GatherLocalsVisitor { fcx: &fcx }.visit_expr(expr);
1175 fcx.check_expr_coercable_to_type(expr, expected_type);
1177 fcx.select_all_obligations_and_apply_defaults();
1178 fcx.closure_analyze_const(expr);
1179 fcx.select_obligations_where_possible();
1181 fcx.select_all_obligations_or_error();
1183 fcx.regionck_expr(expr);
1184 fcx.resolve_type_vars_in_expr(expr, id);
1188 fn check_const<'a, 'tcx>(ccx: &CrateCtxt<'a,'tcx>,
1189 expr: &'tcx hir::Expr,
1191 let decl_ty = ccx.tcx.lookup_item_type(ccx.tcx.map.local_def_id(id)).ty;
1192 check_const_with_type(ccx, expr, decl_ty, id);
1195 /// Checks whether a type can be represented in memory. In particular, it
1196 /// identifies types that contain themselves without indirection through a
1197 /// pointer, which would mean their size is unbounded.
1198 pub fn check_representable<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1200 item_id: ast::NodeId,
1201 _designation: &str) -> bool {
1202 let rty = tcx.node_id_to_type(item_id);
1204 // Check that it is possible to represent this type. This call identifies
1205 // (1) types that contain themselves and (2) types that contain a different
1206 // recursive type. It is only necessary to throw an error on those that
1207 // contain themselves. For case 2, there must be an inner type that will be
1208 // caught by case 1.
1209 match rty.is_representable(tcx, sp) {
1210 Representability::SelfRecursive => {
1211 let item_def_id = tcx.map.local_def_id(item_id);
1212 tcx.recursive_type_with_infinite_size_error(item_def_id).emit();
1215 Representability::Representable | Representability::ContainsRecursive => (),
1220 pub fn check_simd<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span, id: ast::NodeId) {
1221 let t = tcx.node_id_to_type(id);
1223 ty::TyStruct(def, substs) => {
1224 let fields = &def.struct_variant().fields;
1225 if fields.is_empty() {
1226 span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty");
1229 let e = fields[0].ty(tcx, substs);
1230 if !fields.iter().all(|f| f.ty(tcx, substs) == e) {
1231 span_err!(tcx.sess, sp, E0076, "SIMD vector should be homogeneous");
1235 ty::TyParam(_) => { /* struct<T>(T, T, T, T) is ok */ }
1236 _ if e.is_machine() => { /* struct(u8, u8, u8, u8) is ok */ }
1238 span_err!(tcx.sess, sp, E0077,
1239 "SIMD vector element type should be machine type");
1248 #[allow(trivial_numeric_casts)]
1249 pub fn check_enum_variants<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
1251 vs: &'tcx [hir::Variant],
1253 let def_id = ccx.tcx.map.local_def_id(id);
1254 let hint = *ccx.tcx.lookup_repr_hints(def_id).get(0).unwrap_or(&attr::ReprAny);
1256 if hint != attr::ReprAny && vs.is_empty() {
1258 ccx.tcx.sess, sp, E0084,
1259 "unsupported representation for zero-variant enum")
1260 .span_label(sp, &format!("unsupported enum representation"))
1264 let repr_type_ty = ccx.tcx.enum_repr_type(Some(&hint)).to_ty(ccx.tcx);
1266 if let Some(ref e) = v.node.disr_expr {
1267 check_const_with_type(ccx, e, repr_type_ty, e.id);
1271 let def_id = ccx.tcx.map.local_def_id(id);
1273 let variants = &ccx.tcx.lookup_adt_def(def_id).variants;
1274 let mut disr_vals: Vec<ty::Disr> = Vec::new();
1275 for (v, variant) in vs.iter().zip(variants.iter()) {
1276 let current_disr_val = variant.disr_val;
1278 // Check for duplicate discriminant values
1279 if let Some(i) = disr_vals.iter().position(|&x| x == current_disr_val) {
1280 let variant_i_node_id = ccx.tcx.map.as_local_node_id(variants[i].did).unwrap();
1281 let variant_i = ccx.tcx.map.expect_variant(variant_i_node_id);
1282 let i_span = match variant_i.node.disr_expr {
1283 Some(ref expr) => expr.span,
1284 None => ccx.tcx.map.span(variant_i_node_id)
1286 let span = match v.node.disr_expr {
1287 Some(ref expr) => expr.span,
1290 struct_span_err!(ccx.tcx.sess, span, E0081,
1291 "discriminant value `{}` already exists", disr_vals[i])
1292 .span_label(i_span, &format!("first use of `{}`", disr_vals[i]))
1293 .span_label(span , &format!("enum already has `{}`", disr_vals[i]))
1296 disr_vals.push(current_disr_val);
1299 check_representable(ccx.tcx, sp, id, "enum");
1302 impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
1303 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
1305 fn ast_ty_to_ty_cache(&self) -> &RefCell<NodeMap<Ty<'tcx>>> {
1306 &self.ast_ty_to_ty_cache
1309 fn get_generics(&self, _: Span, id: DefId)
1310 -> Result<&'tcx ty::Generics<'tcx>, ErrorReported>
1312 Ok(self.tcx().lookup_generics(id))
1315 fn get_item_type_scheme(&self, _: Span, id: DefId)
1316 -> Result<ty::TypeScheme<'tcx>, ErrorReported>
1318 Ok(self.tcx().lookup_item_type(id))
1321 fn get_trait_def(&self, _: Span, id: DefId)
1322 -> Result<&'tcx ty::TraitDef<'tcx>, ErrorReported>
1324 Ok(self.tcx().lookup_trait_def(id))
1327 fn ensure_super_predicates(&self, _: Span, _: DefId) -> Result<(), ErrorReported> {
1328 // all super predicates are ensured during collect pass
1332 fn get_free_substs(&self) -> Option<&Substs<'tcx>> {
1333 Some(&self.parameter_environment.free_substs)
1336 fn get_type_parameter_bounds(&self,
1338 node_id: ast::NodeId)
1339 -> Result<Vec<ty::PolyTraitRef<'tcx>>, ErrorReported>
1341 let def = self.tcx.type_parameter_def(node_id);
1342 let r = self.parameter_environment
1345 .filter_map(|predicate| {
1347 ty::Predicate::Trait(ref data) => {
1348 if data.0.self_ty().is_param(def.index) {
1349 Some(data.to_poly_trait_ref())
1363 fn trait_defines_associated_type_named(&self,
1364 trait_def_id: DefId,
1365 assoc_name: ast::Name)
1368 let trait_def = self.tcx().lookup_trait_def(trait_def_id);
1369 trait_def.associated_type_names.contains(&assoc_name)
1372 fn ty_infer(&self, _span: Span) -> Ty<'tcx> {
1376 fn ty_infer_for_def(&self,
1377 ty_param_def: &ty::TypeParameterDef<'tcx>,
1378 substs: &Substs<'tcx>,
1379 span: Span) -> Ty<'tcx> {
1380 self.type_var_for_def(span, ty_param_def, substs)
1383 fn projected_ty_from_poly_trait_ref(&self,
1385 poly_trait_ref: ty::PolyTraitRef<'tcx>,
1386 item_name: ast::Name)
1389 let (trait_ref, _) =
1390 self.replace_late_bound_regions_with_fresh_var(
1392 infer::LateBoundRegionConversionTime::AssocTypeProjection(item_name),
1395 self.normalize_associated_type(span, trait_ref, item_name)
1398 fn projected_ty(&self,
1400 trait_ref: ty::TraitRef<'tcx>,
1401 item_name: ast::Name)
1404 self.normalize_associated_type(span, trait_ref, item_name)
1407 fn set_tainted_by_errors(&self) {
1408 self.infcx.set_tainted_by_errors()
1412 impl<'a, 'gcx, 'tcx> RegionScope for FnCtxt<'a, 'gcx, 'tcx> {
1413 fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
1414 Some(self.base_object_lifetime_default(span))
1417 fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
1418 // RFC #599 specifies that object lifetime defaults take
1419 // precedence over other defaults. But within a fn body we
1420 // don't have a *default* region, rather we use inference to
1421 // find the *correct* region, which is strictly more general
1422 // (and anyway, within a fn body the right region may not even
1423 // be something the user can write explicitly, since it might
1424 // be some expression).
1425 self.next_region_var(infer::MiscVariable(span))
1428 fn anon_regions(&self, span: Span, count: usize)
1429 -> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
1430 Ok((0..count).map(|_| {
1431 self.next_region_var(infer::MiscVariable(span))
1436 /// Controls whether the arguments are tupled. This is used for the call
1439 /// Tupling means that all call-side arguments are packed into a tuple and
1440 /// passed as a single parameter. For example, if tupling is enabled, this
1443 /// fn f(x: (isize, isize))
1445 /// Can be called as:
1452 #[derive(Clone, Eq, PartialEq)]
1453 enum TupleArgumentsFlag {
1458 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
1459 pub fn new(inh: &'a Inherited<'a, 'gcx, 'tcx>,
1461 body_id: ast::NodeId)
1462 -> FnCtxt<'a, 'gcx, 'tcx> {
1464 ast_ty_to_ty_cache: RefCell::new(NodeMap()),
1466 writeback_errors: Cell::new(false),
1467 err_count_on_creation: inh.tcx.sess.err_count(),
1469 ps: RefCell::new(UnsafetyState::function(hir::Unsafety::Normal, 0)),
1474 pub fn param_env(&self) -> &ty::ParameterEnvironment<'tcx> {
1475 &self.parameter_environment
1478 pub fn sess(&self) -> &Session {
1482 pub fn err_count_since_creation(&self) -> usize {
1483 self.tcx.sess.err_count() - self.err_count_on_creation
1486 /// Resolves type variables in `ty` if possible. Unlike the infcx
1487 /// version (resolve_type_vars_if_possible), this version will
1488 /// also select obligations if it seems useful, in an effort
1489 /// to get more type information.
1490 fn resolve_type_vars_with_obligations(&self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
1491 debug!("resolve_type_vars_with_obligations(ty={:?})", ty);
1493 // No TyInfer()? Nothing needs doing.
1494 if !ty.has_infer_types() {
1495 debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
1499 // If `ty` is a type variable, see whether we already know what it is.
1500 ty = self.resolve_type_vars_if_possible(&ty);
1501 if !ty.has_infer_types() {
1502 debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
1506 // If not, try resolving pending obligations as much as
1507 // possible. This can help substantially when there are
1508 // indirect dependencies that don't seem worth tracking
1510 self.select_obligations_where_possible();
1511 ty = self.resolve_type_vars_if_possible(&ty);
1513 debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
1517 fn record_deferred_call_resolution(&self,
1518 closure_def_id: DefId,
1519 r: DeferredCallResolutionHandler<'gcx, 'tcx>) {
1520 let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
1521 deferred_call_resolutions.entry(closure_def_id).or_insert(vec![]).push(r);
1524 fn remove_deferred_call_resolutions(&self,
1525 closure_def_id: DefId)
1526 -> Vec<DeferredCallResolutionHandler<'gcx, 'tcx>>
1528 let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
1529 deferred_call_resolutions.remove(&closure_def_id).unwrap_or(Vec::new())
1532 pub fn tag(&self) -> String {
1533 let self_ptr: *const FnCtxt = self;
1534 format!("{:?}", self_ptr)
1537 pub fn local_ty(&self, span: Span, nid: ast::NodeId) -> Ty<'tcx> {
1538 match self.locals.borrow().get(&nid) {
1541 span_err!(self.tcx.sess, span, E0513,
1542 "no type for local variable {}",
1550 pub fn write_ty(&self, node_id: ast::NodeId, ty: Ty<'tcx>) {
1551 debug!("write_ty({}, {:?}) in fcx {}",
1552 node_id, ty, self.tag());
1553 self.tables.borrow_mut().node_types.insert(node_id, ty);
1555 // Add adjustments to !-expressions
1557 if let Some(hir::map::NodeExpr(_)) = self.tcx.map.find(node_id) {
1558 let adj = adjustment::AdjustNeverToAny(self.next_diverging_ty_var());
1559 self.write_adjustment(node_id, adj);
1564 pub fn write_substs(&self, node_id: ast::NodeId, substs: ty::ItemSubsts<'tcx>) {
1565 if !substs.substs.is_noop() {
1566 debug!("write_substs({}, {:?}) in fcx {}",
1571 self.tables.borrow_mut().item_substs.insert(node_id, substs);
1575 pub fn write_autoderef_adjustment(&self,
1576 node_id: ast::NodeId,
1578 self.write_adjustment(
1580 adjustment::AdjustDerefRef(adjustment::AutoDerefRef {
1588 pub fn write_adjustment(&self,
1589 node_id: ast::NodeId,
1590 adj: adjustment::AutoAdjustment<'tcx>) {
1591 debug!("write_adjustment(node_id={}, adj={:?})", node_id, adj);
1593 if adj.is_identity() {
1597 self.tables.borrow_mut().adjustments.insert(node_id, adj);
1600 /// Basically whenever we are converting from a type scheme into
1601 /// the fn body space, we always want to normalize associated
1602 /// types as well. This function combines the two.
1603 fn instantiate_type_scheme<T>(&self,
1605 substs: &Substs<'tcx>,
1608 where T : TypeFoldable<'tcx>
1610 let value = value.subst(self.tcx, substs);
1611 let result = self.normalize_associated_types_in(span, &value);
1612 debug!("instantiate_type_scheme(value={:?}, substs={:?}) = {:?}",
1619 /// As `instantiate_type_scheme`, but for the bounds found in a
1620 /// generic type scheme.
1621 fn instantiate_bounds(&self,
1623 substs: &Substs<'tcx>,
1624 bounds: &ty::GenericPredicates<'tcx>)
1625 -> ty::InstantiatedPredicates<'tcx>
1627 let result = bounds.instantiate(self.tcx, substs);
1628 let result = self.normalize_associated_types_in(span, &result.predicates);
1629 debug!("instantiate_bounds(bounds={:?}, substs={:?}) = {:?}",
1633 ty::InstantiatedPredicates {
1638 /// Replace all anonymized types with fresh inference variables
1639 /// and record them for writeback.
1640 fn instantiate_anon_types<T: TypeFoldable<'tcx>>(&self, value: &T) -> T {
1641 value.fold_with(&mut BottomUpFolder { tcx: self.tcx, fldop: |ty| {
1642 if let ty::TyAnon(def_id, substs) = ty.sty {
1643 // Use the same type variable if the exact same TyAnon appears more
1644 // than once in the return type (e.g. if it's pased to a type alias).
1645 if let Some(ty_var) = self.anon_types.borrow().get(&def_id) {
1648 let ty_var = self.next_ty_var();
1649 self.anon_types.borrow_mut().insert(def_id, ty_var);
1651 let item_predicates = self.tcx.lookup_predicates(def_id);
1652 let bounds = item_predicates.instantiate(self.tcx, substs);
1654 let span = self.tcx.map.def_id_span(def_id, codemap::DUMMY_SP);
1655 for predicate in bounds.predicates {
1656 // Change the predicate to refer to the type variable,
1657 // which will be the concrete type, instead of the TyAnon.
1658 // This also instantiates nested `impl Trait`.
1659 let predicate = self.instantiate_anon_types(&predicate);
1661 // Require that the predicate holds for the concrete type.
1662 let cause = traits::ObligationCause::new(span, self.body_id,
1663 traits::ReturnType);
1664 self.register_predicate(traits::Obligation::new(cause, predicate));
1674 fn normalize_associated_types_in<T>(&self, span: Span, value: &T) -> T
1675 where T : TypeFoldable<'tcx>
1677 self.inh.normalize_associated_types_in(span, self.body_id, value)
1680 fn normalize_associated_type(&self,
1682 trait_ref: ty::TraitRef<'tcx>,
1683 item_name: ast::Name)
1686 let cause = traits::ObligationCause::new(span,
1688 traits::ObligationCauseCode::MiscObligation);
1691 .normalize_projection_type(self,
1693 trait_ref: trait_ref,
1694 item_name: item_name,
1699 /// Instantiates the type in `did` with the generics in `path` and returns
1700 /// it (registering the necessary trait obligations along the way).
1702 /// Note that this function is only intended to be used with type-paths,
1703 /// not with value-paths.
1704 pub fn instantiate_type_path(&self,
1707 node_id: ast::NodeId)
1709 debug!("instantiate_type_path(did={:?}, path={:?})", did, path);
1710 let mut ty = self.tcx.lookup_item_type(did).ty;
1712 // Tuple variants have fn type even in type namespace, extract true variant type from it
1713 ty = self.tcx.no_late_bound_regions(&ty.fn_ret()).unwrap();
1715 let type_predicates = self.tcx.lookup_predicates(did);
1716 let substs = AstConv::ast_path_substs_for_ty(self, self,
1718 PathParamMode::Optional,
1720 path.segments.last().unwrap());
1721 debug!("instantiate_type_path: ty={:?} substs={:?}", ty, substs);
1722 let bounds = self.instantiate_bounds(path.span, substs, &type_predicates);
1723 let cause = traits::ObligationCause::new(path.span, self.body_id,
1724 traits::ItemObligation(did));
1725 self.add_obligations_for_parameters(cause, &bounds);
1727 let ty_substituted = self.instantiate_type_scheme(path.span, substs, &ty);
1728 self.write_ty(node_id, ty_substituted);
1729 self.write_substs(node_id, ty::ItemSubsts {
1735 pub fn write_nil(&self, node_id: ast::NodeId) {
1736 self.write_ty(node_id, self.tcx.mk_nil());
1739 pub fn write_never(&self, node_id: ast::NodeId) {
1740 self.write_ty(node_id, self.tcx.types.never);
1743 pub fn write_error(&self, node_id: ast::NodeId) {
1744 self.write_ty(node_id, self.tcx.types.err);
1747 pub fn require_type_meets(&self,
1750 code: traits::ObligationCauseCode<'tcx>,
1751 bound: ty::BuiltinBound)
1753 self.register_builtin_bound(
1756 traits::ObligationCause::new(span, self.body_id, code));
1759 pub fn require_type_is_sized(&self,
1762 code: traits::ObligationCauseCode<'tcx>)
1764 self.require_type_meets(ty, span, code, ty::BoundSized);
1767 pub fn require_expr_have_sized_type(&self,
1769 code: traits::ObligationCauseCode<'tcx>)
1771 self.require_type_is_sized(self.expr_ty(expr), expr.span, code);
1774 pub fn register_builtin_bound(&self,
1776 builtin_bound: ty::BuiltinBound,
1777 cause: traits::ObligationCause<'tcx>)
1779 self.fulfillment_cx.borrow_mut()
1780 .register_builtin_bound(self, ty, builtin_bound, cause);
1783 pub fn register_predicate(&self,
1784 obligation: traits::PredicateObligation<'tcx>)
1786 debug!("register_predicate({:?})",
1790 .register_predicate_obligation(self, obligation);
1793 pub fn to_ty(&self, ast_t: &hir::Ty) -> Ty<'tcx> {
1794 let t = AstConv::ast_ty_to_ty(self, self, ast_t);
1795 self.register_wf_obligation(t, ast_t.span, traits::MiscObligation);
1799 pub fn expr_ty(&self, ex: &hir::Expr) -> Ty<'tcx> {
1800 if let Some(&adjustment::AdjustNeverToAny(ref t))
1801 = self.tables.borrow().adjustments.get(&ex.id) {
1804 match self.tables.borrow().node_types.get(&ex.id) {
1807 bug!("no type for expr in fcx {}", self.tag());
1812 /// Apply `adjustment` to the type of `expr`
1813 pub fn adjust_expr_ty(&self,
1815 adjustment: Option<&adjustment::AutoAdjustment<'tcx>>)
1818 let raw_ty = self.expr_ty(expr);
1819 let raw_ty = self.shallow_resolve(raw_ty);
1820 let resolve_ty = |ty: Ty<'tcx>| self.resolve_type_vars_if_possible(&ty);
1821 raw_ty.adjust(self.tcx, expr.span, expr.id, adjustment, |method_call| {
1822 self.tables.borrow().method_map.get(&method_call)
1823 .map(|method| resolve_ty(method.ty))
1827 pub fn node_ty(&self, id: ast::NodeId) -> Ty<'tcx> {
1828 match self.tables.borrow().node_types.get(&id) {
1830 None if self.err_count_since_creation() != 0 => self.tcx.types.err,
1832 bug!("no type for node {}: {} in fcx {}",
1833 id, self.tcx.map.node_to_string(id),
1839 pub fn item_substs(&self) -> Ref<NodeMap<ty::ItemSubsts<'tcx>>> {
1840 // NOTE: @jroesch this is hack that appears to be fixed on nightly, will monitor if
1841 // it changes when we upgrade the snapshot compiler
1842 fn project_item_susbts<'a, 'tcx>(tables: &'a ty::Tables<'tcx>)
1843 -> &'a NodeMap<ty::ItemSubsts<'tcx>> {
1847 Ref::map(self.tables.borrow(), project_item_susbts)
1850 pub fn opt_node_ty_substs<F>(&self,
1853 F: FnOnce(&ty::ItemSubsts<'tcx>),
1855 match self.tables.borrow().item_substs.get(&id) {
1861 /// Registers an obligation for checking later, during regionck, that the type `ty` must
1862 /// outlive the region `r`.
1863 pub fn register_region_obligation(&self,
1866 cause: traits::ObligationCause<'tcx>)
1868 let mut fulfillment_cx = self.fulfillment_cx.borrow_mut();
1869 fulfillment_cx.register_region_obligation(ty, region, cause);
1872 /// Registers an obligation for checking later, during regionck, that the type `ty` must
1873 /// outlive the region `r`.
1874 pub fn register_wf_obligation(&self,
1877 code: traits::ObligationCauseCode<'tcx>)
1879 // WF obligations never themselves fail, so no real need to give a detailed cause:
1880 let cause = traits::ObligationCause::new(span, self.body_id, code);
1881 self.register_predicate(traits::Obligation::new(cause, ty::Predicate::WellFormed(ty)));
1884 pub fn register_old_wf_obligation(&self,
1887 code: traits::ObligationCauseCode<'tcx>)
1889 // Registers an "old-style" WF obligation that uses the
1890 // implicator code. This is basically a buggy version of
1891 // `register_wf_obligation` that is being kept around
1892 // temporarily just to help with phasing in the newer rules.
1894 // FIXME(#27579) all uses of this should be migrated to register_wf_obligation eventually
1895 let cause = traits::ObligationCause::new(span, self.body_id, code);
1896 self.register_region_obligation(ty, ty::ReEmpty, cause);
1899 /// Registers obligations that all types appearing in `substs` are well-formed.
1900 pub fn add_wf_bounds(&self, substs: &Substs<'tcx>, expr: &hir::Expr)
1902 for &ty in &substs.types {
1903 self.register_wf_obligation(ty, expr.span, traits::MiscObligation);
1907 /// Given a fully substituted set of bounds (`generic_bounds`), and the values with which each
1908 /// type/region parameter was instantiated (`substs`), creates and registers suitable
1909 /// trait/region obligations.
1911 /// For example, if there is a function:
1914 /// fn foo<'a,T:'a>(...)
1917 /// and a reference:
1923 /// Then we will create a fresh region variable `'$0` and a fresh type variable `$1` for `'a`
1924 /// and `T`. This routine will add a region obligation `$1:'$0` and register it locally.
1925 pub fn add_obligations_for_parameters(&self,
1926 cause: traits::ObligationCause<'tcx>,
1927 predicates: &ty::InstantiatedPredicates<'tcx>)
1929 assert!(!predicates.has_escaping_regions());
1931 debug!("add_obligations_for_parameters(predicates={:?})",
1934 for obligation in traits::predicates_for_generics(cause, predicates) {
1935 self.register_predicate(obligation);
1939 // FIXME(arielb1): use this instead of field.ty everywhere
1940 // Only for fields! Returns <none> for methods>
1941 // Indifferent to privacy flags
1942 pub fn field_ty(&self,
1944 field: ty::FieldDef<'tcx>,
1945 substs: &Substs<'tcx>)
1948 self.normalize_associated_types_in(span,
1949 &field.ty(self.tcx, substs))
1952 fn check_casts(&self) {
1953 let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
1954 for cast in deferred_cast_checks.drain(..) {
1959 /// Apply "fallbacks" to some types
1960 /// ! gets replaced with (), unconstrained ints with i32, and unconstrained floats with f64.
1961 fn default_type_parameters(&self) {
1962 use rustc::ty::error::UnconstrainedNumeric::Neither;
1963 use rustc::ty::error::UnconstrainedNumeric::{UnconstrainedInt, UnconstrainedFloat};
1965 // Defaulting inference variables becomes very dubious if we have
1966 // encountered type-checking errors. Therefore, if we think we saw
1967 // some errors in this function, just resolve all uninstanted type
1968 // varibles to TyError.
1969 if self.is_tainted_by_errors() {
1970 for ty in &self.unsolved_variables() {
1971 if let ty::TyInfer(_) = self.shallow_resolve(ty).sty {
1972 debug!("default_type_parameters: defaulting `{:?}` to error", ty);
1973 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx().types.err);
1979 for ty in &self.unsolved_variables() {
1980 let resolved = self.resolve_type_vars_if_possible(ty);
1981 if self.type_var_diverges(resolved) {
1982 debug!("default_type_parameters: defaulting `{:?}` to `!` because it diverges",
1984 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
1985 self.tcx.mk_diverging_default());
1987 match self.type_is_unconstrained_numeric(resolved) {
1988 UnconstrainedInt => {
1989 debug!("default_type_parameters: defaulting `{:?}` to `i32`",
1991 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.i32)
1993 UnconstrainedFloat => {
1994 debug!("default_type_parameters: defaulting `{:?}` to `f32`",
1996 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.f64)
2004 fn select_all_obligations_and_apply_defaults(&self) {
2005 if self.tcx.sess.features.borrow().default_type_parameter_fallback {
2006 self.new_select_all_obligations_and_apply_defaults();
2008 self.old_select_all_obligations_and_apply_defaults();
2012 // Implements old type inference fallback algorithm
2013 fn old_select_all_obligations_and_apply_defaults(&self) {
2014 self.select_obligations_where_possible();
2015 self.default_type_parameters();
2016 self.select_obligations_where_possible();
2019 fn new_select_all_obligations_and_apply_defaults(&self) {
2020 use rustc::ty::error::UnconstrainedNumeric::Neither;
2021 use rustc::ty::error::UnconstrainedNumeric::{UnconstrainedInt, UnconstrainedFloat};
2023 // For the time being this errs on the side of being memory wasteful but provides better
2025 // let type_variables = self.type_variables.clone();
2027 // There is a possibility that this algorithm will have to run an arbitrary number of times
2028 // to terminate so we bound it by the compiler's recursion limit.
2029 for _ in 0..self.tcx.sess.recursion_limit.get() {
2030 // First we try to solve all obligations, it is possible that the last iteration
2031 // has made it possible to make more progress.
2032 self.select_obligations_where_possible();
2034 let mut conflicts = Vec::new();
2036 // Collect all unsolved type, integral and floating point variables.
2037 let unsolved_variables = self.unsolved_variables();
2039 // We must collect the defaults *before* we do any unification. Because we have
2040 // directly attached defaults to the type variables any unification that occurs
2041 // will erase defaults causing conflicting defaults to be completely ignored.
2042 let default_map: FnvHashMap<_, _> =
2045 .filter_map(|t| self.default(t).map(|d| (t, d)))
2048 let mut unbound_tyvars = HashSet::new();
2050 debug!("select_all_obligations_and_apply_defaults: defaults={:?}", default_map);
2052 // We loop over the unsolved variables, resolving them and if they are
2053 // and unconstrainted numeric type we add them to the set of unbound
2054 // variables. We do this so we only apply literal fallback to type
2055 // variables without defaults.
2056 for ty in &unsolved_variables {
2057 let resolved = self.resolve_type_vars_if_possible(ty);
2058 if self.type_var_diverges(resolved) {
2059 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
2060 self.tcx.mk_diverging_default());
2062 match self.type_is_unconstrained_numeric(resolved) {
2063 UnconstrainedInt | UnconstrainedFloat => {
2064 unbound_tyvars.insert(resolved);
2071 // We now remove any numeric types that also have defaults, and instead insert
2072 // the type variable with a defined fallback.
2073 for ty in &unsolved_variables {
2074 if let Some(_default) = default_map.get(ty) {
2075 let resolved = self.resolve_type_vars_if_possible(ty);
2077 debug!("select_all_obligations_and_apply_defaults: \
2078 ty: {:?} with default: {:?}",
2081 match resolved.sty {
2082 ty::TyInfer(ty::TyVar(_)) => {
2083 unbound_tyvars.insert(ty);
2086 ty::TyInfer(ty::IntVar(_)) | ty::TyInfer(ty::FloatVar(_)) => {
2087 unbound_tyvars.insert(ty);
2088 if unbound_tyvars.contains(resolved) {
2089 unbound_tyvars.remove(resolved);
2098 // If there are no more fallbacks to apply at this point we have applied all possible
2099 // defaults and type inference will proceed as normal.
2100 if unbound_tyvars.is_empty() {
2104 // Finally we go through each of the unbound type variables and unify them with
2105 // the proper fallback, reporting a conflicting default error if any of the
2106 // unifications fail. We know it must be a conflicting default because the
2107 // variable would only be in `unbound_tyvars` and have a concrete value if
2108 // it had been solved by previously applying a default.
2110 // We wrap this in a transaction for error reporting, if we detect a conflict
2111 // we will rollback the inference context to its prior state so we can probe
2112 // for conflicts and correctly report them.
2115 let _ = self.commit_if_ok(|_: &infer::CombinedSnapshot| {
2116 for ty in &unbound_tyvars {
2117 if self.type_var_diverges(ty) {
2118 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
2119 self.tcx.mk_diverging_default());
2121 match self.type_is_unconstrained_numeric(ty) {
2122 UnconstrainedInt => {
2123 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.i32)
2125 UnconstrainedFloat => {
2126 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.f64)
2129 if let Some(default) = default_map.get(ty) {
2130 let default = default.clone();
2131 match self.eq_types(false,
2132 TypeOrigin::Misc(default.origin_span),
2134 Ok(InferOk { obligations, .. }) => {
2135 // FIXME(#32730) propagate obligations
2136 assert!(obligations.is_empty())
2139 conflicts.push((*ty, default));
2148 // If there are conflicts we rollback, otherwise commit
2149 if conflicts.len() > 0 {
2156 if conflicts.len() > 0 {
2157 // Loop through each conflicting default, figuring out the default that caused
2158 // a unification failure and then report an error for each.
2159 for (conflict, default) in conflicts {
2160 let conflicting_default =
2161 self.find_conflicting_default(&unbound_tyvars, &default_map, conflict)
2162 .unwrap_or(type_variable::Default {
2163 ty: self.next_ty_var(),
2164 origin_span: syntax_pos::DUMMY_SP,
2165 def_id: self.tcx.map.local_def_id(0) // what do I put here?
2168 // This is to ensure that we elimnate any non-determinism from the error
2169 // reporting by fixing an order, it doesn't matter what order we choose
2170 // just that it is consistent.
2171 let (first_default, second_default) =
2172 if default.def_id < conflicting_default.def_id {
2173 (default, conflicting_default)
2175 (conflicting_default, default)
2179 self.report_conflicting_default_types(
2180 first_default.origin_span,
2187 self.select_obligations_where_possible();
2190 // For use in error handling related to default type parameter fallback. We explicitly
2191 // apply the default that caused conflict first to a local version of the type variable
2192 // table then apply defaults until we find a conflict. That default must be the one
2193 // that caused conflict earlier.
2194 fn find_conflicting_default(&self,
2195 unbound_vars: &HashSet<Ty<'tcx>>,
2196 default_map: &FnvHashMap<&Ty<'tcx>, type_variable::Default<'tcx>>,
2198 -> Option<type_variable::Default<'tcx>> {
2199 use rustc::ty::error::UnconstrainedNumeric::Neither;
2200 use rustc::ty::error::UnconstrainedNumeric::{UnconstrainedInt, UnconstrainedFloat};
2202 // Ensure that we apply the conflicting default first
2203 let mut unbound_tyvars = Vec::with_capacity(unbound_vars.len() + 1);
2204 unbound_tyvars.push(conflict);
2205 unbound_tyvars.extend(unbound_vars.iter());
2207 let mut result = None;
2208 // We run the same code as above applying defaults in order, this time when
2209 // we find the conflict we just return it for error reporting above.
2211 // We also run this inside snapshot that never commits so we can do error
2212 // reporting for more then one conflict.
2213 for ty in &unbound_tyvars {
2214 if self.type_var_diverges(ty) {
2215 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
2216 self.tcx.mk_diverging_default());
2218 match self.type_is_unconstrained_numeric(ty) {
2219 UnconstrainedInt => {
2220 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.i32)
2222 UnconstrainedFloat => {
2223 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.f64)
2226 if let Some(default) = default_map.get(ty) {
2227 let default = default.clone();
2228 match self.eq_types(false,
2229 TypeOrigin::Misc(default.origin_span),
2231 // FIXME(#32730) propagate obligations
2232 Ok(InferOk { obligations, .. }) => assert!(obligations.is_empty()),
2234 result = Some(default);
2246 fn select_all_obligations_or_error(&self) {
2247 debug!("select_all_obligations_or_error");
2249 // upvar inference should have ensured that all deferred call
2250 // resolutions are handled by now.
2251 assert!(self.deferred_call_resolutions.borrow().is_empty());
2253 self.select_all_obligations_and_apply_defaults();
2255 let mut fulfillment_cx = self.fulfillment_cx.borrow_mut();
2257 // Steal the deferred obligations before the fulfillment
2258 // context can turn all of them into errors.
2259 let obligations = fulfillment_cx.take_deferred_obligations();
2260 self.deferred_obligations.borrow_mut().extend(obligations);
2262 match fulfillment_cx.select_all_or_error(self) {
2264 Err(errors) => { self.report_fulfillment_errors(&errors); }
2267 if let Err(ref errors) = fulfillment_cx.select_rfc1592_obligations(self) {
2268 self.report_fulfillment_errors_as_warnings(errors, self.body_id);
2272 /// Select as many obligations as we can at present.
2273 fn select_obligations_where_possible(&self) {
2274 match self.fulfillment_cx.borrow_mut().select_where_possible(self) {
2276 Err(errors) => { self.report_fulfillment_errors(&errors); }
2280 /// For the overloaded lvalue expressions (`*x`, `x[3]`), the trait
2281 /// returns a type of `&T`, but the actual type we assign to the
2282 /// *expression* is `T`. So this function just peels off the return
2283 /// type by one layer to yield `T`.
2284 fn make_overloaded_lvalue_return_type(&self,
2285 method: MethodCallee<'tcx>)
2286 -> ty::TypeAndMut<'tcx>
2288 // extract method return type, which will be &T;
2289 // all LB regions should have been instantiated during method lookup
2290 let ret_ty = method.ty.fn_ret();
2291 let ret_ty = self.tcx.no_late_bound_regions(&ret_ty).unwrap();
2293 // method returns &T, but the type as visible to user is T, so deref
2294 ret_ty.builtin_deref(true, NoPreference).unwrap()
2297 fn lookup_indexing(&self,
2299 base_expr: &'gcx hir::Expr,
2302 lvalue_pref: LvaluePreference)
2303 -> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)>
2305 // FIXME(#18741) -- this is almost but not quite the same as the
2306 // autoderef that normal method probing does. They could likely be
2309 let mut autoderef = self.autoderef(base_expr.span, base_ty);
2311 while let Some((adj_ty, autoderefs)) = autoderef.next() {
2312 if let Some(final_mt) = self.try_index_step(
2313 MethodCall::expr(expr.id),
2314 expr, base_expr, adj_ty, autoderefs,
2315 false, lvalue_pref, idx_ty)
2317 autoderef.finalize(lvalue_pref, Some(base_expr));
2318 return Some(final_mt);
2321 if let ty::TyArray(element_ty, _) = adj_ty.sty {
2322 autoderef.finalize(lvalue_pref, Some(base_expr));
2323 let adjusted_ty = self.tcx.mk_slice(element_ty);
2324 return self.try_index_step(
2325 MethodCall::expr(expr.id), expr, base_expr,
2326 adjusted_ty, autoderefs, true, lvalue_pref, idx_ty);
2329 autoderef.unambiguous_final_ty();
2333 /// To type-check `base_expr[index_expr]`, we progressively autoderef
2334 /// (and otherwise adjust) `base_expr`, looking for a type which either
2335 /// supports builtin indexing or overloaded indexing.
2336 /// This loop implements one step in that search; the autoderef loop
2337 /// is implemented by `lookup_indexing`.
2338 fn try_index_step(&self,
2339 method_call: MethodCall,
2341 base_expr: &'gcx hir::Expr,
2342 adjusted_ty: Ty<'tcx>,
2345 lvalue_pref: LvaluePreference,
2347 -> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)>
2350 debug!("try_index_step(expr={:?}, base_expr.id={:?}, adjusted_ty={:?}, \
2351 autoderefs={}, unsize={}, index_ty={:?})",
2359 let input_ty = self.next_ty_var();
2361 // First, try built-in indexing.
2362 match (adjusted_ty.builtin_index(), &index_ty.sty) {
2363 (Some(ty), &ty::TyUint(ast::UintTy::Us)) | (Some(ty), &ty::TyInfer(ty::IntVar(_))) => {
2364 debug!("try_index_step: success, using built-in indexing");
2365 // If we had `[T; N]`, we should've caught it before unsizing to `[T]`.
2367 self.write_autoderef_adjustment(base_expr.id, autoderefs);
2368 return Some((tcx.types.usize, ty));
2373 // Try `IndexMut` first, if preferred.
2374 let method = match (lvalue_pref, tcx.lang_items.index_mut_trait()) {
2375 (PreferMutLvalue, Some(trait_did)) => {
2376 self.lookup_method_in_trait_adjusted(expr.span,
2378 token::intern("index_mut"),
2383 Some(vec![input_ty]))
2388 // Otherwise, fall back to `Index`.
2389 let method = match (method, tcx.lang_items.index_trait()) {
2390 (None, Some(trait_did)) => {
2391 self.lookup_method_in_trait_adjusted(expr.span,
2393 token::intern("index"),
2398 Some(vec![input_ty]))
2400 (method, _) => method,
2403 // If some lookup succeeds, write callee into table and extract index/element
2404 // type from the method signature.
2405 // If some lookup succeeded, install method in table
2406 method.map(|method| {
2407 debug!("try_index_step: success, using overloaded indexing");
2408 self.tables.borrow_mut().method_map.insert(method_call, method);
2409 (input_ty, self.make_overloaded_lvalue_return_type(method).ty)
2413 fn check_method_argument_types(&self,
2415 method_fn_ty: Ty<'tcx>,
2416 callee_expr: &'gcx hir::Expr,
2417 args_no_rcvr: &'gcx [P<hir::Expr>],
2418 tuple_arguments: TupleArgumentsFlag,
2419 expected: Expectation<'tcx>)
2421 if method_fn_ty.references_error() {
2422 let err_inputs = self.err_args(args_no_rcvr.len());
2424 let err_inputs = match tuple_arguments {
2425 DontTupleArguments => err_inputs,
2426 TupleArguments => vec![self.tcx.mk_tup(err_inputs)],
2429 self.check_argument_types(sp, &err_inputs[..], &[], args_no_rcvr,
2430 false, tuple_arguments);
2433 match method_fn_ty.sty {
2434 ty::TyFnDef(_, _, ref fty) => {
2435 // HACK(eddyb) ignore self in the definition (see above).
2436 let expected_arg_tys = self.expected_types_for_fn_args(sp, expected,
2438 &fty.sig.0.inputs[1..]);
2439 self.check_argument_types(sp, &fty.sig.0.inputs[1..], &expected_arg_tys[..],
2440 args_no_rcvr, fty.sig.0.variadic, tuple_arguments);
2444 span_bug!(callee_expr.span, "method without bare fn type");
2450 /// Generic function that factors out common logic from function calls,
2451 /// method calls and overloaded operators.
2452 fn check_argument_types(&self,
2454 fn_inputs: &[Ty<'tcx>],
2455 expected_arg_tys: &[Ty<'tcx>],
2456 args: &'gcx [P<hir::Expr>],
2458 tuple_arguments: TupleArgumentsFlag) {
2461 // Grab the argument types, supplying fresh type variables
2462 // if the wrong number of arguments were supplied
2463 let supplied_arg_count = if tuple_arguments == DontTupleArguments {
2469 // All the input types from the fn signature must outlive the call
2470 // so as to validate implied bounds.
2471 for &fn_input_ty in fn_inputs {
2472 self.register_wf_obligation(fn_input_ty, sp, traits::MiscObligation);
2475 let mut expected_arg_tys = expected_arg_tys;
2476 let expected_arg_count = fn_inputs.len();
2478 fn parameter_count_error<'tcx>(sess: &Session, sp: Span, fn_inputs: &[Ty<'tcx>],
2479 expected_count: usize, arg_count: usize, error_code: &str,
2481 let mut err = sess.struct_span_err_with_code(sp,
2482 &format!("this function takes {}{} parameter{} but {} parameter{} supplied",
2483 if variadic {"at least "} else {""},
2485 if expected_count == 1 {""} else {"s"},
2487 if arg_count == 1 {" was"} else {"s were"}),
2490 err.span_label(sp, &format!("expected {}{} parameter{}",
2491 if variadic {"at least "} else {""},
2493 if expected_count == 1 {""} else {"s"}));
2495 let input_types = fn_inputs.iter().map(|i| format!("{:?}", i)).collect::<Vec<String>>();
2496 if input_types.len() > 0 {
2497 err.note(&format!("the following parameter type{} expected: {}",
2498 if expected_count == 1 {" was"} else {"s were"},
2499 input_types.join(", ")));
2504 let formal_tys = if tuple_arguments == TupleArguments {
2505 let tuple_type = self.structurally_resolved_type(sp, fn_inputs[0]);
2506 match tuple_type.sty {
2507 ty::TyTuple(arg_types) if arg_types.len() != args.len() => {
2508 parameter_count_error(tcx.sess, sp, fn_inputs, arg_types.len(), args.len(),
2510 expected_arg_tys = &[];
2511 self.err_args(args.len())
2513 ty::TyTuple(arg_types) => {
2514 expected_arg_tys = match expected_arg_tys.get(0) {
2515 Some(&ty) => match ty.sty {
2516 ty::TyTuple(ref tys) => &tys,
2524 span_err!(tcx.sess, sp, E0059,
2525 "cannot use call notation; the first type parameter \
2526 for the function trait is neither a tuple nor unit");
2527 expected_arg_tys = &[];
2528 self.err_args(args.len())
2531 } else if expected_arg_count == supplied_arg_count {
2533 } else if variadic {
2534 if supplied_arg_count >= expected_arg_count {
2537 parameter_count_error(tcx.sess, sp, fn_inputs, expected_arg_count,
2538 supplied_arg_count, "E0060", true);
2539 expected_arg_tys = &[];
2540 self.err_args(supplied_arg_count)
2543 parameter_count_error(tcx.sess, sp, fn_inputs, expected_arg_count, supplied_arg_count,
2545 expected_arg_tys = &[];
2546 self.err_args(supplied_arg_count)
2549 debug!("check_argument_types: formal_tys={:?}",
2550 formal_tys.iter().map(|t| self.ty_to_string(*t)).collect::<Vec<String>>());
2552 // Check the arguments.
2553 // We do this in a pretty awful way: first we typecheck any arguments
2554 // that are not anonymous functions, then we typecheck the anonymous
2555 // functions. This is so that we have more information about the types
2556 // of arguments when we typecheck the functions. This isn't really the
2557 // right way to do this.
2558 let xs = [false, true];
2559 let mut any_diverges = false; // has any of the arguments diverged?
2560 let mut warned = false; // have we already warned about unreachable code?
2561 for check_blocks in &xs {
2562 let check_blocks = *check_blocks;
2563 debug!("check_blocks={}", check_blocks);
2565 // More awful hacks: before we check argument types, try to do
2566 // an "opportunistic" vtable resolution of any trait bounds on
2567 // the call. This helps coercions.
2569 self.select_obligations_where_possible();
2572 // For variadic functions, we don't have a declared type for all of
2573 // the arguments hence we only do our usual type checking with
2574 // the arguments who's types we do know.
2575 let t = if variadic {
2577 } else if tuple_arguments == TupleArguments {
2582 for (i, arg) in args.iter().take(t).enumerate() {
2583 if any_diverges && !warned {
2586 .add_lint(lint::builtin::UNREACHABLE_CODE,
2589 "unreachable expression".to_string());
2592 let is_block = match arg.node {
2593 hir::ExprClosure(..) => true,
2597 if is_block == check_blocks {
2598 debug!("checking the argument");
2599 let formal_ty = formal_tys[i];
2601 // The special-cased logic below has three functions:
2602 // 1. Provide as good of an expected type as possible.
2603 let expected = expected_arg_tys.get(i).map(|&ty| {
2604 Expectation::rvalue_hint(self, ty)
2607 self.check_expr_with_expectation(&arg,
2608 expected.unwrap_or(ExpectHasType(formal_ty)));
2609 // 2. Coerce to the most detailed type that could be coerced
2610 // to, which is `expected_ty` if `rvalue_hint` returns an
2611 // `ExpectHasType(expected_ty)`, or the `formal_ty` otherwise.
2612 let coerce_ty = expected.and_then(|e| e.only_has_type(self));
2613 self.demand_coerce(&arg, coerce_ty.unwrap_or(formal_ty));
2615 // 3. Relate the expected type and the formal one,
2616 // if the expected type was used for the coercion.
2617 coerce_ty.map(|ty| self.demand_suptype(arg.span, formal_ty, ty));
2620 if let Some(&arg_ty) = self.tables.borrow().node_types.get(&arg.id) {
2621 // FIXME(canndrew): This is_never should probably be an is_uninhabited
2622 any_diverges = any_diverges ||
2623 self.type_var_diverges(arg_ty) ||
2627 if any_diverges && !warned {
2628 let parent = self.tcx.map.get_parent_node(args[0].id);
2631 .add_lint(lint::builtin::UNREACHABLE_CODE,
2634 "unreachable call".to_string());
2640 // We also need to make sure we at least write the ty of the other
2641 // arguments which we skipped above.
2643 for arg in args.iter().skip(expected_arg_count) {
2644 self.check_expr(&arg);
2646 // There are a few types which get autopromoted when passed via varargs
2647 // in C but we just error out instead and require explicit casts.
2648 let arg_ty = self.structurally_resolved_type(arg.span,
2649 self.expr_ty(&arg));
2651 ty::TyFloat(ast::FloatTy::F32) => {
2652 self.type_error_message(arg.span, |t| {
2653 format!("can't pass an `{}` to variadic \
2654 function, cast to `c_double`", t)
2657 ty::TyInt(ast::IntTy::I8) | ty::TyInt(ast::IntTy::I16) | ty::TyBool => {
2658 self.type_error_message(arg.span, |t| {
2659 format!("can't pass `{}` to variadic \
2660 function, cast to `c_int`",
2664 ty::TyUint(ast::UintTy::U8) | ty::TyUint(ast::UintTy::U16) => {
2665 self.type_error_message(arg.span, |t| {
2666 format!("can't pass `{}` to variadic \
2667 function, cast to `c_uint`",
2671 ty::TyFnDef(_, _, f) => {
2672 let ptr_ty = self.tcx.mk_fn_ptr(f);
2673 let ptr_ty = self.resolve_type_vars_if_possible(&ptr_ty);
2674 self.type_error_message(arg.span,
2676 format!("can't pass `{}` to variadic \
2677 function, cast to `{}`", t, ptr_ty)
2686 fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> {
2687 (0..len).map(|_| self.tcx.types.err).collect()
2690 fn write_call(&self,
2691 call_expr: &hir::Expr,
2693 self.write_ty(call_expr.id, output);
2696 // AST fragment checking
2699 expected: Expectation<'tcx>)
2705 ast::LitKind::Str(..) => tcx.mk_static_str(),
2706 ast::LitKind::ByteStr(ref v) => {
2707 tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic),
2708 tcx.mk_array(tcx.types.u8, v.len()))
2710 ast::LitKind::Byte(_) => tcx.types.u8,
2711 ast::LitKind::Char(_) => tcx.types.char,
2712 ast::LitKind::Int(_, ast::LitIntType::Signed(t)) => tcx.mk_mach_int(t),
2713 ast::LitKind::Int(_, ast::LitIntType::Unsigned(t)) => tcx.mk_mach_uint(t),
2714 ast::LitKind::Int(_, ast::LitIntType::Unsuffixed) => {
2715 let opt_ty = expected.to_option(self).and_then(|ty| {
2717 ty::TyInt(_) | ty::TyUint(_) => Some(ty),
2718 ty::TyChar => Some(tcx.types.u8),
2719 ty::TyRawPtr(..) => Some(tcx.types.usize),
2720 ty::TyFnDef(..) | ty::TyFnPtr(_) => Some(tcx.types.usize),
2724 opt_ty.unwrap_or_else(
2725 || tcx.mk_int_var(self.next_int_var_id()))
2727 ast::LitKind::Float(_, t) => tcx.mk_mach_float(t),
2728 ast::LitKind::FloatUnsuffixed(_) => {
2729 let opt_ty = expected.to_option(self).and_then(|ty| {
2731 ty::TyFloat(_) => Some(ty),
2735 opt_ty.unwrap_or_else(
2736 || tcx.mk_float_var(self.next_float_var_id()))
2738 ast::LitKind::Bool(_) => tcx.types.bool
2742 fn check_expr_eq_type(&self,
2743 expr: &'gcx hir::Expr,
2744 expected: Ty<'tcx>) {
2745 self.check_expr_with_hint(expr, expected);
2746 self.demand_eqtype(expr.span, expected, self.expr_ty(expr));
2749 pub fn check_expr_has_type(&self,
2750 expr: &'gcx hir::Expr,
2751 expected: Ty<'tcx>) {
2752 self.check_expr_with_hint(expr, expected);
2753 self.demand_suptype(expr.span, expected, self.expr_ty(expr));
2756 fn check_expr_coercable_to_type(&self,
2757 expr: &'gcx hir::Expr,
2758 expected: Ty<'tcx>) {
2759 self.check_expr_with_hint(expr, expected);
2760 self.demand_coerce(expr, expected);
2763 fn check_expr_with_hint(&self, expr: &'gcx hir::Expr,
2764 expected: Ty<'tcx>) {
2765 self.check_expr_with_expectation(expr, ExpectHasType(expected))
2768 fn check_expr_with_expectation(&self,
2769 expr: &'gcx hir::Expr,
2770 expected: Expectation<'tcx>) {
2771 self.check_expr_with_expectation_and_lvalue_pref(expr, expected, NoPreference)
2774 fn check_expr(&self, expr: &'gcx hir::Expr) {
2775 self.check_expr_with_expectation(expr, NoExpectation)
2778 fn check_expr_with_lvalue_pref(&self, expr: &'gcx hir::Expr,
2779 lvalue_pref: LvaluePreference) {
2780 self.check_expr_with_expectation_and_lvalue_pref(expr, NoExpectation, lvalue_pref)
2783 // determine the `self` type, using fresh variables for all variables
2784 // declared on the impl declaration e.g., `impl<A,B> for Vec<(A,B)>`
2785 // would return ($0, $1) where $0 and $1 are freshly instantiated type
2787 pub fn impl_self_ty(&self,
2788 span: Span, // (potential) receiver for this impl
2790 -> TypeAndSubsts<'tcx> {
2791 let ity = self.tcx.lookup_item_type(did);
2792 debug!("impl_self_ty: ity={:?}", ity);
2794 let substs = self.fresh_substs_for_item(span, did);
2795 let substd_ty = self.instantiate_type_scheme(span, &substs, &ity.ty);
2797 TypeAndSubsts { substs: substs, ty: substd_ty }
2800 /// Unifies the return type with the expected type early, for more coercions
2801 /// and forward type information on the argument expressions.
2802 fn expected_types_for_fn_args(&self,
2804 expected_ret: Expectation<'tcx>,
2805 formal_ret: Ty<'tcx>,
2806 formal_args: &[Ty<'tcx>])
2808 let expected_args = expected_ret.only_has_type(self).and_then(|ret_ty| {
2809 self.commit_regions_if_ok(|| {
2810 // Attempt to apply a subtyping relationship between the formal
2811 // return type (likely containing type variables if the function
2812 // is polymorphic) and the expected return type.
2813 // No argument expectations are produced if unification fails.
2814 let origin = TypeOrigin::Misc(call_span);
2815 let ures = self.sub_types(false, origin, formal_ret, ret_ty);
2816 // FIXME(#15760) can't use try! here, FromError doesn't default
2817 // to identity so the resulting type is not constrained.
2819 // FIXME(#32730) propagate obligations
2820 Ok(InferOk { obligations, .. }) => assert!(obligations.is_empty()),
2821 Err(e) => return Err(e),
2824 // Record all the argument types, with the substitutions
2825 // produced from the above subtyping unification.
2826 Ok(formal_args.iter().map(|ty| {
2827 self.resolve_type_vars_if_possible(ty)
2830 }).unwrap_or(vec![]);
2831 debug!("expected_types_for_fn_args(formal={:?} -> {:?}, expected={:?} -> {:?})",
2832 formal_args, formal_ret,
2833 expected_args, expected_ret);
2837 // Checks a method call.
2838 fn check_method_call(&self,
2839 expr: &'gcx hir::Expr,
2840 method_name: Spanned<ast::Name>,
2841 args: &'gcx [P<hir::Expr>],
2843 expected: Expectation<'tcx>,
2844 lvalue_pref: LvaluePreference) {
2845 let rcvr = &args[0];
2846 self.check_expr_with_lvalue_pref(&rcvr, lvalue_pref);
2848 // no need to check for bot/err -- callee does that
2849 let expr_t = self.structurally_resolved_type(expr.span, self.expr_ty(&rcvr));
2851 let tps = tps.iter().map(|ast_ty| self.to_ty(&ast_ty)).collect::<Vec<_>>();
2852 let fn_ty = match self.lookup_method(method_name.span,
2859 let method_ty = method.ty;
2860 let method_call = MethodCall::expr(expr.id);
2861 self.tables.borrow_mut().method_map.insert(method_call, method);
2865 if method_name.node != keywords::Invalid.name() {
2866 self.report_method_error(method_name.span, expr_t,
2867 method_name.node, Some(rcvr), error);
2869 self.write_error(expr.id);
2874 // Call the generic checker.
2875 let ret_ty = self.check_method_argument_types(method_name.span, fn_ty,
2880 self.write_call(expr, ret_ty);
2883 // A generic function for checking the then and else in an if
2885 fn check_then_else(&self,
2886 cond_expr: &'gcx hir::Expr,
2887 then_blk: &'gcx hir::Block,
2888 opt_else_expr: Option<&'gcx hir::Expr>,
2891 expected: Expectation<'tcx>) {
2892 self.check_expr_has_type(cond_expr, self.tcx.types.bool);
2894 let expected = expected.adjust_for_branches(self);
2895 self.check_block_with_expected(then_blk, expected);
2896 let then_ty = self.node_ty(then_blk.id);
2898 let unit = self.tcx.mk_nil();
2899 let (origin, expected, found, result) =
2900 if let Some(else_expr) = opt_else_expr {
2901 self.check_expr_with_expectation(else_expr, expected);
2902 let else_ty = self.expr_ty(else_expr);
2903 let origin = TypeOrigin::IfExpression(sp);
2905 // Only try to coerce-unify if we have a then expression
2906 // to assign coercions to, otherwise it's () or diverging.
2907 let result = if let Some(ref then) = then_blk.expr {
2908 let res = self.try_find_coercion_lub(origin, || Some(&**then),
2909 then_ty, else_expr);
2911 // In case we did perform an adjustment, we have to update
2912 // the type of the block, because old trans still uses it.
2913 let adj = self.tables.borrow().adjustments.get(&then.id).cloned();
2914 if res.is_ok() && adj.is_some() {
2915 self.write_ty(then_blk.id, self.adjust_expr_ty(then, adj.as_ref()));
2920 self.commit_if_ok(|_| {
2921 let trace = TypeTrace::types(origin, true, then_ty, else_ty);
2922 self.lub(true, trace, &then_ty, &else_ty)
2923 .map(|InferOk { value, obligations }| {
2924 // FIXME(#32730) propagate obligations
2925 assert!(obligations.is_empty());
2930 (origin, then_ty, else_ty, result)
2932 let origin = TypeOrigin::IfExpressionWithNoElse(sp);
2933 (origin, unit, then_ty,
2934 self.eq_types(true, origin, unit, then_ty)
2935 .map(|InferOk { obligations, .. }| {
2936 // FIXME(#32730) propagate obligations
2937 assert!(obligations.is_empty());
2942 let if_ty = match result {
2944 if self.expr_ty(cond_expr).references_error() {
2951 self.report_mismatched_types(origin, expected, found, e);
2956 self.write_ty(id, if_ty);
2959 // Check field access expressions
2960 fn check_field(&self,
2961 expr: &'gcx hir::Expr,
2962 lvalue_pref: LvaluePreference,
2963 base: &'gcx hir::Expr,
2964 field: &Spanned<ast::Name>) {
2965 self.check_expr_with_lvalue_pref(base, lvalue_pref);
2966 let expr_t = self.structurally_resolved_type(expr.span,
2967 self.expr_ty(base));
2968 let mut private_candidate = None;
2969 let mut autoderef = self.autoderef(expr.span, expr_t);
2970 while let Some((base_t, autoderefs)) = autoderef.next() {
2971 if let ty::TyStruct(base_def, substs) = base_t.sty {
2972 debug!("struct named {:?}", base_t);
2973 if let Some(field) = base_def.struct_variant().find_field_named(field.node) {
2974 let field_ty = self.field_ty(expr.span, field, substs);
2975 if field.vis.is_accessible_from(self.body_id, &self.tcx().map) {
2976 autoderef.finalize(lvalue_pref, Some(base));
2977 self.write_ty(expr.id, field_ty);
2978 self.write_autoderef_adjustment(base.id, autoderefs);
2981 private_candidate = Some((base_def.did, field_ty));
2985 autoderef.unambiguous_final_ty();
2987 if let Some((did, field_ty)) = private_candidate {
2988 let struct_path = self.tcx().item_path_str(did);
2989 self.write_ty(expr.id, field_ty);
2990 let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
2991 let mut err = self.tcx().sess.struct_span_err(expr.span, &msg);
2992 // Also check if an accessible method exists, which is often what is meant.
2993 if self.method_exists(field.span, field.node, expr_t, expr.id, false) {
2994 err.note(&format!("a method `{}` also exists, perhaps you wish to call it",
2998 } else if field.node == keywords::Invalid.name() {
2999 self.write_error(expr.id);
3000 } else if self.method_exists(field.span, field.node, expr_t, expr.id, true) {
3001 self.type_error_struct(field.span, |actual| {
3002 format!("attempted to take value of method `{}` on type \
3003 `{}`", field.node, actual)
3005 .help("maybe a `()` to call it is missing? \
3006 If not, try an anonymous function")
3008 self.write_error(expr.id);
3010 let mut err = self.type_error_struct(expr.span, |actual| {
3011 format!("attempted access of field `{}` on type `{}`, \
3012 but no field with that name was found",
3015 if let ty::TyRawPtr(..) = expr_t.sty {
3016 err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \
3017 `(*{0}).{1}`", pprust::expr_to_string(base), field.node));
3019 if let ty::TyStruct(def, _) = expr_t.sty {
3020 Self::suggest_field_names(&mut err, def.struct_variant(), field, vec![]);
3023 self.write_error(expr.id);
3027 // displays hints about the closest matches in field names
3028 fn suggest_field_names(err: &mut DiagnosticBuilder,
3029 variant: ty::VariantDef<'tcx>,
3030 field: &Spanned<ast::Name>,
3031 skip : Vec<InternedString>) {
3032 let name = field.node.as_str();
3033 let names = variant.fields.iter().filter_map(|field| {
3034 // ignore already set fields and private fields from non-local crates
3035 if skip.iter().any(|x| *x == field.name.as_str()) ||
3036 (variant.did.krate != LOCAL_CRATE && field.vis != Visibility::Public) {
3043 // only find fits with at least one matching letter
3044 if let Some(name) = find_best_match_for_name(names, &name, Some(name.len())) {
3045 err.span_help(field.span,
3046 &format!("did you mean `{}`?", name));
3050 // Check tuple index expressions
3051 fn check_tup_field(&self,
3052 expr: &'gcx hir::Expr,
3053 lvalue_pref: LvaluePreference,
3054 base: &'gcx hir::Expr,
3055 idx: codemap::Spanned<usize>) {
3056 self.check_expr_with_lvalue_pref(base, lvalue_pref);
3057 let expr_t = self.structurally_resolved_type(expr.span,
3058 self.expr_ty(base));
3059 let mut private_candidate = None;
3060 let mut tuple_like = false;
3061 let mut autoderef = self.autoderef(expr.span, expr_t);
3062 while let Some((base_t, autoderefs)) = autoderef.next() {
3063 let field = match base_t.sty {
3064 ty::TyStruct(base_def, substs) => {
3065 tuple_like = base_def.struct_variant().kind == ty::VariantKind::Tuple;
3066 if !tuple_like { continue }
3068 debug!("tuple struct named {:?}", base_t);
3069 base_def.struct_variant().fields.get(idx.node).and_then(|field| {
3070 let field_ty = self.field_ty(expr.span, field, substs);
3071 private_candidate = Some((base_def.did, field_ty));
3072 if field.vis.is_accessible_from(self.body_id, &self.tcx().map) {
3079 ty::TyTuple(ref v) => {
3081 v.get(idx.node).cloned()
3086 if let Some(field_ty) = field {
3087 autoderef.finalize(lvalue_pref, Some(base));
3088 self.write_ty(expr.id, field_ty);
3089 self.write_autoderef_adjustment(base.id, autoderefs);
3093 autoderef.unambiguous_final_ty();
3095 if let Some((did, field_ty)) = private_candidate {
3096 let struct_path = self.tcx().item_path_str(did);
3097 let msg = format!("field `{}` of struct `{}` is private", idx.node, struct_path);
3098 self.tcx().sess.span_err(expr.span, &msg);
3099 self.write_ty(expr.id, field_ty);
3103 self.type_error_message(
3107 format!("attempted out-of-bounds tuple index `{}` on \
3112 format!("attempted tuple index `{}` on type `{}`, but the \
3113 type was not a tuple or tuple struct",
3120 self.write_error(expr.id);
3123 fn report_unknown_field(&self,
3125 variant: ty::VariantDef<'tcx>,
3127 skip_fields: &[hir::Field]) {
3128 let mut err = self.type_error_struct_with_diag(
3130 |actual| if let ty::TyEnum(..) = ty.sty {
3131 struct_span_err!(self.tcx.sess, field.name.span, E0559,
3132 "struct variant `{}::{}` has no field named `{}`",
3133 actual, variant.name.as_str(), field.name.node)
3135 struct_span_err!(self.tcx.sess, field.name.span, E0560,
3136 "structure `{}` has no field named `{}`",
3137 actual, field.name.node)
3140 // prevent all specified fields from being suggested
3141 let skip_fields = skip_fields.iter().map(|ref x| x.name.node.as_str());
3142 Self::suggest_field_names(&mut err, variant, &field.name, skip_fields.collect());
3146 fn check_expr_struct_fields(&self,
3149 variant: ty::VariantDef<'tcx>,
3150 ast_fields: &'gcx [hir::Field],
3151 check_completeness: bool) {
3153 let substs = match adt_ty.sty {
3154 ty::TyStruct(_, substs) | ty::TyEnum(_, substs) => substs,
3155 _ => span_bug!(span, "non-ADT passed to check_expr_struct_fields")
3158 let mut remaining_fields = FnvHashMap();
3159 for field in &variant.fields {
3160 remaining_fields.insert(field.name, field);
3163 let mut seen_fields = FnvHashMap();
3165 let mut error_happened = false;
3167 // Typecheck each field.
3168 for field in ast_fields {
3169 let expected_field_type;
3171 if let Some(v_field) = remaining_fields.remove(&field.name.node) {
3172 expected_field_type = self.field_ty(field.span, v_field, substs);
3174 seen_fields.insert(field.name.node, field.span);
3176 error_happened = true;
3177 expected_field_type = tcx.types.err;
3178 if let Some(_) = variant.find_field_named(field.name.node) {
3179 let mut err = struct_span_err!(self.tcx.sess,
3182 "field `{}` specified more than once",
3185 err.span_label(field.name.span, &format!("used more than once"));
3187 if let Some(prev_span) = seen_fields.get(&field.name.node) {
3188 err.span_label(*prev_span, &format!("first use of `{}`", field.name.node));
3193 self.report_unknown_field(adt_ty, variant, field, ast_fields);
3197 // Make sure to give a type to the field even if there's
3198 // an error, so we can continue typechecking
3199 self.check_expr_coercable_to_type(&field.expr, expected_field_type);
3202 // Make sure the programmer specified all the fields.
3203 if check_completeness &&
3205 !remaining_fields.is_empty()
3207 span_err!(tcx.sess, span, E0063,
3208 "missing field{} {} in initializer of `{}`",
3209 if remaining_fields.len() == 1 {""} else {"s"},
3210 remaining_fields.keys()
3211 .map(|n| format!("`{}`", n))
3212 .collect::<Vec<_>>()
3219 fn check_struct_fields_on_error(&self,
3221 fields: &'gcx [hir::Field],
3222 base_expr: &'gcx Option<P<hir::Expr>>) {
3223 // Make sure to still write the types
3224 // otherwise we might ICE
3225 self.write_error(id);
3226 for field in fields {
3227 self.check_expr(&field.expr);
3230 Some(ref base) => self.check_expr(&base),
3235 pub fn check_struct_path(&self,
3237 node_id: ast::NodeId,
3239 -> Option<(ty::VariantDef<'tcx>, Ty<'tcx>)> {
3240 let def = self.finish_resolving_struct_path(path, node_id, span);
3241 let variant = match def {
3243 self.set_tainted_by_errors();
3246 Def::Variant(type_did, _) | Def::Struct(type_did) => {
3247 Some((type_did, self.tcx.expect_variant_def(def)))
3249 Def::TyAlias(did) => {
3250 if let Some(&ty::TyStruct(adt, _)) = self.tcx.opt_lookup_item_type(did)
3251 .map(|scheme| &scheme.ty.sty) {
3252 Some((did, adt.struct_variant()))
3260 if let Some((def_id, variant)) = variant {
3261 if variant.kind == ty::VariantKind::Tuple &&
3262 !self.tcx.sess.features.borrow().relaxed_adts {
3263 emit_feature_err(&self.tcx.sess.parse_sess.span_diagnostic,
3264 "relaxed_adts", span, GateIssue::Language,
3265 "tuple structs and variants in struct patterns are unstable");
3267 let ty = self.instantiate_type_path(def_id, path, node_id);
3270 struct_span_err!(self.tcx.sess, path.span, E0071,
3271 "`{}` does not name a struct or a struct variant",
3272 pprust::path_to_string(path))
3273 .span_label(path.span, &format!("not a struct"))
3279 fn check_expr_struct(&self,
3282 fields: &'gcx [hir::Field],
3283 base_expr: &'gcx Option<P<hir::Expr>>)
3285 // Find the relevant variant
3286 let (variant, expr_ty) = if let Some(variant_ty) = self.check_struct_path(path, expr.id,
3290 self.check_struct_fields_on_error(expr.id, fields, base_expr);
3294 self.check_expr_struct_fields(expr_ty, path.span, variant, fields,
3295 base_expr.is_none());
3296 if let &Some(ref base_expr) = base_expr {
3297 self.check_expr_has_type(base_expr, expr_ty);
3299 ty::TyStruct(adt, substs) => {
3300 self.tables.borrow_mut().fru_field_types.insert(
3302 adt.struct_variant().fields.iter().map(|f| {
3303 self.normalize_associated_types_in(
3304 expr.span, &f.ty(self.tcx, substs)
3310 span_err!(self.tcx.sess, base_expr.span, E0436,
3311 "functional record update syntax requires a struct");
3319 /// If an expression has any sub-expressions that result in a type error,
3320 /// inspecting that expression's type with `ty.references_error()` will return
3321 /// true. Likewise, if an expression is known to diverge, inspecting its
3322 /// type with `ty::type_is_bot` will return true (n.b.: since Rust is
3323 /// strict, _|_ can appear in the type of an expression that does not,
3324 /// itself, diverge: for example, fn() -> _|_.)
3325 /// Note that inspecting a type's structure *directly* may expose the fact
3326 /// that there are actually multiple representations for `TyError`, so avoid
3327 /// that when err needs to be handled differently.
3328 fn check_expr_with_expectation_and_lvalue_pref(&self,
3329 expr: &'gcx hir::Expr,
3330 expected: Expectation<'tcx>,
3331 lvalue_pref: LvaluePreference) {
3332 debug!(">> typechecking: expr={:?} expected={:?}",
3338 hir::ExprBox(ref subexpr) => {
3339 let expected_inner = expected.to_option(self).map_or(NoExpectation, |ty| {
3341 ty::TyBox(ty) => Expectation::rvalue_hint(self, ty),
3345 self.check_expr_with_expectation(subexpr, expected_inner);
3346 let referent_ty = self.expr_ty(&subexpr);
3347 self.write_ty(id, tcx.mk_box(referent_ty));
3350 hir::ExprLit(ref lit) => {
3351 let typ = self.check_lit(&lit, expected);
3352 self.write_ty(id, typ);
3354 hir::ExprBinary(op, ref lhs, ref rhs) => {
3355 self.check_binop(expr, op, lhs, rhs);
3357 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
3358 self.check_binop_assign(expr, op, lhs, rhs);
3360 hir::ExprUnary(unop, ref oprnd) => {
3361 let expected_inner = match unop {
3362 hir::UnNot | hir::UnNeg => {
3369 let lvalue_pref = match unop {
3370 hir::UnDeref => lvalue_pref,
3373 self.check_expr_with_expectation_and_lvalue_pref(&oprnd,
3376 let mut oprnd_t = self.expr_ty(&oprnd);
3378 if !oprnd_t.references_error() {
3381 oprnd_t = self.structurally_resolved_type(expr.span, oprnd_t);
3383 if let Some(mt) = oprnd_t.builtin_deref(true, NoPreference) {
3385 } else if let Some(method) = self.try_overloaded_deref(
3386 expr.span, Some(&oprnd), oprnd_t, lvalue_pref) {
3387 oprnd_t = self.make_overloaded_lvalue_return_type(method).ty;
3388 self.tables.borrow_mut().method_map.insert(MethodCall::expr(expr.id),
3391 self.type_error_message(expr.span, |actual| {
3392 format!("type `{}` cannot be \
3393 dereferenced", actual)
3395 oprnd_t = tcx.types.err;
3399 oprnd_t = self.structurally_resolved_type(oprnd.span,
3401 if !(oprnd_t.is_integral() || oprnd_t.sty == ty::TyBool) {
3402 oprnd_t = self.check_user_unop("!", "not",
3403 tcx.lang_items.not_trait(),
3404 expr, &oprnd, oprnd_t, unop);
3408 oprnd_t = self.structurally_resolved_type(oprnd.span,
3410 if !(oprnd_t.is_integral() || oprnd_t.is_fp()) {
3411 oprnd_t = self.check_user_unop("-", "neg",
3412 tcx.lang_items.neg_trait(),
3413 expr, &oprnd, oprnd_t, unop);
3418 self.write_ty(id, oprnd_t);
3420 hir::ExprAddrOf(mutbl, ref oprnd) => {
3421 let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
3423 ty::TyRef(_, ref mt) | ty::TyRawPtr(ref mt) => {
3424 if self.tcx.expr_is_lval(&oprnd) {
3425 // Lvalues may legitimately have unsized types.
3426 // For example, dereferences of a fat pointer and
3427 // the last field of a struct can be unsized.
3428 ExpectHasType(mt.ty)
3430 Expectation::rvalue_hint(self, mt.ty)
3436 let lvalue_pref = LvaluePreference::from_mutbl(mutbl);
3437 self.check_expr_with_expectation_and_lvalue_pref(&oprnd, hint, lvalue_pref);
3439 let tm = ty::TypeAndMut { ty: self.expr_ty(&oprnd), mutbl: mutbl };
3440 let oprnd_t = if tm.ty.references_error() {
3443 // Note: at this point, we cannot say what the best lifetime
3444 // is to use for resulting pointer. We want to use the
3445 // shortest lifetime possible so as to avoid spurious borrowck
3446 // errors. Moreover, the longest lifetime will depend on the
3447 // precise details of the value whose address is being taken
3448 // (and how long it is valid), which we don't know yet until type
3449 // inference is complete.
3451 // Therefore, here we simply generate a region variable. The
3452 // region inferencer will then select the ultimate value.
3453 // Finally, borrowck is charged with guaranteeing that the
3454 // value whose address was taken can actually be made to live
3455 // as long as it needs to live.
3456 let region = self.next_region_var(infer::AddrOfRegion(expr.span));
3457 tcx.mk_ref(tcx.mk_region(region), tm)
3459 self.write_ty(id, oprnd_t);
3461 hir::ExprPath(ref opt_qself, ref path) => {
3462 let opt_self_ty = opt_qself.as_ref().map(|qself| self.to_ty(&qself.ty));
3463 let (def, opt_ty, segments) = self.resolve_ty_and_def_ufcs(opt_self_ty, path,
3464 expr.id, expr.span);
3465 if def != Def::Err {
3466 self.instantiate_value_path(segments, opt_ty, def, expr.span, id);
3468 self.set_tainted_by_errors();
3469 self.write_error(id);
3472 // We always require that the type provided as the value for
3473 // a type parameter outlives the moment of instantiation.
3474 self.opt_node_ty_substs(expr.id, |item_substs| {
3475 self.add_wf_bounds(&item_substs.substs, expr);
3478 hir::ExprInlineAsm(_, ref outputs, ref inputs) => {
3479 for output in outputs {
3480 self.check_expr(output);
3482 for input in inputs {
3483 self.check_expr(input);
3487 hir::ExprBreak(_) => { self.write_never(id); }
3488 hir::ExprAgain(_) => { self.write_never(id); }
3489 hir::ExprRet(ref expr_opt) => {
3490 if let Some(ref e) = *expr_opt {
3491 self.check_expr_coercable_to_type(&e, self.ret_ty);
3493 let eq_result = self.eq_types(false,
3494 TypeOrigin::Misc(expr.span),
3497 // FIXME(#32730) propagate obligations
3498 .map(|InferOk { obligations, .. }| assert!(obligations.is_empty()));
3499 if eq_result.is_err() {
3500 struct_span_err!(tcx.sess, expr.span, E0069,
3501 "`return;` in a function whose return type is not `()`")
3502 .span_label(expr.span, &format!("return type is not ()"))
3506 self.write_never(id);
3508 hir::ExprAssign(ref lhs, ref rhs) => {
3509 self.check_expr_with_lvalue_pref(&lhs, PreferMutLvalue);
3512 if !tcx.expr_is_lval(&lhs) {
3514 tcx.sess, expr.span, E0070,
3515 "invalid left-hand side expression")
3518 &format!("left-hand of expression not valid"))
3522 let lhs_ty = self.expr_ty(&lhs);
3523 self.check_expr_coercable_to_type(&rhs, lhs_ty);
3524 let rhs_ty = self.expr_ty(&rhs);
3526 self.require_expr_have_sized_type(&lhs, traits::AssignmentLhsSized);
3528 if lhs_ty.references_error() || rhs_ty.references_error() {
3529 self.write_error(id);
3534 hir::ExprIf(ref cond, ref then_blk, ref opt_else_expr) => {
3535 self.check_then_else(&cond, &then_blk, opt_else_expr.as_ref().map(|e| &**e),
3536 id, expr.span, expected);
3538 hir::ExprWhile(ref cond, ref body, _) => {
3539 self.check_expr_has_type(&cond, tcx.types.bool);
3540 self.check_block_no_value(&body);
3541 let cond_ty = self.expr_ty(&cond);
3542 let body_ty = self.node_ty(body.id);
3543 if cond_ty.references_error() || body_ty.references_error() {
3544 self.write_error(id);
3550 hir::ExprLoop(ref body, _) => {
3551 self.check_block_no_value(&body);
3552 if !may_break(tcx, expr.id, &body) {
3553 self.write_never(id);
3558 hir::ExprMatch(ref discrim, ref arms, match_src) => {
3559 self.check_match(expr, &discrim, arms, expected, match_src);
3561 hir::ExprClosure(capture, ref decl, ref body, _) => {
3562 self.check_expr_closure(expr, capture, &decl, &body, expected);
3564 hir::ExprBlock(ref b) => {
3565 self.check_block_with_expected(&b, expected);
3566 self.write_ty(id, self.node_ty(b.id));
3568 hir::ExprCall(ref callee, ref args) => {
3569 self.check_call(expr, &callee, &args[..], expected);
3571 // we must check that return type of called functions is WF:
3572 let ret_ty = self.expr_ty(expr);
3573 self.register_wf_obligation(ret_ty, expr.span, traits::MiscObligation);
3575 hir::ExprMethodCall(name, ref tps, ref args) => {
3576 self.check_method_call(expr, name, &args[..], &tps[..], expected, lvalue_pref);
3577 let arg_tys = args.iter().map(|a| self.expr_ty(&a));
3578 let args_err = arg_tys.fold(false, |rest_err, a| rest_err || a.references_error());
3580 self.write_error(id);
3583 hir::ExprCast(ref e, ref t) => {
3584 if let hir::TyFixedLengthVec(_, ref count_expr) = t.node {
3585 self.check_expr_with_hint(&count_expr, tcx.types.usize);
3588 // Find the type of `e`. Supply hints based on the type we are casting to,
3590 let t_cast = self.to_ty(t);
3591 let t_cast = self.resolve_type_vars_if_possible(&t_cast);
3592 self.check_expr_with_expectation(e, ExpectCastableToType(t_cast));
3593 let t_expr = self.expr_ty(e);
3594 let t_cast = self.resolve_type_vars_if_possible(&t_cast);
3596 // Eagerly check for some obvious errors.
3597 if t_expr.references_error() || t_cast.references_error() {
3598 self.write_error(id);
3600 // Write a type for the whole expression, assuming everything is going
3602 self.write_ty(id, t_cast);
3604 // Defer other checks until we're done type checking.
3605 let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
3606 match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
3608 deferred_cast_checks.push(cast_check);
3610 Err(ErrorReported) => {
3611 self.write_error(id);
3616 hir::ExprType(ref e, ref t) => {
3617 let typ = self.to_ty(&t);
3618 self.check_expr_eq_type(&e, typ);
3619 self.write_ty(id, typ);
3621 hir::ExprVec(ref args) => {
3622 let uty = expected.to_option(self).and_then(|uty| {
3624 ty::TyArray(ty, _) | ty::TySlice(ty) => Some(ty),
3629 let mut unified = self.next_ty_var();
3630 let coerce_to = uty.unwrap_or(unified);
3632 for (i, e) in args.iter().enumerate() {
3633 self.check_expr_with_hint(e, coerce_to);
3634 let e_ty = self.expr_ty(e);
3635 let origin = TypeOrigin::Misc(e.span);
3637 // Special-case the first element, as it has no "previous expressions".
3638 let result = if i == 0 {
3639 self.try_coerce(e, coerce_to)
3641 let prev_elems = || args[..i].iter().map(|e| &**e);
3642 self.try_find_coercion_lub(origin, prev_elems, unified, e)
3646 Ok(ty) => unified = ty,
3648 self.report_mismatched_types(origin, unified, e_ty, e);
3652 self.write_ty(id, tcx.mk_array(unified, args.len()));
3654 hir::ExprRepeat(ref element, ref count_expr) => {
3655 self.check_expr_has_type(&count_expr, tcx.types.usize);
3656 let count = eval_length(self.tcx.global_tcx(), &count_expr, "repeat count")
3659 let uty = match expected {
3660 ExpectHasType(uty) => {
3662 ty::TyArray(ty, _) | ty::TySlice(ty) => Some(ty),
3669 let (element_ty, t) = match uty {
3671 self.check_expr_coercable_to_type(&element, uty);
3675 let t: Ty = self.next_ty_var();
3676 self.check_expr_has_type(&element, t);
3677 (self.expr_ty(&element), t)
3682 // For [foo, ..n] where n > 1, `foo` must have
3684 self.require_type_meets(t, expr.span, traits::RepeatVec, ty::BoundCopy);
3687 if element_ty.references_error() {
3688 self.write_error(id);
3690 let t = tcx.mk_array(t, count);
3691 self.write_ty(id, t);
3694 hir::ExprTup(ref elts) => {
3695 let flds = expected.only_has_type(self).and_then(|ty| {
3697 ty::TyTuple(ref flds) => Some(&flds[..]),
3701 let mut err_field = false;
3703 let elt_ts = elts.iter().enumerate().map(|(i, e)| {
3704 let t = match flds {
3705 Some(ref fs) if i < fs.len() => {
3707 self.check_expr_coercable_to_type(&e, ety);
3711 self.check_expr_with_expectation(&e, NoExpectation);
3715 err_field = err_field || t.references_error();
3719 self.write_error(id);
3721 let typ = tcx.mk_tup(elt_ts);
3722 self.write_ty(id, typ);
3725 hir::ExprStruct(ref path, ref fields, ref base_expr) => {
3726 self.check_expr_struct(expr, path, fields, base_expr);
3728 self.require_expr_have_sized_type(expr, traits::StructInitializerSized);
3730 hir::ExprField(ref base, ref field) => {
3731 self.check_field(expr, lvalue_pref, &base, field);
3733 hir::ExprTupField(ref base, idx) => {
3734 self.check_tup_field(expr, lvalue_pref, &base, idx);
3736 hir::ExprIndex(ref base, ref idx) => {
3737 self.check_expr_with_lvalue_pref(&base, lvalue_pref);
3738 self.check_expr(&idx);
3740 let base_t = self.expr_ty(&base);
3741 let idx_t = self.expr_ty(&idx);
3743 if base_t.references_error() {
3744 self.write_ty(id, base_t);
3745 } else if idx_t.references_error() {
3746 self.write_ty(id, idx_t);
3748 let base_t = self.structurally_resolved_type(expr.span, base_t);
3749 match self.lookup_indexing(expr, base, base_t, idx_t, lvalue_pref) {
3750 Some((index_ty, element_ty)) => {
3751 let idx_expr_ty = self.expr_ty(idx);
3752 self.demand_eqtype(expr.span, index_ty, idx_expr_ty);
3753 self.write_ty(id, element_ty);
3756 self.check_expr_has_type(&idx, self.tcx.types.err);
3757 let mut err = self.type_error_struct(
3760 format!("cannot index a value of type `{}`",
3764 // Try to give some advice about indexing tuples.
3765 if let ty::TyTuple(_) = base_t.sty {
3766 let mut needs_note = true;
3767 // If the index is an integer, we can show the actual
3768 // fixed expression:
3769 if let hir::ExprLit(ref lit) = idx.node {
3770 if let ast::LitKind::Int(i,
3771 ast::LitIntType::Unsuffixed) = lit.node {
3772 let snip = tcx.sess.codemap().span_to_snippet(base.span);
3773 if let Ok(snip) = snip {
3774 err.span_suggestion(expr.span,
3775 "to access tuple elements, \
3776 use tuple indexing syntax \
3778 format!("{}.{}", snip, i));
3784 err.help("to access tuple elements, use tuple indexing \
3785 syntax (e.g. `tuple.0`)");
3789 self.write_ty(id, self.tcx().types.err);
3796 debug!("type of expr({}) {} is...", expr.id,
3797 pprust::expr_to_string(expr));
3798 debug!("... {:?}, expected is {:?}",
3803 // Finish resolving a path in a struct expression or pattern `S::A { .. }` if necessary.
3804 // The newly resolved definition is written into `def_map`.
3805 pub fn finish_resolving_struct_path(&self,
3807 node_id: ast::NodeId,
3811 let path_res = self.tcx().expect_resolution(node_id);
3812 if path_res.depth == 0 {
3813 // If fully resolved already, we don't have to do anything.
3816 let base_ty_end = path.segments.len() - path_res.depth;
3817 let (_ty, def) = AstConv::finish_resolving_def_to_ty(self, self, span,
3818 PathParamMode::Optional,
3822 &path.segments[..base_ty_end],
3823 &path.segments[base_ty_end..]);
3824 // Write back the new resolution.
3825 self.tcx().def_map.borrow_mut().insert(node_id, PathResolution::new(def));
3830 // Resolve associated value path into a base type and associated constant or method definition.
3831 // The newly resolved definition is written into `def_map`.
3832 pub fn resolve_ty_and_def_ufcs<'b>(&self,
3833 opt_self_ty: Option<Ty<'tcx>>,
3834 path: &'b hir::Path,
3835 node_id: ast::NodeId,
3837 -> (Def, Option<Ty<'tcx>>, &'b [hir::PathSegment])
3839 let path_res = self.tcx().expect_resolution(node_id);
3840 if path_res.depth == 0 {
3841 // If fully resolved already, we don't have to do anything.
3842 (path_res.base_def, opt_self_ty, &path.segments)
3844 // Try to resolve everything except for the last segment as a type.
3845 let ty_segments = path.segments.split_last().unwrap().1;
3846 let base_ty_end = path.segments.len() - path_res.depth;
3847 let (ty, _def) = AstConv::finish_resolving_def_to_ty(self, self, span,
3848 PathParamMode::Optional,
3852 &ty_segments[..base_ty_end],
3853 &ty_segments[base_ty_end..]);
3855 // Resolve an associated constant or method on the previously resolved type.
3856 let item_segment = path.segments.last().unwrap();
3857 let item_name = item_segment.name;
3858 let def = match self.resolve_ufcs(span, item_name, ty, node_id) {
3861 let def = match error {
3862 method::MethodError::PrivateMatch(def) => def,
3865 if item_name != keywords::Invalid.name() {
3866 self.report_method_error(span, ty, item_name, None, error);
3872 // Write back the new resolution.
3873 self.tcx().def_map.borrow_mut().insert(node_id, PathResolution::new(def));
3874 (def, Some(ty), slice::ref_slice(item_segment))
3878 pub fn check_decl_initializer(&self,
3879 local: &'gcx hir::Local,
3880 init: &'gcx hir::Expr)
3882 let ref_bindings = self.tcx.pat_contains_ref_binding(&local.pat);
3884 let local_ty = self.local_ty(init.span, local.id);
3885 if let Some(m) = ref_bindings {
3886 // Somewhat subtle: if we have a `ref` binding in the pattern,
3887 // we want to avoid introducing coercions for the RHS. This is
3888 // both because it helps preserve sanity and, in the case of
3889 // ref mut, for soundness (issue #23116). In particular, in
3890 // the latter case, we need to be clear that the type of the
3891 // referent for the reference that results is *equal to* the
3892 // type of the lvalue it is referencing, and not some
3893 // supertype thereof.
3894 self.check_expr_with_lvalue_pref(init, LvaluePreference::from_mutbl(m));
3895 let init_ty = self.expr_ty(init);
3896 self.demand_eqtype(init.span, init_ty, local_ty);
3898 self.check_expr_coercable_to_type(init, local_ty)
3902 pub fn check_decl_local(&self, local: &'gcx hir::Local) {
3903 let t = self.local_ty(local.span, local.id);
3904 self.write_ty(local.id, t);
3906 if let Some(ref init) = local.init {
3907 self.check_decl_initializer(local, &init);
3908 let init_ty = self.expr_ty(&init);
3909 if init_ty.references_error() {
3910 self.write_ty(local.id, init_ty);
3914 self.check_pat(&local.pat, t);
3915 let pat_ty = self.node_ty(local.pat.id);
3916 if pat_ty.references_error() {
3917 self.write_ty(local.id, pat_ty);
3921 pub fn check_stmt(&self, stmt: &'gcx hir::Stmt) {
3923 let mut saw_bot = false;
3924 let mut saw_err = false;
3926 hir::StmtDecl(ref decl, id) => {
3929 hir::DeclLocal(ref l) => {
3930 self.check_decl_local(&l);
3931 let l_t = self.node_ty(l.id);
3932 saw_bot = saw_bot || self.type_var_diverges(l_t);
3933 saw_err = saw_err || l_t.references_error();
3935 hir::DeclItem(_) => {/* ignore for now */ }
3938 hir::StmtExpr(ref expr, id) => {
3940 // Check with expected type of ()
3941 self.check_expr_has_type(&expr, self.tcx.mk_nil());
3942 let expr_ty = self.expr_ty(&expr);
3943 saw_bot = saw_bot || self.type_var_diverges(expr_ty);
3944 saw_err = saw_err || expr_ty.references_error();
3946 hir::StmtSemi(ref expr, id) => {
3948 self.check_expr(&expr);
3949 let expr_ty = self.expr_ty(&expr);
3950 saw_bot |= self.type_var_diverges(expr_ty);
3951 saw_err |= expr_ty.references_error();
3955 self.write_ty(node_id, self.next_diverging_ty_var());
3958 self.write_error(node_id);
3961 self.write_nil(node_id)
3965 pub fn check_block_no_value(&self, blk: &'gcx hir::Block) {
3966 self.check_block_with_expected(blk, ExpectHasType(self.tcx.mk_nil()));
3967 let blkty = self.node_ty(blk.id);
3968 if blkty.references_error() {
3969 self.write_error(blk.id);
3971 let nilty = self.tcx.mk_nil();
3972 self.demand_suptype(blk.span, nilty, blkty);
3976 fn check_block_with_expected(&self,
3977 blk: &'gcx hir::Block,
3978 expected: Expectation<'tcx>) {
3980 let mut fcx_ps = self.ps.borrow_mut();
3981 let unsafety_state = fcx_ps.recurse(blk);
3982 replace(&mut *fcx_ps, unsafety_state)
3985 let mut warned = false;
3986 let mut any_diverges = false;
3987 let mut any_err = false;
3988 for s in &blk.stmts {
3990 let s_id = s.node.id();
3991 let s_ty = self.node_ty(s_id);
3992 if any_diverges && !warned && match s.node {
3993 hir::StmtDecl(ref decl, _) => {
3995 hir::DeclLocal(_) => true,
3999 hir::StmtExpr(_, _) | hir::StmtSemi(_, _) => true,
4003 .add_lint(lint::builtin::UNREACHABLE_CODE,
4006 "unreachable statement".to_string());
4009 // FIXME(canndrew): This is_never should probably be an is_uninhabited
4010 any_diverges = any_diverges ||
4011 self.type_var_diverges(s_ty) ||
4013 any_err = any_err || s_ty.references_error();
4016 None => if any_err {
4017 self.write_error(blk.id);
4018 } else if any_diverges {
4019 self.write_ty(blk.id, self.next_diverging_ty_var());
4021 self.write_nil(blk.id);
4024 if any_diverges && !warned {
4027 .add_lint(lint::builtin::UNREACHABLE_CODE,
4030 "unreachable expression".to_string());
4032 let ety = match expected {
4033 ExpectHasType(ety) => {
4034 self.check_expr_coercable_to_type(&e, ety);
4038 self.check_expr_with_expectation(&e, expected);
4044 self.write_error(blk.id);
4045 } else if any_diverges {
4046 self.write_ty(blk.id, self.next_diverging_ty_var());
4048 self.write_ty(blk.id, ety);
4053 *self.ps.borrow_mut() = prev;
4056 // Instantiates the given path, which must refer to an item with the given
4057 // number of type parameters and type.
4058 pub fn instantiate_value_path(&self,
4059 segments: &[hir::PathSegment],
4060 opt_self_ty: Option<Ty<'tcx>>,
4063 node_id: ast::NodeId)
4065 debug!("instantiate_value_path(path={:?}, def={:?}, node_id={})",
4070 // We need to extract the type parameters supplied by the user in
4071 // the path `path`. Due to the current setup, this is a bit of a
4072 // tricky-process; the problem is that resolve only tells us the
4073 // end-point of the path resolution, and not the intermediate steps.
4074 // Luckily, we can (at least for now) deduce the intermediate steps
4075 // just from the end-point.
4077 // There are basically four cases to consider:
4079 // 1. Reference to a *type*, such as a struct or enum:
4081 // mod a { struct Foo<T> { ... } }
4083 // Because we don't allow types to be declared within one
4084 // another, a path that leads to a type will always look like
4085 // `a::b::Foo<T>` where `a` and `b` are modules. This implies
4086 // that only the final segment can have type parameters, and
4087 // they are located in the TypeSpace.
4089 // *Note:* Generally speaking, references to types don't
4090 // actually pass through this function, but rather the
4091 // `ast_ty_to_ty` function in `astconv`. However, in the case
4092 // of struct patterns (and maybe literals) we do invoke
4093 // `instantiate_value_path` to get the general type of an instance of
4094 // a struct. (In these cases, there are actually no type
4095 // parameters permitted at present, but perhaps we will allow
4096 // them in the future.)
4098 // 1b. Reference to an enum variant or tuple-like struct:
4100 // struct foo<T>(...)
4101 // enum E<T> { foo(...) }
4103 // In these cases, the parameters are declared in the type
4106 // 2. Reference to a *fn item*:
4110 // In this case, the path will again always have the form
4111 // `a::b::foo::<T>` where only the final segment should have
4112 // type parameters. However, in this case, those parameters are
4113 // declared on a value, and hence are in the `FnSpace`.
4115 // 3. Reference to a *method*:
4117 // impl<A> SomeStruct<A> {
4121 // Here we can have a path like
4122 // `a::b::SomeStruct::<A>::foo::<B>`, in which case parameters
4123 // may appear in two places. The penultimate segment,
4124 // `SomeStruct::<A>`, contains parameters in TypeSpace, and the
4125 // final segment, `foo::<B>` contains parameters in fn space.
4127 // 4. Reference to an *associated const*:
4129 // impl<A> AnotherStruct<A> {
4130 // const FOO: B = BAR;
4133 // The path in this case will look like
4134 // `a::b::AnotherStruct::<A>::FOO`, so the penultimate segment
4135 // only will have parameters in TypeSpace.
4137 // The first step then is to categorize the segments appropriately.
4139 assert!(!segments.is_empty());
4141 let mut ufcs_associated = None;
4142 let mut type_segment = None;
4143 let mut fn_segment = None;
4145 // Case 1 and 1b. Reference to a *type* or *enum variant*.
4146 Def::Struct(def_id) |
4147 Def::Variant(_, def_id) |
4149 Def::TyAlias(def_id) |
4150 Def::AssociatedTy(_, def_id) |
4151 Def::Trait(def_id) => {
4152 // Everything but the final segment should have no
4153 // parameters at all.
4154 let mut generics = self.tcx.lookup_generics(def_id);
4155 if let Some(def_id) = generics.parent {
4156 // Variant and struct constructors use the
4157 // generics of their parent type definition.
4158 generics = self.tcx.lookup_generics(def_id);
4160 type_segment = Some((segments.last().unwrap(), generics));
4163 // Case 2. Reference to a top-level value.
4165 Def::Const(def_id) |
4166 Def::Static(def_id, _) => {
4167 fn_segment = Some((segments.last().unwrap(),
4168 self.tcx.lookup_generics(def_id)));
4171 // Case 3. Reference to a method or associated const.
4172 Def::Method(def_id) |
4173 Def::AssociatedConst(def_id) => {
4174 let container = self.tcx.impl_or_trait_item(def_id).container();
4176 ty::TraitContainer(trait_did) => {
4177 callee::check_legal_trait_for_method_call(self.ccx, span, trait_did)
4179 ty::ImplContainer(_) => {}
4182 let generics = self.tcx.lookup_generics(def_id);
4183 if segments.len() >= 2 {
4184 let parent_generics = self.tcx.lookup_generics(generics.parent.unwrap());
4185 type_segment = Some((&segments[segments.len() - 2], parent_generics));
4187 // `<T>::assoc` will end up here, and so can `T::assoc`.
4188 let self_ty = opt_self_ty.expect("UFCS sugared assoc missing Self");
4189 ufcs_associated = Some((container, self_ty));
4191 fn_segment = Some((segments.last().unwrap(), generics));
4194 // Other cases. Various nonsense that really shouldn't show up
4195 // here. If they do, an error will have been reported
4196 // elsewhere. (I hope)
4198 Def::ForeignMod(..) |
4208 // In `<T as Trait<A, B>>::method`, `A` and `B` are mandatory, but
4209 // `opt_self_ty` can also be Some for `Foo::method`, where Foo's
4210 // type parameters are not mandatory.
4211 let require_type_space = opt_self_ty.is_some() && ufcs_associated.is_none();
4213 debug!("type_segment={:?} fn_segment={:?}", type_segment, fn_segment);
4215 // Now that we have categorized what space the parameters for each
4216 // segment belong to, let's sort out the parameters that the user
4217 // provided (if any) into their appropriate spaces. We'll also report
4218 // errors if type parameters are provided in an inappropriate place.
4219 let poly_segments = type_segment.is_some() as usize +
4220 fn_segment.is_some() as usize;
4221 self.tcx.prohibit_type_params(&segments[..segments.len() - poly_segments]);
4224 Def::Local(_, nid) | Def::Upvar(_, nid, _, _) => {
4225 let ty = self.local_ty(span, nid);
4226 let ty = self.normalize_associated_types_in(span, &ty);
4227 self.write_ty(node_id, ty);
4228 self.write_substs(node_id, ty::ItemSubsts {
4229 substs: Substs::empty(self.tcx)
4236 // Now we have to compare the types that the user *actually*
4237 // provided against the types that were *expected*. If the user
4238 // did not provide any types, then we want to substitute inference
4239 // variables. If the user provided some types, we may still need
4240 // to add defaults. If the user provided *too many* types, that's
4242 self.check_path_parameter_count(span, !require_type_space, &mut type_segment);
4243 self.check_path_parameter_count(span, true, &mut fn_segment);
4245 let substs = Substs::for_item(self.tcx, def.def_id(), |def, _| {
4246 let mut i = def.index as usize;
4247 let type_regions = match (type_segment, fn_segment) {
4248 (_, Some((_, generics))) => generics.parent_regions as usize,
4249 (Some((_, generics)), None) => generics.regions.len(),
4253 let segment = if i < type_regions {
4259 let lifetimes = match segment.map(|(s, _)| &s.parameters) {
4260 Some(&hir::AngleBracketedParameters(ref data)) => &data.lifetimes[..],
4261 Some(&hir::ParenthesizedParameters(_)) => bug!(),
4265 if let Some(ast_lifetime) = lifetimes.get(i) {
4266 ast_region_to_region(self.tcx, ast_lifetime)
4268 self.region_var_for_def(span, def)
4271 let mut i = def.index as usize;
4272 let (type_types, has_self) = match (type_segment, fn_segment) {
4273 (_, Some((_, generics))) => {
4274 (generics.parent_types as usize, generics.has_self)
4276 (Some((_, generics)), None) => {
4277 (generics.types.len(), generics.has_self)
4279 (None, None) => (0, false)
4282 let can_omit = i >= type_types || !require_type_space;
4283 let segment = if i < type_types {
4284 // Handle Self first, so we can adjust the index to match the AST.
4285 if has_self && i == 0 {
4286 return opt_self_ty.unwrap_or_else(|| {
4287 self.type_var_for_def(span, def, substs)
4290 i -= has_self as usize;
4296 let types = match segment.map(|(s, _)| &s.parameters) {
4297 Some(&hir::AngleBracketedParameters(ref data)) => &data.types[..],
4298 Some(&hir::ParenthesizedParameters(_)) => bug!(),
4302 let omitted = can_omit && types.is_empty();
4303 if let Some(ast_ty) = types.get(i) {
4304 // A provided type parameter.
4306 } else if let (false, Some(default)) = (omitted, def.default) {
4307 // No type parameter provided, but a default exists.
4308 default.subst_spanned(self.tcx, substs, Some(span))
4310 // No type parameters were provided, we can infer all.
4311 // This can also be reached in some error cases:
4312 // We prefer to use inference variables instead of
4313 // TyError to let type inference recover somewhat.
4314 self.type_var_for_def(span, def, substs)
4318 // The things we are substituting into the type should not contain
4319 // escaping late-bound regions, and nor should the base type scheme.
4320 let scheme = self.tcx.lookup_item_type(def.def_id());
4321 let type_predicates = self.tcx.lookup_predicates(def.def_id());
4322 assert!(!substs.has_escaping_regions());
4323 assert!(!scheme.ty.has_escaping_regions());
4325 // Add all the obligations that are required, substituting and
4326 // normalized appropriately.
4327 let bounds = self.instantiate_bounds(span, &substs, &type_predicates);
4328 self.add_obligations_for_parameters(
4329 traits::ObligationCause::new(span, self.body_id, traits::ItemObligation(def.def_id())),
4332 // Substitute the values for the type parameters into the type of
4333 // the referenced item.
4334 let ty_substituted = self.instantiate_type_scheme(span, &substs, &scheme.ty);
4337 if let Some((ty::ImplContainer(impl_def_id), self_ty)) = ufcs_associated {
4338 // In the case of `Foo<T>::method` and `<Foo<T>>::method`, if `method`
4339 // is inherent, there is no `Self` parameter, instead, the impl needs
4340 // type parameters, which we can infer by unifying the provided `Self`
4341 // with the substituted impl type.
4342 let impl_scheme = self.tcx.lookup_item_type(impl_def_id);
4344 let impl_ty = self.instantiate_type_scheme(span, &substs, &impl_scheme.ty);
4345 match self.sub_types(false, TypeOrigin::Misc(span), self_ty, impl_ty) {
4346 Ok(InferOk { obligations, .. }) => {
4347 // FIXME(#32730) propagate obligations
4348 assert!(obligations.is_empty());
4352 "instantiate_value_path: (UFCS) {:?} was a subtype of {:?} but now is not?",
4359 debug!("instantiate_value_path: type of {:?} is {:?}",
4362 self.write_ty(node_id, ty_substituted);
4363 self.write_substs(node_id, ty::ItemSubsts {
4369 /// Report errors if the provided parameters are too few or too many.
4370 fn check_path_parameter_count(&self,
4373 segment: &mut Option<(&hir::PathSegment, &ty::Generics)>) {
4374 let (lifetimes, types, bindings) = match segment.map(|(s, _)| &s.parameters) {
4375 Some(&hir::AngleBracketedParameters(ref data)) => {
4376 (&data.lifetimes[..], &data.types[..], &data.bindings[..])
4378 Some(&hir::ParenthesizedParameters(_)) => {
4379 span_bug!(span, "parenthesized parameters cannot appear in ExprPath");
4381 None => (&[][..], &[][..], &[][..])
4385 format!("{} parameter{}", n, if n == 1 { "" } else { "s" })
4388 // Check provided lifetime parameters.
4389 let lifetime_defs = segment.map_or(&[][..], |(_, generics)| &generics.regions);
4390 if lifetimes.len() > lifetime_defs.len() {
4391 let span = lifetimes[lifetime_defs.len()].span;
4392 span_err!(self.tcx.sess, span, E0088,
4393 "too many lifetime parameters provided: \
4394 expected {}, found {}",
4395 count(lifetime_defs.len()),
4396 count(lifetimes.len()));
4397 } else if lifetimes.len() > 0 && lifetimes.len() < lifetime_defs.len() {
4398 span_err!(self.tcx.sess, span, E0090,
4399 "too few lifetime parameters provided: \
4400 expected {}, found {}",
4401 count(lifetime_defs.len()),
4402 count(lifetimes.len()));
4405 // Check provided type parameters.
4406 let type_defs = segment.map_or(&[][..], |(_, generics)| {
4407 if generics.parent.is_none() {
4408 &generics.types[generics.has_self as usize..]
4413 let required_len = type_defs.iter()
4414 .take_while(|d| d.default.is_none())
4416 if types.len() > type_defs.len() {
4417 let span = types[type_defs.len()].span;
4418 struct_span_err!(self.tcx.sess, span, E0087,
4419 "too many type parameters provided: \
4420 expected at most {}, found {}",
4421 count(type_defs.len()),
4423 .span_label(span, &format!("too many type parameters")).emit();
4425 // To prevent derived errors to accumulate due to extra
4426 // type parameters, we force instantiate_value_path to
4427 // use inference variables instead of the provided types.
4429 } else if !(can_omit && types.len() == 0) && types.len() < required_len {
4431 if type_defs.len() != required_len { "at least " } else { "" };
4432 span_err!(self.tcx.sess, span, E0089,
4433 "too few type parameters provided: \
4434 expected {}{}, found {}",
4436 count(required_len),
4437 count(types.len()));
4440 if !bindings.is_empty() {
4441 span_err!(self.tcx.sess, bindings[0].span, E0182,
4442 "unexpected binding of associated item in expression path \
4443 (only allowed in type paths)");
4447 fn structurally_resolve_type_or_else<F>(&self, sp: Span, ty: Ty<'tcx>, f: F)
4449 where F: Fn() -> Ty<'tcx>
4451 let mut ty = self.resolve_type_vars_with_obligations(ty);
4454 let alternative = f();
4457 if alternative.is_ty_var() || alternative.references_error() {
4458 if !self.is_tainted_by_errors() {
4459 self.type_error_message(sp, |_actual| {
4460 "the type of this value must be known in this context".to_string()
4463 self.demand_suptype(sp, self.tcx.types.err, ty);
4464 ty = self.tcx.types.err;
4466 self.demand_suptype(sp, alternative, ty);
4474 // Resolves `typ` by a single level if `typ` is a type variable. If no
4475 // resolution is possible, then an error is reported.
4476 pub fn structurally_resolved_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
4477 self.structurally_resolve_type_or_else(sp, ty, || {
4483 // Returns true if b contains a break that can exit from b
4484 pub fn may_break(tcx: TyCtxt, id: ast::NodeId, b: &hir::Block) -> bool {
4485 // First: is there an unlabeled break immediately
4487 (loop_query(&b, |e| {
4489 hir::ExprBreak(None) => true,
4493 // Second: is there a labeled break with label
4494 // <id> nested anywhere inside the loop?
4495 (block_query(b, |e| {
4496 if let hir::ExprBreak(Some(_)) = e.node {
4497 tcx.expect_def(e.id) == Def::Label(id)
4504 pub fn check_bounds_are_used<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
4505 tps: &[hir::TyParam],
4507 debug!("check_bounds_are_used(n_tps={}, ty={:?})",
4510 // make a vector of booleans initially false, set to true when used
4511 if tps.is_empty() { return; }
4512 let mut tps_used = vec![false; tps.len()];
4514 for leaf_ty in ty.walk() {
4515 if let ty::TyParam(ParamTy {idx, ..}) = leaf_ty.sty {
4516 debug!("Found use of ty param num {}", idx);
4517 tps_used[idx as usize] = true;
4521 for (i, b) in tps_used.iter().enumerate() {
4523 struct_span_err!(ccx.tcx.sess, tps[i].span, E0091,
4524 "type parameter `{}` is unused",
4526 .span_label(tps[i].span, &format!("unused type parameter"))