1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 Within the check phase of type check, we check each item one at a time
16 (bodies of function expressions are checked as part of the containing
17 function). Inference is used to supply types wherever they are
20 By far the most complex case is checking the body of a function. This
21 can be broken down into several distinct phases:
23 - gather: creates type variables to represent the type of each local
24 variable and pattern binding.
26 - main: the main pass does the lion's share of the work: it
27 determines the types of all expressions, resolves
28 methods, checks for most invalid conditions, and so forth. In
29 some cases, where a type is unknown, it may create a type or region
30 variable and use that as the type of an expression.
32 In the process of checking, various constraints will be placed on
33 these type variables through the subtyping relationships requested
34 through the `demand` module. The `infer` module is in charge
35 of resolving those constraints.
37 - regionck: after main is complete, the regionck pass goes over all
38 types looking for regions and making sure that they did not escape
39 into places they are not in scope. This may also influence the
40 final assignments of the various region variables if there is some
43 - vtable: find and records the impls to use for each trait bound that
44 appears on a type parameter.
46 - writeback: writes the final types within a function body, replacing
47 type variables with their final inferred types. These final types
48 are written into the `tcx.node_types` table, which should *never* contain
49 any reference to a type variable.
53 While type checking a function, the intermediate types for the
54 expressions, blocks, and so forth contained within the function are
55 stored in `fcx.node_types` and `fcx.item_substs`. These types
56 may contain unresolved type variables. After type checking is
57 complete, the functions in the writeback module are used to take the
58 types from this table, resolve them, and then write them into their
59 permanent home in the type context `tcx`.
61 This means that during inferencing you should use `fcx.write_ty()`
62 and `fcx.expr_ty()` / `fcx.node_ty()` to write/obtain the types of
63 nodes within the function.
65 The types of top-level items, which never contain unbound type
66 variables, are stored directly into the `tcx` tables.
68 n.b.: A type variable is not the same thing as a type parameter. A
69 type variable is rather an "instance" of a type parameter: that is,
70 given a generic function `fn foo<T>(t: T)`: while checking the
71 function `foo`, the type `ty_param(0)` refers to the type `T`, which
72 is treated in abstract. When `foo()` is called, however, `T` will be
73 substituted for a fresh type variable `N`. This variable will
74 eventually be resolved to some concrete type (which might itself be
79 pub use self::Expectation::*;
80 use self::coercion::{CoerceMany, DynamicCoerceMany};
81 pub use self::compare_method::{compare_impl_method, compare_const_impl};
82 use self::TupleArgumentsFlag::*;
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};
106 use util::common::{ErrorReported, indenter};
107 use util::nodemap::{DefIdMap, FxHashMap, NodeMap};
109 use std::cell::{Cell, RefCell};
110 use std::collections::hash_map::Entry;
112 use std::mem::replace;
113 use std::ops::{self, Deref};
114 use syntax::abi::Abi;
116 use syntax::codemap::{self, original_sp, Spanned};
117 use syntax::feature_gate::{GateIssue, emit_feature_err};
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};
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;
148 /// closures defined within the function. For example:
151 /// bar(move|| { ... })
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>,
160 locals: RefCell<NodeMap<Ty<'tcx>>>,
162 fulfillment_cx: RefCell<traits::FulfillmentContext<'tcx>>,
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>>>>,
173 deferred_cast_checks: RefCell<Vec<cast::CastCheck<'tcx>>>,
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>>>,
182 impl<'a, 'gcx, 'tcx> Deref for Inherited<'a, 'gcx, 'tcx> {
183 type Target = InferCtxt<'a, 'gcx, 'tcx>;
184 fn deref(&self) -> &Self::Target {
189 trait DeferredCallResolution<'gcx, 'tcx> {
190 fn resolve<'a>(&mut self, fcx: &FnCtxt<'a, 'gcx, 'tcx>);
193 type DeferredCallResolutionHandler<'gcx, 'tcx> = Box<DeferredCallResolution<'gcx, 'tcx>+'tcx>;
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.
202 /// This expression should have the type given (or some subtype)
203 ExpectHasType(Ty<'tcx>),
205 /// This expression will be cast to the `Ty`
206 ExpectCastableToType(Ty<'tcx>),
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>),
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.
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
230 fn adjust_for_branches(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Expectation<'tcx> {
232 ExpectHasType(ety) => {
233 let ety = fcx.shallow_resolve(ety);
234 if !ety.is_ty_var() {
240 ExpectRvalueLikeUnsized(ety) => {
241 ExpectRvalueLikeUnsized(ety)
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.
252 /// The primary use case is where the expected type is a fat pointer,
253 /// like `&[isize]`. For example, consider the following statement:
255 /// let x: &[isize] = &[1, 2, 3];
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)
271 _ => ExpectHasType(ty)
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> {
283 ExpectCastableToType(t) => {
284 ExpectCastableToType(fcx.resolve_type_vars_if_possible(&t))
286 ExpectHasType(t) => {
287 ExpectHasType(fcx.resolve_type_vars_if_possible(&t))
289 ExpectRvalueLikeUnsized(t) => {
290 ExpectRvalueLikeUnsized(fcx.resolve_type_vars_if_possible(&t))
295 fn to_option(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
296 match self.resolve(fcx) {
297 NoExpectation => None,
298 ExpectCastableToType(ty) |
300 ExpectRvalueLikeUnsized(ty) => Some(ty),
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),
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)))
323 #[derive(Copy, Clone)]
324 pub struct UnsafetyState {
325 pub def: ast::NodeId,
326 pub unsafety: hir::Unsafety,
327 pub unsafe_push_count: u32,
332 pub fn function(unsafety: hir::Unsafety, def: ast::NodeId) -> UnsafetyState {
333 UnsafetyState { def: def, unsafety: unsafety, unsafe_push_count: 0, from_fn: true }
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,
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),
353 (unsafety, self.def, self.unsafe_push_count),
355 UnsafetyState{ def: def,
357 unsafe_push_count: count,
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)]
371 /// Potentially unknown, some cases converge,
372 /// others require a CFG to determine them.
375 /// Definitely known to diverge and therefore
376 /// not reach the next sibling or its parent.
379 /// Same as `Always` but with a reachability
380 /// warning already emitted
384 // Convenience impls for combinig `Diverges`.
386 impl ops::BitAnd for Diverges {
388 fn bitand(self, other: Self) -> Self {
389 cmp::min(self, other)
393 impl ops::BitOr for Diverges {
395 fn bitor(self, other: Self) -> Self {
396 cmp::max(self, other)
400 impl ops::BitAndAssign for Diverges {
401 fn bitand_assign(&mut self, other: Self) {
402 *self = *self & other;
406 impl ops::BitOrAssign for Diverges {
407 fn bitor_assign(&mut self, other: Self) {
408 *self = *self | other;
413 fn always(self) -> bool {
414 self >= Diverges::Always
418 pub struct BreakableCtxt<'gcx: 'tcx, 'tcx> {
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>>,
426 pub struct EnclosingBreakables<'gcx: 'tcx, 'tcx> {
427 stack: Vec<BreakableCtxt<'gcx, 'tcx>>,
428 by_id: NodeMap<usize>,
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);
440 pub struct FnCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
441 ast_ty_to_ty_cache: RefCell<NodeMap<Ty<'tcx>>>,
443 body_id: ast::NodeId,
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,
451 ret_coercion: Option<RefCell<DynamicCoerceMany<'gcx, 'tcx>>>,
453 ps: RefCell<UnsafetyState>,
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.
470 /// We use this flag for two purposes:
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
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)`).
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>,
496 /// Whether any child nodes have any type errors.
497 has_errors: Cell<bool>,
499 enclosing_breakables: RefCell<EnclosingBreakables<'gcx, 'tcx>>,
501 inh: &'a Inherited<'a, 'gcx, 'tcx>,
504 impl<'a, 'gcx, 'tcx> Deref for FnCtxt<'a, 'gcx, 'tcx> {
505 type Target = Inherited<'a, 'gcx, 'tcx>;
506 fn deref(&self) -> &Self::Target {
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>
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);
524 infcx: tcx.infer_ctxt((tables, param_env), Reveal::UserFacing)
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
533 self.infcx.enter(|infcx| f(Inherited::new(infcx)))
537 impl<'a, 'gcx, 'tcx> Inherited<'a, 'gcx, 'tcx> {
538 fn new(infcx: InferCtxt<'a, 'gcx, 'tcx>) -> Self {
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()),
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 {:?}",
557 .register_predicate_obligation(self, obligation);
560 fn register_predicates(&self, obligations: Vec<traits::PredicateObligation<'tcx>>) {
561 for obligation in obligations {
562 self.register_predicate(obligation);
566 fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
567 self.register_predicates(infer_ok.obligations);
571 fn normalize_associated_types_in<T>(&self,
573 body_id: ast::NodeId,
575 where T : TypeFoldable<'tcx>
577 let ok = self.normalize_associated_types_in_as_infer_ok(span, body_id, value);
578 self.register_infer_ok_obligations(ok)
581 fn normalize_associated_types_in_as_infer_ok<T>(&self,
583 body_id: ast::NodeId,
586 where T : TypeFoldable<'tcx>
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={:?}",
596 InferOk { value, obligations }
600 struct CheckItemTypesVisitor<'a, 'tcx: 'a> { tcx: TyCtxt<'a, 'tcx, 'tcx> }
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);
606 fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) { }
607 fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) { }
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());
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 });
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)
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);
636 pub fn provide(providers: &mut Providers) {
637 *providers = Providers {
647 fn closure_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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]
654 fn closure_kind<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
657 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
658 tcx.item_tables(def_id).closure_kinds[&node_id]
661 fn adt_destructor<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
663 -> Option<ty::Destructor> {
664 tcx.calculate_dtor(def_id, &mut dropck::check_drop_impl)
667 fn typeck_tables<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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);
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);
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) => {
688 hir::ItemConst(_, body) |
689 hir::ItemStatic(_, _, body) => body,
690 hir::ItemFn(ref decl, .., body) => {
691 fn_decl = Some(decl);
697 hir::map::NodeTraitItem(item) => {
699 hir::TraitItemKind::Const(_, Some(body)) => body,
700 hir::TraitItemKind::Method(ref sig,
701 hir::TraitMethod::Provided(body)) => {
702 fn_decl = Some(&sig.decl);
708 hir::map::NodeImplItem(item) => {
710 hir::ImplItemKind::Const(_, body) => body,
711 hir::ImplItemKind::Method(ref sig, body) => {
712 fn_decl = Some(&sig.decl);
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.
726 hir::ExprClosure(..) => {
727 // We should've bailed out above for closures.
728 span_bug!(expr.span, "unexpected closure")
730 _ => hir::BodyId { node_id: expr.id }
735 let body = tcx.hir.body(body_id);
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();
741 check_abi(tcx, span, fn_sig.abi());
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);
746 fn_sig.subst(inh.tcx, &inh.parameter_environment.free_substs);
748 inh.tcx.liberate_late_bound_regions(fn_scope, &fn_sig);
750 inh.normalize_associated_types_in(body.value.span, body_id.node_id, &fn_sig);
752 check_fn(&inh, fn_sig, decl, id, body)
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);
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
763 GatherLocalsVisitor { fcx: &fcx }.visit_body(body);
765 fcx.check_expr_coercable_to_type(&body.value, expected_type);
770 fcx.select_all_obligations_and_apply_defaults();
771 fcx.closure_analyze(body);
772 fcx.select_obligations_where_possible();
774 fcx.select_all_obligations_or_error();
776 if fn_decl.is_some() {
777 fcx.regionck_fn(id, body);
779 fcx.regionck_expr(body);
782 fcx.resolve_type_vars_in_body(body)
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()
793 struct GatherLocalsVisitor<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
794 fcx: &'a FnCtxt<'a, 'gcx, 'tcx>
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> {
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);
807 // take type that the user specified
808 self.fcx.locals.borrow_mut().insert(nid, typ);
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
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)),
826 self.assign(local.span, local.id, o_ty);
827 debug!("Local variable {:?} is assigned type {}",
829 self.fcx.ty_to_string(
830 self.fcx.locals.borrow().get(&local.id).unwrap().clone()));
831 intravisit::walk_local(self, local);
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);
839 self.fcx.require_type_is_sized(var_ty, p.span,
840 traits::VariableType(p.id));
842 debug!("Pattern binding {} is assigned to {} with type {:?}",
844 self.fcx.ty_to_string(
845 self.fcx.locals.borrow().get(&p.id).unwrap().clone()),
848 intravisit::walk_pat(self, p);
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) { }
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.
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,
866 body: &'gcx hir::Body)
867 -> FnCtxt<'a, 'gcx, 'tcx>
869 let mut fn_sig = fn_sig.clone();
871 debug!("check_fn(sig={:?}, fn_id={})", fn_sig, fn_id);
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);
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(),
890 GatherLocalsVisitor { fcx: &fcx, }.visit_body(body);
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.
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);
902 // Check the pattern.
903 fcx.check_pat_arg(&arg.pat, arg_ty, true);
904 fcx.write_ty(arg.id, arg_ty);
907 inherited.tables.borrow_mut().liberated_fn_sigs.insert(fn_id, fn_sig);
909 fcx.check_return_expr(&body.value);
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.
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 `!`:
923 // let x = move || panic!();
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
930 // let y: Option<u32> = Some(x());
932 // which would then cause this return type to become `u32`, not
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));
940 fcx.demand_suptype(body.value.span, ret_ty, actual_return_ty);
945 fn check_struct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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);
954 check_simd(tcx, span, def_id);
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);
964 fn check_union<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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);
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={})",
976 tcx.item_path_str(tcx.hir.local_def_id(it.id)));
977 let _indenter = indenter();
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));
984 hir::ItemEnum(ref enum_definition, _) => {
987 &enum_definition.variants,
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,
1000 let trait_def_id = impl_trait_ref.def_id;
1001 check_on_unimplemented(tcx, trait_def_id, it);
1004 hir::ItemTrait(..) => {
1005 let def_id = tcx.hir.local_def_id(it.id);
1006 check_on_unimplemented(tcx, def_id, it);
1008 hir::ItemStruct(..) => {
1009 check_struct(tcx, it.id, it.span);
1011 hir::ItemUnion(..) => {
1012 check_union(tcx, it.id, it.span);
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);
1019 hir::ItemForeignMod(ref m) => {
1020 check_abi(tcx, it.span, m.abi);
1022 if m.abi == Abi::RustIntrinsic {
1023 for item in &m.items {
1024 intrinsic::check_intrinsic_type(tcx, item);
1026 } else if m.abi == Abi::PlatformIntrinsic {
1027 for item in &m.items {
1028 intrinsic::check_platform_intrinsic_type(tcx, item);
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 \
1042 if let hir::ForeignItemFn(ref fn_decl, _, _) = item.node {
1043 require_c_abi_if_variadic(tcx, fn_decl, m.abi, item.span);
1048 _ => {/* nothing to do */ }
1052 fn check_on_unimplemented<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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")
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 {
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| {
1075 let name = tcx.item_name(def_id);
1076 span_err!(tcx.sess, attr.span, E0230,
1077 "there is no type parameter \
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");
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\"]`"))
1102 fn report_forbidden_specialization<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1103 impl_item: &hir::ImplItem,
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`",
1111 err.span_label(impl_item.span, &format!("cannot specialize default item `{}`",
1114 match tcx.span_of_impl(parent_impl) {
1116 err.span_label(span, &"parent `impl` is here");
1117 err.note(&format!("to specialize, `{}` in the parent `impl` must be marked `default`",
1121 err.note(&format!("parent implementation is in crate `{}`", cname));
1128 fn check_specialization_validity<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1129 trait_def: &ty::TraitDef,
1131 impl_item: &hir::ImplItem)
1133 let ancestors = trait_def.ancestors(impl_id);
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
1140 let parent = ancestors.defs(tcx, impl_item.name, kind).skip(1).next()
1141 .map(|node_item| node_item.map(|parent| parent.defaultness));
1143 if let Some(parent) = parent {
1144 if parent.item.is_final() {
1145 report_forbidden_specialization(tcx, impl_item, parent.node.def_id());
1151 fn check_impl_items_against_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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; }
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;
1165 let impl_items = || impl_item_refs.iter().map(|iiref| tcx.hir.impl_item(iiref.id));
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);
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,
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 `{}`",
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"));
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,
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,
1221 false); // use the new mode
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 `{}`",
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"));
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);
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 `{}`",
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"));
1257 check_specialization_validity(tcx, trait_def, impl_id, impl_item);
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)
1268 .map(|node_item| !node_item.node.is_from_trait())
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);
1280 let signature = |item: &ty::AssociatedItem| {
1282 ty::AssociatedKind::Method => {
1283 format!("{}", tcx.item_type(item.def_id).fn_sig().0)
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))
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));
1306 err.note(&format!("`{}` from trait: `{}`",
1308 signature(&trait_item)));
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: `{}`",
1320 invalidated_items.iter()
1321 .map(|name| name.to_string())
1322 .collect::<Vec<_>>().join("`, `"))
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>,
1333 let rty = tcx.item_type(item_def_id);
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();
1345 Representability::Representable | Representability::ContainsRecursive => (),
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);
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");
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"))
1367 ty::TyParam(_) => { /* struct<T>(T, T, T, T) is ok */ }
1368 _ if e.is_machine() => { /* struct(u8, u8, u8, u8) is ok */ }
1370 span_err!(tcx.sess, sp, E0077,
1371 "SIMD vector element type should be machine type");
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();
1387 fn check_packed_inner<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
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);
1396 ty::TyAdt(def, substs) if def.is_struct() => {
1397 if tcx.lookup_adt_def(def.did).repr.align > 0 {
1400 // push struct def_id before checking fields
1402 for field in &def.struct_variant().fields {
1403 let f = field.ty(tcx, substs);
1405 ty::TyAdt(def, _) => {
1406 if check_packed_inner(tcx, def.did, stack) {
1413 // only need to pop if not early out
1421 #[allow(trivial_numeric_casts)]
1422 pub fn check_enum<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1424 vs: &'tcx [hir::Variant],
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
1430 if vs.is_empty() && tcx.has_attr(def_id, "repr") {
1432 tcx.sess, sp, E0084,
1433 "unsupported representation for zero-variant enum")
1434 .span_label(sp, &format!("unsupported enum representation"))
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");
1447 if let Some(e) = v.node.disr_expr {
1448 tcx.item_tables(tcx.hir.local_def_id(e.node_id));
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)
1462 let span = match v.node.disr_expr {
1463 Some(expr) => tcx.hir.span(expr.node_id),
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]))
1472 disr_vals.push(discr);
1475 check_representable(tcx, sp, def_id);
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 }
1481 fn ast_ty_to_ty_cache(&self) -> &RefCell<NodeMap<Ty<'tcx>>> {
1482 &self.ast_ty_to_ty_cache
1485 fn get_free_substs(&self) -> Option<&Substs<'tcx>> {
1486 Some(&self.parameter_environment.free_substs)
1489 fn get_type_parameter_bounds(&self, _: Span, def_id: DefId)
1490 -> ty::GenericPredicates<'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 {
1500 predicates: self.parameter_environment.caller_bounds.iter().filter(|predicate| {
1502 ty::Predicate::Trait(ref data) => {
1503 data.0.self_ty().is_param(index)
1507 }).cloned().collect()
1511 fn re_infer(&self, span: Span, def: Option<&ty::RegionParameterDef>)
1512 -> Option<&'tcx ty::Region> {
1514 Some(def) => infer::EarlyBoundRegion(span, def.name, def.issue_32330),
1515 None => infer::MiscVariable(span)
1517 Some(self.next_region_var(v))
1520 fn ty_infer(&self, span: Span) -> Ty<'tcx> {
1521 self.next_ty_var(TypeVariableOrigin::TypeInference(span))
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)
1531 fn projected_ty_from_poly_trait_ref(&self,
1533 poly_trait_ref: ty::PolyTraitRef<'tcx>,
1534 item_name: ast::Name)
1537 let (trait_ref, _) =
1538 self.replace_late_bound_regions_with_fresh_var(
1540 infer::LateBoundRegionConversionTime::AssocTypeProjection(item_name),
1543 self.tcx().mk_projection(trait_ref, item_name)
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
1550 self.normalize_associated_types_in(span, &ty)
1554 fn set_tainted_by_errors(&self) {
1555 self.infcx.set_tainted_by_errors()
1559 /// Controls whether the arguments are tupled. This is used for the call
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
1566 /// fn f(x: (isize, isize))
1568 /// Can be called as:
1575 #[derive(Clone, Eq, PartialEq)]
1576 enum TupleArgumentsFlag {
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> {
1586 ast_ty_to_ty_cache: RefCell::new(NodeMap()),
1588 err_count_on_creation: inh.tcx.sess.err_count(),
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 {
1602 pub fn sess(&self) -> &Session {
1606 pub fn err_count_since_creation(&self) -> usize {
1607 self.tcx.sess.err_count() - self.err_count_on_creation
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);
1616 debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
1618 self.tables.borrow_mut().lints.add_lint(
1619 lint::builtin::UNREACHABLE_CODE,
1621 format!("unreachable {}", kind));
1627 code: ObligationCauseCode<'tcx>)
1628 -> ObligationCause<'tcx> {
1629 ObligationCause::new(span, self.body_id, code)
1632 pub fn misc(&self, span: Span) -> ObligationCause<'tcx> {
1633 self.cause(span, ObligationCauseCode::MiscObligation)
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);
1643 // No TyInfer()? Nothing needs doing.
1644 if !ty.has_infer_types() {
1645 debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
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);
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
1660 self.select_obligations_where_possible();
1661 ty = self.resolve_type_vars_if_possible(&ty);
1663 debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
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);
1674 fn remove_deferred_call_resolutions(&self,
1675 closure_def_id: DefId)
1676 -> Vec<DeferredCallResolutionHandler<'gcx, 'tcx>>
1678 let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
1679 deferred_call_resolutions.remove(&closure_def_id).unwrap_or(Vec::new())
1682 pub fn tag(&self) -> String {
1683 let self_ptr: *const FnCtxt = self;
1684 format!("{:?}", self_ptr)
1687 pub fn local_ty(&self, span: Span, nid: ast::NodeId) -> Ty<'tcx> {
1688 match self.locals.borrow().get(&nid) {
1691 span_bug!(span, "no type for local variable {}",
1692 self.tcx.hir.node_to_string(nid));
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);
1703 if ty.references_error() {
1704 self.has_errors.set(true);
1705 self.set_tainted_by_errors();
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 {}",
1716 self.tables.borrow_mut().item_substs.insert(node_id, substs);
1720 pub fn apply_autoderef_adjustment(&self,
1721 node_id: ast::NodeId,
1723 adjusted_ty: Ty<'tcx>) {
1724 self.apply_adjustment(node_id, Adjustment {
1725 kind: Adjust::DerefRef {
1734 pub fn apply_adjustment(&self, node_id: ast::NodeId, adj: Adjustment<'tcx>) {
1735 debug!("apply_adjustment(node_id={}, adj={:?})", node_id, adj);
1737 if adj.is_identity() {
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
1749 (Adjust::NeverToAny, _) => Adjust::NeverToAny,
1752 autoref: Some(AutoBorrow::Ref(..)),
1754 }, Adjust::DerefRef { autoderefs, .. }) if autoderefs > 0 => {
1755 // A reborrow has no effect before a dereference.
1758 // FIXME: currently we never try to compose autoderefs
1759 // and ReifyFnPointer/UnsafeFnPointer, but we could.
1761 bug!("while adjusting {}, can't compose {:?} and {:?}",
1762 node_id, entry.get(), adj)
1764 *entry.get_mut() = Adjustment {
1765 kind: composed_kind,
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,
1777 substs: &Substs<'tcx>,
1780 where T : TypeFoldable<'tcx>
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={:?}) = {:?}",
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={:?}) = {:?}",
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) {
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);
1820 let item_predicates = self.tcx.item_predicates(def_id);
1821 let bounds = item_predicates.instantiate(self.tcx, substs);
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);
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));
1842 fn normalize_associated_types_in<T>(&self, span: Span, value: &T) -> T
1843 where T : TypeFoldable<'tcx>
1845 let ok = self.normalize_associated_types_in_as_infer_ok(span, value);
1846 self.register_infer_ok_obligations(ok)
1849 fn normalize_associated_types_in_as_infer_ok<T>(&self, span: Span, value: &T)
1851 where T : TypeFoldable<'tcx>
1853 self.inh.normalize_associated_types_in_as_infer_ok(span, self.body_id, value)
1856 pub fn write_nil(&self, node_id: ast::NodeId) {
1857 self.write_ty(node_id, self.tcx.mk_nil());
1860 pub fn write_error(&self, node_id: ast::NodeId) {
1861 self.write_ty(node_id, self.tcx.types.err);
1864 pub fn require_type_meets(&self,
1867 code: traits::ObligationCauseCode<'tcx>,
1870 self.register_bound(
1873 traits::ObligationCause::new(span, self.body_id, code));
1876 pub fn require_type_is_sized(&self,
1879 code: traits::ObligationCauseCode<'tcx>)
1881 let lang_item = self.tcx.require_lang_item(lang_items::SizedTraitLangItem);
1882 self.require_type_meets(ty, span, code, lang_item);
1885 pub fn register_bound(&self,
1888 cause: traits::ObligationCause<'tcx>)
1890 self.fulfillment_cx.borrow_mut()
1891 .register_bound(self, ty, def_id, cause);
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);
1900 pub fn node_ty(&self, id: ast::NodeId) -> Ty<'tcx> {
1901 match self.tables.borrow().node_types.get(&id) {
1903 None if self.err_count_since_creation() != 0 => self.tcx.types.err,
1905 bug!("no type for node {}: {} in fcx {}",
1906 id, self.tcx.hir.node_to_string(id),
1912 pub fn opt_node_ty_substs<F>(&self,
1915 F: FnOnce(&ty::ItemSubsts<'tcx>),
1917 if let Some(s) = self.tables.borrow().item_substs.get(&id) {
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,
1926 region: &'tcx ty::Region,
1927 cause: traits::ObligationCause<'tcx>)
1929 let mut fulfillment_cx = self.fulfillment_cx.borrow_mut();
1930 fulfillment_cx.register_region_obligation(ty, region, cause);
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,
1938 code: traits::ObligationCauseCode<'tcx>)
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)));
1945 pub fn register_old_wf_obligation(&self,
1948 code: traits::ObligationCauseCode<'tcx>)
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.
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);
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)
1963 for ty in substs.types() {
1964 self.register_wf_obligation(ty, expr.span, traits::MiscObligation);
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.
1972 /// For example, if there is a function:
1975 /// fn foo<'a,T:'a>(...)
1978 /// and a reference:
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>)
1990 assert!(!predicates.has_escaping_regions());
1992 debug!("add_obligations_for_parameters(predicates={:?})",
1995 for obligation in traits::predicates_for_generics(cause, predicates) {
1996 self.register_predicate(obligation);
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,
2005 field: &'tcx ty::FieldDef,
2006 substs: &Substs<'tcx>)
2009 self.normalize_associated_types_in(span,
2010 &field.ty(self.tcx, substs))
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(..) {
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};
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);
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",
2047 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty,
2048 self.tcx.mk_diverging_default());
2050 match self.type_is_unconstrained_numeric(resolved) {
2051 UnconstrainedInt => {
2052 debug!("default_type_parameters: defaulting `{:?}` to `i32`",
2054 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.i32)
2056 UnconstrainedFloat => {
2057 debug!("default_type_parameters: defaulting `{:?}` to `f32`",
2059 self.demand_eqtype(syntax_pos::DUMMY_SP, *ty, self.tcx.types.f64)
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();
2074 fn select_all_obligations_or_error(&self) {
2075 debug!("select_all_obligations_or_error");
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());
2081 self.select_all_obligations_and_apply_defaults();
2083 let mut fulfillment_cx = self.fulfillment_cx.borrow_mut();
2085 match fulfillment_cx.select_all_or_error(self) {
2087 Err(errors) => { self.report_fulfillment_errors(&errors); }
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) {
2095 Err(errors) => { self.report_fulfillment_errors(&errors); }
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>
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();
2112 // method returns &T, but the type as visible to user is T, so deref
2113 ret_ty.builtin_deref(true, NoPreference).unwrap()
2116 fn lookup_indexing(&self,
2118 base_expr: &'gcx hir::Expr,
2121 lvalue_pref: LvaluePreference)
2122 -> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)>
2124 // FIXME(#18741) -- this is almost but not quite the same as the
2125 // autoderef that normal method probing does. They could likely be
2128 let mut autoderef = self.autoderef(base_expr.span, base_ty);
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)
2136 autoderef.finalize(lvalue_pref, base_expr);
2137 return Some(final_mt);
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);
2148 autoderef.unambiguous_final_ty();
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,
2160 base_expr: &'gcx hir::Expr,
2161 adjusted_ty: Ty<'tcx>,
2164 lvalue_pref: LvaluePreference,
2166 -> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)>
2169 debug!("try_index_step(expr={:?}, base_expr.id={:?}, adjusted_ty={:?}, \
2170 autoderefs={}, unsize={}, index_ty={:?})",
2178 let input_ty = self.next_ty_var(TypeVariableOrigin::AutoDeref(base_expr.span));
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]`.
2186 self.apply_autoderef_adjustment(base_expr.id, autoderefs, adjusted_ty);
2187 return Some((tcx.types.usize, ty));
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,
2197 Symbol::intern("index_mut"),
2202 Some(vec![input_ty]))
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,
2212 Symbol::intern("index"),
2217 Some(vec![input_ty]))
2219 (method, _) => method,
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
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)
2233 fn check_method_argument_types(&self,
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>)
2241 if method_fn_ty.references_error() {
2242 let err_inputs = self.err_args(args_no_rcvr.len());
2244 let err_inputs = match tuple_arguments {
2245 DontTupleArguments => err_inputs,
2246 TupleArguments => vec![self.tcx.intern_tup(&err_inputs[..], false)],
2249 self.check_argument_types(sp, &err_inputs[..], &[], args_no_rcvr,
2250 false, tuple_arguments, None);
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(
2260 &fty.0.inputs()[1..]
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));
2268 span_bug!(callee_expr.span, "method without bare fn type");
2274 /// Generic function that factors out common logic from function calls,
2275 /// method calls and overloaded operators.
2276 fn check_argument_types(&self,
2278 fn_inputs: &[Ty<'tcx>],
2279 expected_arg_tys: &[Ty<'tcx>],
2280 args: &'gcx [hir::Expr],
2282 tuple_arguments: TupleArgumentsFlag,
2283 def_span: Option<Span>) {
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 {
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);
2300 let mut expected_arg_tys = expected_arg_tys;
2301 let expected_arg_count = fn_inputs.len();
2303 let sp_args = if args.len() > 0 {
2304 let (first, args) = args.split_at(1);
2305 let mut sp_tmp = first[0].span;
2307 let sp_opt = self.sess().codemap().merge_spans(sp_tmp, arg.span);
2308 if ! sp_opt.is_some() {
2311 sp_tmp = sp_opt.unwrap();
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 {""},
2325 if expected_count == 1 {""} else {"s"},
2327 if arg_count == 1 {" was"} else {"s were"}),
2330 err.span_label(sp, &format!("expected {}{} parameter{}",
2331 if variadic {"at least "} else {""},
2333 if expected_count == 1 {""} else {"s"}));
2334 if let Some(def_s) = def_span {
2335 err.span_label(def_s, &format!("defined here"));
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())
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,
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())
2367 } else if expected_arg_count == supplied_arg_count {
2369 } else if variadic {
2370 if supplied_arg_count >= expected_arg_count {
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)
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)
2385 debug!("check_argument_types: formal_tys={:?}",
2386 formal_tys.iter().map(|t| self.ty_to_string(*t)).collect::<Vec<String>>());
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);
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.
2400 self.select_obligations_where_possible();
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 {
2408 } else if tuple_arguments == TupleArguments {
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");
2421 let is_closure = match arg.node {
2422 hir::ExprClosure(..) => true,
2426 if is_closure != check_closures {
2430 debug!("checking the argument");
2431 let formal_ty = formal_tys[i];
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)
2439 let checked_ty = self.check_expr_with_expectation(
2441 expected.unwrap_or(ExpectHasType(formal_ty)));
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));
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));
2455 // We also need to make sure we at least write the ty of the other
2456 // arguments which we skipped above.
2458 for arg in args.iter().skip(expected_arg_count) {
2459 let arg_ty = self.check_expr(&arg);
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,
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)
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`",
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`",
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,
2491 format!("can't pass `{}` to variadic \
2492 function, cast to `{}`", t, ptr_ty)
2501 fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> {
2502 (0..len).map(|_| self.tcx.types.err).collect()
2505 // AST fragment checking
2508 expected: Expectation<'tcx>)
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()))
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| {
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),
2533 opt_ty.unwrap_or_else(
2534 || tcx.mk_int_var(self.next_int_var_id()))
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| {
2540 ty::TyFloat(_) => Some(ty),
2544 opt_ty.unwrap_or_else(
2545 || tcx.mk_float_var(self.next_float_var_id()))
2547 ast::LitKind::Bool(_) => tcx.types.bool
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);
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);
2563 // While we don't allow *arbitrary* coercions here, we *do* allow
2564 // coercions from ! to `expected`.
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,
2577 self.demand_suptype(expr.span, expected, ty);
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);
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))
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)
2600 fn check_expr(&self, expr: &'gcx hir::Expr) -> Ty<'tcx> {
2601 self.check_expr_with_expectation(expr, NoExpectation)
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)
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
2613 pub fn impl_self_ty(&self,
2614 span: Span, // (potential) receiver for this impl
2616 -> TypeAndSubsts<'tcx> {
2617 let ity = self.tcx.item_type(did);
2618 debug!("impl_self_ty: ity={:?}", ity);
2620 let substs = self.fresh_substs_for_item(span, did);
2621 let substd_ty = self.instantiate_type_scheme(span, &substs, &ity);
2623 TypeAndSubsts { substs: substs, ty: substd_ty }
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,
2630 expected_ret: Expectation<'tcx>,
2631 formal_ret: Ty<'tcx>,
2632 formal_args: &[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);
2643 // FIXME(#15760) can't use try! here, FromError doesn't default
2644 // to identity so the resulting type is not constrained.
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);
2657 fulfill.select_where_possible(self)
2662 Err(_) => return Err(()),
2665 Err(_) => return Err(()),
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)
2674 }).unwrap_or(vec![]);
2675 debug!("expected_inputs_for_expected_output(formal={:?} -> {:?}, expected={:?} -> {:?})",
2676 formal_args, formal_ret,
2677 expected_args, expected_ret);
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],
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);
2692 // no need to check for bot/err -- callee does that
2693 let expr_t = self.structurally_resolved_type(expr.span, rcvr_t);
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,
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);
2709 if method_name.node != keywords::Invalid.name() {
2710 self.report_method_error(method_name.span,
2717 self.write_error(expr.id);
2722 // Call the generic checker.
2723 let ret_ty = self.check_method_argument_types(method_name.span, fn_ty,
2731 fn check_return_expr(&self, return_expr: &'gcx hir::Expr) {
2735 .unwrap_or_else(|| span_bug!(return_expr.span,
2736 "check_return_expr called outside fn body"));
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()
2742 &self.misc(return_expr.span),
2745 self.diverges.get());
2749 // A generic function for checking the then and else in an if
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>,
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);
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);
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);
2775 let if_cause = self.cause(sp, ObligationCauseCode::IfExpression);
2776 coerce.coerce(self, &if_cause, then_expr, then_ty, then_diverges);
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();
2782 coerce.coerce(self, &if_cause, else_expr, else_ty, else_diverges);
2784 // We won't diverge unless both branches do (or the condition does).
2785 self.diverges.set(cond_diverges | then_diverges & else_diverges);
2787 let else_cause = self.cause(sp, ObligationCauseCode::IfExpressionWithNoElse);
2788 coerce.coerce_forced_unit(self, &else_cause, &mut |_| ());
2790 // If the condition is false we can't diverge.
2791 self.diverges.set(cond_diverges);
2794 let result_ty = coerce.complete(self);
2795 if cond_ty.references_error() {
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,
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() {
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);
2823 self.tcx.check_stability(field.did, expr.id, expr.span);
2827 private_candidate = Some((base_def.did, field_ty));
2833 autoderef.unambiguous_final_ty();
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",
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)
2853 .help("maybe a `()` to call it is missing? \
2854 If not, try an anonymous function")
2856 self.tcx().types.err
2858 let mut err = self.type_error_struct(field.span, |actual| {
2859 format!("no field `{}` on type `{}`",
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));
2869 err.span_label(field.span,
2870 &format!("unknown field"));
2873 ty::TyRawPtr(..) => {
2874 err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \
2876 self.tcx.hir.node_to_pretty_string(base.id),
2882 self.tcx().types.err
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>)
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) {
2902 // only find fits with at least one matching letter
2903 find_best_match_for_name(names, &name, Some(name.len()))
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,
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 }
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);
2936 ty::TyTuple(ref v, _) => {
2938 v.get(idx.node).cloned()
2943 if let Some(field_ty) = field {
2944 autoderef.finalize(lvalue_pref, base);
2945 self.apply_autoderef_adjustment(base.id, autoderefs, base_t);
2949 autoderef.unambiguous_final_ty();
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);
2958 self.type_error_message(
2962 format!("attempted out-of-bounds tuple index `{}` on \
2967 format!("attempted tuple index `{}` on type `{}`, but the \
2968 type was not a tuple or tuple struct",
2975 self.tcx().types.err
2978 fn report_unknown_field(&self,
2980 variant: &'tcx ty::VariantDef,
2982 skip_fields: &[hir::Field],
2984 let mut err = self.type_error_struct_with_diag(
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)
2993 struct_span_err!(self.tcx.sess, field.name.span, E0560,
2994 "{} `{}` has no field named `{}`",
2995 kind_name, actual, field.name.node)
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,
3003 skip_fields.collect()) {
3004 err.span_label(field.name.span,
3005 &format!("field does not exist - did you mean `{}`?", field_name));
3008 ty::TyAdt(adt, ..) if adt.is_enum() => {
3009 err.span_label(field.name.span, &format!("`{}::{}` does not have this field",
3013 err.span_label(field.name.span, &format!("`{}` does not have this field", ty));
3020 fn check_expr_struct_fields(&self,
3022 expected: Expectation<'tcx>,
3023 expr_id: ast::NodeId,
3025 variant: &'tcx ty::VariantDef,
3026 ast_fields: &'gcx [hir::Field],
3027 check_completeness: bool) {
3031 self.expected_inputs_for_expected_output(span, expected, adt_ty, &[adt_ty])
3032 .get(0).cloned().unwrap_or(adt_ty);
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())
3038 _ => span_bug!(span, "non-ADT passed to check_expr_struct_fields")
3041 let mut remaining_fields = FxHashMap();
3042 for field in &variant.fields {
3043 remaining_fields.insert(field.name, field);
3046 let mut seen_fields = FxHashMap();
3048 let mut error_happened = false;
3050 // Typecheck each field.
3051 for field in ast_fields {
3052 let final_field_type;
3053 let field_type_hint;
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);
3059 seen_fields.insert(field.name.node, field.span);
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);
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,
3075 "field `{}` specified more than once",
3078 err.span_label(field.name.span, &format!("used more than once"));
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));
3086 self.report_unknown_field(adt_ty, variant, field, ast_fields, kind_name);
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);
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");
3101 } else if check_completeness && !error_happened && !remaining_fields.is_empty() {
3102 let len = remaining_fields.len();
3104 let mut displayable_field_names = remaining_fields
3106 .map(|x| x.as_str())
3107 .collect::<Vec<_>>();
3109 displayable_field_names.sort();
3111 let truncated_fields_error = if len <= 3 {
3114 format!(" and {} other field{}", (len - 3), if len - 3 == 1 {""} else {"s"})
3117 let remaining_fields_names = displayable_field_names.iter().take(3)
3118 .map(|n| format!("`{}`", n))
3119 .collect::<Vec<_>>()
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,
3128 .span_label(span, &format!("missing {}{}",
3129 remaining_fields_names,
3130 truncated_fields_error))
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);
3143 self.check_expr(&base);
3149 pub fn check_struct_path(&self,
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
3157 let (def, ty) = self.finish_resolving_struct_path(qpath, path_span, node_id);
3158 let variant = match def {
3160 self.set_tainted_by_errors();
3163 Def::Variant(..) => {
3165 ty::TyAdt(adt, substs) => {
3166 Some((adt.variant_of_def(def), adt.did, substs))
3168 _ => bug!("unexpected type: {:?}", ty.sty)
3171 Def::Struct(..) | Def::Union(..) | Def::TyAlias(..) |
3172 Def::AssociatedTy(..) | Def::SelfTy(..) => {
3174 ty::TyAdt(adt, substs) if !adt.is_enum() => {
3175 Some((adt.struct_variant(), adt.did, substs))
3180 _ => bug!("unexpected definition: {:?}", def)
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);
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"))
3201 fn check_expr_struct(&self,
3203 expected: Expectation<'tcx>,
3205 fields: &'gcx [hir::Field],
3206 base_expr: &'gcx Option<P<hir::Expr>>) -> Ty<'tcx>
3208 // Find the relevant variant
3209 let (variant, struct_ty) =
3210 if let Some(variant_ty) = self.check_struct_path(qpath, expr.id) {
3213 self.check_struct_fields_on_error(fields, base_expr);
3214 return self.tcx.types.err;
3217 let path_span = match *qpath {
3218 hir::QPath::Resolved(_, ref path) => path.span,
3219 hir::QPath::TypeRelative(ref qself, _) => qself.span
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(
3230 adt.struct_variant().fields.iter().map(|f| {
3231 self.normalize_associated_types_in(
3232 expr.span, &f.ty(self.tcx, substs)
3238 span_err!(self.tcx.sess, base_expr.span, E0436,
3239 "functional record update syntax requires a struct");
3243 self.require_type_is_sized(struct_ty, expr.span, traits::StructInitializerSized);
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={:?}",
3265 // Warn for expressions after diverging siblings.
3266 self.warn_if_unreachable(expr.id, expr.span, "expression");
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);
3274 let ty = self.check_expr_kind(expr, expected, lvalue_pref);
3276 // Warn for non-block expressions with diverging children.
3279 hir::ExprLoop(..) | hir::ExprWhile(..) |
3280 hir::ExprIf(..) | hir::ExprMatch(..) => {}
3282 _ => self.warn_if_unreachable(expr.id, expr.span, "expression")
3285 // Any expression that produces a value of type `!` must have diverged
3287 self.diverges.set(self.diverges.get() | Diverges::Always);
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);
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);
3299 debug!("type of {} is...", self.tcx.hir.node_to_string(expr.id));
3300 debug!("... {:?}, expected is {:?}", ty, expected);
3305 fn check_expr_kind(&self,
3306 expr: &'gcx hir::Expr,
3307 expected: Expectation<'tcx>,
3308 lvalue_pref: LvaluePreference) -> Ty<'tcx> {
3312 hir::ExprBox(ref subexpr) => {
3313 let expected_inner = expected.to_option(self).map_or(NoExpectation, |ty| {
3315 ty::TyAdt(def, _) if def.is_box()
3316 => Expectation::rvalue_hint(self, ty.boxed_ty()),
3320 let referent_ty = self.check_expr_with_expectation(subexpr, expected_inner);
3321 tcx.mk_box(referent_ty)
3324 hir::ExprLit(ref lit) => {
3325 self.check_lit(&lit, expected)
3327 hir::ExprBinary(op, ref lhs, ref rhs) => {
3328 self.check_binop(expr, op, lhs, rhs)
3330 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
3331 self.check_binop_assign(expr, op, lhs, rhs)
3333 hir::ExprUnary(unop, ref oprnd) => {
3334 let expected_inner = match unop {
3335 hir::UnNot | hir::UnNeg => {
3342 let lvalue_pref = match unop {
3343 hir::UnDeref => lvalue_pref,
3346 let mut oprnd_t = self.check_expr_with_expectation_and_lvalue_pref(&oprnd,
3350 if !oprnd_t.references_error() {
3353 oprnd_t = self.structurally_resolved_type(expr.span, oprnd_t);
3355 if let Some(mt) = oprnd_t.builtin_deref(true, NoPreference) {
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),
3364 self.type_error_message(expr.span, |actual| {
3365 format!("type `{}` cannot be \
3366 dereferenced", actual)
3368 oprnd_t = tcx.types.err;
3372 oprnd_t = self.structurally_resolved_type(oprnd.span,
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) {
3383 oprnd_t = self.structurally_resolved_type(oprnd.span,
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()) {
3397 hir::ExprAddrOf(mutbl, ref oprnd) => {
3398 let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
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)
3407 Expectation::rvalue_hint(self, mt.ty)
3413 let lvalue_pref = LvaluePreference::from_mutbl(mutbl);
3414 let ty = self.check_expr_with_expectation_and_lvalue_pref(&oprnd, hint, lvalue_pref);
3416 let tm = ty::TypeAndMut { ty: ty, mutbl: mutbl };
3417 if tm.ty.references_error() {
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.
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)
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)
3443 self.set_tainted_by_errors();
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);
3455 hir::ExprInlineAsm(_, ref outputs, ref inputs) => {
3456 for output in outputs {
3457 self.check_expr(output);
3459 for input in inputs {
3460 self.check_expr(input);
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)
3475 .map(|coerce| coerce.expected_ty())
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);
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);
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);
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);
3504 assert!(e_ty.is_nil());
3505 coerce.coerce_forced_unit(self, &cause, &mut |_| ());
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);
3518 ctxt.may_break = true;
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);
3527 // the type of a `break` is always `!`, since it diverges
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);
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 |_| ());
3544 hir::ExprAssign(ref lhs, ref rhs) => {
3545 let lhs_ty = self.check_expr_with_lvalue_pref(&lhs, PreferMutLvalue);
3548 if !tcx.expr_is_lval(&lhs) {
3550 tcx.sess, expr.span, E0070,
3551 "invalid left-hand side expression")
3554 &format!("left-hand of expression not valid"))
3558 let rhs_ty = self.check_expr_coercable_to_type(&rhs, lhs_ty);
3560 self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized);
3562 if lhs_ty.references_error() || rhs_ty.references_error() {
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)
3572 hir::ExprWhile(ref cond, ref body, _) => {
3573 let ctxt = BreakableCtxt {
3574 // cannot use break with a value from a while loop
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);
3584 // We may never reach the body so it diverging means nothing.
3585 self.diverges.set(cond_diverging);
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))
3598 hir::LoopSource::WhileLet |
3599 hir::LoopSource::ForLoop => {
3604 let ctxt = BreakableCtxt {
3606 may_break: false, // will get updated if/when we find a `break`
3609 let (ctxt, ()) = self.with_breakable_ctxt(expr.id, ctxt, || {
3610 self.check_block_no_value(&body);
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);
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())
3627 hir::ExprMatch(ref discrim, ref arms, match_src) => {
3628 self.check_match(expr, &discrim, arms, expected, match_src)
3630 hir::ExprClosure(capture, ref decl, body_id, _) => {
3631 self.check_expr_closure(expr, capture, &decl, body_id, expected)
3633 hir::ExprBlock(ref body) => {
3634 self.check_block_with_expected(&body, expected)
3636 hir::ExprCall(ref callee, ref args) => {
3637 self.check_call(expr, &callee, args, expected)
3639 hir::ExprMethodCall(name, ref tps, ref args) => {
3640 self.check_method_call(expr, name, args, &tps[..], expected, lvalue_pref)
3642 hir::ExprCast(ref e, ref t) => {
3643 // Find the type of `e`. Supply hints based on the type we are casting to,
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();
3651 // Eagerly check for some obvious errors.
3652 if t_expr.references_error() || t_cast.references_error() {
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) {
3659 deferred_cast_checks.push(cast_check);
3662 Err(ErrorReported) => {
3668 hir::ExprType(ref e, ref t) => {
3669 let typ = self.to_ty(&t);
3670 self.check_expr_eq_type(&e, typ);
3673 hir::ExprArray(ref args) => {
3674 let uty = expected.to_option(self).and_then(|uty| {
3676 ty::TyArray(ty, _) | ty::TySlice(ty) => Some(ty),
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);
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());
3691 coerce.complete(self)
3693 self.next_ty_var(TypeVariableOrigin::TypeInference(expr.span))
3695 tcx.mk_array(element_ty, args.len())
3697 hir::ExprRepeat(ref element, count) => {
3698 let count = eval_length(self.tcx, count, "repeat count")
3701 let uty = match expected {
3702 ExpectHasType(uty) => {
3704 ty::TyArray(ty, _) | ty::TySlice(ty) => Some(ty),
3711 let (element_ty, t) = match uty {
3713 self.check_expr_coercable_to_type(&element, uty);
3717 let t: Ty = self.next_ty_var(TypeVariableOrigin::MiscVariable(element.span));
3718 let element_ty = self.check_expr_has_type(&element, t);
3724 // For [foo, ..n] where n > 1, `foo` must have
3726 let lang_item = self.tcx.require_lang_item(lang_items::CopyTraitLangItem);
3727 self.require_type_meets(t, expr.span, traits::RepeatVec, lang_item);
3730 if element_ty.references_error() {
3733 tcx.mk_array(t, count)
3736 hir::ExprTup(ref elts) => {
3737 let flds = expected.only_has_type(self).and_then(|ty| {
3739 ty::TyTuple(ref flds, _) => Some(&flds[..]),
3744 let elt_ts_iter = elts.iter().enumerate().map(|(i, e)| {
3745 let t = match flds {
3746 Some(ref fs) if i < fs.len() => {
3748 self.check_expr_coercable_to_type(&e, ety);
3752 self.check_expr_with_expectation(&e, NoExpectation)
3757 let tuple = tcx.mk_tup(elt_ts_iter, false);
3758 if tuple.references_error() {
3764 hir::ExprStruct(ref qpath, ref fields, ref base_expr) => {
3765 self.check_expr_struct(expr, expected, qpath, fields, base_expr)
3767 hir::ExprField(ref base, ref field) => {
3768 self.check_field(expr, lvalue_pref, &base, field)
3770 hir::ExprTupField(ref base, idx) => {
3771 self.check_tup_field(expr, lvalue_pref, &base, idx)
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);
3777 if base_t.references_error() {
3779 } else if idx_t.references_error() {
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);
3789 let mut err = self.type_error_struct(
3792 format!("cannot index a value of type `{}`",
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 \
3810 format!("{}.{}", snip, i));
3816 err.help("to access tuple elements, use tuple indexing \
3817 syntax (e.g. `tuple.0`)");
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,
3834 node_id: ast::NodeId)
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);
3843 hir::QPath::TypeRelative(ref qself, ref segment) => {
3844 let ty = self.to_ty(qself);
3846 let def = if let hir::TyPath(hir::QPath::Resolved(_, ref path)) = qself.node {
3851 let (ty, def) = AstConv::associated_path_def_to_ty(self, node_id, path_span,
3854 // Write back the new resolution.
3855 self.tables.borrow_mut().type_relative_path_defs.insert(node_id, def);
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,
3868 -> (Def, Option<Ty<'tcx>>, &'b [hir::PathSegment])
3870 let (ty, item_segment) = match *qpath {
3871 hir::QPath::Resolved(ref opt_qself, ref path) => {
3873 opt_qself.as_ref().map(|qself| self.to_ty(qself)),
3874 &path.segments[..]);
3876 hir::QPath::TypeRelative(ref qself, ref segment) => {
3877 (self.to_ty(qself), segment)
3880 let item_name = item_segment.name;
3881 let def = match self.resolve_ufcs(span, item_name, ty, node_id) {
3884 let def = match error {
3885 method::MethodError::PrivateMatch(def) => def,
3888 if item_name != keywords::Invalid.name() {
3889 self.report_method_error(span, ty, item_name, None, error, None);
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))
3900 pub fn check_decl_initializer(&self,
3901 local: &'gcx hir::Local,
3902 init: &'gcx hir::Expr) -> Ty<'tcx>
3904 let ref_bindings = local.pat.contains_ref_binding();
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);
3920 self.check_expr_coercable_to_type(init, local_ty)
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);
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);
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);
3942 pub fn check_stmt(&self, stmt: &'gcx hir::Stmt) {
3943 // Don't do all the complex logic below for DeclItem.
3945 hir::StmtDecl(ref decl, id) => {
3947 hir::DeclLocal(_) => {}
3948 hir::DeclItem(_) => {
3954 hir::StmtExpr(..) | hir::StmtSemi(..) => {}
3957 self.warn_if_unreachable(stmt.node.id(), stmt.span, "statement");
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);
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);
3972 hir::DeclItem(_) => {/* ignore for now */
3978 hir::StmtExpr(ref expr, id) => {
3979 // Check with expected type of ()
3980 self.check_expr_has_type(&expr, self.tcx.mk_nil());
3983 hir::StmtSemi(ref expr, id) => {
3984 self.check_expr(&expr);
3989 if self.has_errors.get() {
3990 self.write_error(node_id);
3992 self.write_nil(node_id);
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);
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));
4004 // if the block produces a `!` value, that can always be
4005 // (effectively) coerced to unit.
4007 self.demand_suptype(blk.span, unit, ty);
4011 fn check_block_with_expected(&self,
4012 blk: &'gcx hir::Block,
4013 expected: Expectation<'tcx>) -> Ty<'tcx> {
4015 let mut fcx_ps = self.ps.borrow_mut();
4016 let unsafety_state = fcx_ps.recurse(blk);
4017 replace(&mut *fcx_ps, unsafety_state)
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.
4027 // 'a: { if true { break 'a Err(()); } Ok(()) }
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
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)
4041 let tail_expr: &[P<hir::Expr>] = match tail_expr {
4042 Some(e) => ref_slice(e),
4045 CoerceMany::with_coercion_sites(coerce_to_ty, tail_expr)
4048 let ctxt = BreakableCtxt {
4049 coerce: Some(coerce),
4053 let (ctxt, ()) = self.with_breakable_ctxt(blk.id, ctxt, || {
4054 for s in &blk.stmts {
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));
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();
4068 &self.misc(tail_expr.span),
4071 self.diverges.get());
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
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,
4092 let mut ty = ctxt.coerce.unwrap().complete(self);
4094 if self.has_errors.get() || ty.references_error() {
4095 ty = self.tcx.types.err
4098 self.write_ty(blk.id, ty);
4100 *self.ps.borrow_mut() = prev;
4104 /// A common error is to add an extra semicolon:
4107 /// fn foo() -> usize {
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() {
4125 let last_expr = match last_stmt.node {
4126 hir::StmtSemi(ref e, _) => e,
4129 let last_expr_ty = self.expr_ty(last_expr);
4130 if self.can_sub_types(last_expr_ty, expected_ty).is_err() {
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,
4139 err.span_help(span_semi, "consider removing this semicolon:");
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>>,
4149 node_id: ast::NodeId)
4151 debug!("instantiate_value_path(path={:?}, def={:?}, node_id={})",
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.
4163 // There are basically four cases to consider:
4165 // 1. Reference to a constructor of enum variant or struct:
4167 // struct Foo<T>(...)
4168 // enum E<T> { Foo(...) }
4170 // In these cases, the parameters are declared in the type
4173 // 2. Reference to a fn item or a free constant:
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`.
4182 // 3. Reference to a method or an associated constant:
4184 // impl<A> SomeStruct<A> {
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.
4194 // 4. Reference to a local variable
4196 // Local variables can't have any type parameters.
4198 // The first step then is to categorize the segments appropriately.
4200 assert!(!segments.is_empty());
4202 let mut ufcs_associated = None;
4203 let mut type_segment = None;
4204 let mut fn_segment = None;
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);
4217 type_segment = Some((segments.last().unwrap(), generics));
4220 // Case 2. Reference to a top-level value.
4222 Def::Const(def_id) |
4223 Def::Static(def_id, _) => {
4224 fn_segment = Some((segments.last().unwrap(),
4225 self.tcx.item_generics(def_id)));
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;
4233 ty::TraitContainer(trait_did) => {
4234 callee::check_legal_trait_for_method_call(self.tcx, span, trait_did)
4236 ty::ImplContainer(_) => {}
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));
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));
4248 fn_segment = Some((segments.last().unwrap(), generics));
4251 // Case 4. Local variable, no generics.
4252 Def::Local(..) | Def::Upvar(..) => {}
4254 _ => bug!("unexpected definition: {:?}", def),
4257 debug!("type_segment={:?} fn_segment={:?}", type_segment, fn_segment);
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]);
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(&[])
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
4287 self.check_path_parameter_count(span, &mut type_segment);
4288 self.check_path_parameter_count(span, &mut fn_segment);
4290 let (fn_start, has_self) = match (type_segment, fn_segment) {
4291 (_, Some((_, generics))) => {
4292 (generics.parent_count(), generics.has_self)
4294 (Some((_, generics)), None) => {
4295 (generics.own_count(), generics.has_self)
4297 (None, None) => (0, false)
4299 let substs = Substs::for_item(self.tcx, def.def_id(), |def, _| {
4300 let mut i = def.index as usize;
4302 let segment = if i < fn_start {
4303 i -= has_self as usize;
4309 let lifetimes = match segment.map(|(s, _)| &s.parameters) {
4310 Some(&hir::AngleBracketedParameters(ref data)) => &data.lifetimes[..],
4311 Some(&hir::ParenthesizedParameters(_)) => bug!(),
4315 if let Some(lifetime) = lifetimes.get(i) {
4316 AstConv::ast_region_to_region(self, lifetime, Some(def))
4318 self.re_infer(span, Some(def)).unwrap()
4321 let mut i = def.index as usize;
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)
4330 i -= has_self as usize;
4336 let (types, infer_types) = match segment.map(|(s, _)| &s.parameters) {
4337 Some(&hir::AngleBracketedParameters(ref data)) => {
4338 (&data.types[..], data.infer_types)
4340 Some(&hir::ParenthesizedParameters(_)) => bug!(),
4341 None => (&[][..], true)
4344 // Skip over the lifetimes in the same segment.
4345 if let Some((_, generics)) = segment {
4346 i -= generics.regions.len();
4349 if let Some(ast_ty) = types.get(i) {
4350 // A provided type parameter.
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);
4357 default.subst_spanned(self.tcx, substs, Some(span))
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)
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());
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())),
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);
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);
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),
4397 "instantiate_value_path: (UFCS) {:?} was a subtype of {:?} but now is not?",
4404 debug!("instantiate_value_path: type of {:?} is {:?}",
4407 self.write_substs(node_id, ty::ItemSubsts {
4413 /// Report errors if the provided parameters are too few or too many.
4414 fn check_path_parameter_count(&self,
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[..])
4422 Some(&hir::ParenthesizedParameters(_)) => {
4423 span_bug!(span, "parenthesized parameters cannot appear in ExprPath");
4425 None => (&[][..], &[][..], true, &[][..])
4429 let count_lifetime_params = |n| {
4430 format!("{} lifetime parameter{}", n, if n == 1 { "" } else { "s" })
4432 let count_type_params = |n| {
4433 format!("{} type parameter{}", n, if n == 1 { "" } else { "s" })
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))
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))
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.
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..]
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))
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.
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))
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)");
4504 fn structurally_resolve_type_or_else<F>(&self, sp: Span, ty: Ty<'tcx>, f: F)
4506 where F: Fn() -> Ty<'tcx>
4508 let mut ty = self.resolve_type_vars_with_obligations(ty);
4511 let alternative = f();
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()
4520 self.demand_suptype(sp, self.tcx.types.err, ty);
4521 ty = self.tcx.types.err;
4523 self.demand_suptype(sp, alternative, ty);
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, || {
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) {
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);
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")
4560 pub fn check_bounds_are_used<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
4561 generics: &hir::Generics,
4563 debug!("check_bounds_are_used(n_tps={}, ty={:?})",
4564 generics.ty_params.len(), ty);
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()];
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;
4577 for (&used, param) in tps_used.iter().zip(&generics.ty_params) {
4579 struct_span_err!(tcx.sess, param.span, E0091,
4580 "type parameter `{}` is unused",
4582 .span_label(param.span, &format!("unused type parameter"))