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