]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/mod.rs
rustc: replace TyFnDef in MethodCallee with just the FnSig.
[rust.git] / src / librustc_typeck / check / mod.rs
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.
4 //
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.
10
11 /*
12
13 # check.rs
14
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
18 unknown.
19
20 By far the most complex case is checking the body of a function. This
21 can be broken down into several distinct phases:
22
23 - gather: creates type variables to represent the type of each local
24   variable and pattern binding.
25
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.
31
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.
36
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
41   flexibility.
42
43 - vtable: find and records the impls to use for each trait bound that
44   appears on a type parameter.
45
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.
50
51 ## Intermediate types
52
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 `tcx`.
60
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.
64
65 The types of top-level items, which never contain unbound type
66 variables, are stored directly into the `tcx` tables.
67
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
75 type parameter).
76
77 */
78
79 pub use self::Expectation::*;
80 use self::autoderef::Autoderef;
81 use self::callee::DeferredCallResolution;
82 use self::coercion::{CoerceMany, DynamicCoerceMany};
83 pub use self::compare_method::{compare_impl_method, compare_const_impl};
84 use self::TupleArgumentsFlag::*;
85
86 use astconv::AstConv;
87 use fmt_macros::{Parser, Piece, Position};
88 use hir::def::{Def, CtorKind};
89 use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
90 use rustc_back::slice::ref_slice;
91 use rustc::infer::{self, InferCtxt, InferOk, RegionVariableOrigin};
92 use rustc::infer::type_variable::{TypeVariableOrigin};
93 use rustc::middle::region::CodeExtent;
94 use rustc::ty::subst::{Kind, Subst, Substs};
95 use rustc::traits::{self, FulfillmentContext, ObligationCause, ObligationCauseCode, Reveal};
96 use rustc::ty::{ParamTy, LvaluePreference, NoPreference, PreferMutLvalue};
97 use rustc::ty::{self, Ty, TyCtxt, Visibility};
98 use rustc::ty::{MethodCallee};
99 use rustc::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
100 use rustc::ty::fold::{BottomUpFolder, TypeFoldable};
101 use rustc::ty::maps::Providers;
102 use rustc::ty::util::{Representability, IntTypeExt};
103 use errors::DiagnosticBuilder;
104 use require_c_abi_if_variadic;
105 use session::{Session, CompileResult};
106 use TypeAndSubsts;
107 use lint;
108 use util::common::{ErrorReported, indenter};
109 use util::nodemap::{DefIdMap, FxHashMap, NodeMap};
110
111 use std::cell::{Cell, RefCell};
112 use std::collections::hash_map::Entry;
113 use std::cmp;
114 use std::mem::replace;
115 use std::ops::{self, Deref};
116 use syntax::abi::Abi;
117 use syntax::ast;
118 use syntax::codemap::{self, original_sp, Spanned};
119 use syntax::feature_gate::{GateIssue, emit_feature_err};
120 use syntax::ptr::P;
121 use syntax::symbol::{Symbol, InternedString, keywords};
122 use syntax::util::lev_distance::find_best_match_for_name;
123 use syntax_pos::{self, BytePos, Span, DUMMY_SP};
124
125 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
126 use rustc::hir::itemlikevisit::ItemLikeVisitor;
127 use rustc::hir::{self, PatKind};
128 use rustc::middle::lang_items;
129 use rustc_back::slice;
130 use rustc::middle::const_val::eval_length;
131 use rustc_const_math::ConstInt;
132
133 mod autoderef;
134 pub mod dropck;
135 pub mod _match;
136 pub mod writeback;
137 pub mod regionck;
138 pub mod coercion;
139 pub mod demand;
140 pub mod method;
141 mod upvar;
142 mod wfcheck;
143 mod cast;
144 mod closure;
145 mod callee;
146 mod compare_method;
147 mod intrinsic;
148 mod op;
149
150 /// closures defined within the function.  For example:
151 ///
152 ///     fn foo() {
153 ///         bar(move|| { ... })
154 ///     }
155 ///
156 /// Here, the function `foo()` and the closure passed to
157 /// `bar()` will each have their own `FnCtxt`, but they will
158 /// share the inherited fields.
159 pub struct Inherited<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
160     infcx: InferCtxt<'a, 'gcx, 'tcx>,
161
162     locals: RefCell<NodeMap<Ty<'tcx>>>,
163
164     fulfillment_cx: RefCell<traits::FulfillmentContext<'tcx>>,
165
166     // When we process a call like `c()` where `c` is a closure type,
167     // we may not have decided yet whether `c` is a `Fn`, `FnMut`, or
168     // `FnOnce` closure. In that case, we defer full resolution of the
169     // call until upvar inference can kick in and make the
170     // decision. We keep these deferred resolutions grouped by the
171     // def-id of the closure, so that once we decide, we can easily go
172     // back and process them.
173     deferred_call_resolutions: RefCell<DefIdMap<Vec<DeferredCallResolution<'gcx, 'tcx>>>>,
174
175     deferred_cast_checks: RefCell<Vec<cast::CastCheck<'tcx>>>,
176
177     // Anonymized types found in explicit return types and their
178     // associated fresh inference variable. Writeback resolves these
179     // variables to get the concrete type, which can be used to
180     // deanonymize TyAnon, after typeck is done with all functions.
181     anon_types: RefCell<NodeMap<Ty<'tcx>>>,
182
183     /// Each type parameter has an implicit region bound that
184     /// indicates it must outlive at least the function body (the user
185     /// may specify stronger requirements). This field indicates the
186     /// region of the callee. If it is `None`, then the parameter
187     /// environment is for an item or something where the "callee" is
188     /// not clear.
189     implicit_region_bound: Option<ty::Region<'tcx>>,
190 }
191
192 impl<'a, 'gcx, 'tcx> Deref for Inherited<'a, 'gcx, 'tcx> {
193     type Target = InferCtxt<'a, 'gcx, 'tcx>;
194     fn deref(&self) -> &Self::Target {
195         &self.infcx
196     }
197 }
198
199 /// When type-checking an expression, we propagate downward
200 /// whatever type hint we are able in the form of an `Expectation`.
201 #[derive(Copy, Clone, Debug)]
202 pub enum Expectation<'tcx> {
203     /// We know nothing about what type this expression should have.
204     NoExpectation,
205
206     /// This expression should have the type given (or some subtype)
207     ExpectHasType(Ty<'tcx>),
208
209     /// This expression will be cast to the `Ty`
210     ExpectCastableToType(Ty<'tcx>),
211
212     /// This rvalue expression will be wrapped in `&` or `Box` and coerced
213     /// to `&Ty` or `Box<Ty>`, respectively. `Ty` is `[A]` or `Trait`.
214     ExpectRvalueLikeUnsized(Ty<'tcx>),
215 }
216
217 impl<'a, 'gcx, 'tcx> Expectation<'tcx> {
218     // Disregard "castable to" expectations because they
219     // can lead us astray. Consider for example `if cond
220     // {22} else {c} as u8` -- if we propagate the
221     // "castable to u8" constraint to 22, it will pick the
222     // type 22u8, which is overly constrained (c might not
223     // be a u8). In effect, the problem is that the
224     // "castable to" expectation is not the tightest thing
225     // we can say, so we want to drop it in this case.
226     // The tightest thing we can say is "must unify with
227     // else branch". Note that in the case of a "has type"
228     // constraint, this limitation does not hold.
229
230     // If the expected type is just a type variable, then don't use
231     // an expected type. Otherwise, we might write parts of the type
232     // when checking the 'then' block which are incompatible with the
233     // 'else' branch.
234     fn adjust_for_branches(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Expectation<'tcx> {
235         match *self {
236             ExpectHasType(ety) => {
237                 let ety = fcx.shallow_resolve(ety);
238                 if !ety.is_ty_var() {
239                     ExpectHasType(ety)
240                 } else {
241                     NoExpectation
242                 }
243             }
244             ExpectRvalueLikeUnsized(ety) => {
245                 ExpectRvalueLikeUnsized(ety)
246             }
247             _ => NoExpectation
248         }
249     }
250
251     /// Provide an expectation for an rvalue expression given an *optional*
252     /// hint, which is not required for type safety (the resulting type might
253     /// be checked higher up, as is the case with `&expr` and `box expr`), but
254     /// is useful in determining the concrete type.
255     ///
256     /// The primary use case is where the expected type is a fat pointer,
257     /// like `&[isize]`. For example, consider the following statement:
258     ///
259     ///    let x: &[isize] = &[1, 2, 3];
260     ///
261     /// In this case, the expected type for the `&[1, 2, 3]` expression is
262     /// `&[isize]`. If however we were to say that `[1, 2, 3]` has the
263     /// expectation `ExpectHasType([isize])`, that would be too strong --
264     /// `[1, 2, 3]` does not have the type `[isize]` but rather `[isize; 3]`.
265     /// It is only the `&[1, 2, 3]` expression as a whole that can be coerced
266     /// to the type `&[isize]`. Therefore, we propagate this more limited hint,
267     /// which still is useful, because it informs integer literals and the like.
268     /// See the test case `test/run-pass/coerce-expect-unsized.rs` and #20169
269     /// for examples of where this comes up,.
270     fn rvalue_hint(fcx: &FnCtxt<'a, 'gcx, 'tcx>, ty: Ty<'tcx>) -> Expectation<'tcx> {
271         match fcx.tcx.struct_tail(ty).sty {
272             ty::TySlice(_) | ty::TyStr | ty::TyDynamic(..) => {
273                 ExpectRvalueLikeUnsized(ty)
274             }
275             _ => ExpectHasType(ty)
276         }
277     }
278
279     // Resolves `expected` by a single level if it is a variable. If
280     // there is no expected type or resolution is not possible (e.g.,
281     // no constraints yet present), just returns `None`.
282     fn resolve(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Expectation<'tcx> {
283         match self {
284             NoExpectation => {
285                 NoExpectation
286             }
287             ExpectCastableToType(t) => {
288                 ExpectCastableToType(fcx.resolve_type_vars_if_possible(&t))
289             }
290             ExpectHasType(t) => {
291                 ExpectHasType(fcx.resolve_type_vars_if_possible(&t))
292             }
293             ExpectRvalueLikeUnsized(t) => {
294                 ExpectRvalueLikeUnsized(fcx.resolve_type_vars_if_possible(&t))
295             }
296         }
297     }
298
299     fn to_option(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
300         match self.resolve(fcx) {
301             NoExpectation => None,
302             ExpectCastableToType(ty) |
303             ExpectHasType(ty) |
304             ExpectRvalueLikeUnsized(ty) => Some(ty),
305         }
306     }
307
308     /// It sometimes happens that we want to turn an expectation into
309     /// a **hard constraint** (i.e., something that must be satisfied
310     /// for the program to type-check). `only_has_type` will return
311     /// such a constraint, if it exists.
312     fn only_has_type(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
313         match self.resolve(fcx) {
314             ExpectHasType(ty) => Some(ty),
315             _ => None
316         }
317     }
318
319     /// Like `only_has_type`, but instead of returning `None` if no
320     /// hard constraint exists, creates a fresh type variable.
321     fn coercion_target_type(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, span: Span) -> Ty<'tcx> {
322         self.only_has_type(fcx)
323             .unwrap_or_else(|| fcx.next_ty_var(TypeVariableOrigin::MiscVariable(span)))
324     }
325 }
326
327 #[derive(Copy, Clone)]
328 pub struct UnsafetyState {
329     pub def: ast::NodeId,
330     pub unsafety: hir::Unsafety,
331     pub unsafe_push_count: u32,
332     from_fn: bool
333 }
334
335 impl UnsafetyState {
336     pub fn function(unsafety: hir::Unsafety, def: ast::NodeId) -> UnsafetyState {
337         UnsafetyState { def: def, unsafety: unsafety, unsafe_push_count: 0, from_fn: true }
338     }
339
340     pub fn recurse(&mut self, blk: &hir::Block) -> UnsafetyState {
341         match self.unsafety {
342             // If this unsafe, then if the outer function was already marked as
343             // unsafe we shouldn't attribute the unsafe'ness to the block. This
344             // way the block can be warned about instead of ignoring this
345             // extraneous block (functions are never warned about).
346             hir::Unsafety::Unsafe if self.from_fn => *self,
347
348             unsafety => {
349                 let (unsafety, def, count) = match blk.rules {
350                     hir::PushUnsafeBlock(..) =>
351                         (unsafety, blk.id, self.unsafe_push_count.checked_add(1).unwrap()),
352                     hir::PopUnsafeBlock(..) =>
353                         (unsafety, blk.id, self.unsafe_push_count.checked_sub(1).unwrap()),
354                     hir::UnsafeBlock(..) =>
355                         (hir::Unsafety::Unsafe, blk.id, self.unsafe_push_count),
356                     hir::DefaultBlock =>
357                         (unsafety, self.def, self.unsafe_push_count),
358                 };
359                 UnsafetyState{ def: def,
360                                unsafety: unsafety,
361                                unsafe_push_count: count,
362                                from_fn: false }
363             }
364         }
365     }
366 }
367
368 #[derive(Debug, Copy, Clone)]
369 pub enum LvalueOp {
370     Deref,
371     Index
372 }
373
374 /// Tracks whether executing a node may exit normally (versus
375 /// return/break/panic, which "diverge", leaving dead code in their
376 /// wake). Tracked semi-automatically (through type variables marked
377 /// as diverging), with some manual adjustments for control-flow
378 /// primitives (approximating a CFG).
379 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
380 pub enum Diverges {
381     /// Potentially unknown, some cases converge,
382     /// others require a CFG to determine them.
383     Maybe,
384
385     /// Definitely known to diverge and therefore
386     /// not reach the next sibling or its parent.
387     Always,
388
389     /// Same as `Always` but with a reachability
390     /// warning already emitted
391     WarnedAlways
392 }
393
394 // Convenience impls for combinig `Diverges`.
395
396 impl ops::BitAnd for Diverges {
397     type Output = Self;
398     fn bitand(self, other: Self) -> Self {
399         cmp::min(self, other)
400     }
401 }
402
403 impl ops::BitOr for Diverges {
404     type Output = Self;
405     fn bitor(self, other: Self) -> Self {
406         cmp::max(self, other)
407     }
408 }
409
410 impl ops::BitAndAssign for Diverges {
411     fn bitand_assign(&mut self, other: Self) {
412         *self = *self & other;
413     }
414 }
415
416 impl ops::BitOrAssign for Diverges {
417     fn bitor_assign(&mut self, other: Self) {
418         *self = *self | other;
419     }
420 }
421
422 impl Diverges {
423     fn always(self) -> bool {
424         self >= Diverges::Always
425     }
426 }
427
428 pub struct BreakableCtxt<'gcx: 'tcx, 'tcx> {
429     may_break: bool,
430
431     // this is `null` for loops where break with a value is illegal,
432     // such as `while`, `for`, and `while let`
433     coerce: Option<DynamicCoerceMany<'gcx, 'tcx>>,
434 }
435
436 pub struct EnclosingBreakables<'gcx: 'tcx, 'tcx> {
437     stack: Vec<BreakableCtxt<'gcx, 'tcx>>,
438     by_id: NodeMap<usize>,
439 }
440
441 impl<'gcx, 'tcx> EnclosingBreakables<'gcx, 'tcx> {
442     fn find_breakable(&mut self, target_id: ast::NodeId) -> &mut BreakableCtxt<'gcx, 'tcx> {
443         let ix = *self.by_id.get(&target_id).unwrap_or_else(|| {
444             bug!("could not find enclosing breakable with id {}", target_id);
445         });
446         &mut self.stack[ix]
447     }
448 }
449
450 pub struct FnCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
451     body_id: ast::NodeId,
452
453     // Number of errors that had been reported when we started
454     // checking this function. On exit, if we find that *more* errors
455     // have been reported, we will skip regionck and other work that
456     // expects the types within the function to be consistent.
457     err_count_on_creation: usize,
458
459     ret_coercion: Option<RefCell<DynamicCoerceMany<'gcx, 'tcx>>>,
460
461     ps: RefCell<UnsafetyState>,
462
463     /// Whether the last checked node generates a divergence (e.g.,
464     /// `return` will set this to Always). In general, when entering
465     /// an expression or other node in the tree, the initial value
466     /// indicates whether prior parts of the containing expression may
467     /// have diverged. It is then typically set to `Maybe` (and the
468     /// old value remembered) for processing the subparts of the
469     /// current expression. As each subpart is processed, they may set
470     /// the flag to `Always` etc.  Finally, at the end, we take the
471     /// result and "union" it with the original value, so that when we
472     /// return the flag indicates if any subpart of the the parent
473     /// expression (up to and including this part) has diverged.  So,
474     /// if you read it after evaluating a subexpression `X`, the value
475     /// you get indicates whether any subexpression that was
476     /// evaluating up to and including `X` diverged.
477     ///
478     /// We use this flag for two purposes:
479     ///
480     /// - To warn about unreachable code: if, after processing a
481     ///   sub-expression but before we have applied the effects of the
482     ///   current node, we see that the flag is set to `Always`, we
483     ///   can issue a warning. This corresponds to something like
484     ///   `foo(return)`; we warn on the `foo()` expression. (We then
485     ///   update the flag to `WarnedAlways` to suppress duplicate
486     ///   reports.) Similarly, if we traverse to a fresh statement (or
487     ///   tail expression) from a `Always` setting, we will isssue a
488     ///   warning. This corresponds to something like `{return;
489     ///   foo();}` or `{return; 22}`, where we would warn on the
490     ///   `foo()` or `22`.
491     ///
492     /// - To permit assignment into a local variable or other lvalue
493     ///   (including the "return slot") of type `!`.  This is allowed
494     ///   if **either** the type of value being assigned is `!`, which
495     ///   means the current code is dead, **or** the expression's
496     ///   divering flag is true, which means that a divering value was
497     ///   wrapped (e.g., `let x: ! = foo(return)`).
498     ///
499     /// To repeat the last point: an expression represents dead-code
500     /// if, after checking it, **either** its type is `!` OR the
501     /// diverges flag is set to something other than `Maybe`.
502     diverges: Cell<Diverges>,
503
504     /// Whether any child nodes have any type errors.
505     has_errors: Cell<bool>,
506
507     enclosing_breakables: RefCell<EnclosingBreakables<'gcx, 'tcx>>,
508
509     inh: &'a Inherited<'a, 'gcx, 'tcx>,
510 }
511
512 impl<'a, 'gcx, 'tcx> Deref for FnCtxt<'a, 'gcx, 'tcx> {
513     type Target = Inherited<'a, 'gcx, 'tcx>;
514     fn deref(&self) -> &Self::Target {
515         &self.inh
516     }
517 }
518
519 /// Helper type of a temporary returned by Inherited::build(...).
520 /// Necessary because we can't write the following bound:
521 /// F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(Inherited<'b, 'gcx, 'tcx>).
522 pub struct InheritedBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
523     infcx: infer::InferCtxtBuilder<'a, 'gcx, 'tcx>,
524     def_id: DefId,
525 }
526
527 impl<'a, 'gcx, 'tcx> Inherited<'a, 'gcx, 'tcx> {
528     pub fn build(tcx: TyCtxt<'a, 'gcx, 'gcx>, def_id: DefId)
529                  -> InheritedBuilder<'a, 'gcx, 'tcx> {
530         let tables = ty::TypeckTables::empty();
531         let param_env = tcx.param_env(def_id);
532         InheritedBuilder {
533             infcx: tcx.infer_ctxt((tables, param_env), Reveal::UserFacing),
534             def_id,
535         }
536     }
537 }
538
539 impl<'a, 'gcx, 'tcx> InheritedBuilder<'a, 'gcx, 'tcx> {
540     fn enter<F, R>(&'tcx mut self, f: F) -> R
541         where F: for<'b> FnOnce(Inherited<'b, 'gcx, 'tcx>) -> R
542     {
543         let def_id = self.def_id;
544         self.infcx.enter(|infcx| f(Inherited::new(infcx, def_id)))
545     }
546 }
547
548 impl<'a, 'gcx, 'tcx> Inherited<'a, 'gcx, 'tcx> {
549     fn new(infcx: InferCtxt<'a, 'gcx, 'tcx>, def_id: DefId) -> Self {
550         let tcx = infcx.tcx;
551         let item_id = tcx.hir.as_local_node_id(def_id);
552         let body_id = item_id.and_then(|id| tcx.hir.maybe_body_owned_by(id));
553         let implicit_region_bound = body_id.map(|body| {
554             tcx.mk_region(ty::ReScope(CodeExtent::CallSiteScope(body)))
555         });
556
557         Inherited {
558             infcx: infcx,
559             fulfillment_cx: RefCell::new(traits::FulfillmentContext::new()),
560             locals: RefCell::new(NodeMap()),
561             deferred_call_resolutions: RefCell::new(DefIdMap()),
562             deferred_cast_checks: RefCell::new(Vec::new()),
563             anon_types: RefCell::new(NodeMap()),
564             implicit_region_bound,
565         }
566     }
567
568     fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
569         debug!("register_predicate({:?})", obligation);
570         if obligation.has_escaping_regions() {
571             span_bug!(obligation.cause.span, "escaping regions in predicate {:?}",
572                       obligation);
573         }
574         self.fulfillment_cx
575             .borrow_mut()
576             .register_predicate_obligation(self, obligation);
577     }
578
579     fn register_predicates(&self, obligations: Vec<traits::PredicateObligation<'tcx>>) {
580         for obligation in obligations {
581             self.register_predicate(obligation);
582         }
583     }
584
585     fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
586         self.register_predicates(infer_ok.obligations);
587         infer_ok.value
588     }
589
590     fn normalize_associated_types_in<T>(&self,
591                                         span: Span,
592                                         body_id: ast::NodeId,
593                                         value: &T) -> T
594         where T : TypeFoldable<'tcx>
595     {
596         let ok = self.normalize_associated_types_in_as_infer_ok(span, body_id, value);
597         self.register_infer_ok_obligations(ok)
598     }
599
600     fn normalize_associated_types_in_as_infer_ok<T>(&self,
601                                                     span: Span,
602                                                     body_id: ast::NodeId,
603                                                     value: &T)
604                                                     -> InferOk<'tcx, T>
605         where T : TypeFoldable<'tcx>
606     {
607         debug!("normalize_associated_types_in(value={:?})", value);
608         let mut selcx = traits::SelectionContext::new(self);
609         let cause = ObligationCause::misc(span, body_id);
610         let traits::Normalized { value, obligations } =
611             traits::normalize(&mut selcx, cause, value);
612         debug!("normalize_associated_types_in: result={:?} predicates={:?}",
613             value,
614             obligations);
615         InferOk { value, obligations }
616     }
617
618     /// Replace any late-bound regions bound in `value` with
619     /// free variants attached to `all_outlive_scope`.
620     fn liberate_late_bound_regions<T>(&self,
621         all_outlive_scope: DefId,
622         value: &ty::Binder<T>)
623         -> T
624         where T: TypeFoldable<'tcx>
625     {
626         self.tcx.replace_late_bound_regions(value, |br| {
627             self.tcx.mk_region(ty::ReFree(ty::FreeRegion {
628                 scope: all_outlive_scope,
629                 bound_region: br
630             }))
631         }).0
632     }
633 }
634
635 struct CheckItemTypesVisitor<'a, 'tcx: 'a> { tcx: TyCtxt<'a, 'tcx, 'tcx> }
636
637 impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CheckItemTypesVisitor<'a, 'tcx> {
638     fn visit_item(&mut self, i: &'tcx hir::Item) {
639         check_item_type(self.tcx, i);
640     }
641     fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) { }
642     fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) { }
643 }
644
645 pub fn check_wf_new<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> CompileResult {
646     tcx.sess.track_errors(|| {
647         let mut visit = wfcheck::CheckTypeWellFormedVisitor::new(tcx);
648         tcx.hir.krate().visit_all_item_likes(&mut visit.as_deep_visitor());
649     })
650 }
651
652 pub fn check_item_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> CompileResult {
653     tcx.sess.track_errors(|| {
654         tcx.hir.krate().visit_all_item_likes(&mut CheckItemTypesVisitor { tcx });
655     })
656 }
657
658 pub fn check_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> CompileResult {
659     tcx.typeck_item_bodies(LOCAL_CRATE)
660 }
661
662 fn typeck_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) -> CompileResult {
663     debug_assert!(crate_num == LOCAL_CRATE);
664     tcx.sess.track_errors(|| {
665         for body_owner_def_id in tcx.body_owners() {
666             tcx.typeck_tables_of(body_owner_def_id);
667         }
668     })
669 }
670
671 pub fn provide(providers: &mut Providers) {
672     *providers = Providers {
673         typeck_item_bodies,
674         typeck_tables_of,
675         has_typeck_tables,
676         closure_type,
677         closure_kind,
678         adt_destructor,
679         ..*providers
680     };
681 }
682
683 fn closure_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
684                           def_id: DefId)
685                           -> ty::PolyFnSig<'tcx> {
686     let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
687     tcx.typeck_tables_of(def_id).closure_tys[&node_id]
688 }
689
690 fn closure_kind<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
691                           def_id: DefId)
692                           -> ty::ClosureKind {
693     let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
694     tcx.typeck_tables_of(def_id).closure_kinds[&node_id].0
695 }
696
697 fn adt_destructor<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
698                             def_id: DefId)
699                             -> Option<ty::Destructor> {
700     tcx.calculate_dtor(def_id, &mut dropck::check_drop_impl)
701 }
702
703 /// If this def-id is a "primary tables entry", returns `Some((body_id, decl))`
704 /// with information about it's body-id and fn-decl (if any). Otherwise,
705 /// returns `None`.
706 ///
707 /// If this function returns "some", then `typeck_tables(def_id)` will
708 /// succeed; if it returns `None`, then `typeck_tables(def_id)` may or
709 /// may not succeed.  In some cases where this function returns `None`
710 /// (notably closures), `typeck_tables(def_id)` would wind up
711 /// redirecting to the owning function.
712 fn primary_body_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
713                              id: ast::NodeId)
714                              -> Option<(hir::BodyId, Option<&'tcx hir::FnDecl>)>
715 {
716     match tcx.hir.get(id) {
717         hir::map::NodeItem(item) => {
718             match item.node {
719                 hir::ItemConst(_, body) |
720                 hir::ItemStatic(_, _, body) =>
721                     Some((body, None)),
722                 hir::ItemFn(ref decl, .., body) =>
723                     Some((body, Some(decl))),
724                 _ =>
725                     None,
726             }
727         }
728         hir::map::NodeTraitItem(item) => {
729             match item.node {
730                 hir::TraitItemKind::Const(_, Some(body)) =>
731                     Some((body, None)),
732                 hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) =>
733                     Some((body, Some(&sig.decl))),
734                 _ =>
735                     None,
736             }
737         }
738         hir::map::NodeImplItem(item) => {
739             match item.node {
740                 hir::ImplItemKind::Const(_, body) =>
741                     Some((body, None)),
742                 hir::ImplItemKind::Method(ref sig, body) =>
743                     Some((body, Some(&sig.decl))),
744                 _ =>
745                     None,
746             }
747         }
748         hir::map::NodeExpr(expr) => {
749             // FIXME(eddyb) Closures should have separate
750             // function definition IDs and expression IDs.
751             // Type-checking should not let closures get
752             // this far in a constant position.
753             // Assume that everything other than closures
754             // is a constant "initializer" expression.
755             match expr.node {
756                 hir::ExprClosure(..) =>
757                     None,
758                 _ =>
759                     Some((hir::BodyId { node_id: expr.id }, None)),
760             }
761         }
762         _ => None,
763     }
764 }
765
766 fn has_typeck_tables<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
767                                def_id: DefId)
768                                -> bool {
769     // Closures' tables come from their outermost function,
770     // as they are part of the same "inference environment".
771     let outer_def_id = tcx.closure_base_def_id(def_id);
772     if outer_def_id != def_id {
773         return tcx.has_typeck_tables(outer_def_id);
774     }
775
776     let id = tcx.hir.as_local_node_id(def_id).unwrap();
777     primary_body_of(tcx, id).is_some()
778 }
779
780 fn typeck_tables_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
781                               def_id: DefId)
782                               -> &'tcx ty::TypeckTables<'tcx> {
783     // Closures' tables come from their outermost function,
784     // as they are part of the same "inference environment".
785     let outer_def_id = tcx.closure_base_def_id(def_id);
786     if outer_def_id != def_id {
787         return tcx.typeck_tables_of(outer_def_id);
788     }
789
790     let id = tcx.hir.as_local_node_id(def_id).unwrap();
791     let span = tcx.hir.span(id);
792
793     // Figure out what primary body this item has.
794     let (body_id, fn_decl) = primary_body_of(tcx, id).unwrap_or_else(|| {
795         span_bug!(span, "can't type-check body of {:?}", def_id);
796     });
797     let body = tcx.hir.body(body_id);
798
799     Inherited::build(tcx, def_id).enter(|inh| {
800         let fcx = if let Some(decl) = fn_decl {
801             let fn_sig = tcx.type_of(def_id).fn_sig();
802
803             check_abi(tcx, span, fn_sig.abi());
804
805             // Compute the fty from point of view of inside fn.
806             let fn_sig =
807                 inh.liberate_late_bound_regions(def_id, &fn_sig);
808             let fn_sig =
809                 inh.normalize_associated_types_in(body.value.span, body_id.node_id, &fn_sig);
810
811             check_fn(&inh, fn_sig, decl, id, body)
812         } else {
813             let fcx = FnCtxt::new(&inh, body.value.id);
814             let expected_type = tcx.type_of(def_id);
815             let expected_type = fcx.normalize_associated_types_in(body.value.span, &expected_type);
816             fcx.require_type_is_sized(expected_type, body.value.span, traits::ConstSized);
817
818             // Gather locals in statics (because of block expressions).
819             // This is technically unnecessary because locals in static items are forbidden,
820             // but prevents type checking from blowing up before const checking can properly
821             // emit an error.
822             GatherLocalsVisitor { fcx: &fcx }.visit_body(body);
823
824             fcx.check_expr_coercable_to_type(&body.value, expected_type);
825
826             fcx
827         };
828
829         fcx.select_all_obligations_and_apply_defaults();
830         fcx.closure_analyze(body);
831         fcx.select_obligations_where_possible();
832         fcx.check_casts();
833         fcx.select_all_obligations_or_error();
834
835         if fn_decl.is_some() {
836             fcx.regionck_fn(id, body);
837         } else {
838             fcx.regionck_expr(body);
839         }
840
841         fcx.resolve_type_vars_in_body(body)
842     })
843 }
844
845 fn check_abi<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, span: Span, abi: Abi) {
846     if !tcx.sess.target.target.is_abi_supported(abi) {
847         struct_span_err!(tcx.sess, span, E0570,
848             "The ABI `{}` is not supported for the current target", abi).emit()
849     }
850 }
851
852 struct GatherLocalsVisitor<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
853     fcx: &'a FnCtxt<'a, 'gcx, 'tcx>
854 }
855
856 impl<'a, 'gcx, 'tcx> GatherLocalsVisitor<'a, 'gcx, 'tcx> {
857     fn assign(&mut self, span: Span, nid: ast::NodeId, ty_opt: Option<Ty<'tcx>>) -> Ty<'tcx> {
858         match ty_opt {
859             None => {
860                 // infer the variable's type
861                 let var_ty = self.fcx.next_ty_var(TypeVariableOrigin::TypeInference(span));
862                 self.fcx.locals.borrow_mut().insert(nid, var_ty);
863                 var_ty
864             }
865             Some(typ) => {
866                 // take type that the user specified
867                 self.fcx.locals.borrow_mut().insert(nid, typ);
868                 typ
869             }
870         }
871     }
872 }
873
874 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for GatherLocalsVisitor<'a, 'gcx, 'tcx> {
875     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
876         NestedVisitorMap::None
877     }
878
879     // Add explicitly-declared locals.
880     fn visit_local(&mut self, local: &'gcx hir::Local) {
881         let o_ty = match local.ty {
882             Some(ref ty) => Some(self.fcx.to_ty(&ty)),
883             None => None
884         };
885         self.assign(local.span, local.id, o_ty);
886         debug!("Local variable {:?} is assigned type {}",
887                local.pat,
888                self.fcx.ty_to_string(
889                    self.fcx.locals.borrow().get(&local.id).unwrap().clone()));
890         intravisit::walk_local(self, local);
891     }
892
893     // Add pattern bindings.
894     fn visit_pat(&mut self, p: &'gcx hir::Pat) {
895         if let PatKind::Binding(_, _, ref path1, _) = p.node {
896             let var_ty = self.assign(p.span, p.id, None);
897
898             self.fcx.require_type_is_sized(var_ty, p.span,
899                                            traits::VariableType(p.id));
900
901             debug!("Pattern binding {} is assigned to {} with type {:?}",
902                    path1.node,
903                    self.fcx.ty_to_string(
904                        self.fcx.locals.borrow().get(&p.id).unwrap().clone()),
905                    var_ty);
906         }
907         intravisit::walk_pat(self, p);
908     }
909
910     // Don't descend into the bodies of nested closures
911     fn visit_fn(&mut self, _: intravisit::FnKind<'gcx>, _: &'gcx hir::FnDecl,
912                 _: hir::BodyId, _: Span, _: ast::NodeId) { }
913 }
914
915 /// Helper used for fns and closures. Does the grungy work of checking a function
916 /// body and returns the function context used for that purpose, since in the case of a fn item
917 /// there is still a bit more to do.
918 ///
919 /// * ...
920 /// * inherited: other fields inherited from the enclosing fn (if any)
921 fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
922                             fn_sig: ty::FnSig<'tcx>,
923                             decl: &'gcx hir::FnDecl,
924                             fn_id: ast::NodeId,
925                             body: &'gcx hir::Body)
926                             -> FnCtxt<'a, 'gcx, 'tcx>
927 {
928     let mut fn_sig = fn_sig.clone();
929
930     debug!("check_fn(sig={:?}, fn_id={})", fn_sig, fn_id);
931
932     // Create the function context.  This is either derived from scratch or,
933     // in the case of function expressions, based on the outer context.
934     let mut fcx = FnCtxt::new(inherited, body.value.id);
935     *fcx.ps.borrow_mut() = UnsafetyState::function(fn_sig.unsafety, fn_id);
936
937     let ret_ty = fn_sig.output();
938     fcx.require_type_is_sized(ret_ty, decl.output.span(), traits::ReturnType);
939     let ret_ty = fcx.instantiate_anon_types(&ret_ty);
940     fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
941     fn_sig = fcx.tcx.mk_fn_sig(
942         fn_sig.inputs().iter().cloned(),
943         ret_ty,
944         fn_sig.variadic,
945         fn_sig.unsafety,
946         fn_sig.abi
947     );
948
949     GatherLocalsVisitor { fcx: &fcx, }.visit_body(body);
950
951     // Add formal parameters.
952     for (arg_ty, arg) in fn_sig.inputs().iter().zip(&body.arguments) {
953         // The type of the argument must be well-formed.
954         //
955         // NB -- this is now checked in wfcheck, but that
956         // currently only results in warnings, so we issue an
957         // old-style WF obligation here so that we still get the
958         // errors that we used to get.
959         fcx.register_old_wf_obligation(arg_ty, arg.pat.span, traits::MiscObligation);
960
961         // Check the pattern.
962         fcx.check_pat_arg(&arg.pat, arg_ty, true);
963         fcx.write_ty(arg.id, arg_ty);
964     }
965
966     inherited.tables.borrow_mut().liberated_fn_sigs.insert(fn_id, fn_sig);
967
968     fcx.check_return_expr(&body.value);
969
970     // Finalize the return check by taking the LUB of the return types
971     // we saw and assigning it to the expected return type. This isn't
972     // really expected to fail, since the coercions would have failed
973     // earlier when trying to find a LUB.
974     //
975     // However, the behavior around `!` is sort of complex. In the
976     // event that the `actual_return_ty` comes back as `!`, that
977     // indicates that the fn either does not return or "returns" only
978     // values of type `!`. In this case, if there is an expected
979     // return type that is *not* `!`, that should be ok. But if the
980     // return type is being inferred, we want to "fallback" to `!`:
981     //
982     //     let x = move || panic!();
983     //
984     // To allow for that, I am creating a type variable with diverging
985     // fallback. This was deemed ever so slightly better than unifying
986     // the return value with `!` because it allows for the caller to
987     // make more assumptions about the return type (e.g., they could do
988     //
989     //     let y: Option<u32> = Some(x());
990     //
991     // which would then cause this return type to become `u32`, not
992     // `!`).
993     let coercion = fcx.ret_coercion.take().unwrap().into_inner();
994     let mut actual_return_ty = coercion.complete(&fcx);
995     if actual_return_ty.is_never() {
996         actual_return_ty = fcx.next_diverging_ty_var(
997             TypeVariableOrigin::DivergingFn(body.value.span));
998     }
999     fcx.demand_suptype(body.value.span, ret_ty, actual_return_ty);
1000
1001     fcx
1002 }
1003
1004 fn check_struct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1005                           id: ast::NodeId,
1006                           span: Span) {
1007     let def_id = tcx.hir.local_def_id(id);
1008     let def = tcx.adt_def(def_id);
1009     def.destructor(tcx); // force the destructor to be evaluated
1010     check_representable(tcx, span, def_id);
1011
1012     if def.repr.simd() {
1013         check_simd(tcx, span, def_id);
1014     }
1015
1016     // if struct is packed and not aligned, check fields for alignment.
1017     // Checks for combining packed and align attrs on single struct are done elsewhere.
1018     if tcx.adt_def(def_id).repr.packed() && tcx.adt_def(def_id).repr.align == 0 {
1019         check_packed(tcx, span, def_id);
1020     }
1021 }
1022
1023 fn check_union<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1024                          id: ast::NodeId,
1025                          span: Span) {
1026     let def_id = tcx.hir.local_def_id(id);
1027     let def = tcx.adt_def(def_id);
1028     def.destructor(tcx); // force the destructor to be evaluated
1029     check_representable(tcx, span, def_id);
1030 }
1031
1032 pub fn check_item_type<'a,'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, it: &'tcx hir::Item) {
1033     debug!("check_item_type(it.id={}, it.name={})",
1034            it.id,
1035            tcx.item_path_str(tcx.hir.local_def_id(it.id)));
1036     let _indenter = indenter();
1037     match it.node {
1038       // Consts can play a role in type-checking, so they are included here.
1039       hir::ItemStatic(..) |
1040       hir::ItemConst(..) => {
1041         tcx.typeck_tables_of(tcx.hir.local_def_id(it.id));
1042       }
1043       hir::ItemEnum(ref enum_definition, _) => {
1044         check_enum(tcx,
1045                    it.span,
1046                    &enum_definition.variants,
1047                    it.id);
1048       }
1049       hir::ItemFn(..) => {} // entirely within check_item_body
1050       hir::ItemImpl(.., ref impl_item_refs) => {
1051           debug!("ItemImpl {} with id {}", it.name, it.id);
1052           let impl_def_id = tcx.hir.local_def_id(it.id);
1053           if let Some(impl_trait_ref) = tcx.impl_trait_ref(impl_def_id) {
1054               check_impl_items_against_trait(tcx,
1055                                              it.span,
1056                                              impl_def_id,
1057                                              impl_trait_ref,
1058                                              impl_item_refs);
1059               let trait_def_id = impl_trait_ref.def_id;
1060               check_on_unimplemented(tcx, trait_def_id, it);
1061           }
1062       }
1063       hir::ItemTrait(..) => {
1064         let def_id = tcx.hir.local_def_id(it.id);
1065         check_on_unimplemented(tcx, def_id, it);
1066       }
1067       hir::ItemStruct(..) => {
1068         check_struct(tcx, it.id, it.span);
1069       }
1070       hir::ItemUnion(..) => {
1071         check_union(tcx, it.id, it.span);
1072       }
1073       hir::ItemTy(_, ref generics) => {
1074         let def_id = tcx.hir.local_def_id(it.id);
1075         let pty_ty = tcx.type_of(def_id);
1076         check_bounds_are_used(tcx, generics, pty_ty);
1077       }
1078       hir::ItemForeignMod(ref m) => {
1079         check_abi(tcx, it.span, m.abi);
1080
1081         if m.abi == Abi::RustIntrinsic {
1082             for item in &m.items {
1083                 intrinsic::check_intrinsic_type(tcx, item);
1084             }
1085         } else if m.abi == Abi::PlatformIntrinsic {
1086             for item in &m.items {
1087                 intrinsic::check_platform_intrinsic_type(tcx, item);
1088             }
1089         } else {
1090             for item in &m.items {
1091                 let generics = tcx.generics_of(tcx.hir.local_def_id(item.id));
1092                 if !generics.types.is_empty() {
1093                     let mut err = struct_span_err!(tcx.sess, item.span, E0044,
1094                         "foreign items may not have type parameters");
1095                     span_help!(&mut err, item.span,
1096                         "consider using specialization instead of \
1097                         type parameters");
1098                     err.emit();
1099                 }
1100
1101                 if let hir::ForeignItemFn(ref fn_decl, _, _) = item.node {
1102                     require_c_abi_if_variadic(tcx, fn_decl, m.abi, item.span);
1103                 }
1104             }
1105         }
1106       }
1107       _ => {/* nothing to do */ }
1108     }
1109 }
1110
1111 fn check_on_unimplemented<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1112                                     def_id: DefId,
1113                                     item: &hir::Item) {
1114     let generics = tcx.generics_of(def_id);
1115     if let Some(ref attr) = item.attrs.iter().find(|a| {
1116         a.check_name("rustc_on_unimplemented")
1117     }) {
1118         if let Some(istring) = attr.value_str() {
1119             let istring = istring.as_str();
1120             let parser = Parser::new(&istring);
1121             let types = &generics.types;
1122             for token in parser {
1123                 match token {
1124                     Piece::String(_) => (), // Normal string, no need to check it
1125                     Piece::NextArgument(a) => match a.position {
1126                         // `{Self}` is allowed
1127                         Position::ArgumentNamed(s) if s == "Self" => (),
1128                         // So is `{A}` if A is a type parameter
1129                         Position::ArgumentNamed(s) => match types.iter().find(|t| {
1130                             t.name == s
1131                         }) {
1132                             Some(_) => (),
1133                             None => {
1134                                 let name = tcx.item_name(def_id);
1135                                 span_err!(tcx.sess, attr.span, E0230,
1136                                                  "there is no type parameter \
1137                                                           {} on trait {}",
1138                                                            s, name);
1139                             }
1140                         },
1141                         // `{:1}` and `{}` are not to be used
1142                         Position::ArgumentIs(_) => {
1143                             span_err!(tcx.sess, attr.span, E0231,
1144                                                   "only named substitution \
1145                                                    parameters are allowed");
1146                         }
1147                     }
1148                 }
1149             }
1150         } else {
1151             struct_span_err!(
1152                 tcx.sess, attr.span, E0232,
1153                 "this attribute must have a value")
1154                 .span_label(attr.span, "attribute requires a value")
1155                 .note(&format!("eg `#[rustc_on_unimplemented = \"foo\"]`"))
1156                 .emit();
1157         }
1158     }
1159 }
1160
1161 fn report_forbidden_specialization<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1162                                              impl_item: &hir::ImplItem,
1163                                              parent_impl: DefId)
1164 {
1165     let mut err = struct_span_err!(
1166         tcx.sess, impl_item.span, E0520,
1167         "`{}` specializes an item from a parent `impl`, but \
1168          that item is not marked `default`",
1169         impl_item.name);
1170     err.span_label(impl_item.span, format!("cannot specialize default item `{}`",
1171                                             impl_item.name));
1172
1173     match tcx.span_of_impl(parent_impl) {
1174         Ok(span) => {
1175             err.span_label(span, "parent `impl` is here");
1176             err.note(&format!("to specialize, `{}` in the parent `impl` must be marked `default`",
1177                               impl_item.name));
1178         }
1179         Err(cname) => {
1180             err.note(&format!("parent implementation is in crate `{}`", cname));
1181         }
1182     }
1183
1184     err.emit();
1185 }
1186
1187 fn check_specialization_validity<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1188                                            trait_def: &ty::TraitDef,
1189                                            impl_id: DefId,
1190                                            impl_item: &hir::ImplItem)
1191 {
1192     let ancestors = trait_def.ancestors(tcx, impl_id);
1193
1194     let kind = match impl_item.node {
1195         hir::ImplItemKind::Const(..) => ty::AssociatedKind::Const,
1196         hir::ImplItemKind::Method(..) => ty::AssociatedKind::Method,
1197         hir::ImplItemKind::Type(_) => ty::AssociatedKind::Type
1198     };
1199     let parent = ancestors.defs(tcx, impl_item.name, kind).skip(1).next()
1200         .map(|node_item| node_item.map(|parent| parent.defaultness));
1201
1202     if let Some(parent) = parent {
1203         if tcx.impl_item_is_final(&parent) {
1204             report_forbidden_specialization(tcx, impl_item, parent.node.def_id());
1205         }
1206     }
1207
1208 }
1209
1210 fn check_impl_items_against_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1211                                             impl_span: Span,
1212                                             impl_id: DefId,
1213                                             impl_trait_ref: ty::TraitRef<'tcx>,
1214                                             impl_item_refs: &[hir::ImplItemRef]) {
1215     // If the trait reference itself is erroneous (so the compilation is going
1216     // to fail), skip checking the items here -- the `impl_item` table in `tcx`
1217     // isn't populated for such impls.
1218     if impl_trait_ref.references_error() { return; }
1219
1220     // Locate trait definition and items
1221     let trait_def = tcx.trait_def(impl_trait_ref.def_id);
1222     let mut overridden_associated_type = None;
1223
1224     let impl_items = || impl_item_refs.iter().map(|iiref| tcx.hir.impl_item(iiref.id));
1225
1226     // Check existing impl methods to see if they are both present in trait
1227     // and compatible with trait signature
1228     for impl_item in impl_items() {
1229         let ty_impl_item = tcx.associated_item(tcx.hir.local_def_id(impl_item.id));
1230         let ty_trait_item = tcx.associated_items(impl_trait_ref.def_id)
1231             .find(|ac| ac.name == ty_impl_item.name);
1232
1233         // Check that impl definition matches trait definition
1234         if let Some(ty_trait_item) = ty_trait_item {
1235             match impl_item.node {
1236                 hir::ImplItemKind::Const(..) => {
1237                     // Find associated const definition.
1238                     if ty_trait_item.kind == ty::AssociatedKind::Const {
1239                         compare_const_impl(tcx,
1240                                            &ty_impl_item,
1241                                            impl_item.span,
1242                                            &ty_trait_item,
1243                                            impl_trait_ref);
1244                     } else {
1245                          let mut err = struct_span_err!(tcx.sess, impl_item.span, E0323,
1246                                   "item `{}` is an associated const, \
1247                                   which doesn't match its trait `{}`",
1248                                   ty_impl_item.name,
1249                                   impl_trait_ref);
1250                          err.span_label(impl_item.span, "does not match trait");
1251                          // We can only get the spans from local trait definition
1252                          // Same for E0324 and E0325
1253                          if let Some(trait_span) = tcx.hir.span_if_local(ty_trait_item.def_id) {
1254                             err.span_label(trait_span, "item in trait");
1255                          }
1256                          err.emit()
1257                     }
1258                 }
1259                 hir::ImplItemKind::Method(..) => {
1260                     let trait_span = tcx.hir.span_if_local(ty_trait_item.def_id);
1261                     if ty_trait_item.kind == ty::AssociatedKind::Method {
1262                         let err_count = tcx.sess.err_count();
1263                         compare_impl_method(tcx,
1264                                             &ty_impl_item,
1265                                             impl_item.span,
1266                                             &ty_trait_item,
1267                                             impl_trait_ref,
1268                                             trait_span,
1269                                             true); // start with old-broken-mode
1270                         if err_count == tcx.sess.err_count() {
1271                             // old broken mode did not report an error. Try with the new mode.
1272                             compare_impl_method(tcx,
1273                                                 &ty_impl_item,
1274                                                 impl_item.span,
1275                                                 &ty_trait_item,
1276                                                 impl_trait_ref,
1277                                                 trait_span,
1278                                                 false); // use the new mode
1279                         }
1280                     } else {
1281                         let mut err = struct_span_err!(tcx.sess, impl_item.span, E0324,
1282                                   "item `{}` is an associated method, \
1283                                   which doesn't match its trait `{}`",
1284                                   ty_impl_item.name,
1285                                   impl_trait_ref);
1286                          err.span_label(impl_item.span, "does not match trait");
1287                          if let Some(trait_span) = tcx.hir.span_if_local(ty_trait_item.def_id) {
1288                             err.span_label(trait_span, "item in trait");
1289                          }
1290                          err.emit()
1291                     }
1292                 }
1293                 hir::ImplItemKind::Type(_) => {
1294                     if ty_trait_item.kind == ty::AssociatedKind::Type {
1295                         if ty_trait_item.defaultness.has_value() {
1296                             overridden_associated_type = Some(impl_item);
1297                         }
1298                     } else {
1299                         let mut err = struct_span_err!(tcx.sess, impl_item.span, E0325,
1300                                   "item `{}` is an associated type, \
1301                                   which doesn't match its trait `{}`",
1302                                   ty_impl_item.name,
1303                                   impl_trait_ref);
1304                          err.span_label(impl_item.span, "does not match trait");
1305                          if let Some(trait_span) = tcx.hir.span_if_local(ty_trait_item.def_id) {
1306                             err.span_label(trait_span, "item in trait");
1307                          }
1308                          err.emit()
1309                     }
1310                 }
1311             }
1312         }
1313
1314         check_specialization_validity(tcx, trait_def, impl_id, impl_item);
1315     }
1316
1317     // Check for missing items from trait
1318     let mut missing_items = Vec::new();
1319     let mut invalidated_items = Vec::new();
1320     let associated_type_overridden = overridden_associated_type.is_some();
1321     for trait_item in tcx.associated_items(impl_trait_ref.def_id) {
1322         let is_implemented = trait_def.ancestors(tcx, impl_id)
1323             .defs(tcx, trait_item.name, trait_item.kind)
1324             .next()
1325             .map(|node_item| !node_item.node.is_from_trait())
1326             .unwrap_or(false);
1327
1328         if !is_implemented {
1329             if !trait_item.defaultness.has_value() {
1330                 missing_items.push(trait_item);
1331             } else if associated_type_overridden {
1332                 invalidated_items.push(trait_item.name);
1333             }
1334         }
1335     }
1336
1337     let signature = |item: &ty::AssociatedItem| {
1338         match item.kind {
1339             ty::AssociatedKind::Method => {
1340                 format!("{}", tcx.type_of(item.def_id).fn_sig().0)
1341             }
1342             ty::AssociatedKind::Type => format!("type {};", item.name.to_string()),
1343             ty::AssociatedKind::Const => {
1344                 format!("const {}: {:?};", item.name.to_string(), tcx.type_of(item.def_id))
1345             }
1346         }
1347     };
1348
1349     if !missing_items.is_empty() {
1350         let mut err = struct_span_err!(tcx.sess, impl_span, E0046,
1351             "not all trait items implemented, missing: `{}`",
1352             missing_items.iter()
1353                   .map(|trait_item| trait_item.name.to_string())
1354                   .collect::<Vec<_>>().join("`, `"));
1355         err.span_label(impl_span, format!("missing `{}` in implementation",
1356                 missing_items.iter()
1357                     .map(|trait_item| trait_item.name.to_string())
1358                     .collect::<Vec<_>>().join("`, `")));
1359         for trait_item in missing_items {
1360             if let Some(span) = tcx.hir.span_if_local(trait_item.def_id) {
1361                 err.span_label(span, format!("`{}` from trait", trait_item.name));
1362             } else {
1363                 err.note(&format!("`{}` from trait: `{}`",
1364                                   trait_item.name,
1365                                   signature(&trait_item)));
1366             }
1367         }
1368         err.emit();
1369     }
1370
1371     if !invalidated_items.is_empty() {
1372         let invalidator = overridden_associated_type.unwrap();
1373         span_err!(tcx.sess, invalidator.span, E0399,
1374                   "the following trait items need to be reimplemented \
1375                    as `{}` was overridden: `{}`",
1376                   invalidator.name,
1377                   invalidated_items.iter()
1378                                    .map(|name| name.to_string())
1379                                    .collect::<Vec<_>>().join("`, `"))
1380     }
1381 }
1382
1383 /// Checks whether a type can be represented in memory. In particular, it
1384 /// identifies types that contain themselves without indirection through a
1385 /// pointer, which would mean their size is unbounded.
1386 fn check_representable<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1387                                  sp: Span,
1388                                  item_def_id: DefId)
1389                                  -> bool {
1390     let rty = tcx.type_of(item_def_id);
1391
1392     // Check that it is possible to represent this type. This call identifies
1393     // (1) types that contain themselves and (2) types that contain a different
1394     // recursive type. It is only necessary to throw an error on those that
1395     // contain themselves. For case 2, there must be an inner type that will be
1396     // caught by case 1.
1397     match rty.is_representable(tcx, sp) {
1398         Representability::SelfRecursive(spans) => {
1399             let mut err = tcx.recursive_type_with_infinite_size_error(item_def_id);
1400             for span in spans {
1401                 err.span_label(span, "recursive without indirection");
1402             }
1403             err.emit();
1404             return false
1405         }
1406         Representability::Representable | Representability::ContainsRecursive => (),
1407     }
1408     return true
1409 }
1410
1411 pub fn check_simd<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span, def_id: DefId) {
1412     let t = tcx.type_of(def_id);
1413     match t.sty {
1414         ty::TyAdt(def, substs) if def.is_struct() => {
1415             let fields = &def.struct_variant().fields;
1416             if fields.is_empty() {
1417                 span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty");
1418                 return;
1419             }
1420             let e = fields[0].ty(tcx, substs);
1421             if !fields.iter().all(|f| f.ty(tcx, substs) == e) {
1422                 struct_span_err!(tcx.sess, sp, E0076, "SIMD vector should be homogeneous")
1423                                 .span_label(sp, "SIMD elements must have the same type")
1424                                 .emit();
1425                 return;
1426             }
1427             match e.sty {
1428                 ty::TyParam(_) => { /* struct<T>(T, T, T, T) is ok */ }
1429                 _ if e.is_machine()  => { /* struct(u8, u8, u8, u8) is ok */ }
1430                 _ => {
1431                     span_err!(tcx.sess, sp, E0077,
1432                               "SIMD vector element type should be machine type");
1433                     return;
1434                 }
1435             }
1436         }
1437         _ => ()
1438     }
1439 }
1440
1441 fn check_packed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span, def_id: DefId) {
1442     if check_packed_inner(tcx, def_id, &mut Vec::new()) {
1443         struct_span_err!(tcx.sess, sp, E0588,
1444             "packed struct cannot transitively contain a `[repr(align)]` struct").emit();
1445     }
1446 }
1447
1448 fn check_packed_inner<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1449                                 def_id: DefId,
1450                                 stack: &mut Vec<DefId>) -> bool {
1451     let t = tcx.type_of(def_id);
1452     if stack.contains(&def_id) {
1453         debug!("check_packed_inner: {:?} is recursive", t);
1454         return false;
1455     }
1456     match t.sty {
1457         ty::TyAdt(def, substs) if def.is_struct() => {
1458             if tcx.adt_def(def.did).repr.align > 0 {
1459                 return true;
1460             }
1461             // push struct def_id before checking fields
1462             stack.push(def_id);
1463             for field in &def.struct_variant().fields {
1464                 let f = field.ty(tcx, substs);
1465                 match f.sty {
1466                     ty::TyAdt(def, _) => {
1467                         if check_packed_inner(tcx, def.did, stack) {
1468                             return true;
1469                         }
1470                     }
1471                     _ => ()
1472                 }
1473             }
1474             // only need to pop if not early out
1475             stack.pop();
1476         }
1477         _ => ()
1478     }
1479     false
1480 }
1481
1482 #[allow(trivial_numeric_casts)]
1483 pub fn check_enum<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1484                             sp: Span,
1485                             vs: &'tcx [hir::Variant],
1486                             id: ast::NodeId) {
1487     let def_id = tcx.hir.local_def_id(id);
1488     let def = tcx.adt_def(def_id);
1489     def.destructor(tcx); // force the destructor to be evaluated
1490
1491     if vs.is_empty() && tcx.has_attr(def_id, "repr") {
1492         struct_span_err!(
1493             tcx.sess, sp, E0084,
1494             "unsupported representation for zero-variant enum")
1495             .span_label(sp, "unsupported enum representation")
1496             .emit();
1497     }
1498
1499     let repr_type_ty = def.repr.discr_type().to_ty(tcx);
1500     if repr_type_ty == tcx.types.i128 || repr_type_ty == tcx.types.u128 {
1501         if !tcx.sess.features.borrow().i128_type {
1502             emit_feature_err(&tcx.sess.parse_sess,
1503                              "i128_type", sp, GateIssue::Language, "128-bit type is unstable");
1504         }
1505     }
1506
1507     for v in vs {
1508         if let Some(e) = v.node.disr_expr {
1509             tcx.typeck_tables_of(tcx.hir.local_def_id(e.node_id));
1510         }
1511     }
1512
1513     let mut disr_vals: Vec<ConstInt> = Vec::new();
1514     for (discr, v) in def.discriminants(tcx).zip(vs) {
1515         // Check for duplicate discriminant values
1516         if let Some(i) = disr_vals.iter().position(|&x| x == discr) {
1517             let variant_i_node_id = tcx.hir.as_local_node_id(def.variants[i].did).unwrap();
1518             let variant_i = tcx.hir.expect_variant(variant_i_node_id);
1519             let i_span = match variant_i.node.disr_expr {
1520                 Some(expr) => tcx.hir.span(expr.node_id),
1521                 None => tcx.hir.span(variant_i_node_id)
1522             };
1523             let span = match v.node.disr_expr {
1524                 Some(expr) => tcx.hir.span(expr.node_id),
1525                 None => v.span
1526             };
1527             struct_span_err!(tcx.sess, span, E0081,
1528                              "discriminant value `{}` already exists", disr_vals[i])
1529                 .span_label(i_span, format!("first use of `{}`", disr_vals[i]))
1530                 .span_label(span , format!("enum already has `{}`", disr_vals[i]))
1531                 .emit();
1532         }
1533         disr_vals.push(discr);
1534     }
1535
1536     check_representable(tcx, sp, def_id);
1537 }
1538
1539 impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
1540     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
1541
1542     fn get_type_parameter_bounds(&self, _: Span, def_id: DefId)
1543                                  -> ty::GenericPredicates<'tcx>
1544     {
1545         let tcx = self.tcx;
1546         let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
1547         let item_id = tcx.hir.ty_param_owner(node_id);
1548         let item_def_id = tcx.hir.local_def_id(item_id);
1549         let generics = tcx.generics_of(item_def_id);
1550         let index = generics.type_param_to_index[&def_id.index];
1551         ty::GenericPredicates {
1552             parent: None,
1553             predicates: self.param_env.caller_bounds.iter().filter(|predicate| {
1554                 match **predicate {
1555                     ty::Predicate::Trait(ref data) => {
1556                         data.0.self_ty().is_param(index)
1557                     }
1558                     _ => false
1559                 }
1560             }).cloned().collect()
1561         }
1562     }
1563
1564     fn re_infer(&self, span: Span, def: Option<&ty::RegionParameterDef>)
1565                 -> Option<ty::Region<'tcx>> {
1566         let v = match def {
1567             Some(def) => infer::EarlyBoundRegion(span, def.name, def.issue_32330),
1568             None => infer::MiscVariable(span)
1569         };
1570         Some(self.next_region_var(v))
1571     }
1572
1573     fn ty_infer(&self, span: Span) -> Ty<'tcx> {
1574         self.next_ty_var(TypeVariableOrigin::TypeInference(span))
1575     }
1576
1577     fn ty_infer_for_def(&self,
1578                         ty_param_def: &ty::TypeParameterDef,
1579                         substs: &[Kind<'tcx>],
1580                         span: Span) -> Ty<'tcx> {
1581         self.type_var_for_def(span, ty_param_def, substs)
1582     }
1583
1584     fn projected_ty_from_poly_trait_ref(&self,
1585                                         span: Span,
1586                                         poly_trait_ref: ty::PolyTraitRef<'tcx>,
1587                                         item_name: ast::Name)
1588                                         -> Ty<'tcx>
1589     {
1590         let (trait_ref, _) =
1591             self.replace_late_bound_regions_with_fresh_var(
1592                 span,
1593                 infer::LateBoundRegionConversionTime::AssocTypeProjection(item_name),
1594                 &poly_trait_ref);
1595
1596         self.tcx().mk_projection(trait_ref, item_name)
1597     }
1598
1599     fn normalize_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
1600         if ty.has_escaping_regions() {
1601             ty // FIXME: normalization and escaping regions
1602         } else {
1603             self.normalize_associated_types_in(span, &ty)
1604         }
1605     }
1606
1607     fn set_tainted_by_errors(&self) {
1608         self.infcx.set_tainted_by_errors()
1609     }
1610 }
1611
1612 /// Controls whether the arguments are tupled. This is used for the call
1613 /// operator.
1614 ///
1615 /// Tupling means that all call-side arguments are packed into a tuple and
1616 /// passed as a single parameter. For example, if tupling is enabled, this
1617 /// function:
1618 ///
1619 ///     fn f(x: (isize, isize))
1620 ///
1621 /// Can be called as:
1622 ///
1623 ///     f(1, 2);
1624 ///
1625 /// Instead of:
1626 ///
1627 ///     f((1, 2));
1628 #[derive(Clone, Eq, PartialEq)]
1629 enum TupleArgumentsFlag {
1630     DontTupleArguments,
1631     TupleArguments,
1632 }
1633
1634 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
1635     pub fn new(inh: &'a Inherited<'a, 'gcx, 'tcx>,
1636                body_id: ast::NodeId)
1637                -> FnCtxt<'a, 'gcx, 'tcx> {
1638         FnCtxt {
1639             body_id: body_id,
1640             err_count_on_creation: inh.tcx.sess.err_count(),
1641             ret_coercion: None,
1642             ps: RefCell::new(UnsafetyState::function(hir::Unsafety::Normal,
1643                                                      ast::CRATE_NODE_ID)),
1644             diverges: Cell::new(Diverges::Maybe),
1645             has_errors: Cell::new(false),
1646             enclosing_breakables: RefCell::new(EnclosingBreakables {
1647                 stack: Vec::new(),
1648                 by_id: NodeMap(),
1649             }),
1650             inh: inh,
1651         }
1652     }
1653
1654     pub fn sess(&self) -> &Session {
1655         &self.tcx.sess
1656     }
1657
1658     pub fn err_count_since_creation(&self) -> usize {
1659         self.tcx.sess.err_count() - self.err_count_on_creation
1660     }
1661
1662     /// Produce warning on the given node, if the current point in the
1663     /// function is unreachable, and there hasn't been another warning.
1664     fn warn_if_unreachable(&self, id: ast::NodeId, span: Span, kind: &str) {
1665         if self.diverges.get() == Diverges::Always {
1666             self.diverges.set(Diverges::WarnedAlways);
1667
1668             debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
1669
1670             self.tables.borrow_mut().lints.add_lint(
1671                 lint::builtin::UNREACHABLE_CODE,
1672                 id, span,
1673                 format!("unreachable {}", kind));
1674         }
1675     }
1676
1677     pub fn cause(&self,
1678                  span: Span,
1679                  code: ObligationCauseCode<'tcx>)
1680                  -> ObligationCause<'tcx> {
1681         ObligationCause::new(span, self.body_id, code)
1682     }
1683
1684     pub fn misc(&self, span: Span) -> ObligationCause<'tcx> {
1685         self.cause(span, ObligationCauseCode::MiscObligation)
1686     }
1687
1688     /// Resolves type variables in `ty` if possible. Unlike the infcx
1689     /// version (resolve_type_vars_if_possible), this version will
1690     /// also select obligations if it seems useful, in an effort
1691     /// to get more type information.
1692     fn resolve_type_vars_with_obligations(&self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
1693         debug!("resolve_type_vars_with_obligations(ty={:?})", ty);
1694
1695         // No TyInfer()? Nothing needs doing.
1696         if !ty.has_infer_types() {
1697             debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
1698             return ty;
1699         }
1700
1701         // If `ty` is a type variable, see whether we already know what it is.
1702         ty = self.resolve_type_vars_if_possible(&ty);
1703         if !ty.has_infer_types() {
1704             debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
1705             return ty;
1706         }
1707
1708         // If not, try resolving pending obligations as much as
1709         // possible. This can help substantially when there are
1710         // indirect dependencies that don't seem worth tracking
1711         // precisely.
1712         self.select_obligations_where_possible();
1713         ty = self.resolve_type_vars_if_possible(&ty);
1714
1715         debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
1716         ty
1717     }
1718
1719     fn record_deferred_call_resolution(&self,
1720                                        closure_def_id: DefId,
1721                                        r: DeferredCallResolution<'gcx, 'tcx>) {
1722         let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
1723         deferred_call_resolutions.entry(closure_def_id).or_insert(vec![]).push(r);
1724     }
1725
1726     fn remove_deferred_call_resolutions(&self,
1727                                         closure_def_id: DefId)
1728                                         -> Vec<DeferredCallResolution<'gcx, 'tcx>>
1729     {
1730         let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
1731         deferred_call_resolutions.remove(&closure_def_id).unwrap_or(vec![])
1732     }
1733
1734     pub fn tag(&self) -> String {
1735         let self_ptr: *const FnCtxt = self;
1736         format!("{:?}", self_ptr)
1737     }
1738
1739     pub fn local_ty(&self, span: Span, nid: ast::NodeId) -> Ty<'tcx> {
1740         match self.locals.borrow().get(&nid) {
1741             Some(&t) => t,
1742             None => {
1743                 span_bug!(span, "no type for local variable {}",
1744                           self.tcx.hir.node_to_string(nid));
1745             }
1746         }
1747     }
1748
1749     #[inline]
1750     pub fn write_ty(&self, node_id: ast::NodeId, ty: Ty<'tcx>) {
1751         debug!("write_ty({}, {:?}) in fcx {}",
1752                node_id, self.resolve_type_vars_if_possible(&ty), self.tag());
1753         self.tables.borrow_mut().node_types.insert(node_id, ty);
1754
1755         if ty.references_error() {
1756             self.has_errors.set(true);
1757             self.set_tainted_by_errors();
1758         }
1759     }
1760
1761     pub fn write_substs(&self, node_id: ast::NodeId, substs: ty::ItemSubsts<'tcx>) {
1762         if !substs.substs.is_noop() {
1763             debug!("write_substs({}, {:?}) in fcx {}",
1764                    node_id,
1765                    substs,
1766                    self.tag());
1767
1768             self.tables.borrow_mut().item_substs.insert(node_id, substs);
1769         }
1770     }
1771
1772     pub fn apply_autoderef_adjustment(&self,
1773                                       node_id: ast::NodeId,
1774                                       autoderefs: Vec<Option<ty::MethodCallee<'tcx>>>,
1775                                       adjusted_ty: Ty<'tcx>) {
1776         self.apply_adjustment(node_id, Adjustment {
1777             kind: Adjust::DerefRef {
1778                 autoderefs,
1779                 autoref: None,
1780                 unsize: false
1781             },
1782             target: adjusted_ty
1783         });
1784     }
1785
1786     pub fn apply_adjustment(&self, node_id: ast::NodeId, adj: Adjustment<'tcx>) {
1787         debug!("apply_adjustment(node_id={}, adj={:?})", node_id, adj);
1788
1789         if adj.is_identity() {
1790             return;
1791         }
1792
1793         match self.tables.borrow_mut().adjustments.entry(node_id) {
1794             Entry::Vacant(entry) => { entry.insert(adj); },
1795             Entry::Occupied(mut entry) => {
1796                 debug!(" - composing on top of {:?}", entry.get());
1797                 match (&entry.get().kind, &adj.kind) {
1798                     // Applying any adjustment on top of a NeverToAny
1799                     // is a valid NeverToAny adjustment, because it can't
1800                     // be reached.
1801                     (&Adjust::NeverToAny, _) => return,
1802                     (&Adjust::DerefRef {
1803                         autoderefs: ref old,
1804                         autoref: Some(AutoBorrow::Ref(..)),
1805                         unsize: false
1806                     }, &Adjust::DerefRef {
1807                         autoderefs: ref new, ..
1808                     }) if old.len() == 1 && new.len() >= 1 => {
1809                         // A reborrow has no effect before a dereference.
1810                     }
1811                     // FIXME: currently we never try to compose autoderefs
1812                     // and ReifyFnPointer/UnsafeFnPointer, but we could.
1813                     _ =>
1814                         bug!("while adjusting {}, can't compose {:?} and {:?}",
1815                              node_id, entry.get(), adj)
1816                 };
1817                 *entry.get_mut() = adj;
1818             }
1819         }
1820     }
1821
1822     /// Basically whenever we are converting from a type scheme into
1823     /// the fn body space, we always want to normalize associated
1824     /// types as well. This function combines the two.
1825     fn instantiate_type_scheme<T>(&self,
1826                                   span: Span,
1827                                   substs: &Substs<'tcx>,
1828                                   value: &T)
1829                                   -> T
1830         where T : TypeFoldable<'tcx>
1831     {
1832         let value = value.subst(self.tcx, substs);
1833         let result = self.normalize_associated_types_in(span, &value);
1834         debug!("instantiate_type_scheme(value={:?}, substs={:?}) = {:?}",
1835                value,
1836                substs,
1837                result);
1838         result
1839     }
1840
1841     /// As `instantiate_type_scheme`, but for the bounds found in a
1842     /// generic type scheme.
1843     fn instantiate_bounds(&self, span: Span, def_id: DefId, substs: &Substs<'tcx>)
1844                           -> ty::InstantiatedPredicates<'tcx> {
1845         let bounds = self.tcx.predicates_of(def_id);
1846         let result = bounds.instantiate(self.tcx, substs);
1847         let result = self.normalize_associated_types_in(span, &result);
1848         debug!("instantiate_bounds(bounds={:?}, substs={:?}) = {:?}",
1849                bounds,
1850                substs,
1851                result);
1852         result
1853     }
1854
1855     /// Replace all anonymized types with fresh inference variables
1856     /// and record them for writeback.
1857     fn instantiate_anon_types<T: TypeFoldable<'tcx>>(&self, value: &T) -> T {
1858         value.fold_with(&mut BottomUpFolder { tcx: self.tcx, fldop: |ty| {
1859             if let ty::TyAnon(def_id, substs) = ty.sty {
1860                 // Use the same type variable if the exact same TyAnon appears more
1861                 // than once in the return type (e.g. if it's pased to a type alias).
1862                 let id = self.tcx.hir.as_local_node_id(def_id).unwrap();
1863                 if let Some(ty_var) = self.anon_types.borrow().get(&id) {
1864                     return ty_var;
1865                 }
1866                 let span = self.tcx.def_span(def_id);
1867                 let ty_var = self.next_ty_var(TypeVariableOrigin::TypeInference(span));
1868                 self.anon_types.borrow_mut().insert(id, ty_var);
1869
1870                 let predicates_of = self.tcx.predicates_of(def_id);
1871                 let bounds = predicates_of.instantiate(self.tcx, substs);
1872
1873                 for predicate in bounds.predicates {
1874                     // Change the predicate to refer to the type variable,
1875                     // which will be the concrete type, instead of the TyAnon.
1876                     // This also instantiates nested `impl Trait`.
1877                     let predicate = self.instantiate_anon_types(&predicate);
1878
1879                     // Require that the predicate holds for the concrete type.
1880                     let cause = traits::ObligationCause::new(span, self.body_id,
1881                                                              traits::ReturnType);
1882                     self.register_predicate(traits::Obligation::new(cause, predicate));
1883                 }
1884
1885                 ty_var
1886             } else {
1887                 ty
1888             }
1889         }})
1890     }
1891
1892     fn normalize_associated_types_in<T>(&self, span: Span, value: &T) -> T
1893         where T : TypeFoldable<'tcx>
1894     {
1895         let ok = self.normalize_associated_types_in_as_infer_ok(span, value);
1896         self.register_infer_ok_obligations(ok)
1897     }
1898
1899     fn normalize_associated_types_in_as_infer_ok<T>(&self, span: Span, value: &T)
1900                                                     -> InferOk<'tcx, T>
1901         where T : TypeFoldable<'tcx>
1902     {
1903         self.inh.normalize_associated_types_in_as_infer_ok(span, self.body_id, value)
1904     }
1905
1906     pub fn write_nil(&self, node_id: ast::NodeId) {
1907         self.write_ty(node_id, self.tcx.mk_nil());
1908     }
1909
1910     pub fn write_error(&self, node_id: ast::NodeId) {
1911         self.write_ty(node_id, self.tcx.types.err);
1912     }
1913
1914     pub fn require_type_meets(&self,
1915                               ty: Ty<'tcx>,
1916                               span: Span,
1917                               code: traits::ObligationCauseCode<'tcx>,
1918                               def_id: DefId)
1919     {
1920         self.register_bound(
1921             ty,
1922             def_id,
1923             traits::ObligationCause::new(span, self.body_id, code));
1924     }
1925
1926     pub fn require_type_is_sized(&self,
1927                                  ty: Ty<'tcx>,
1928                                  span: Span,
1929                                  code: traits::ObligationCauseCode<'tcx>)
1930     {
1931         let lang_item = self.tcx.require_lang_item(lang_items::SizedTraitLangItem);
1932         self.require_type_meets(ty, span, code, lang_item);
1933     }
1934
1935     pub fn register_bound(&self,
1936                           ty: Ty<'tcx>,
1937                           def_id: DefId,
1938                           cause: traits::ObligationCause<'tcx>)
1939     {
1940         self.fulfillment_cx.borrow_mut()
1941             .register_bound(self, ty, def_id, cause);
1942     }
1943
1944     pub fn to_ty(&self, ast_t: &hir::Ty) -> Ty<'tcx> {
1945         let t = AstConv::ast_ty_to_ty(self, ast_t);
1946         self.register_wf_obligation(t, ast_t.span, traits::MiscObligation);
1947         t
1948     }
1949
1950     pub fn node_ty(&self, id: ast::NodeId) -> Ty<'tcx> {
1951         match self.tables.borrow().node_types.get(&id) {
1952             Some(&t) => t,
1953             None if self.err_count_since_creation() != 0 => self.tcx.types.err,
1954             None => {
1955                 bug!("no type for node {}: {} in fcx {}",
1956                      id, self.tcx.hir.node_to_string(id),
1957                      self.tag());
1958             }
1959         }
1960     }
1961
1962     pub fn opt_node_ty_substs<F>(&self,
1963                                  id: ast::NodeId,
1964                                  f: F) where
1965         F: FnOnce(&ty::ItemSubsts<'tcx>),
1966     {
1967         if let Some(s) = self.tables.borrow().item_substs.get(&id) {
1968             f(s);
1969         }
1970     }
1971
1972     /// Registers an obligation for checking later, during regionck, that the type `ty` must
1973     /// outlive the region `r`.
1974     pub fn register_region_obligation(&self,
1975                                       ty: Ty<'tcx>,
1976                                       region: ty::Region<'tcx>,
1977                                       cause: traits::ObligationCause<'tcx>)
1978     {
1979         let mut fulfillment_cx = self.fulfillment_cx.borrow_mut();
1980         fulfillment_cx.register_region_obligation(ty, region, cause);
1981     }
1982
1983     /// Registers an obligation for checking later, during regionck, that the type `ty` must
1984     /// outlive the region `r`.
1985     pub fn register_wf_obligation(&self,
1986                                   ty: Ty<'tcx>,
1987                                   span: Span,
1988                                   code: traits::ObligationCauseCode<'tcx>)
1989     {
1990         // WF obligations never themselves fail, so no real need to give a detailed cause:
1991         let cause = traits::ObligationCause::new(span, self.body_id, code);
1992         self.register_predicate(traits::Obligation::new(cause, ty::Predicate::WellFormed(ty)));
1993     }
1994
1995     pub fn register_old_wf_obligation(&self,
1996                                       ty: Ty<'tcx>,
1997                                       span: Span,
1998                                       code: traits::ObligationCauseCode<'tcx>)
1999     {
2000         // Registers an "old-style" WF obligation that uses the
2001         // implicator code.  This is basically a buggy version of
2002         // `register_wf_obligation` that is being kept around
2003         // temporarily just to help with phasing in the newer rules.
2004         //
2005         // FIXME(#27579) all uses of this should be migrated to register_wf_obligation eventually
2006         let cause = traits::ObligationCause::new(span, self.body_id, code);
2007         self.register_region_obligation(ty, self.tcx.types.re_empty, cause);
2008     }
2009
2010     /// Registers obligations that all types appearing in `substs` are well-formed.
2011     pub fn add_wf_bounds(&self, substs: &Substs<'tcx>, expr: &hir::Expr)
2012     {
2013         for ty in substs.types() {
2014             self.register_wf_obligation(ty, expr.span, traits::MiscObligation);
2015         }
2016     }
2017
2018     /// Given a fully substituted set of bounds (`generic_bounds`), and the values with which each
2019     /// type/region parameter was instantiated (`substs`), creates and registers suitable
2020     /// trait/region obligations.
2021     ///
2022     /// For example, if there is a function:
2023     ///
2024     /// ```
2025     /// fn foo<'a,T:'a>(...)
2026     /// ```
2027     ///
2028     /// and a reference:
2029     ///
2030     /// ```
2031     /// let f = foo;
2032     /// ```
2033     ///
2034     /// Then we will create a fresh region variable `'$0` and a fresh type variable `$1` for `'a`
2035     /// and `T`. This routine will add a region obligation `$1:'$0` and register it locally.
2036     pub fn add_obligations_for_parameters(&self,
2037                                           cause: traits::ObligationCause<'tcx>,
2038                                           predicates: &ty::InstantiatedPredicates<'tcx>)
2039     {
2040         assert!(!predicates.has_escaping_regions());
2041
2042         debug!("add_obligations_for_parameters(predicates={:?})",
2043                predicates);
2044
2045         for obligation in traits::predicates_for_generics(cause, predicates) {
2046             self.register_predicate(obligation);
2047         }
2048     }
2049
2050     // FIXME(arielb1): use this instead of field.ty everywhere
2051     // Only for fields! Returns <none> for methods>
2052     // Indifferent to privacy flags
2053     pub fn field_ty(&self,
2054                     span: Span,
2055                     field: &'tcx ty::FieldDef,
2056                     substs: &Substs<'tcx>)
2057                     -> Ty<'tcx>
2058     {
2059         self.normalize_associated_types_in(span,
2060                                            &field.ty(self.tcx, substs))
2061     }
2062
2063     fn check_casts(&self) {
2064         let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
2065         for cast in deferred_cast_checks.drain(..) {
2066             cast.check(self);
2067         }
2068     }
2069
2070     /// Apply "fallbacks" to some types
2071     /// unconstrained types get replaced with ! or  () (depending on whether
2072     /// feature(never_type) is enabled), unconstrained ints with i32, and
2073     /// unconstrained floats with f64.
2074     fn default_type_parameters(&self) {
2075         use rustc::ty::error::UnconstrainedNumeric::Neither;
2076         use rustc::ty::error::UnconstrainedNumeric::{UnconstrainedInt, UnconstrainedFloat};
2077
2078         // Defaulting inference variables becomes very dubious if we have
2079         // encountered type-checking errors. Therefore, if we think we saw
2080         // some errors in this function, just resolve all uninstanted type
2081         // varibles to TyError.
2082         if self.is_tainted_by_errors() {
2083             for ty in &self.unsolved_variables() {
2084                 if let ty::TyInfer(_) = self.shallow_resolve(ty).sty {
2085                     debug!("default_type_parameters: defaulting `{:?}` to error", ty);
2086                     self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx().types.err);
2087                 }
2088             }
2089             return;
2090         }
2091
2092         for ty in &self.unsolved_variables() {
2093             let resolved = self.resolve_type_vars_if_possible(ty);
2094             if self.type_var_diverges(resolved) {
2095                 debug!("default_type_parameters: defaulting `{:?}` to `!` because it diverges",
2096                        resolved);
2097                 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
2098                                    self.tcx.mk_diverging_default());
2099             } else {
2100                 match self.type_is_unconstrained_numeric(resolved) {
2101                     UnconstrainedInt => {
2102                         debug!("default_type_parameters: defaulting `{:?}` to `i32`",
2103                                resolved);
2104                         self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.i32)
2105                     },
2106                     UnconstrainedFloat => {
2107                         debug!("default_type_parameters: defaulting `{:?}` to `f32`",
2108                                resolved);
2109                         self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.f64)
2110                     }
2111                     Neither => { }
2112                 }
2113             }
2114         }
2115     }
2116
2117     // Implements type inference fallback algorithm
2118     fn select_all_obligations_and_apply_defaults(&self) {
2119         self.select_obligations_where_possible();
2120         self.default_type_parameters();
2121         self.select_obligations_where_possible();
2122     }
2123
2124     fn select_all_obligations_or_error(&self) {
2125         debug!("select_all_obligations_or_error");
2126
2127         // upvar inference should have ensured that all deferred call
2128         // resolutions are handled by now.
2129         assert!(self.deferred_call_resolutions.borrow().is_empty());
2130
2131         self.select_all_obligations_and_apply_defaults();
2132
2133         let mut fulfillment_cx = self.fulfillment_cx.borrow_mut();
2134
2135         match fulfillment_cx.select_all_or_error(self) {
2136             Ok(()) => { }
2137             Err(errors) => { self.report_fulfillment_errors(&errors); }
2138         }
2139     }
2140
2141     /// Select as many obligations as we can at present.
2142     fn select_obligations_where_possible(&self) {
2143         match self.fulfillment_cx.borrow_mut().select_where_possible(self) {
2144             Ok(()) => { }
2145             Err(errors) => { self.report_fulfillment_errors(&errors); }
2146         }
2147     }
2148
2149     /// For the overloaded lvalue expressions (`*x`, `x[3]`), the trait
2150     /// returns a type of `&T`, but the actual type we assign to the
2151     /// *expression* is `T`. So this function just peels off the return
2152     /// type by one layer to yield `T`.
2153     fn make_overloaded_lvalue_return_type(&self,
2154                                           method: MethodCallee<'tcx>)
2155                                           -> ty::TypeAndMut<'tcx>
2156     {
2157         // extract method return type, which will be &T;
2158         // all LB regions should have been instantiated during method lookup
2159         let ret_ty = method.sig.output();
2160
2161         // method returns &T, but the type as visible to user is T, so deref
2162         ret_ty.builtin_deref(true, NoPreference).unwrap()
2163     }
2164
2165     fn lookup_indexing(&self,
2166                        expr: &hir::Expr,
2167                        base_expr: &'gcx hir::Expr,
2168                        base_ty: Ty<'tcx>,
2169                        idx_ty: Ty<'tcx>,
2170                        lvalue_pref: LvaluePreference)
2171                        -> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)>
2172     {
2173         // FIXME(#18741) -- this is almost but not quite the same as the
2174         // autoderef that normal method probing does. They could likely be
2175         // consolidated.
2176
2177         let mut autoderef = self.autoderef(base_expr.span, base_ty);
2178         let mut result = None;
2179         while result.is_none() && autoderef.next().is_some() {
2180             result = self.try_index_step(expr, base_expr, &autoderef, lvalue_pref, idx_ty);
2181         }
2182         autoderef.finalize();
2183         result
2184     }
2185
2186     /// To type-check `base_expr[index_expr]`, we progressively autoderef
2187     /// (and otherwise adjust) `base_expr`, looking for a type which either
2188     /// supports builtin indexing or overloaded indexing.
2189     /// This loop implements one step in that search; the autoderef loop
2190     /// is implemented by `lookup_indexing`.
2191     fn try_index_step(&self,
2192                       expr: &hir::Expr,
2193                       base_expr: &hir::Expr,
2194                       autoderef: &Autoderef<'a, 'gcx, 'tcx>,
2195                       lvalue_pref: LvaluePreference,
2196                       index_ty: Ty<'tcx>)
2197                       -> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)>
2198     {
2199         let mut adjusted_ty = autoderef.unambiguous_final_ty();
2200         debug!("try_index_step(expr={:?}, base_expr={:?}, adjusted_ty={:?}, \
2201                                index_ty={:?})",
2202                expr,
2203                base_expr,
2204                adjusted_ty,
2205                index_ty);
2206
2207
2208         // First, try built-in indexing.
2209         match (adjusted_ty.builtin_index(), &index_ty.sty) {
2210             (Some(ty), &ty::TyUint(ast::UintTy::Us)) | (Some(ty), &ty::TyInfer(ty::IntVar(_))) => {
2211                 debug!("try_index_step: success, using built-in indexing");
2212                 let autoderefs = autoderef.adjust_steps(lvalue_pref);
2213                 self.apply_autoderef_adjustment(
2214                     base_expr.id, autoderefs, adjusted_ty);
2215                 return Some((self.tcx.types.usize, ty));
2216             }
2217             _ => {}
2218         }
2219
2220         for &unsize in &[false, true] {
2221             if unsize {
2222                 // We only unsize arrays here.
2223                 if let ty::TyArray(element_ty, _) = adjusted_ty.sty {
2224                     adjusted_ty = self.tcx.mk_slice(element_ty);
2225                 } else {
2226                     continue;
2227                 }
2228             }
2229
2230             // If some lookup succeeds, write callee into table and extract index/element
2231             // type from the method signature.
2232             // If some lookup succeeded, install method in table
2233             let input_ty = self.next_ty_var(TypeVariableOrigin::AutoDeref(base_expr.span));
2234             let method = self.try_overloaded_lvalue_op(
2235                 expr.span, adjusted_ty, &[input_ty], lvalue_pref, LvalueOp::Index);
2236
2237             let result = method.map(|ok| {
2238                 debug!("try_index_step: success, using overloaded indexing");
2239                 let (autoref, method) = self.register_infer_ok_obligations(ok);
2240
2241                 let autoderefs = autoderef.adjust_steps(lvalue_pref);
2242                 self.apply_adjustment(base_expr.id, Adjustment {
2243                     kind: Adjust::DerefRef {
2244                         autoderefs,
2245                         autoref,
2246                         unsize
2247                     },
2248                     target: method.sig.inputs()[0]
2249                 });
2250
2251                 self.tables.borrow_mut().method_map.insert(expr.id, method);
2252                 (input_ty, self.make_overloaded_lvalue_return_type(method).ty)
2253             });
2254             if result.is_some() {
2255                 return result;
2256             }
2257         }
2258
2259         None
2260     }
2261
2262     fn resolve_lvalue_op(&self, op: LvalueOp, is_mut: bool) -> (Option<DefId>, Symbol) {
2263         let (tr, name) = match (op, is_mut) {
2264             (LvalueOp::Deref, false) =>
2265                 (self.tcx.lang_items.deref_trait(), "deref"),
2266             (LvalueOp::Deref, true) =>
2267                 (self.tcx.lang_items.deref_mut_trait(), "deref_mut"),
2268             (LvalueOp::Index, false) =>
2269                 (self.tcx.lang_items.index_trait(), "index"),
2270             (LvalueOp::Index, true) =>
2271                 (self.tcx.lang_items.index_mut_trait(), "index_mut"),
2272         };
2273         (tr, Symbol::intern(name))
2274     }
2275
2276     fn try_overloaded_lvalue_op(&self,
2277                                 span: Span,
2278                                 base_ty: Ty<'tcx>,
2279                                 arg_tys: &[Ty<'tcx>],
2280                                 lvalue_pref: LvaluePreference,
2281                                 op: LvalueOp)
2282                                 -> Option<InferOk<'tcx,
2283                                     (Option<AutoBorrow<'tcx>>,
2284                                      ty::MethodCallee<'tcx>)>>
2285     {
2286         debug!("try_overloaded_lvalue_op({:?},{:?},{:?},{:?})",
2287                span,
2288                base_ty,
2289                lvalue_pref,
2290                op);
2291
2292         // Try Mut first, if preferred.
2293         let (mut_tr, mut_op) = self.resolve_lvalue_op(op, true);
2294         let method = match (lvalue_pref, mut_tr) {
2295             (PreferMutLvalue, Some(trait_did)) => {
2296                 self.lookup_method_in_trait_adjusted(span,
2297                                                      mut_op,
2298                                                      trait_did,
2299                                                      base_ty,
2300                                                      Some(arg_tys))
2301             }
2302             _ => None,
2303         };
2304
2305         // Otherwise, fall back to the immutable version.
2306         let (imm_tr, imm_op) = self.resolve_lvalue_op(op, false);
2307         let method = match (method, imm_tr) {
2308             (None, Some(trait_did)) => {
2309                 self.lookup_method_in_trait_adjusted(span,
2310                                                      imm_op,
2311                                                      trait_did,
2312                                                      base_ty,
2313                                                      Some(arg_tys))
2314             }
2315             (method, _) => method,
2316         };
2317
2318         method
2319     }
2320
2321     fn check_method_argument_types(&self,
2322                                    sp: Span,
2323                                    method: Result<ty::MethodCallee<'tcx>, ()>,
2324                                    args_no_rcvr: &'gcx [hir::Expr],
2325                                    tuple_arguments: TupleArgumentsFlag,
2326                                    expected: Expectation<'tcx>)
2327                                    -> Ty<'tcx> {
2328         let has_error = match method {
2329             Ok(method) => {
2330                 method.substs.references_error() || method.sig.references_error()
2331             }
2332             Err(_) => true
2333         };
2334         if has_error {
2335             let err_inputs = self.err_args(args_no_rcvr.len());
2336
2337             let err_inputs = match tuple_arguments {
2338                 DontTupleArguments => err_inputs,
2339                 TupleArguments => vec![self.tcx.intern_tup(&err_inputs[..], false)],
2340             };
2341
2342             self.check_argument_types(sp, &err_inputs[..], &[], args_no_rcvr,
2343                                       false, tuple_arguments, None);
2344             return self.tcx.types.err;
2345         }
2346
2347         let method = method.unwrap();
2348         // HACK(eddyb) ignore self in the definition (see above).
2349         let expected_arg_tys = self.expected_inputs_for_expected_output(
2350             sp,
2351             expected,
2352             method.sig.output(),
2353             &method.sig.inputs()[1..]
2354         );
2355         self.check_argument_types(sp, &method.sig.inputs()[1..], &expected_arg_tys[..],
2356                                   args_no_rcvr, method.sig.variadic, tuple_arguments,
2357                                   self.tcx.hir.span_if_local(method.def_id));
2358         method.sig.output()
2359     }
2360
2361     /// Generic function that factors out common logic from function calls,
2362     /// method calls and overloaded operators.
2363     fn check_argument_types(&self,
2364                             sp: Span,
2365                             fn_inputs: &[Ty<'tcx>],
2366                             expected_arg_tys: &[Ty<'tcx>],
2367                             args: &'gcx [hir::Expr],
2368                             variadic: bool,
2369                             tuple_arguments: TupleArgumentsFlag,
2370                             def_span: Option<Span>) {
2371         let tcx = self.tcx;
2372
2373         // Grab the argument types, supplying fresh type variables
2374         // if the wrong number of arguments were supplied
2375         let supplied_arg_count = if tuple_arguments == DontTupleArguments {
2376             args.len()
2377         } else {
2378             1
2379         };
2380
2381         // All the input types from the fn signature must outlive the call
2382         // so as to validate implied bounds.
2383         for &fn_input_ty in fn_inputs {
2384             self.register_wf_obligation(fn_input_ty, sp, traits::MiscObligation);
2385         }
2386
2387         let mut expected_arg_tys = expected_arg_tys;
2388         let expected_arg_count = fn_inputs.len();
2389
2390         let sp_args = if args.len() > 0 {
2391             let (first, args) = args.split_at(1);
2392             let mut sp_tmp = first[0].span;
2393             for arg in args {
2394                 let sp_opt = self.sess().codemap().merge_spans(sp_tmp, arg.span);
2395                 if ! sp_opt.is_some() {
2396                     break;
2397                 }
2398                 sp_tmp = sp_opt.unwrap();
2399             };
2400             sp_tmp
2401         } else {
2402             sp
2403         };
2404
2405         fn parameter_count_error<'tcx>(sess: &Session, sp: Span, expected_count: usize,
2406                                        arg_count: usize, error_code: &str, variadic: bool,
2407                                        def_span: Option<Span>) {
2408             let mut err = sess.struct_span_err_with_code(sp,
2409                 &format!("this function takes {}{} parameter{} but {} parameter{} supplied",
2410                     if variadic {"at least "} else {""},
2411                     expected_count,
2412                     if expected_count == 1 {""} else {"s"},
2413                     arg_count,
2414                     if arg_count == 1 {" was"} else {"s were"}),
2415                 error_code);
2416
2417             err.span_label(sp, format!("expected {}{} parameter{}",
2418                                         if variadic {"at least "} else {""},
2419                                         expected_count,
2420                                         if expected_count == 1 {""} else {"s"}));
2421             if let Some(def_s) = def_span {
2422                 err.span_label(def_s, "defined here");
2423             }
2424             err.emit();
2425         }
2426
2427         let formal_tys = if tuple_arguments == TupleArguments {
2428             let tuple_type = self.structurally_resolved_type(sp, fn_inputs[0]);
2429             match tuple_type.sty {
2430                 ty::TyTuple(arg_types, _) if arg_types.len() != args.len() => {
2431                     parameter_count_error(tcx.sess, sp_args, arg_types.len(), args.len(),
2432                                           "E0057", false, def_span);
2433                     expected_arg_tys = &[];
2434                     self.err_args(args.len())
2435                 }
2436                 ty::TyTuple(arg_types, _) => {
2437                     expected_arg_tys = match expected_arg_tys.get(0) {
2438                         Some(&ty) => match ty.sty {
2439                             ty::TyTuple(ref tys, _) => &tys,
2440                             _ => &[]
2441                         },
2442                         None => &[]
2443                     };
2444                     arg_types.to_vec()
2445                 }
2446                 _ => {
2447                     span_err!(tcx.sess, sp, E0059,
2448                         "cannot use call notation; the first type parameter \
2449                          for the function trait is neither a tuple nor unit");
2450                     expected_arg_tys = &[];
2451                     self.err_args(args.len())
2452                 }
2453             }
2454         } else if expected_arg_count == supplied_arg_count {
2455             fn_inputs.to_vec()
2456         } else if variadic {
2457             if supplied_arg_count >= expected_arg_count {
2458                 fn_inputs.to_vec()
2459             } else {
2460                 parameter_count_error(tcx.sess, sp_args, expected_arg_count,
2461                                       supplied_arg_count, "E0060", true, def_span);
2462                 expected_arg_tys = &[];
2463                 self.err_args(supplied_arg_count)
2464             }
2465         } else {
2466             parameter_count_error(tcx.sess, sp_args, expected_arg_count,
2467                                   supplied_arg_count, "E0061", false, def_span);
2468             expected_arg_tys = &[];
2469             self.err_args(supplied_arg_count)
2470         };
2471
2472         debug!("check_argument_types: formal_tys={:?}",
2473                formal_tys.iter().map(|t| self.ty_to_string(*t)).collect::<Vec<String>>());
2474
2475         // Check the arguments.
2476         // We do this in a pretty awful way: first we typecheck any arguments
2477         // that are not closures, then we typecheck the closures. This is so
2478         // that we have more information about the types of arguments when we
2479         // typecheck the functions. This isn't really the right way to do this.
2480         for &check_closures in &[false, true] {
2481             debug!("check_closures={}", check_closures);
2482
2483             // More awful hacks: before we check argument types, try to do
2484             // an "opportunistic" vtable resolution of any trait bounds on
2485             // the call. This helps coercions.
2486             if check_closures {
2487                 self.select_obligations_where_possible();
2488             }
2489
2490             // For variadic functions, we don't have a declared type for all of
2491             // the arguments hence we only do our usual type checking with
2492             // the arguments who's types we do know.
2493             let t = if variadic {
2494                 expected_arg_count
2495             } else if tuple_arguments == TupleArguments {
2496                 args.len()
2497             } else {
2498                 supplied_arg_count
2499             };
2500             for (i, arg) in args.iter().take(t).enumerate() {
2501                 // Warn only for the first loop (the "no closures" one).
2502                 // Closure arguments themselves can't be diverging, but
2503                 // a previous argument can, e.g. `foo(panic!(), || {})`.
2504                 if !check_closures {
2505                     self.warn_if_unreachable(arg.id, arg.span, "expression");
2506                 }
2507
2508                 let is_closure = match arg.node {
2509                     hir::ExprClosure(..) => true,
2510                     _ => false
2511                 };
2512
2513                 if is_closure != check_closures {
2514                     continue;
2515                 }
2516
2517                 debug!("checking the argument");
2518                 let formal_ty = formal_tys[i];
2519
2520                 // The special-cased logic below has three functions:
2521                 // 1. Provide as good of an expected type as possible.
2522                 let expected = expected_arg_tys.get(i).map(|&ty| {
2523                     Expectation::rvalue_hint(self, ty)
2524                 });
2525
2526                 let checked_ty = self.check_expr_with_expectation(
2527                     &arg,
2528                     expected.unwrap_or(ExpectHasType(formal_ty)));
2529
2530                 // 2. Coerce to the most detailed type that could be coerced
2531                 //    to, which is `expected_ty` if `rvalue_hint` returns an
2532                 //    `ExpectHasType(expected_ty)`, or the `formal_ty` otherwise.
2533                 let coerce_ty = expected.and_then(|e| e.only_has_type(self));
2534                 self.demand_coerce(&arg, checked_ty, coerce_ty.unwrap_or(formal_ty));
2535
2536                 // 3. Relate the expected type and the formal one,
2537                 //    if the expected type was used for the coercion.
2538                 coerce_ty.map(|ty| self.demand_suptype(arg.span, formal_ty, ty));
2539             }
2540         }
2541
2542         // We also need to make sure we at least write the ty of the other
2543         // arguments which we skipped above.
2544         if variadic {
2545             for arg in args.iter().skip(expected_arg_count) {
2546                 let arg_ty = self.check_expr(&arg);
2547
2548                 // There are a few types which get autopromoted when passed via varargs
2549                 // in C but we just error out instead and require explicit casts.
2550                 let arg_ty = self.structurally_resolved_type(arg.span,
2551                                                              arg_ty);
2552                 match arg_ty.sty {
2553                     ty::TyFloat(ast::FloatTy::F32) => {
2554                         self.type_error_message(arg.span, |t| {
2555                             format!("can't pass an `{}` to variadic \
2556                                      function, cast to `c_double`", t)
2557                         }, arg_ty);
2558                     }
2559                     ty::TyInt(ast::IntTy::I8) | ty::TyInt(ast::IntTy::I16) | ty::TyBool => {
2560                         self.type_error_message(arg.span, |t| {
2561                             format!("can't pass `{}` to variadic \
2562                                      function, cast to `c_int`",
2563                                            t)
2564                         }, arg_ty);
2565                     }
2566                     ty::TyUint(ast::UintTy::U8) | ty::TyUint(ast::UintTy::U16) => {
2567                         self.type_error_message(arg.span, |t| {
2568                             format!("can't pass `{}` to variadic \
2569                                      function, cast to `c_uint`",
2570                                            t)
2571                         }, arg_ty);
2572                     }
2573                     ty::TyFnDef(.., f) => {
2574                         let ptr_ty = self.tcx.mk_fn_ptr(f);
2575                         let ptr_ty = self.resolve_type_vars_if_possible(&ptr_ty);
2576                         self.type_error_message(arg.span,
2577                                                 |t| {
2578                             format!("can't pass `{}` to variadic \
2579                                      function, cast to `{}`", t, ptr_ty)
2580                         }, arg_ty);
2581                     }
2582                     _ => {}
2583                 }
2584             }
2585         }
2586     }
2587
2588     fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> {
2589         (0..len).map(|_| self.tcx.types.err).collect()
2590     }
2591
2592     // AST fragment checking
2593     fn check_lit(&self,
2594                  lit: &ast::Lit,
2595                  expected: Expectation<'tcx>)
2596                  -> Ty<'tcx>
2597     {
2598         let tcx = self.tcx;
2599
2600         match lit.node {
2601             ast::LitKind::Str(..) => tcx.mk_static_str(),
2602             ast::LitKind::ByteStr(ref v) => {
2603                 tcx.mk_imm_ref(tcx.types.re_static,
2604                                 tcx.mk_array(tcx.types.u8, v.len()))
2605             }
2606             ast::LitKind::Byte(_) => tcx.types.u8,
2607             ast::LitKind::Char(_) => tcx.types.char,
2608             ast::LitKind::Int(_, ast::LitIntType::Signed(t)) => tcx.mk_mach_int(t),
2609             ast::LitKind::Int(_, ast::LitIntType::Unsigned(t)) => tcx.mk_mach_uint(t),
2610             ast::LitKind::Int(_, ast::LitIntType::Unsuffixed) => {
2611                 let opt_ty = expected.to_option(self).and_then(|ty| {
2612                     match ty.sty {
2613                         ty::TyInt(_) | ty::TyUint(_) => Some(ty),
2614                         ty::TyChar => Some(tcx.types.u8),
2615                         ty::TyRawPtr(..) => Some(tcx.types.usize),
2616                         ty::TyFnDef(..) | ty::TyFnPtr(_) => Some(tcx.types.usize),
2617                         _ => None
2618                     }
2619                 });
2620                 opt_ty.unwrap_or_else(
2621                     || tcx.mk_int_var(self.next_int_var_id()))
2622             }
2623             ast::LitKind::Float(_, t) => tcx.mk_mach_float(t),
2624             ast::LitKind::FloatUnsuffixed(_) => {
2625                 let opt_ty = expected.to_option(self).and_then(|ty| {
2626                     match ty.sty {
2627                         ty::TyFloat(_) => Some(ty),
2628                         _ => None
2629                     }
2630                 });
2631                 opt_ty.unwrap_or_else(
2632                     || tcx.mk_float_var(self.next_float_var_id()))
2633             }
2634             ast::LitKind::Bool(_) => tcx.types.bool
2635         }
2636     }
2637
2638     fn check_expr_eq_type(&self,
2639                           expr: &'gcx hir::Expr,
2640                           expected: Ty<'tcx>) {
2641         let ty = self.check_expr_with_hint(expr, expected);
2642         self.demand_eqtype(expr.span, expected, ty);
2643     }
2644
2645     pub fn check_expr_has_type(&self,
2646                                expr: &'gcx hir::Expr,
2647                                expected: Ty<'tcx>) -> Ty<'tcx> {
2648         let mut ty = self.check_expr_with_hint(expr, expected);
2649
2650         // While we don't allow *arbitrary* coercions here, we *do* allow
2651         // coercions from ! to `expected`.
2652         if ty.is_never() {
2653             assert!(!self.tables.borrow().adjustments.contains_key(&expr.id),
2654                     "expression with never type wound up being adjusted");
2655             let adj_ty = self.next_diverging_ty_var(
2656                 TypeVariableOrigin::AdjustmentType(expr.span));
2657             self.apply_adjustment(expr.id, Adjustment {
2658                 kind: Adjust::NeverToAny,
2659                 target: adj_ty
2660             });
2661             ty = adj_ty;
2662         }
2663
2664         self.demand_suptype(expr.span, expected, ty);
2665         ty
2666     }
2667
2668     fn check_expr_coercable_to_type(&self,
2669                                     expr: &'gcx hir::Expr,
2670                                     expected: Ty<'tcx>) -> Ty<'tcx> {
2671         let ty = self.check_expr_with_hint(expr, expected);
2672         self.demand_coerce(expr, ty, expected);
2673         ty
2674     }
2675
2676     fn check_expr_with_hint(&self, expr: &'gcx hir::Expr,
2677                             expected: Ty<'tcx>) -> Ty<'tcx> {
2678         self.check_expr_with_expectation(expr, ExpectHasType(expected))
2679     }
2680
2681     fn check_expr_with_expectation(&self,
2682                                    expr: &'gcx hir::Expr,
2683                                    expected: Expectation<'tcx>) -> Ty<'tcx> {
2684         self.check_expr_with_expectation_and_lvalue_pref(expr, expected, NoPreference)
2685     }
2686
2687     fn check_expr(&self, expr: &'gcx hir::Expr) -> Ty<'tcx> {
2688         self.check_expr_with_expectation(expr, NoExpectation)
2689     }
2690
2691     fn check_expr_with_lvalue_pref(&self, expr: &'gcx hir::Expr,
2692                                    lvalue_pref: LvaluePreference) -> Ty<'tcx> {
2693         self.check_expr_with_expectation_and_lvalue_pref(expr, NoExpectation, lvalue_pref)
2694     }
2695
2696     // determine the `self` type, using fresh variables for all variables
2697     // declared on the impl declaration e.g., `impl<A,B> for Vec<(A,B)>`
2698     // would return ($0, $1) where $0 and $1 are freshly instantiated type
2699     // variables.
2700     pub fn impl_self_ty(&self,
2701                         span: Span, // (potential) receiver for this impl
2702                         did: DefId)
2703                         -> TypeAndSubsts<'tcx> {
2704         let ity = self.tcx.type_of(did);
2705         debug!("impl_self_ty: ity={:?}", ity);
2706
2707         let substs = self.fresh_substs_for_item(span, did);
2708         let substd_ty = self.instantiate_type_scheme(span, &substs, &ity);
2709
2710         TypeAndSubsts { substs: substs, ty: substd_ty }
2711     }
2712
2713     /// Unifies the output type with the expected type early, for more coercions
2714     /// and forward type information on the input expressions.
2715     fn expected_inputs_for_expected_output(&self,
2716                                            call_span: Span,
2717                                            expected_ret: Expectation<'tcx>,
2718                                            formal_ret: Ty<'tcx>,
2719                                            formal_args: &[Ty<'tcx>])
2720                                            -> Vec<Ty<'tcx>> {
2721         let expected_args = expected_ret.only_has_type(self).and_then(|ret_ty| {
2722             self.fudge_regions_if_ok(&RegionVariableOrigin::Coercion(call_span), || {
2723                 // Attempt to apply a subtyping relationship between the formal
2724                 // return type (likely containing type variables if the function
2725                 // is polymorphic) and the expected return type.
2726                 // No argument expectations are produced if unification fails.
2727                 let origin = self.misc(call_span);
2728                 let ures = self.sub_types(false, &origin, formal_ret, ret_ty);
2729
2730                 // FIXME(#15760) can't use try! here, FromError doesn't default
2731                 // to identity so the resulting type is not constrained.
2732                 match ures {
2733                     Ok(ok) => {
2734                         // Process any obligations locally as much as
2735                         // we can.  We don't care if some things turn
2736                         // out unconstrained or ambiguous, as we're
2737                         // just trying to get hints here.
2738                         let result = self.save_and_restore_in_snapshot_flag(|_| {
2739                             let mut fulfill = FulfillmentContext::new();
2740                             let ok = ok; // FIXME(#30046)
2741                             for obligation in ok.obligations {
2742                                 fulfill.register_predicate_obligation(self, obligation);
2743                             }
2744                             fulfill.select_where_possible(self)
2745                         });
2746
2747                         match result {
2748                             Ok(()) => { }
2749                             Err(_) => return Err(()),
2750                         }
2751                     }
2752                     Err(_) => return Err(()),
2753                 }
2754
2755                 // Record all the argument types, with the substitutions
2756                 // produced from the above subtyping unification.
2757                 Ok(formal_args.iter().map(|ty| {
2758                     self.resolve_type_vars_if_possible(ty)
2759                 }).collect())
2760             }).ok()
2761         }).unwrap_or(vec![]);
2762         debug!("expected_inputs_for_expected_output(formal={:?} -> {:?}, expected={:?} -> {:?})",
2763                formal_args, formal_ret,
2764                expected_args, expected_ret);
2765         expected_args
2766     }
2767
2768     // Checks a method call.
2769     fn check_method_call(&self,
2770                          expr: &'gcx hir::Expr,
2771                          method_name: Spanned<ast::Name>,
2772                          args: &'gcx [hir::Expr],
2773                          tps: &[P<hir::Ty>],
2774                          expected: Expectation<'tcx>,
2775                          lvalue_pref: LvaluePreference) -> Ty<'tcx> {
2776         let rcvr = &args[0];
2777         let rcvr_t = self.check_expr_with_lvalue_pref(&rcvr, lvalue_pref);
2778
2779         // no need to check for bot/err -- callee does that
2780         let expr_t = self.structurally_resolved_type(expr.span, rcvr_t);
2781
2782         let tps = tps.iter().map(|ast_ty| self.to_ty(&ast_ty)).collect::<Vec<_>>();
2783         let method = match self.lookup_method(method_name.span,
2784                                               method_name.node,
2785                                               expr_t,
2786                                               tps,
2787                                               expr,
2788                                               rcvr) {
2789             Ok(method) => {
2790                 self.tables.borrow_mut().method_map.insert(expr.id, method);
2791                 Ok(method)
2792             }
2793             Err(error) => {
2794                 if method_name.node != keywords::Invalid.name() {
2795                     self.report_method_error(method_name.span,
2796                                              expr_t,
2797                                              method_name.node,
2798                                              Some(rcvr),
2799                                              error,
2800                                              Some(args));
2801                 }
2802                 Err(())
2803             }
2804         };
2805
2806         // Call the generic checker.
2807         self.check_method_argument_types(method_name.span, method,
2808                                          &args[1..],
2809                                          DontTupleArguments,
2810                                          expected)
2811     }
2812
2813     fn check_return_expr(&self, return_expr: &'gcx hir::Expr) {
2814         let ret_coercion =
2815             self.ret_coercion
2816                 .as_ref()
2817                 .unwrap_or_else(|| span_bug!(return_expr.span,
2818                                              "check_return_expr called outside fn body"));
2819
2820         let ret_ty = ret_coercion.borrow().expected_ty();
2821         let return_expr_ty = self.check_expr_with_hint(return_expr, ret_ty);
2822         ret_coercion.borrow_mut()
2823                     .coerce(self,
2824                             &self.misc(return_expr.span),
2825                             return_expr,
2826                             return_expr_ty,
2827                             self.diverges.get());
2828     }
2829
2830
2831     // A generic function for checking the then and else in an if
2832     // or if-else.
2833     fn check_then_else(&self,
2834                        cond_expr: &'gcx hir::Expr,
2835                        then_expr: &'gcx hir::Expr,
2836                        opt_else_expr: Option<&'gcx hir::Expr>,
2837                        sp: Span,
2838                        expected: Expectation<'tcx>) -> Ty<'tcx> {
2839         let cond_ty = self.check_expr_has_type(cond_expr, self.tcx.types.bool);
2840         let cond_diverges = self.diverges.get();
2841         self.diverges.set(Diverges::Maybe);
2842
2843         let expected = expected.adjust_for_branches(self);
2844         let then_ty = self.check_expr_with_expectation(then_expr, expected);
2845         let then_diverges = self.diverges.get();
2846         self.diverges.set(Diverges::Maybe);
2847
2848         // We've already taken the expected type's preferences
2849         // into account when typing the `then` branch. To figure
2850         // out the initial shot at a LUB, we thus only consider
2851         // `expected` if it represents a *hard* constraint
2852         // (`only_has_type`); otherwise, we just go with a
2853         // fresh type variable.
2854         let coerce_to_ty = expected.coercion_target_type(self, sp);
2855         let mut coerce: DynamicCoerceMany = CoerceMany::new(coerce_to_ty);
2856
2857         let if_cause = self.cause(sp, ObligationCauseCode::IfExpression);
2858         coerce.coerce(self, &if_cause, then_expr, then_ty, then_diverges);
2859
2860         if let Some(else_expr) = opt_else_expr {
2861             let else_ty = self.check_expr_with_expectation(else_expr, expected);
2862             let else_diverges = self.diverges.get();
2863
2864             coerce.coerce(self, &if_cause, else_expr, else_ty, else_diverges);
2865
2866             // We won't diverge unless both branches do (or the condition does).
2867             self.diverges.set(cond_diverges | then_diverges & else_diverges);
2868         } else {
2869             let else_cause = self.cause(sp, ObligationCauseCode::IfExpressionWithNoElse);
2870             coerce.coerce_forced_unit(self, &else_cause, &mut |_| (), true);
2871
2872             // If the condition is false we can't diverge.
2873             self.diverges.set(cond_diverges);
2874         }
2875
2876         let result_ty = coerce.complete(self);
2877         if cond_ty.references_error() {
2878             self.tcx.types.err
2879         } else {
2880             result_ty
2881         }
2882     }
2883
2884     // Check field access expressions
2885     fn check_field(&self,
2886                    expr: &'gcx hir::Expr,
2887                    lvalue_pref: LvaluePreference,
2888                    base: &'gcx hir::Expr,
2889                    field: &Spanned<ast::Name>) -> Ty<'tcx> {
2890         let expr_t = self.check_expr_with_lvalue_pref(base, lvalue_pref);
2891         let expr_t = self.structurally_resolved_type(expr.span,
2892                                                      expr_t);
2893         let mut private_candidate = None;
2894         let mut autoderef = self.autoderef(expr.span, expr_t);
2895         while let Some((base_t, _)) = autoderef.next() {
2896             match base_t.sty {
2897                 ty::TyAdt(base_def, substs) if !base_def.is_enum() => {
2898                     debug!("struct named {:?}",  base_t);
2899                     let (ident, def_scope) =
2900                         self.tcx.adjust(field.node, base_def.did, self.body_id);
2901                     let fields = &base_def.struct_variant().fields;
2902                     if let Some(field) = fields.iter().find(|f| f.name.to_ident() == ident) {
2903                         let field_ty = self.field_ty(expr.span, field, substs);
2904                         if field.vis.is_accessible_from(def_scope, self.tcx) {
2905                             let autoderefs = autoderef.adjust_steps(lvalue_pref);
2906                             self.apply_autoderef_adjustment(base.id, autoderefs, base_t);
2907                             autoderef.finalize();
2908
2909                             self.tcx.check_stability(field.did, expr.id, expr.span);
2910
2911                             return field_ty;
2912                         }
2913                         private_candidate = Some((base_def.did, field_ty));
2914                     }
2915                 }
2916                 _ => {}
2917             }
2918         }
2919         autoderef.unambiguous_final_ty();
2920
2921         if let Some((did, field_ty)) = private_candidate {
2922             let struct_path = self.tcx().item_path_str(did);
2923             let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
2924             let mut err = self.tcx().sess.struct_span_err(expr.span, &msg);
2925             // Also check if an accessible method exists, which is often what is meant.
2926             if self.method_exists(field.span, field.node, expr_t, expr.id, false) {
2927                 err.note(&format!("a method `{}` also exists, perhaps you wish to call it",
2928                                   field.node));
2929             }
2930             err.emit();
2931             field_ty
2932         } else if field.node == keywords::Invalid.name() {
2933             self.tcx().types.err
2934         } else if self.method_exists(field.span, field.node, expr_t, expr.id, true) {
2935             self.type_error_struct(field.span, |actual| {
2936                 format!("attempted to take value of method `{}` on type \
2937                          `{}`", field.node, actual)
2938             }, expr_t)
2939                 .help("maybe a `()` to call it is missing? \
2940                        If not, try an anonymous function")
2941                 .emit();
2942             self.tcx().types.err
2943         } else {
2944             let mut err = self.type_error_struct(field.span, |actual| {
2945                 format!("no field `{}` on type `{}`",
2946                         field.node, actual)
2947             }, expr_t);
2948             match expr_t.sty {
2949                 ty::TyAdt(def, _) if !def.is_enum() => {
2950                     if let Some(suggested_field_name) =
2951                         Self::suggest_field_name(def.struct_variant(), field, vec![]) {
2952                             err.span_label(field.span,
2953                                            format!("did you mean `{}`?", suggested_field_name));
2954                         } else {
2955                             err.span_label(field.span,
2956                                            "unknown field");
2957                         };
2958                 }
2959                 ty::TyRawPtr(..) => {
2960                     err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \
2961                                       `(*{0}).{1}`",
2962                                       self.tcx.hir.node_to_pretty_string(base.id),
2963                                       field.node));
2964                 }
2965                 _ => {}
2966             }
2967             err.emit();
2968             self.tcx().types.err
2969         }
2970     }
2971
2972     // Return an hint about the closest match in field names
2973     fn suggest_field_name(variant: &'tcx ty::VariantDef,
2974                           field: &Spanned<ast::Name>,
2975                           skip : Vec<InternedString>)
2976                           -> Option<Symbol> {
2977         let name = field.node.as_str();
2978         let names = variant.fields.iter().filter_map(|field| {
2979             // ignore already set fields and private fields from non-local crates
2980             if skip.iter().any(|x| *x == field.name.as_str()) ||
2981                (variant.did.krate != LOCAL_CRATE && field.vis != Visibility::Public) {
2982                 None
2983             } else {
2984                 Some(&field.name)
2985             }
2986         });
2987
2988         // only find fits with at least one matching letter
2989         find_best_match_for_name(names, &name, Some(name.len()))
2990     }
2991
2992     // Check tuple index expressions
2993     fn check_tup_field(&self,
2994                        expr: &'gcx hir::Expr,
2995                        lvalue_pref: LvaluePreference,
2996                        base: &'gcx hir::Expr,
2997                        idx: codemap::Spanned<usize>) -> Ty<'tcx> {
2998         let expr_t = self.check_expr_with_lvalue_pref(base, lvalue_pref);
2999         let expr_t = self.structurally_resolved_type(expr.span,
3000                                                      expr_t);
3001         let mut private_candidate = None;
3002         let mut tuple_like = false;
3003         let mut autoderef = self.autoderef(expr.span, expr_t);
3004         while let Some((base_t, _)) = autoderef.next() {
3005             let field = match base_t.sty {
3006                 ty::TyAdt(base_def, substs) if base_def.is_struct() => {
3007                     tuple_like = base_def.struct_variant().ctor_kind == CtorKind::Fn;
3008                     if !tuple_like { continue }
3009
3010                     debug!("tuple struct named {:?}",  base_t);
3011                     let ident = ast::Ident {
3012                         name: Symbol::intern(&idx.node.to_string()),
3013                         ctxt: idx.span.ctxt.modern(),
3014                     };
3015                     let (ident, def_scope) =
3016                         self.tcx.adjust_ident(ident, base_def.did, self.body_id);
3017                     let fields = &base_def.struct_variant().fields;
3018                     if let Some(field) = fields.iter().find(|f| f.name.to_ident() == ident) {
3019                         let field_ty = self.field_ty(expr.span, field, substs);
3020                         if field.vis.is_accessible_from(def_scope, self.tcx) {
3021                             self.tcx.check_stability(field.did, expr.id, expr.span);
3022                             Some(field_ty)
3023                         } else {
3024                             private_candidate = Some((base_def.did, field_ty));
3025                             None
3026                         }
3027                     } else {
3028                         None
3029                     }
3030                 }
3031                 ty::TyTuple(ref v, _) => {
3032                     tuple_like = true;
3033                     v.get(idx.node).cloned()
3034                 }
3035                 _ => continue
3036             };
3037
3038             if let Some(field_ty) = field {
3039                 let autoderefs = autoderef.adjust_steps(lvalue_pref);
3040                 self.apply_autoderef_adjustment(base.id, autoderefs, base_t);
3041                 autoderef.finalize();
3042                 return field_ty;
3043             }
3044         }
3045         autoderef.unambiguous_final_ty();
3046
3047         if let Some((did, field_ty)) = private_candidate {
3048             let struct_path = self.tcx().item_path_str(did);
3049             let msg = format!("field `{}` of struct `{}` is private", idx.node, struct_path);
3050             self.tcx().sess.span_err(expr.span, &msg);
3051             return field_ty;
3052         }
3053
3054         self.type_error_message(
3055             expr.span,
3056             |actual| {
3057                 if tuple_like {
3058                     format!("attempted out-of-bounds tuple index `{}` on \
3059                                     type `{}`",
3060                                    idx.node,
3061                                    actual)
3062                 } else {
3063                     format!("attempted tuple index `{}` on type `{}`, but the \
3064                                      type was not a tuple or tuple struct",
3065                                     idx.node,
3066                                     actual)
3067                 }
3068             },
3069             expr_t);
3070
3071         self.tcx().types.err
3072     }
3073
3074     fn report_unknown_field(&self,
3075                             ty: Ty<'tcx>,
3076                             variant: &'tcx ty::VariantDef,
3077                             field: &hir::Field,
3078                             skip_fields: &[hir::Field],
3079                             kind_name: &str) {
3080         let mut err = self.type_error_struct_with_diag(
3081             field.name.span,
3082             |actual| match ty.sty {
3083                 ty::TyAdt(adt, ..) if adt.is_enum() => {
3084                     struct_span_err!(self.tcx.sess, field.name.span, E0559,
3085                                     "{} `{}::{}` has no field named `{}`",
3086                                     kind_name, actual, variant.name, field.name.node)
3087                 }
3088                 _ => {
3089                     struct_span_err!(self.tcx.sess, field.name.span, E0560,
3090                                     "{} `{}` has no field named `{}`",
3091                                     kind_name, actual, field.name.node)
3092                 }
3093             },
3094             ty);
3095         // prevent all specified fields from being suggested
3096         let skip_fields = skip_fields.iter().map(|ref x| x.name.node.as_str());
3097         if let Some(field_name) = Self::suggest_field_name(variant,
3098                                                            &field.name,
3099                                                            skip_fields.collect()) {
3100             err.span_label(field.name.span,
3101                            format!("field does not exist - did you mean `{}`?", field_name));
3102         } else {
3103             match ty.sty {
3104                 ty::TyAdt(adt, ..) if adt.is_enum() => {
3105                     err.span_label(field.name.span, format!("`{}::{}` does not have this field",
3106                                                              ty, variant.name));
3107                 }
3108                 _ => {
3109                     err.span_label(field.name.span, format!("`{}` does not have this field", ty));
3110                 }
3111             }
3112         };
3113         err.emit();
3114     }
3115
3116     fn check_expr_struct_fields(&self,
3117                                 adt_ty: Ty<'tcx>,
3118                                 expected: Expectation<'tcx>,
3119                                 expr_id: ast::NodeId,
3120                                 span: Span,
3121                                 variant: &'tcx ty::VariantDef,
3122                                 ast_fields: &'gcx [hir::Field],
3123                                 check_completeness: bool) {
3124         let tcx = self.tcx;
3125
3126         let adt_ty_hint =
3127             self.expected_inputs_for_expected_output(span, expected, adt_ty, &[adt_ty])
3128                 .get(0).cloned().unwrap_or(adt_ty);
3129
3130         let (substs, hint_substs, adt_kind, kind_name) = match (&adt_ty.sty, &adt_ty_hint.sty) {
3131             (&ty::TyAdt(adt, substs), &ty::TyAdt(_, hint_substs)) => {
3132                 (substs, hint_substs, adt.adt_kind(), adt.variant_descr())
3133             }
3134             _ => span_bug!(span, "non-ADT passed to check_expr_struct_fields")
3135         };
3136
3137         let mut remaining_fields = FxHashMap();
3138         for field in &variant.fields {
3139             remaining_fields.insert(field.name.to_ident(), field);
3140         }
3141
3142         let mut seen_fields = FxHashMap();
3143
3144         let mut error_happened = false;
3145
3146         // Typecheck each field.
3147         for field in ast_fields {
3148             let final_field_type;
3149             let field_type_hint;
3150
3151             let ident = tcx.adjust(field.name.node, variant.did, self.body_id).0;
3152             if let Some(v_field) = remaining_fields.remove(&ident) {
3153                 final_field_type = self.field_ty(field.span, v_field, substs);
3154                 field_type_hint = self.field_ty(field.span, v_field, hint_substs);
3155
3156                 seen_fields.insert(field.name.node, field.span);
3157
3158                 // we don't look at stability attributes on
3159                 // struct-like enums (yet...), but it's definitely not
3160                 // a bug to have construct one.
3161                 if adt_kind != ty::AdtKind::Enum {
3162                     tcx.check_stability(v_field.did, expr_id, field.span);
3163                 }
3164             } else {
3165                 error_happened = true;
3166                 final_field_type = tcx.types.err;
3167                 field_type_hint = tcx.types.err;
3168                 if let Some(_) = variant.find_field_named(field.name.node) {
3169                     let mut err = struct_span_err!(self.tcx.sess,
3170                                                 field.name.span,
3171                                                 E0062,
3172                                                 "field `{}` specified more than once",
3173                                                 field.name.node);
3174
3175                     err.span_label(field.name.span, "used more than once");
3176
3177                     if let Some(prev_span) = seen_fields.get(&field.name.node) {
3178                         err.span_label(*prev_span, format!("first use of `{}`", field.name.node));
3179                     }
3180
3181                     err.emit();
3182                 } else {
3183                     self.report_unknown_field(adt_ty, variant, field, ast_fields, kind_name);
3184                 }
3185             }
3186
3187             // Make sure to give a type to the field even if there's
3188             // an error, so we can continue typechecking
3189             let ty = self.check_expr_with_hint(&field.expr, field_type_hint);
3190             self.demand_coerce(&field.expr, ty, final_field_type);
3191         }
3192
3193         // Make sure the programmer specified correct number of fields.
3194         if kind_name == "union" {
3195             if ast_fields.len() != 1 {
3196                 tcx.sess.span_err(span, "union expressions should have exactly one field");
3197             }
3198         } else if check_completeness && !error_happened && !remaining_fields.is_empty() {
3199             let len = remaining_fields.len();
3200
3201             let mut displayable_field_names = remaining_fields
3202                                               .keys()
3203                                               .map(|ident| ident.name.as_str())
3204                                               .collect::<Vec<_>>();
3205
3206             displayable_field_names.sort();
3207
3208             let truncated_fields_error = if len <= 3 {
3209                 "".to_string()
3210             } else {
3211                 format!(" and {} other field{}", (len - 3), if len - 3 == 1 {""} else {"s"})
3212             };
3213
3214             let remaining_fields_names = displayable_field_names.iter().take(3)
3215                                         .map(|n| format!("`{}`", n))
3216                                         .collect::<Vec<_>>()
3217                                         .join(", ");
3218
3219             struct_span_err!(tcx.sess, span, E0063,
3220                         "missing field{} {}{} in initializer of `{}`",
3221                         if remaining_fields.len() == 1 {""} else {"s"},
3222                         remaining_fields_names,
3223                         truncated_fields_error,
3224                         adt_ty)
3225                         .span_label(span, format!("missing {}{}",
3226                             remaining_fields_names,
3227                             truncated_fields_error))
3228                         .emit();
3229         }
3230     }
3231
3232     fn check_struct_fields_on_error(&self,
3233                                     fields: &'gcx [hir::Field],
3234                                     base_expr: &'gcx Option<P<hir::Expr>>) {
3235         for field in fields {
3236             self.check_expr(&field.expr);
3237         }
3238         match *base_expr {
3239             Some(ref base) => {
3240                 self.check_expr(&base);
3241             },
3242             None => {}
3243         }
3244     }
3245
3246     pub fn check_struct_path(&self,
3247                              qpath: &hir::QPath,
3248                              node_id: ast::NodeId)
3249                              -> Option<(&'tcx ty::VariantDef,  Ty<'tcx>)> {
3250         let path_span = match *qpath {
3251             hir::QPath::Resolved(_, ref path) => path.span,
3252             hir::QPath::TypeRelative(ref qself, _) => qself.span
3253         };
3254         let (def, ty) = self.finish_resolving_struct_path(qpath, path_span, node_id);
3255         let variant = match def {
3256             Def::Err => {
3257                 self.set_tainted_by_errors();
3258                 return None;
3259             }
3260             Def::Variant(..) => {
3261                 match ty.sty {
3262                     ty::TyAdt(adt, substs) => {
3263                         Some((adt.variant_of_def(def), adt.did, substs))
3264                     }
3265                     _ => bug!("unexpected type: {:?}", ty.sty)
3266                 }
3267             }
3268             Def::Struct(..) | Def::Union(..) | Def::TyAlias(..) |
3269             Def::AssociatedTy(..) | Def::SelfTy(..) => {
3270                 match ty.sty {
3271                     ty::TyAdt(adt, substs) if !adt.is_enum() => {
3272                         Some((adt.struct_variant(), adt.did, substs))
3273                     }
3274                     _ => None,
3275                 }
3276             }
3277             _ => bug!("unexpected definition: {:?}", def)
3278         };
3279
3280         if let Some((variant, did, substs)) = variant {
3281             // Check bounds on type arguments used in the path.
3282             let bounds = self.instantiate_bounds(path_span, did, substs);
3283             let cause = traits::ObligationCause::new(path_span, self.body_id,
3284                                                      traits::ItemObligation(did));
3285             self.add_obligations_for_parameters(cause, &bounds);
3286
3287             Some((variant, ty))
3288         } else {
3289             struct_span_err!(self.tcx.sess, path_span, E0071,
3290                              "expected struct, variant or union type, found {}",
3291                              ty.sort_string(self.tcx))
3292                 .span_label(path_span, "not a struct")
3293                 .emit();
3294             None
3295         }
3296     }
3297
3298     fn check_expr_struct(&self,
3299                          expr: &hir::Expr,
3300                          expected: Expectation<'tcx>,
3301                          qpath: &hir::QPath,
3302                          fields: &'gcx [hir::Field],
3303                          base_expr: &'gcx Option<P<hir::Expr>>) -> Ty<'tcx>
3304     {
3305         // Find the relevant variant
3306         let (variant, struct_ty) =
3307         if let Some(variant_ty) = self.check_struct_path(qpath, expr.id) {
3308             variant_ty
3309         } else {
3310             self.check_struct_fields_on_error(fields, base_expr);
3311             return self.tcx.types.err;
3312         };
3313
3314         let path_span = match *qpath {
3315             hir::QPath::Resolved(_, ref path) => path.span,
3316             hir::QPath::TypeRelative(ref qself, _) => qself.span
3317         };
3318
3319         self.check_expr_struct_fields(struct_ty, expected, expr.id, path_span, variant, fields,
3320                                       base_expr.is_none());
3321         if let &Some(ref base_expr) = base_expr {
3322             self.check_expr_has_type(base_expr, struct_ty);
3323             match struct_ty.sty {
3324                 ty::TyAdt(adt, substs) if adt.is_struct() => {
3325                     self.tables.borrow_mut().fru_field_types.insert(
3326                         expr.id,
3327                         adt.struct_variant().fields.iter().map(|f| {
3328                             self.normalize_associated_types_in(
3329                                 expr.span, &f.ty(self.tcx, substs)
3330                             )
3331                         }).collect()
3332                     );
3333                 }
3334                 _ => {
3335                     span_err!(self.tcx.sess, base_expr.span, E0436,
3336                               "functional record update syntax requires a struct");
3337                 }
3338             }
3339         }
3340         self.require_type_is_sized(struct_ty, expr.span, traits::StructInitializerSized);
3341         struct_ty
3342     }
3343
3344
3345     /// Invariant:
3346     /// If an expression has any sub-expressions that result in a type error,
3347     /// inspecting that expression's type with `ty.references_error()` will return
3348     /// true. Likewise, if an expression is known to diverge, inspecting its
3349     /// type with `ty::type_is_bot` will return true (n.b.: since Rust is
3350     /// strict, _|_ can appear in the type of an expression that does not,
3351     /// itself, diverge: for example, fn() -> _|_.)
3352     /// Note that inspecting a type's structure *directly* may expose the fact
3353     /// that there are actually multiple representations for `TyError`, so avoid
3354     /// that when err needs to be handled differently.
3355     fn check_expr_with_expectation_and_lvalue_pref(&self,
3356                                                    expr: &'gcx hir::Expr,
3357                                                    expected: Expectation<'tcx>,
3358                                                    lvalue_pref: LvaluePreference) -> Ty<'tcx> {
3359         debug!(">> typechecking: expr={:?} expected={:?}",
3360                expr, expected);
3361
3362         // Warn for expressions after diverging siblings.
3363         self.warn_if_unreachable(expr.id, expr.span, "expression");
3364
3365         // Hide the outer diverging and has_errors flags.
3366         let old_diverges = self.diverges.get();
3367         let old_has_errors = self.has_errors.get();
3368         self.diverges.set(Diverges::Maybe);
3369         self.has_errors.set(false);
3370
3371         let ty = self.check_expr_kind(expr, expected, lvalue_pref);
3372
3373         // Warn for non-block expressions with diverging children.
3374         match expr.node {
3375             hir::ExprBlock(_) |
3376             hir::ExprLoop(..) | hir::ExprWhile(..) |
3377             hir::ExprIf(..) | hir::ExprMatch(..) => {}
3378
3379             _ => self.warn_if_unreachable(expr.id, expr.span, "expression")
3380         }
3381
3382         // Any expression that produces a value of type `!` must have diverged
3383         if ty.is_never() {
3384             self.diverges.set(self.diverges.get() | Diverges::Always);
3385         }
3386
3387         // Record the type, which applies it effects.
3388         // We need to do this after the warning above, so that
3389         // we don't warn for the diverging expression itself.
3390         self.write_ty(expr.id, ty);
3391
3392         // Combine the diverging and has_error flags.
3393         self.diverges.set(self.diverges.get() | old_diverges);
3394         self.has_errors.set(self.has_errors.get() | old_has_errors);
3395
3396         debug!("type of {} is...", self.tcx.hir.node_to_string(expr.id));
3397         debug!("... {:?}, expected is {:?}", ty, expected);
3398
3399         ty
3400     }
3401
3402     fn check_expr_kind(&self,
3403                        expr: &'gcx hir::Expr,
3404                        expected: Expectation<'tcx>,
3405                        lvalue_pref: LvaluePreference) -> Ty<'tcx> {
3406         let tcx = self.tcx;
3407         let id = expr.id;
3408         match expr.node {
3409           hir::ExprBox(ref subexpr) => {
3410             let expected_inner = expected.to_option(self).map_or(NoExpectation, |ty| {
3411                 match ty.sty {
3412                     ty::TyAdt(def, _) if def.is_box()
3413                         => Expectation::rvalue_hint(self, ty.boxed_ty()),
3414                     _ => NoExpectation
3415                 }
3416             });
3417             let referent_ty = self.check_expr_with_expectation(subexpr, expected_inner);
3418             tcx.mk_box(referent_ty)
3419           }
3420
3421           hir::ExprLit(ref lit) => {
3422             self.check_lit(&lit, expected)
3423           }
3424           hir::ExprBinary(op, ref lhs, ref rhs) => {
3425             self.check_binop(expr, op, lhs, rhs)
3426           }
3427           hir::ExprAssignOp(op, ref lhs, ref rhs) => {
3428             self.check_binop_assign(expr, op, lhs, rhs)
3429           }
3430           hir::ExprUnary(unop, ref oprnd) => {
3431             let expected_inner = match unop {
3432                 hir::UnNot | hir::UnNeg => {
3433                     expected
3434                 }
3435                 hir::UnDeref => {
3436                     NoExpectation
3437                 }
3438             };
3439             let lvalue_pref = match unop {
3440                 hir::UnDeref => lvalue_pref,
3441                 _ => NoPreference
3442             };
3443             let mut oprnd_t = self.check_expr_with_expectation_and_lvalue_pref(&oprnd,
3444                                                                                expected_inner,
3445                                                                                lvalue_pref);
3446
3447             if !oprnd_t.references_error() {
3448                 match unop {
3449                     hir::UnDeref => {
3450                         oprnd_t = self.structurally_resolved_type(expr.span, oprnd_t);
3451
3452                         if let Some(mt) = oprnd_t.builtin_deref(true, NoPreference) {
3453                             oprnd_t = mt.ty;
3454                         } else if let Some(ok) = self.try_overloaded_deref(
3455                                 expr.span, oprnd_t, lvalue_pref) {
3456                             let (autoref, method) = self.register_infer_ok_obligations(ok);
3457                             self.apply_adjustment(oprnd.id, Adjustment {
3458                                 kind: Adjust::DerefRef {
3459                                     autoderefs: vec![],
3460                                     autoref,
3461                                     unsize: false
3462                                 },
3463                                 target: method.sig.inputs()[0]
3464                             });
3465                             oprnd_t = self.make_overloaded_lvalue_return_type(method).ty;
3466                             self.tables.borrow_mut().method_map.insert(expr.id, method);
3467                         } else {
3468                             self.type_error_message(expr.span, |actual| {
3469                                 format!("type `{}` cannot be \
3470                                         dereferenced", actual)
3471                             }, oprnd_t);
3472                             oprnd_t = tcx.types.err;
3473                         }
3474                     }
3475                     hir::UnNot => {
3476                         oprnd_t = self.structurally_resolved_type(oprnd.span,
3477                                                                   oprnd_t);
3478                         let result = self.check_user_unop("!", "not",
3479                                                           tcx.lang_items.not_trait(),
3480                                                           expr, &oprnd, oprnd_t, unop);
3481                         // If it's builtin, we can reuse the type, this helps inference.
3482                         if !(oprnd_t.is_integral() || oprnd_t.sty == ty::TyBool) {
3483                             oprnd_t = result;
3484                         }
3485                     }
3486                     hir::UnNeg => {
3487                         oprnd_t = self.structurally_resolved_type(oprnd.span,
3488                                                                   oprnd_t);
3489                         let result = self.check_user_unop("-", "neg",
3490                                                           tcx.lang_items.neg_trait(),
3491                                                           expr, &oprnd, oprnd_t, unop);
3492                         // If it's builtin, we can reuse the type, this helps inference.
3493                         if !(oprnd_t.is_integral() || oprnd_t.is_fp()) {
3494                             oprnd_t = result;
3495                         }
3496                     }
3497                 }
3498             }
3499             oprnd_t
3500           }
3501           hir::ExprAddrOf(mutbl, ref oprnd) => {
3502             let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
3503                 match ty.sty {
3504                     ty::TyRef(_, ref mt) | ty::TyRawPtr(ref mt) => {
3505                         if self.tcx.expr_is_lval(&oprnd) {
3506                             // Lvalues may legitimately have unsized types.
3507                             // For example, dereferences of a fat pointer and
3508                             // the last field of a struct can be unsized.
3509                             ExpectHasType(mt.ty)
3510                         } else {
3511                             Expectation::rvalue_hint(self, mt.ty)
3512                         }
3513                     }
3514                     _ => NoExpectation
3515                 }
3516             });
3517             let lvalue_pref = LvaluePreference::from_mutbl(mutbl);
3518             let ty = self.check_expr_with_expectation_and_lvalue_pref(&oprnd, hint, lvalue_pref);
3519
3520             let tm = ty::TypeAndMut { ty: ty, mutbl: mutbl };
3521             if tm.ty.references_error() {
3522                 tcx.types.err
3523             } else {
3524                 // Note: at this point, we cannot say what the best lifetime
3525                 // is to use for resulting pointer.  We want to use the
3526                 // shortest lifetime possible so as to avoid spurious borrowck
3527                 // errors.  Moreover, the longest lifetime will depend on the
3528                 // precise details of the value whose address is being taken
3529                 // (and how long it is valid), which we don't know yet until type
3530                 // inference is complete.
3531                 //
3532                 // Therefore, here we simply generate a region variable.  The
3533                 // region inferencer will then select the ultimate value.
3534                 // Finally, borrowck is charged with guaranteeing that the
3535                 // value whose address was taken can actually be made to live
3536                 // as long as it needs to live.
3537                 let region = self.next_region_var(infer::AddrOfRegion(expr.span));
3538                 tcx.mk_ref(region, tm)
3539             }
3540           }
3541           hir::ExprPath(ref qpath) => {
3542               let (def, opt_ty, segments) = self.resolve_ty_and_def_ufcs(qpath,
3543                                                                          expr.id, expr.span);
3544               let ty = if def != Def::Err {
3545                   self.instantiate_value_path(segments, opt_ty, def, expr.span, id)
3546               } else {
3547                   self.set_tainted_by_errors();
3548                   tcx.types.err
3549               };
3550
3551               // We always require that the type provided as the value for
3552               // a type parameter outlives the moment of instantiation.
3553               self.opt_node_ty_substs(expr.id, |item_substs| {
3554                   self.add_wf_bounds(&item_substs.substs, expr);
3555               });
3556
3557               ty
3558           }
3559           hir::ExprInlineAsm(_, ref outputs, ref inputs) => {
3560               for output in outputs {
3561                   self.check_expr(output);
3562               }
3563               for input in inputs {
3564                   self.check_expr(input);
3565               }
3566               tcx.mk_nil()
3567           }
3568           hir::ExprBreak(destination, ref expr_opt) => {
3569               if let Some(target_id) = destination.target_id.opt_id() {
3570                   let (e_ty, e_diverges, cause);
3571                   if let Some(ref e) = *expr_opt {
3572                       // If this is a break with a value, we need to type-check
3573                       // the expression. Get an expected type from the loop context.
3574                       let opt_coerce_to = {
3575                           let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
3576                           enclosing_breakables.find_breakable(target_id)
3577                                               .coerce
3578                                               .as_ref()
3579                                               .map(|coerce| coerce.expected_ty())
3580                       };
3581
3582                       // If the loop context is not a `loop { }`, then break with
3583                       // a value is illegal, and `opt_coerce_to` will be `None`.
3584                       // Just set expectation to error in that case.
3585                       let coerce_to = opt_coerce_to.unwrap_or(tcx.types.err);
3586
3587                       // Recurse without `enclosing_breakables` borrowed.
3588                       e_ty = self.check_expr_with_hint(e, coerce_to);
3589                       e_diverges = self.diverges.get();
3590                       cause = self.misc(e.span);
3591                   } else {
3592                       // Otherwise, this is a break *without* a value. That's
3593                       // always legal, and is equivalent to `break ()`.
3594                       e_ty = tcx.mk_nil();
3595                       e_diverges = Diverges::Maybe;
3596                       cause = self.misc(expr.span);
3597                   }
3598
3599                   // Now that we have type-checked `expr_opt`, borrow
3600                   // the `enclosing_loops` field and let's coerce the
3601                   // type of `expr_opt` into what is expected.
3602                   let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
3603                   let ctxt = enclosing_breakables.find_breakable(target_id);
3604                   if let Some(ref mut coerce) = ctxt.coerce {
3605                       if let Some(ref e) = *expr_opt {
3606                           coerce.coerce(self, &cause, e, e_ty, e_diverges);
3607                       } else {
3608                           assert!(e_ty.is_nil());
3609                           coerce.coerce_forced_unit(self, &cause, &mut |_| (), true);
3610                       }
3611                   } else {
3612                       // If `ctxt.coerce` is `None`, we can just ignore
3613                       // the type of the expresison.  This is because
3614                       // either this was a break *without* a value, in
3615                       // which case it is always a legal type (`()`), or
3616                       // else an error would have been flagged by the
3617                       // `loops` pass for using break with an expression
3618                       // where you are not supposed to.
3619                       assert!(expr_opt.is_none() || self.tcx.sess.err_count() > 0);
3620                   }
3621
3622                   ctxt.may_break = true;
3623               } else {
3624                   // Otherwise, we failed to find the enclosing loop;
3625                   // this can only happen if the `break` was not
3626                   // inside a loop at all, which is caught by the
3627                   // loop-checking pass.
3628                   assert!(self.tcx.sess.err_count() > 0);
3629               }
3630
3631               // the type of a `break` is always `!`, since it diverges
3632               tcx.types.never
3633           }
3634           hir::ExprAgain(_) => { tcx.types.never }
3635           hir::ExprRet(ref expr_opt) => {
3636             if self.ret_coercion.is_none() {
3637                 struct_span_err!(self.tcx.sess, expr.span, E0572,
3638                                  "return statement outside of function body").emit();
3639             } else if let Some(ref e) = *expr_opt {
3640                 self.check_return_expr(e);
3641             } else {
3642                 let mut coercion = self.ret_coercion.as_ref().unwrap().borrow_mut();
3643                 let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
3644                 coercion.coerce_forced_unit(self, &cause, &mut |_| (), true);
3645             }
3646             tcx.types.never
3647           }
3648           hir::ExprAssign(ref lhs, ref rhs) => {
3649             let lhs_ty = self.check_expr_with_lvalue_pref(&lhs, PreferMutLvalue);
3650
3651             let tcx = self.tcx;
3652             if !tcx.expr_is_lval(&lhs) {
3653                 struct_span_err!(
3654                     tcx.sess, expr.span, E0070,
3655                     "invalid left-hand side expression")
3656                 .span_label(
3657                     expr.span,
3658                     "left-hand of expression not valid")
3659                 .emit();
3660             }
3661
3662             let rhs_ty = self.check_expr_coercable_to_type(&rhs, lhs_ty);
3663
3664             self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized);
3665
3666             if lhs_ty.references_error() || rhs_ty.references_error() {
3667                 tcx.types.err
3668             } else {
3669                 tcx.mk_nil()
3670             }
3671           }
3672           hir::ExprIf(ref cond, ref then_expr, ref opt_else_expr) => {
3673               self.check_then_else(&cond, then_expr, opt_else_expr.as_ref().map(|e| &**e),
3674                                    expr.span, expected)
3675           }
3676           hir::ExprWhile(ref cond, ref body, _) => {
3677               let ctxt = BreakableCtxt {
3678                   // cannot use break with a value from a while loop
3679                   coerce: None,
3680                   may_break: true,
3681               };
3682
3683               self.with_breakable_ctxt(expr.id, ctxt, || {
3684                   self.check_expr_has_type(&cond, tcx.types.bool);
3685                   let cond_diverging = self.diverges.get();
3686                   self.check_block_no_value(&body);
3687
3688                   // We may never reach the body so it diverging means nothing.
3689                   self.diverges.set(cond_diverging);
3690               });
3691
3692               self.tcx.mk_nil()
3693           }
3694           hir::ExprLoop(ref body, _, source) => {
3695               let coerce = match source {
3696                   // you can only use break with a value from a normal `loop { }`
3697                   hir::LoopSource::Loop => {
3698                       let coerce_to = expected.coercion_target_type(self, body.span);
3699                       Some(CoerceMany::new(coerce_to))
3700                   }
3701
3702                   hir::LoopSource::WhileLet |
3703                   hir::LoopSource::ForLoop => {
3704                       None
3705                   }
3706               };
3707
3708               let ctxt = BreakableCtxt {
3709                   coerce: coerce,
3710                   may_break: false, // will get updated if/when we find a `break`
3711               };
3712
3713               let (ctxt, ()) = self.with_breakable_ctxt(expr.id, ctxt, || {
3714                   self.check_block_no_value(&body);
3715               });
3716
3717               if ctxt.may_break {
3718                   // No way to know whether it's diverging because
3719                   // of a `break` or an outer `break` or `return.
3720                   self.diverges.set(Diverges::Maybe);
3721               }
3722
3723               // If we permit break with a value, then result type is
3724               // the LUB of the breaks (possibly ! if none); else, it
3725               // is nil. This makes sense because infinite loops
3726               // (which would have type !) are only possible iff we
3727               // permit break with a value [1].
3728               assert!(ctxt.coerce.is_some() || ctxt.may_break); // [1]
3729               ctxt.coerce.map(|c| c.complete(self)).unwrap_or(self.tcx.mk_nil())
3730           }
3731           hir::ExprMatch(ref discrim, ref arms, match_src) => {
3732             self.check_match(expr, &discrim, arms, expected, match_src)
3733           }
3734           hir::ExprClosure(capture, ref decl, body_id, _) => {
3735               self.check_expr_closure(expr, capture, &decl, body_id, expected)
3736           }
3737           hir::ExprBlock(ref body) => {
3738             self.check_block_with_expected(&body, expected)
3739           }
3740           hir::ExprCall(ref callee, ref args) => {
3741               self.check_call(expr, &callee, args, expected)
3742           }
3743           hir::ExprMethodCall(name, ref tps, ref args) => {
3744               self.check_method_call(expr, name, args, &tps[..], expected, lvalue_pref)
3745           }
3746           hir::ExprCast(ref e, ref t) => {
3747             // Find the type of `e`. Supply hints based on the type we are casting to,
3748             // if appropriate.
3749             let t_cast = self.to_ty(t);
3750             let t_cast = self.resolve_type_vars_if_possible(&t_cast);
3751             let t_expr = self.check_expr_with_expectation(e, ExpectCastableToType(t_cast));
3752             let t_cast = self.resolve_type_vars_if_possible(&t_cast);
3753             let diverges = self.diverges.get();
3754
3755             // Eagerly check for some obvious errors.
3756             if t_expr.references_error() || t_cast.references_error() {
3757                 tcx.types.err
3758             } else {
3759                 // Defer other checks until we're done type checking.
3760                 let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
3761                 match cast::CastCheck::new(self, e, t_expr, diverges, t_cast, t.span, expr.span) {
3762                     Ok(cast_check) => {
3763                         deferred_cast_checks.push(cast_check);
3764                         t_cast
3765                     }
3766                     Err(ErrorReported) => {
3767                         tcx.types.err
3768                     }
3769                 }
3770             }
3771           }
3772           hir::ExprType(ref e, ref t) => {
3773             let typ = self.to_ty(&t);
3774             self.check_expr_eq_type(&e, typ);
3775             typ
3776           }
3777           hir::ExprArray(ref args) => {
3778               let uty = expected.to_option(self).and_then(|uty| {
3779                   match uty.sty {
3780                       ty::TyArray(ty, _) | ty::TySlice(ty) => Some(ty),
3781                       _ => None
3782                   }
3783               });
3784
3785               let element_ty = if !args.is_empty() {
3786                   let coerce_to = uty.unwrap_or_else(
3787                       || self.next_ty_var(TypeVariableOrigin::TypeInference(expr.span)));
3788                   let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
3789                   assert_eq!(self.diverges.get(), Diverges::Maybe);
3790                   for e in args {
3791                       let e_ty = self.check_expr_with_hint(e, coerce_to);
3792                       let cause = self.misc(e.span);
3793                       coerce.coerce(self, &cause, e, e_ty, self.diverges.get());
3794                   }
3795                   coerce.complete(self)
3796               } else {
3797                   self.next_ty_var(TypeVariableOrigin::TypeInference(expr.span))
3798               };
3799               tcx.mk_array(element_ty, args.len())
3800           }
3801           hir::ExprRepeat(ref element, count) => {
3802             let count = eval_length(self.tcx, count, "repeat count")
3803                   .unwrap_or(0);
3804
3805             let uty = match expected {
3806                 ExpectHasType(uty) => {
3807                     match uty.sty {
3808                         ty::TyArray(ty, _) | ty::TySlice(ty) => Some(ty),
3809                         _ => None
3810                     }
3811                 }
3812                 _ => None
3813             };
3814
3815             let (element_ty, t) = match uty {
3816                 Some(uty) => {
3817                     self.check_expr_coercable_to_type(&element, uty);
3818                     (uty, uty)
3819                 }
3820                 None => {
3821                     let t: Ty = self.next_ty_var(TypeVariableOrigin::MiscVariable(element.span));
3822                     let element_ty = self.check_expr_has_type(&element, t);
3823                     (element_ty, t)
3824                 }
3825             };
3826
3827             if count > 1 {
3828                 // For [foo, ..n] where n > 1, `foo` must have
3829                 // Copy type:
3830                 let lang_item = self.tcx.require_lang_item(lang_items::CopyTraitLangItem);
3831                 self.require_type_meets(t, expr.span, traits::RepeatVec, lang_item);
3832             }
3833
3834             if element_ty.references_error() {
3835                 tcx.types.err
3836             } else {
3837                 tcx.mk_array(t, count)
3838             }
3839           }
3840           hir::ExprTup(ref elts) => {
3841             let flds = expected.only_has_type(self).and_then(|ty| {
3842                 match ty.sty {
3843                     ty::TyTuple(ref flds, _) => Some(&flds[..]),
3844                     _ => None
3845                 }
3846             });
3847
3848             let elt_ts_iter = elts.iter().enumerate().map(|(i, e)| {
3849                 let t = match flds {
3850                     Some(ref fs) if i < fs.len() => {
3851                         let ety = fs[i];
3852                         self.check_expr_coercable_to_type(&e, ety);
3853                         ety
3854                     }
3855                     _ => {
3856                         self.check_expr_with_expectation(&e, NoExpectation)
3857                     }
3858                 };
3859                 t
3860             });
3861             let tuple = tcx.mk_tup(elt_ts_iter, false);
3862             if tuple.references_error() {
3863                 tcx.types.err
3864             } else {
3865                 tuple
3866             }
3867           }
3868           hir::ExprStruct(ref qpath, ref fields, ref base_expr) => {
3869             self.check_expr_struct(expr, expected, qpath, fields, base_expr)
3870           }
3871           hir::ExprField(ref base, ref field) => {
3872             self.check_field(expr, lvalue_pref, &base, field)
3873           }
3874           hir::ExprTupField(ref base, idx) => {
3875             self.check_tup_field(expr, lvalue_pref, &base, idx)
3876           }
3877           hir::ExprIndex(ref base, ref idx) => {
3878               let base_t = self.check_expr_with_lvalue_pref(&base, lvalue_pref);
3879               let idx_t = self.check_expr(&idx);
3880
3881               if base_t.references_error() {
3882                   base_t
3883               } else if idx_t.references_error() {
3884                   idx_t
3885               } else {
3886                   let base_t = self.structurally_resolved_type(expr.span, base_t);
3887                   match self.lookup_indexing(expr, base, base_t, idx_t, lvalue_pref) {
3888                       Some((index_ty, element_ty)) => {
3889                           self.demand_coerce(idx, idx_t, index_ty);
3890                           element_ty
3891                       }
3892                       None => {
3893                           let mut err = self.type_error_struct(
3894                               expr.span,
3895                               |actual| {
3896                                   format!("cannot index a value of type `{}`",
3897                                           actual)
3898                               },
3899                               base_t);
3900                           // Try to give some advice about indexing tuples.
3901                           if let ty::TyTuple(..) = base_t.sty {
3902                               let mut needs_note = true;
3903                               // If the index is an integer, we can show the actual
3904                               // fixed expression:
3905                               if let hir::ExprLit(ref lit) = idx.node {
3906                                   if let ast::LitKind::Int(i,
3907                                             ast::LitIntType::Unsuffixed) = lit.node {
3908                                       let snip = tcx.sess.codemap().span_to_snippet(base.span);
3909                                       if let Ok(snip) = snip {
3910                                           err.span_suggestion(expr.span,
3911                                                               "to access tuple elements, use",
3912                                                               format!("{}.{}", snip, i));
3913                                           needs_note = false;
3914                                       }
3915                                   }
3916                               }
3917                               if needs_note {
3918                                   err.help("to access tuple elements, use tuple indexing \
3919                                             syntax (e.g. `tuple.0`)");
3920                               }
3921                           }
3922                           err.emit();
3923                           self.tcx.types.err
3924                       }
3925                   }
3926               }
3927            }
3928         }
3929     }
3930
3931     // Finish resolving a path in a struct expression or pattern `S::A { .. }` if necessary.
3932     // The newly resolved definition is written into `type_relative_path_defs`.
3933     fn finish_resolving_struct_path(&self,
3934                                     qpath: &hir::QPath,
3935                                     path_span: Span,
3936                                     node_id: ast::NodeId)
3937                                     -> (Def, Ty<'tcx>)
3938     {
3939         match *qpath {
3940             hir::QPath::Resolved(ref maybe_qself, ref path) => {
3941                 let opt_self_ty = maybe_qself.as_ref().map(|qself| self.to_ty(qself));
3942                 let ty = AstConv::def_to_ty(self, opt_self_ty, path, true);
3943                 (path.def, ty)
3944             }
3945             hir::QPath::TypeRelative(ref qself, ref segment) => {
3946                 let ty = self.to_ty(qself);
3947
3948                 let def = if let hir::TyPath(hir::QPath::Resolved(_, ref path)) = qself.node {
3949                     path.def
3950                 } else {
3951                     Def::Err
3952                 };
3953                 let (ty, def) = AstConv::associated_path_def_to_ty(self, node_id, path_span,
3954                                                                    ty, def, segment);
3955
3956                 // Write back the new resolution.
3957                 self.tables.borrow_mut().type_relative_path_defs.insert(node_id, def);
3958
3959                 (def, ty)
3960             }
3961         }
3962     }
3963
3964     // Resolve associated value path into a base type and associated constant or method definition.
3965     // The newly resolved definition is written into `type_relative_path_defs`.
3966     pub fn resolve_ty_and_def_ufcs<'b>(&self,
3967                                        qpath: &'b hir::QPath,
3968                                        node_id: ast::NodeId,
3969                                        span: Span)
3970                                        -> (Def, Option<Ty<'tcx>>, &'b [hir::PathSegment])
3971     {
3972         let (ty, item_segment) = match *qpath {
3973             hir::QPath::Resolved(ref opt_qself, ref path) => {
3974                 return (path.def,
3975                         opt_qself.as_ref().map(|qself| self.to_ty(qself)),
3976                         &path.segments[..]);
3977             }
3978             hir::QPath::TypeRelative(ref qself, ref segment) => {
3979                 (self.to_ty(qself), segment)
3980             }
3981         };
3982         let item_name = item_segment.name;
3983         let def = match self.resolve_ufcs(span, item_name, ty, node_id) {
3984             Ok(def) => def,
3985             Err(error) => {
3986                 let def = match error {
3987                     method::MethodError::PrivateMatch(def) => def,
3988                     _ => Def::Err,
3989                 };
3990                 if item_name != keywords::Invalid.name() {
3991                     self.report_method_error(span, ty, item_name, None, error, None);
3992                 }
3993                 def
3994             }
3995         };
3996
3997         // Write back the new resolution.
3998         self.tables.borrow_mut().type_relative_path_defs.insert(node_id, def);
3999         (def, Some(ty), slice::ref_slice(&**item_segment))
4000     }
4001
4002     pub fn check_decl_initializer(&self,
4003                                   local: &'gcx hir::Local,
4004                                   init: &'gcx hir::Expr) -> Ty<'tcx>
4005     {
4006         let ref_bindings = local.pat.contains_ref_binding();
4007
4008         let local_ty = self.local_ty(init.span, local.id);
4009         if let Some(m) = ref_bindings {
4010             // Somewhat subtle: if we have a `ref` binding in the pattern,
4011             // we want to avoid introducing coercions for the RHS. This is
4012             // both because it helps preserve sanity and, in the case of
4013             // ref mut, for soundness (issue #23116). In particular, in
4014             // the latter case, we need to be clear that the type of the
4015             // referent for the reference that results is *equal to* the
4016             // type of the lvalue it is referencing, and not some
4017             // supertype thereof.
4018             let init_ty = self.check_expr_with_lvalue_pref(init, LvaluePreference::from_mutbl(m));
4019             self.demand_eqtype(init.span, init_ty, local_ty);
4020             init_ty
4021         } else {
4022             self.check_expr_coercable_to_type(init, local_ty)
4023         }
4024     }
4025
4026     pub fn check_decl_local(&self, local: &'gcx hir::Local)  {
4027         let t = self.local_ty(local.span, local.id);
4028         self.write_ty(local.id, t);
4029
4030         if let Some(ref init) = local.init {
4031             let init_ty = self.check_decl_initializer(local, &init);
4032             if init_ty.references_error() {
4033                 self.write_ty(local.id, init_ty);
4034             }
4035         }
4036
4037         self.check_pat(&local.pat, t);
4038         let pat_ty = self.node_ty(local.pat.id);
4039         if pat_ty.references_error() {
4040             self.write_ty(local.id, pat_ty);
4041         }
4042     }
4043
4044     pub fn check_stmt(&self, stmt: &'gcx hir::Stmt) {
4045         // Don't do all the complex logic below for DeclItem.
4046         match stmt.node {
4047             hir::StmtDecl(ref decl, id) => {
4048                 match decl.node {
4049                     hir::DeclLocal(_) => {}
4050                     hir::DeclItem(_) => {
4051                         self.write_nil(id);
4052                         return;
4053                     }
4054                 }
4055             }
4056             hir::StmtExpr(..) | hir::StmtSemi(..) => {}
4057         }
4058
4059         self.warn_if_unreachable(stmt.node.id(), stmt.span, "statement");
4060
4061         // Hide the outer diverging and has_errors flags.
4062         let old_diverges = self.diverges.get();
4063         let old_has_errors = self.has_errors.get();
4064         self.diverges.set(Diverges::Maybe);
4065         self.has_errors.set(false);
4066
4067         let (node_id, _span) = match stmt.node {
4068             hir::StmtDecl(ref decl, id) => {
4069                 let span = match decl.node {
4070                     hir::DeclLocal(ref l) => {
4071                         self.check_decl_local(&l);
4072                         l.span
4073                     }
4074                     hir::DeclItem(_) => {/* ignore for now */
4075                         DUMMY_SP
4076                     }
4077                 };
4078                 (id, span)
4079             }
4080             hir::StmtExpr(ref expr, id) => {
4081                 // Check with expected type of ()
4082                 self.check_expr_has_type(&expr, self.tcx.mk_nil());
4083                 (id, expr.span)
4084             }
4085             hir::StmtSemi(ref expr, id) => {
4086                 self.check_expr(&expr);
4087                 (id, expr.span)
4088             }
4089         };
4090
4091         if self.has_errors.get() {
4092             self.write_error(node_id);
4093         } else {
4094             self.write_nil(node_id);
4095         }
4096
4097         // Combine the diverging and has_error flags.
4098         self.diverges.set(self.diverges.get() | old_diverges);
4099         self.has_errors.set(self.has_errors.get() | old_has_errors);
4100     }
4101
4102     pub fn check_block_no_value(&self, blk: &'gcx hir::Block)  {
4103         let unit = self.tcx.mk_nil();
4104         let ty = self.check_block_with_expected(blk, ExpectHasType(unit));
4105
4106         // if the block produces a `!` value, that can always be
4107         // (effectively) coerced to unit.
4108         if !ty.is_never() {
4109             self.demand_suptype(blk.span, unit, ty);
4110         }
4111     }
4112
4113     fn check_block_with_expected(&self,
4114                                  blk: &'gcx hir::Block,
4115                                  expected: Expectation<'tcx>) -> Ty<'tcx> {
4116         let prev = {
4117             let mut fcx_ps = self.ps.borrow_mut();
4118             let unsafety_state = fcx_ps.recurse(blk);
4119             replace(&mut *fcx_ps, unsafety_state)
4120         };
4121
4122         // In some cases, blocks have just one exit, but other blocks
4123         // can be targeted by multiple breaks. This cannot happen in
4124         // normal Rust syntax today, but it can happen when we desugar
4125         // a `do catch { ... }` expression.
4126         //
4127         // Example 1:
4128         //
4129         //    'a: { if true { break 'a Err(()); } Ok(()) }
4130         //
4131         // Here we would wind up with two coercions, one from
4132         // `Err(())` and the other from the tail expression
4133         // `Ok(())`. If the tail expression is omitted, that's a
4134         // "forced unit" -- unless the block diverges, in which
4135         // case we can ignore the tail expression (e.g., `'a: {
4136         // break 'a 22; }` would not force the type of the block
4137         // to be `()`).
4138         let tail_expr = blk.expr.as_ref();
4139         let coerce_to_ty = expected.coercion_target_type(self, blk.span);
4140         let coerce = if blk.targeted_by_break {
4141             CoerceMany::new(coerce_to_ty)
4142         } else {
4143             let tail_expr: &[P<hir::Expr>] = match tail_expr {
4144                 Some(e) => ref_slice(e),
4145                 None => &[],
4146             };
4147             CoerceMany::with_coercion_sites(coerce_to_ty, tail_expr)
4148         };
4149
4150         let ctxt = BreakableCtxt {
4151             coerce: Some(coerce),
4152             may_break: false,
4153         };
4154
4155         let (ctxt, ()) = self.with_breakable_ctxt(blk.id, ctxt, || {
4156             for s in &blk.stmts {
4157                 self.check_stmt(s);
4158             }
4159
4160             // check the tail expression **without** holding the
4161             // `enclosing_breakables` lock below.
4162             let tail_expr_ty = tail_expr.map(|t| self.check_expr_with_expectation(t, expected));
4163
4164             let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
4165             let mut ctxt = enclosing_breakables.find_breakable(blk.id);
4166             let mut coerce = ctxt.coerce.as_mut().unwrap();
4167             if let Some(tail_expr_ty) = tail_expr_ty {
4168                 let tail_expr = tail_expr.unwrap();
4169                 coerce.coerce(self,
4170                               &self.misc(tail_expr.span),
4171                               tail_expr,
4172                               tail_expr_ty,
4173                               self.diverges.get());
4174             } else {
4175                 // Subtle: if there is no explicit tail expression,
4176                 // that is typically equivalent to a tail expression
4177                 // of `()` -- except if the block diverges. In that
4178                 // case, there is no value supplied from the tail
4179                 // expression (assuming there are no other breaks,
4180                 // this implies that the type of the block will be
4181                 // `!`).
4182                 //
4183                 // #41425 -- label the implicit `()` as being the
4184                 // "found type" here, rather than the "expected type".
4185                 if !self.diverges.get().always() {
4186                     coerce.coerce_forced_unit(self, &self.misc(blk.span), &mut |err| {
4187                         if let Some(expected_ty) = expected.only_has_type(self) {
4188                             self.consider_hint_about_removing_semicolon(blk,
4189                                                                         expected_ty,
4190                                                                         err);
4191                         }
4192                     }, false);
4193                 }
4194             }
4195         });
4196
4197         let mut ty = ctxt.coerce.unwrap().complete(self);
4198
4199         if self.has_errors.get() || ty.references_error() {
4200             ty = self.tcx.types.err
4201         }
4202
4203         self.write_ty(blk.id, ty);
4204
4205         *self.ps.borrow_mut() = prev;
4206         ty
4207     }
4208
4209     /// A common error is to add an extra semicolon:
4210     ///
4211     /// ```
4212     /// fn foo() -> usize {
4213     ///     22;
4214     /// }
4215     /// ```
4216     ///
4217     /// This routine checks if the final statement in a block is an
4218     /// expression with an explicit semicolon whose type is compatible
4219     /// with `expected_ty`. If so, it suggests removing the semicolon.
4220     fn consider_hint_about_removing_semicolon(&self,
4221                                               blk: &'gcx hir::Block,
4222                                               expected_ty: Ty<'tcx>,
4223                                               err: &mut DiagnosticBuilder) {
4224         // Be helpful when the user wrote `{... expr;}` and
4225         // taking the `;` off is enough to fix the error.
4226         let last_stmt = match blk.stmts.last() {
4227             Some(s) => s,
4228             None => return,
4229         };
4230         let last_expr = match last_stmt.node {
4231             hir::StmtSemi(ref e, _) => e,
4232             _ => return,
4233         };
4234         let last_expr_ty = self.expr_ty(last_expr);
4235         if self.can_sub_types(last_expr_ty, expected_ty).is_err() {
4236             return;
4237         }
4238         let original_span = original_sp(last_stmt.span, blk.span);
4239         let span_semi = Span {
4240             lo: original_span.hi - BytePos(1),
4241             hi: original_span.hi,
4242             ctxt: original_span.ctxt,
4243         };
4244         err.span_help(span_semi, "consider removing this semicolon:");
4245     }
4246
4247     // Instantiates the given path, which must refer to an item with the given
4248     // number of type parameters and type.
4249     pub fn instantiate_value_path(&self,
4250                                   segments: &[hir::PathSegment],
4251                                   opt_self_ty: Option<Ty<'tcx>>,
4252                                   def: Def,
4253                                   span: Span,
4254                                   node_id: ast::NodeId)
4255                                   -> Ty<'tcx> {
4256         debug!("instantiate_value_path(path={:?}, def={:?}, node_id={})",
4257                segments,
4258                def,
4259                node_id);
4260
4261         // We need to extract the type parameters supplied by the user in
4262         // the path `path`. Due to the current setup, this is a bit of a
4263         // tricky-process; the problem is that resolve only tells us the
4264         // end-point of the path resolution, and not the intermediate steps.
4265         // Luckily, we can (at least for now) deduce the intermediate steps
4266         // just from the end-point.
4267         //
4268         // There are basically four cases to consider:
4269         //
4270         // 1. Reference to a constructor of enum variant or struct:
4271         //
4272         //        struct Foo<T>(...)
4273         //        enum E<T> { Foo(...) }
4274         //
4275         //    In these cases, the parameters are declared in the type
4276         //    space.
4277         //
4278         // 2. Reference to a fn item or a free constant:
4279         //
4280         //        fn foo<T>() { }
4281         //
4282         //    In this case, the path will again always have the form
4283         //    `a::b::foo::<T>` where only the final segment should have
4284         //    type parameters. However, in this case, those parameters are
4285         //    declared on a value, and hence are in the `FnSpace`.
4286         //
4287         // 3. Reference to a method or an associated constant:
4288         //
4289         //        impl<A> SomeStruct<A> {
4290         //            fn foo<B>(...)
4291         //        }
4292         //
4293         //    Here we can have a path like
4294         //    `a::b::SomeStruct::<A>::foo::<B>`, in which case parameters
4295         //    may appear in two places. The penultimate segment,
4296         //    `SomeStruct::<A>`, contains parameters in TypeSpace, and the
4297         //    final segment, `foo::<B>` contains parameters in fn space.
4298         //
4299         // 4. Reference to a local variable
4300         //
4301         //    Local variables can't have any type parameters.
4302         //
4303         // The first step then is to categorize the segments appropriately.
4304
4305         assert!(!segments.is_empty());
4306
4307         let mut ufcs_associated = None;
4308         let mut type_segment = None;
4309         let mut fn_segment = None;
4310         match def {
4311             // Case 1. Reference to a struct/variant constructor.
4312             Def::StructCtor(def_id, ..) |
4313             Def::VariantCtor(def_id, ..) => {
4314                 // Everything but the final segment should have no
4315                 // parameters at all.
4316                 let mut generics = self.tcx.generics_of(def_id);
4317                 if let Some(def_id) = generics.parent {
4318                     // Variant and struct constructors use the
4319                     // generics of their parent type definition.
4320                     generics = self.tcx.generics_of(def_id);
4321                 }
4322                 type_segment = Some((segments.last().unwrap(), generics));
4323             }
4324
4325             // Case 2. Reference to a top-level value.
4326             Def::Fn(def_id) |
4327             Def::Const(def_id) |
4328             Def::Static(def_id, _) => {
4329                 fn_segment = Some((segments.last().unwrap(),
4330                                    self.tcx.generics_of(def_id)));
4331             }
4332
4333             // Case 3. Reference to a method or associated const.
4334             Def::Method(def_id) |
4335             Def::AssociatedConst(def_id) => {
4336                 let container = self.tcx.associated_item(def_id).container;
4337                 match container {
4338                     ty::TraitContainer(trait_did) => {
4339                         callee::check_legal_trait_for_method_call(self.tcx, span, trait_did)
4340                     }
4341                     ty::ImplContainer(_) => {}
4342                 }
4343
4344                 let generics = self.tcx.generics_of(def_id);
4345                 if segments.len() >= 2 {
4346                     let parent_generics = self.tcx.generics_of(generics.parent.unwrap());
4347                     type_segment = Some((&segments[segments.len() - 2], parent_generics));
4348                 } else {
4349                     // `<T>::assoc` will end up here, and so can `T::assoc`.
4350                     let self_ty = opt_self_ty.expect("UFCS sugared assoc missing Self");
4351                     ufcs_associated = Some((container, self_ty));
4352                 }
4353                 fn_segment = Some((segments.last().unwrap(), generics));
4354             }
4355
4356             // Case 4. Local variable, no generics.
4357             Def::Local(..) | Def::Upvar(..) => {}
4358
4359             _ => bug!("unexpected definition: {:?}", def),
4360         }
4361
4362         debug!("type_segment={:?} fn_segment={:?}", type_segment, fn_segment);
4363
4364         // Now that we have categorized what space the parameters for each
4365         // segment belong to, let's sort out the parameters that the user
4366         // provided (if any) into their appropriate spaces. We'll also report
4367         // errors if type parameters are provided in an inappropriate place.
4368         let poly_segments = type_segment.is_some() as usize +
4369                             fn_segment.is_some() as usize;
4370         AstConv::prohibit_type_params(self, &segments[..segments.len() - poly_segments]);
4371
4372         match def {
4373             Def::Local(def_id) | Def::Upvar(def_id, ..) => {
4374                 let nid = self.tcx.hir.as_local_node_id(def_id).unwrap();
4375                 let ty = self.local_ty(span, nid);
4376                 let ty = self.normalize_associated_types_in(span, &ty);
4377                 self.write_ty(node_id, ty);
4378                 self.write_substs(node_id, ty::ItemSubsts {
4379                     substs: self.tcx.intern_substs(&[])
4380                 });
4381                 return ty;
4382             }
4383             _ => {}
4384         }
4385
4386         // Now we have to compare the types that the user *actually*
4387         // provided against the types that were *expected*. If the user
4388         // did not provide any types, then we want to substitute inference
4389         // variables. If the user provided some types, we may still need
4390         // to add defaults. If the user provided *too many* types, that's
4391         // a problem.
4392         self.check_path_parameter_count(span, &mut type_segment);
4393         self.check_path_parameter_count(span, &mut fn_segment);
4394
4395         let (fn_start, has_self) = match (type_segment, fn_segment) {
4396             (_, Some((_, generics))) => {
4397                 (generics.parent_count(), generics.has_self)
4398             }
4399             (Some((_, generics)), None) => {
4400                 (generics.own_count(), generics.has_self)
4401             }
4402             (None, None) => (0, false)
4403         };
4404         let substs = Substs::for_item(self.tcx, def.def_id(), |def, _| {
4405             let mut i = def.index as usize;
4406
4407             let segment = if i < fn_start {
4408                 i -= has_self as usize;
4409                 type_segment
4410             } else {
4411                 i -= fn_start;
4412                 fn_segment
4413             };
4414             let lifetimes = match segment.map(|(s, _)| &s.parameters) {
4415                 Some(&hir::AngleBracketedParameters(ref data)) => &data.lifetimes[..],
4416                 Some(&hir::ParenthesizedParameters(_)) => bug!(),
4417                 None => &[]
4418             };
4419
4420             if let Some(lifetime) = lifetimes.get(i) {
4421                 AstConv::ast_region_to_region(self, lifetime, Some(def))
4422             } else {
4423                 self.re_infer(span, Some(def)).unwrap()
4424             }
4425         }, |def, substs| {
4426             let mut i = def.index as usize;
4427
4428             let segment = if i < fn_start {
4429                 // Handle Self first, so we can adjust the index to match the AST.
4430                 if has_self && i == 0 {
4431                     return opt_self_ty.unwrap_or_else(|| {
4432                         self.type_var_for_def(span, def, substs)
4433                     });
4434                 }
4435                 i -= has_self as usize;
4436                 type_segment
4437             } else {
4438                 i -= fn_start;
4439                 fn_segment
4440             };
4441             let (types, infer_types) = match segment.map(|(s, _)| &s.parameters) {
4442                 Some(&hir::AngleBracketedParameters(ref data)) => {
4443                     (&data.types[..], data.infer_types)
4444                 }
4445                 Some(&hir::ParenthesizedParameters(_)) => bug!(),
4446                 None => (&[][..], true)
4447             };
4448
4449             // Skip over the lifetimes in the same segment.
4450             if let Some((_, generics)) = segment {
4451                 i -= generics.regions.len();
4452             }
4453
4454             if let Some(ast_ty) = types.get(i) {
4455                 // A provided type parameter.
4456                 self.to_ty(ast_ty)
4457             } else if !infer_types && def.has_default {
4458                 // No type parameter provided, but a default exists.
4459                 let default = self.tcx.type_of(def.def_id);
4460                 self.normalize_ty(
4461                     span,
4462                     default.subst_spanned(self.tcx, substs, Some(span))
4463                 )
4464             } else {
4465                 // No type parameters were provided, we can infer all.
4466                 // This can also be reached in some error cases:
4467                 // We prefer to use inference variables instead of
4468                 // TyError to let type inference recover somewhat.
4469                 self.type_var_for_def(span, def, substs)
4470             }
4471         });
4472
4473         // The things we are substituting into the type should not contain
4474         // escaping late-bound regions, and nor should the base type scheme.
4475         let ty = self.tcx.type_of(def.def_id());
4476         assert!(!substs.has_escaping_regions());
4477         assert!(!ty.has_escaping_regions());
4478
4479         // Add all the obligations that are required, substituting and
4480         // normalized appropriately.
4481         let bounds = self.instantiate_bounds(span, def.def_id(), &substs);
4482         self.add_obligations_for_parameters(
4483             traits::ObligationCause::new(span, self.body_id, traits::ItemObligation(def.def_id())),
4484             &bounds);
4485
4486         // Substitute the values for the type parameters into the type of
4487         // the referenced item.
4488         let ty_substituted = self.instantiate_type_scheme(span, &substs, &ty);
4489
4490         if let Some((ty::ImplContainer(impl_def_id), self_ty)) = ufcs_associated {
4491             // In the case of `Foo<T>::method` and `<Foo<T>>::method`, if `method`
4492             // is inherent, there is no `Self` parameter, instead, the impl needs
4493             // type parameters, which we can infer by unifying the provided `Self`
4494             // with the substituted impl type.
4495             let ty = self.tcx.type_of(impl_def_id);
4496
4497             let impl_ty = self.instantiate_type_scheme(span, &substs, &ty);
4498             match self.sub_types(false, &self.misc(span), self_ty, impl_ty) {
4499                 Ok(ok) => self.register_infer_ok_obligations(ok),
4500                 Err(_) => {
4501                     span_bug!(span,
4502                         "instantiate_value_path: (UFCS) {:?} was a subtype of {:?} but now is not?",
4503                         self_ty,
4504                         impl_ty);
4505                 }
4506             }
4507         }
4508
4509         debug!("instantiate_value_path: type of {:?} is {:?}",
4510                node_id,
4511                ty_substituted);
4512         self.write_substs(node_id, ty::ItemSubsts {
4513             substs: substs
4514         });
4515         ty_substituted
4516     }
4517
4518     /// Report errors if the provided parameters are too few or too many.
4519     fn check_path_parameter_count(&self,
4520                                   span: Span,
4521                                   segment: &mut Option<(&hir::PathSegment, &ty::Generics)>) {
4522         let (lifetimes, types, infer_types, bindings) = {
4523             match segment.map(|(s, _)| &s.parameters) {
4524                 Some(&hir::AngleBracketedParameters(ref data)) => {
4525                     (&data.lifetimes[..], &data.types[..], data.infer_types, &data.bindings[..])
4526                 }
4527                 Some(&hir::ParenthesizedParameters(_)) => {
4528                     AstConv::prohibit_parenthesized_params(self, &segment.as_ref().unwrap().0,
4529                                                            false);
4530                     (&[][..], &[][..], true, &[][..])
4531                 }
4532                 None => (&[][..], &[][..], true, &[][..])
4533             }
4534         };
4535
4536         let count_lifetime_params = |n| {
4537             format!("{} lifetime parameter{}", n, if n == 1 { "" } else { "s" })
4538         };
4539         let count_type_params = |n| {
4540             format!("{} type parameter{}", n, if n == 1 { "" } else { "s" })
4541         };
4542
4543         // Check provided lifetime parameters.
4544         let lifetime_defs = segment.map_or(&[][..], |(_, generics)| &generics.regions);
4545         if lifetimes.len() > lifetime_defs.len() {
4546             let expected_text = count_lifetime_params(lifetime_defs.len());
4547             let actual_text = count_lifetime_params(lifetimes.len());
4548             struct_span_err!(self.tcx.sess, span, E0088,
4549                              "too many lifetime parameters provided: \
4550                               expected at most {}, found {}",
4551                              expected_text, actual_text)
4552                 .span_label(span, format!("expected {}", expected_text))
4553                 .emit();
4554         } else if lifetimes.len() > 0 && lifetimes.len() < lifetime_defs.len() {
4555             let expected_text = count_lifetime_params(lifetime_defs.len());
4556             let actual_text = count_lifetime_params(lifetimes.len());
4557             struct_span_err!(self.tcx.sess, span, E0090,
4558                              "too few lifetime parameters provided: \
4559                               expected {}, found {}",
4560                              expected_text, actual_text)
4561                 .span_label(span, format!("expected {}", expected_text))
4562                 .emit();
4563         }
4564
4565         // The case where there is not enough lifetime parameters is not checked,
4566         // because this is not possible - a function never takes lifetime parameters.
4567         // See discussion for Pull Request 36208.
4568
4569         // Check provided type parameters.
4570         let type_defs = segment.map_or(&[][..], |(_, generics)| {
4571             if generics.parent.is_none() {
4572                 &generics.types[generics.has_self as usize..]
4573             } else {
4574                 &generics.types
4575             }
4576         });
4577         let required_len = type_defs.iter().take_while(|d| !d.has_default).count();
4578         if types.len() > type_defs.len() {
4579             let span = types[type_defs.len()].span;
4580             let expected_text = count_type_params(type_defs.len());
4581             let actual_text = count_type_params(types.len());
4582             struct_span_err!(self.tcx.sess, span, E0087,
4583                              "too many type parameters provided: \
4584                               expected at most {}, found {}",
4585                              expected_text, actual_text)
4586                 .span_label(span, format!("expected {}", expected_text))
4587                 .emit();
4588
4589             // To prevent derived errors to accumulate due to extra
4590             // type parameters, we force instantiate_value_path to
4591             // use inference variables instead of the provided types.
4592             *segment = None;
4593         } else if !infer_types && types.len() < required_len {
4594             let expected_text = count_type_params(required_len);
4595             let actual_text = count_type_params(types.len());
4596             struct_span_err!(self.tcx.sess, span, E0089,
4597                              "too few type parameters provided: \
4598                               expected {}, found {}",
4599                              expected_text, actual_text)
4600                 .span_label(span, format!("expected {}", expected_text))
4601                 .emit();
4602         }
4603
4604         if !bindings.is_empty() {
4605             span_err!(self.tcx.sess, bindings[0].span, E0182,
4606                       "unexpected binding of associated item in expression path \
4607                        (only allowed in type paths)");
4608         }
4609     }
4610
4611     fn structurally_resolve_type_or_else<F>(&self, sp: Span, ty: Ty<'tcx>, f: F)
4612                                             -> Ty<'tcx>
4613         where F: Fn() -> Ty<'tcx>
4614     {
4615         let mut ty = self.resolve_type_vars_with_obligations(ty);
4616
4617         if ty.is_ty_var() {
4618             let alternative = f();
4619
4620             // If not, error.
4621             if alternative.is_ty_var() || alternative.references_error() {
4622                 if !self.is_tainted_by_errors() {
4623                     self.type_error_message(sp, |_actual| {
4624                         "the type of this value must be known in this context".to_string()
4625                     }, ty);
4626                 }
4627                 self.demand_suptype(sp, self.tcx.types.err, ty);
4628                 ty = self.tcx.types.err;
4629             } else {
4630                 self.demand_suptype(sp, alternative, ty);
4631                 ty = alternative;
4632             }
4633         }
4634
4635         ty
4636     }
4637
4638     // Resolves `typ` by a single level if `typ` is a type variable.  If no
4639     // resolution is possible, then an error is reported.
4640     pub fn structurally_resolved_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
4641         self.structurally_resolve_type_or_else(sp, ty, || {
4642             self.tcx.types.err
4643         })
4644     }
4645
4646     fn with_breakable_ctxt<F: FnOnce() -> R, R>(&self, id: ast::NodeId,
4647                                         ctxt: BreakableCtxt<'gcx, 'tcx>, f: F)
4648                                    -> (BreakableCtxt<'gcx, 'tcx>, R) {
4649         let index;
4650         {
4651             let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
4652             index = enclosing_breakables.stack.len();
4653             enclosing_breakables.by_id.insert(id, index);
4654             enclosing_breakables.stack.push(ctxt);
4655         }
4656         let result = f();
4657         let ctxt = {
4658             let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
4659             debug_assert!(enclosing_breakables.stack.len() == index + 1);
4660             enclosing_breakables.by_id.remove(&id).expect("missing breakable context");
4661             enclosing_breakables.stack.pop().expect("missing breakable context")
4662         };
4663         (ctxt, result)
4664     }
4665 }
4666
4667 pub fn check_bounds_are_used<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
4668                                        generics: &hir::Generics,
4669                                        ty: Ty<'tcx>) {
4670     debug!("check_bounds_are_used(n_tps={}, ty={:?})",
4671            generics.ty_params.len(),  ty);
4672
4673     // make a vector of booleans initially false, set to true when used
4674     if generics.ty_params.is_empty() { return; }
4675     let mut tps_used = vec![false; generics.ty_params.len()];
4676
4677     for leaf_ty in ty.walk() {
4678         if let ty::TyParam(ParamTy {idx, ..}) = leaf_ty.sty {
4679             debug!("Found use of ty param num {}", idx);
4680             tps_used[idx as usize - generics.lifetimes.len()] = true;
4681         }
4682     }
4683
4684     for (&used, param) in tps_used.iter().zip(&generics.ty_params) {
4685         if !used {
4686             struct_span_err!(tcx.sess, param.span, E0091,
4687                 "type parameter `{}` is unused",
4688                 param.name)
4689                 .span_label(param.span, "unused type parameter")
4690                 .emit();
4691         }
4692     }
4693 }