1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /*! See doc.rs for documentation */
13 #![allow(non_camel_case_types)]
15 pub use middle::ty::IntVarValue;
16 pub use self::resolve::resolve_and_force_all_but_regions;
17 pub use self::resolve::{force_all, not_regions};
18 pub use self::resolve::{force_ivar};
19 pub use self::resolve::{force_tvar, force_rvar};
20 pub use self::resolve::{resolve_ivar, resolve_all};
21 pub use self::resolve::{resolve_nested_tvar};
22 pub use self::resolve::{resolve_rvar};
23 pub use self::skolemize::TypeSkolemizer;
26 use middle::subst::Substs;
27 use middle::ty::{TyVid, IntVid, FloatVid, RegionVid};
30 use middle::ty_fold::{TypeFolder, TypeFoldable};
31 use middle::typeck::check::regionmanip::replace_late_bound_regions_in_fn_sig;
32 use std::cell::{RefCell};
33 use std::collections::HashMap;
37 use syntax::codemap::Span;
38 use util::common::indent;
39 use util::ppaux::{bound_region_to_string, ty_to_string, trait_ref_to_string, Repr};
41 use self::coercion::Coerce;
42 use self::combine::{Combine, CombineFields};
43 use self::region_inference::{RegionVarBindings, RegionSnapshot};
44 use self::resolve::{resolver};
45 use self::equate::Equate;
48 use self::unify::{UnificationTable, InferCtxtMethodsForSimplyUnifiableTypes};
49 use self::error_reporting::ErrorReporting;
55 pub mod error_reporting;
59 pub mod region_inference;
64 pub mod type_variable;
67 pub type Bound<T> = Option<T>;
69 pub type cres<T> = Result<T,ty::type_err>; // "combine result"
70 pub type ures = cres<()>; // "unify result"
71 pub type fres<T> = Result<T, fixup_err>; // "fixup result"
72 pub type CoerceResult = cres<Option<ty::AutoAdjustment>>;
74 pub struct InferCtxt<'a, 'tcx: 'a> {
75 pub tcx: &'a ty::ctxt<'tcx>,
77 // We instantiate UnificationTable with bounds<ty::t> because the
78 // types that might instantiate a general type variable have an
79 // order, represented by its upper and lower bounds.
80 type_variables: RefCell<type_variable::TypeVariableTable>,
82 // Map from integral variable to the kind of integer it represents
83 int_unification_table:
84 RefCell<UnificationTable<ty::IntVid, Option<IntVarValue>>>,
86 // Map from floating variable to the kind of float it represents
87 float_unification_table:
88 RefCell<UnificationTable<ty::FloatVid, Option<ast::FloatTy>>>,
90 // For region variables.
92 RegionVarBindings<'a, 'tcx>,
95 /// Why did we require that the two types be related?
97 /// See `error_reporting.rs` for more details
100 // Not yet categorized in a better way
103 // Checking that method of impl is compatible with trait
104 MethodCompatCheck(Span),
106 // Checking that this expression can be assigned where it needs to be
107 // FIXME(eddyb) #11161 is the original Expr required?
108 ExprAssignable(Span),
110 // Relating trait refs when resolving vtables
111 RelateTraitRefs(Span),
113 // Relating self types when resolving vtables
114 RelateSelfType(Span),
116 // Relating trait type parameters to those found in impl etc
117 RelateOutputImplTypes(Span),
119 // Computing common supertype in the arms of a match expression
120 MatchExpressionArm(Span, Span),
122 // Computing common supertype in an if expression
125 // Computing common supertype of an if expression with no else counter-part
126 IfExpressionWithNoElse(Span)
129 /// See `error_reporting.rs` for more details
131 pub enum ValuePairs {
132 Types(ty::expected_found<ty::t>),
133 TraitRefs(ty::expected_found<Rc<ty::TraitRef>>),
136 /// The trace designates the path through inference that we took to
137 /// encounter an error or subtyping constraint.
139 /// See `error_reporting.rs` for more details.
141 pub struct TypeTrace {
146 /// The origin of a `r1 <= r2` constraint.
148 /// See `error_reporting.rs` for more details
150 pub enum SubregionOrigin {
151 // Arose from a subtyping relation
154 // Stack-allocated closures cannot outlive innermost loop
155 // or function so as to ensure we only require finite stack
156 InfStackClosure(Span),
158 // Invocation of closure must be within its lifetime
161 // Dereference of reference must be within its lifetime
164 // Closure bound must not outlive captured free variables
165 FreeVariable(Span, ast::NodeId),
167 // Proc upvars must be 'static
168 ProcCapture(Span, ast::NodeId),
170 // Index into slice must be within its lifetime
173 // When casting `&'a T` to an `&'b Trait` object,
174 // relating `'a` to `'b`
175 RelateObjectBound(Span),
177 // When closing over a variable in a closure/proc, ensure that the
178 // type of the variable outlives the lifetime bound.
179 RelateProcBound(Span, ast::NodeId, ty::t),
181 // The given type parameter was instantiated with the given type,
182 // and that type must outlive some region.
183 RelateParamBound(Span, ty::ParamTy, ty::t),
185 // The given region parameter was instantiated with a region
186 // that must outlive some other region.
187 RelateRegionParamBound(Span),
189 // A bound placed on type parameters that states that must outlive
190 // the moment of their instantiation.
191 RelateDefaultParamBound(Span, ty::t),
193 // Creating a pointer `b` to contents of another reference
196 // Creating a pointer `b` to contents of an upvar
197 ReborrowUpvar(Span, ty::UpvarId),
199 // (&'a &'b T) where a >= b
200 ReferenceOutlivesReferent(ty::t, Span),
202 // The type T of an expression E must outlive the lifetime for E.
203 ExprTypeIsNotInScope(ty::t, Span),
205 // A `ref b` whose region does not enclose the decl site
206 BindingTypeIsNotValidAtDecl(Span),
208 // Regions appearing in a method receiver must outlive method call
211 // Regions appearing in a function argument must outlive func call
214 // Region in return type of invoked fn must enclose call
217 // Region resulting from a `&` expr must enclose the `&` expr
220 // An auto-borrow that does not enclose the expr where it occurs
224 /// Reasons to create a region inference variable
226 /// See `error_reporting.rs` for more details
228 pub enum RegionVariableOrigin {
229 // Region variables created for ill-categorized reasons,
230 // mostly indicates places in need of refactoring
233 // Regions created by a `&P` or `[...]` pattern
236 // Regions created by `&` operator
239 // Regions created by `&[...]` literal
242 // Regions created as part of an autoref of a method receiver
245 // Regions created as part of an automatic coercion
248 // Region variables created as the values for early-bound regions
249 EarlyBoundRegion(Span, ast::Name),
251 // Region variables created for bound regions
252 // in a function or method that is called
253 LateBoundRegion(Span, ty::BoundRegion),
255 // Region variables created for bound regions
256 // when doing subtyping/lub/glb computations
257 BoundRegionInFnType(Span, ty::BoundRegion),
259 UpvarRegion(ty::UpvarId, Span),
261 BoundRegionInCoherence(ast::Name),
266 unresolved_int_ty(IntVid),
267 unresolved_float_ty(FloatVid),
271 pub fn fixup_err_to_string(f: fixup_err) -> String {
273 unresolved_int_ty(_) => {
274 "cannot determine the type of this integer; add a suffix to \
275 specify the type explicitly".to_string()
277 unresolved_float_ty(_) => {
278 "cannot determine the type of this number; add a suffix to specify \
279 the type explicitly".to_string()
281 unresolved_ty(_) => "unconstrained type".to_string(),
285 pub fn new_infer_ctxt<'a, 'tcx>(tcx: &'a ty::ctxt<'tcx>)
286 -> InferCtxt<'a, 'tcx> {
289 type_variables: RefCell::new(type_variable::TypeVariableTable::new()),
290 int_unification_table: RefCell::new(UnificationTable::new()),
291 float_unification_table: RefCell::new(UnificationTable::new()),
292 region_vars: RegionVarBindings::new(tcx),
296 pub fn common_supertype(cx: &InferCtxt,
304 * Computes the least upper-bound of `a` and `b`. If this is
305 * not possible, reports an error and returns ty::err.
308 debug!("common_supertype({}, {})",
309 a.repr(cx.tcx), b.repr(cx.tcx));
311 let trace = TypeTrace {
313 values: Types(expected_found(a_is_expected, a, b))
317 cx.commit_if_ok(|| cx.lub(a_is_expected, trace.clone()).tys(a, b));
321 cx.report_and_explain_type_error(trace, err);
327 pub fn mk_subty(cx: &InferCtxt,
334 debug!("mk_subty({} <: {})", a.repr(cx.tcx), b.repr(cx.tcx));
336 cx.sub_types(a_is_expected, origin, a, b)
340 pub fn can_mk_subty(cx: &InferCtxt, a: ty::t, b: ty::t) -> ures {
341 debug!("can_mk_subty({} <: {})", a.repr(cx.tcx), b.repr(cx.tcx));
343 let trace = TypeTrace {
344 origin: Misc(codemap::DUMMY_SP),
345 values: Types(expected_found(true, a, b))
347 cx.sub(true, trace).tys(a, b).to_ures()
351 pub fn can_mk_eqty(cx: &InferCtxt, a: ty::t, b: ty::t) -> ures {
352 debug!("can_mk_subty({} <: {})", a.repr(cx.tcx), b.repr(cx.tcx));
354 let trace = TypeTrace {
355 origin: Misc(codemap::DUMMY_SP),
356 values: Types(expected_found(true, a, b))
358 cx.equate(true, trace).tys(a, b)
362 pub fn mk_subr(cx: &InferCtxt,
363 origin: SubregionOrigin,
366 debug!("mk_subr({} <: {})", a.repr(cx.tcx), b.repr(cx.tcx));
367 let snapshot = cx.region_vars.start_snapshot();
368 cx.region_vars.make_subregion(origin, a, b);
369 cx.region_vars.commit(snapshot);
372 pub fn verify_param_bound(cx: &InferCtxt,
373 origin: SubregionOrigin,
374 param_ty: ty::ParamTy,
376 bs: Vec<ty::Region>) {
377 debug!("verify_param_bound({}, {} <: {})",
378 param_ty.repr(cx.tcx),
382 cx.region_vars.verify_param_bound(origin, param_ty, a, bs);
385 pub fn mk_eqty(cx: &InferCtxt,
392 debug!("mk_eqty({} <: {})", a.repr(cx.tcx), b.repr(cx.tcx));
394 || cx.eq_types(a_is_expected, origin, a, b))
397 pub fn mk_sub_trait_refs(cx: &InferCtxt,
404 debug!("mk_sub_trait_refs({} <: {})",
405 a.repr(cx.tcx), b.repr(cx.tcx));
407 || cx.sub_trait_refs(a_is_expected, origin, a.clone(), b.clone()))
410 fn expected_found<T>(a_is_expected: bool,
413 -> ty::expected_found<T>
416 ty::expected_found {expected: a, found: b}
418 ty::expected_found {expected: b, found: a}
422 pub fn mk_coercety(cx: &InferCtxt,
428 debug!("mk_coercety({} -> {})", a.repr(cx.tcx), b.repr(cx.tcx));
431 let trace = TypeTrace {
433 values: Types(expected_found(a_is_expected, a, b))
435 Coerce(cx.combine_fields(a_is_expected, trace)).tys(a, b)
440 // See comment on the type `resolve_state` below
441 pub fn resolve_type(cx: &InferCtxt,
446 let mut resolver = resolver(cx, modes, span);
447 cx.commit_unconditionally(|| resolver.resolve_type_chk(a))
450 pub fn resolve_region(cx: &InferCtxt, r: ty::Region, modes: uint)
451 -> fres<ty::Region> {
452 let mut resolver = resolver(cx, modes, None);
453 resolver.resolve_region_chk(r)
457 fn then<T:Clone>(&self, f: || -> Result<T,ty::type_err>)
458 -> Result<T,ty::type_err>;
462 fn then<T:Clone>(&self, f: || -> Result<T,ty::type_err>)
463 -> Result<T,ty::type_err> {
464 self.and_then(|_i| f())
469 fn to_ures(&self) -> ures;
472 impl<T> ToUres for cres<T> {
473 fn to_ures(&self) -> ures {
475 Ok(ref _v) => Ok(()),
476 Err(ref e) => Err((*e))
481 trait CresCompare<T> {
482 fn compare(&self, t: T, f: || -> ty::type_err) -> cres<T>;
485 impl<T:Clone + PartialEq> CresCompare<T> for cres<T> {
486 fn compare(&self, t: T, f: || -> ty::type_err) -> cres<T> {
487 (*self).clone().and_then(|s| {
497 pub fn uok() -> ures {
501 pub struct CombinedSnapshot {
502 type_snapshot: type_variable::Snapshot,
503 int_snapshot: unify::Snapshot<ty::IntVid>,
504 float_snapshot: unify::Snapshot<ty::FloatVid>,
505 region_vars_snapshot: RegionSnapshot,
508 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
509 pub fn skolemize<T:TypeFoldable>(&self, t: T) -> T {
510 t.fold_with(&mut self.skolemizer())
513 pub fn skolemizer<'a>(&'a self) -> TypeSkolemizer<'a, 'tcx> {
514 skolemize::TypeSkolemizer::new(self)
517 pub fn combine_fields<'a>(&'a self, a_is_expected: bool, trace: TypeTrace)
518 -> CombineFields<'a, 'tcx> {
519 CombineFields {infcx: self,
520 a_is_expected: a_is_expected,
524 pub fn equate<'a>(&'a self, a_is_expected: bool, trace: TypeTrace) -> Equate<'a, 'tcx> {
525 Equate(self.combine_fields(a_is_expected, trace))
528 pub fn sub<'a>(&'a self, a_is_expected: bool, trace: TypeTrace) -> Sub<'a, 'tcx> {
529 Sub(self.combine_fields(a_is_expected, trace))
532 pub fn lub<'a>(&'a self, a_is_expected: bool, trace: TypeTrace) -> Lub<'a, 'tcx> {
533 Lub(self.combine_fields(a_is_expected, trace))
536 fn start_snapshot(&self) -> CombinedSnapshot {
538 type_snapshot: self.type_variables.borrow_mut().snapshot(),
539 int_snapshot: self.int_unification_table.borrow_mut().snapshot(),
540 float_snapshot: self.float_unification_table.borrow_mut().snapshot(),
541 region_vars_snapshot: self.region_vars.start_snapshot(),
545 fn rollback_to(&self, snapshot: CombinedSnapshot) {
547 let CombinedSnapshot { type_snapshot,
550 region_vars_snapshot } = snapshot;
554 .rollback_to(type_snapshot);
555 self.int_unification_table
557 .rollback_to(int_snapshot);
558 self.float_unification_table
560 .rollback_to(float_snapshot);
562 .rollback_to(region_vars_snapshot);
565 fn commit_from(&self, snapshot: CombinedSnapshot) {
566 debug!("commit_from!");
567 let CombinedSnapshot { type_snapshot,
570 region_vars_snapshot } = snapshot;
574 .commit(type_snapshot);
575 self.int_unification_table
577 .commit(int_snapshot);
578 self.float_unification_table
580 .commit(float_snapshot);
582 .commit(region_vars_snapshot);
585 /// Execute `f` and commit the bindings
586 pub fn commit_unconditionally<R>(&self, f: || -> R) -> R {
588 let snapshot = self.start_snapshot();
590 self.commit_from(snapshot);
594 /// Execute `f` and commit the bindings if successful
595 pub fn commit_if_ok<T,E>(&self, f: || -> Result<T,E>) -> Result<T,E> {
596 self.commit_unconditionally(|| self.try(|| f()))
599 /// Execute `f`, unroll bindings on failure
600 pub fn try<T,E>(&self, f: || -> Result<T,E>) -> Result<T,E> {
602 let snapshot = self.start_snapshot();
604 debug!("try() -- r.is_ok() = {}", r.is_ok());
607 self.commit_from(snapshot);
610 self.rollback_to(snapshot);
616 /// Execute `f` then unroll any bindings it creates
617 pub fn probe<R>(&self, f: || -> R) -> R {
619 let snapshot = self.start_snapshot();
621 self.rollback_to(snapshot);
625 pub fn add_given(&self,
629 self.region_vars.add_given(sub, sup);
632 pub fn sub_types(&self,
639 debug!("sub_types({} <: {})", a.repr(self.tcx), b.repr(self.tcx));
640 self.commit_if_ok(|| {
641 let trace = TypeTrace {
643 values: Types(expected_found(a_is_expected, a, b))
645 self.sub(a_is_expected, trace).tys(a, b).to_ures()
649 pub fn eq_types(&self,
656 self.commit_if_ok(|| {
657 let trace = TypeTrace {
659 values: Types(expected_found(a_is_expected, a, b))
661 self.equate(a_is_expected, trace).tys(a, b).to_ures()
665 pub fn sub_trait_refs(&self,
672 debug!("sub_trait_refs({} <: {})",
675 self.commit_if_ok(|| {
676 let trace = TypeTrace {
678 values: TraitRefs(expected_found(a_is_expected,
679 a.clone(), b.clone()))
681 self.sub(a_is_expected, trace).trait_refs(&*a, &*b).to_ures()
686 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
687 pub fn next_ty_var_id(&self) -> TyVid {
693 pub fn next_ty_var(&self) -> ty::t {
694 ty::mk_var(self.tcx, self.next_ty_var_id())
697 pub fn next_ty_vars(&self, n: uint) -> Vec<ty::t> {
698 Vec::from_fn(n, |_i| self.next_ty_var())
701 pub fn next_int_var_id(&self) -> IntVid {
702 self.int_unification_table
707 pub fn next_float_var_id(&self) -> FloatVid {
708 self.float_unification_table
713 pub fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region {
714 ty::ReInfer(ty::ReVar(self.region_vars.new_region_var(origin)))
717 pub fn region_vars_for_defs(&self,
719 defs: &[ty::RegionParameterDef])
722 .map(|d| self.next_region_var(EarlyBoundRegion(span, d.name)))
726 pub fn fresh_substs_for_generics(&self,
728 generics: &ty::Generics)
732 * Given a set of generics defined on a type or impl, returns
733 * a substitution mapping each type/region parameter to a
734 * fresh inference variable.
739 |_| self.next_ty_var());
741 generics.regions.map(
742 |d| self.next_region_var(EarlyBoundRegion(span, d.name)));
743 subst::Substs::new(type_params, region_params)
746 pub fn fresh_substs_for_trait(&self,
748 generics: &ty::Generics,
753 * Given a set of generics defined on a trait, returns a
754 * substitution mapping each output type/region parameter to a
755 * fresh inference variable, and mapping the self type to
759 assert!(generics.types.len(subst::SelfSpace) == 1);
760 assert!(generics.types.len(subst::FnSpace) == 0);
761 assert!(generics.regions.len(subst::SelfSpace) == 0);
762 assert!(generics.regions.len(subst::FnSpace) == 0);
764 let type_parameter_count = generics.types.len(subst::TypeSpace);
765 let region_param_defs = generics.regions.get_slice(subst::TypeSpace);
766 let regions = self.region_vars_for_defs(span, region_param_defs);
767 let type_parameters = self.next_ty_vars(type_parameter_count);
768 subst::Substs::new_trait(type_parameters, regions, self_ty)
771 pub fn fresh_bound_region(&self, binder_id: ast::NodeId) -> ty::Region {
772 self.region_vars.new_bound(binder_id)
775 pub fn resolve_regions_and_report_errors(&self) {
776 let errors = self.region_vars.resolve_regions();
777 self.report_region_errors(&errors); // see error_reporting.rs
780 pub fn ty_to_string(&self, t: ty::t) -> String {
781 ty_to_string(self.tcx,
782 self.resolve_type_vars_if_possible(t))
785 pub fn tys_to_string(&self, ts: &[ty::t]) -> String {
786 let tstrs: Vec<String> = ts.iter().map(|t| self.ty_to_string(*t)).collect();
787 format!("({})", tstrs.connect(", "))
790 pub fn trait_ref_to_string(&self, t: &ty::TraitRef) -> String {
791 let t = self.resolve_type_vars_in_trait_ref_if_possible(t);
792 trait_ref_to_string(self.tcx, &t)
795 pub fn contains_unbound_type_variables(&self, typ: ty::t) -> ty::t {
796 match resolve_type(self,
798 typ, resolve_nested_tvar | resolve_ivar) {
799 Ok(new_type) => new_type,
804 pub fn shallow_resolve(&self, typ: ty::t) -> ty::t {
805 match ty::get(typ).sty {
806 ty::ty_infer(ty::TyVar(v)) => {
807 self.type_variables.borrow()
812 ty::ty_infer(ty::IntVar(v)) => {
817 ty::ty_infer(ty::FloatVar(v)) => {
828 pub fn resolve_type_vars_if_possible(&self, typ: ty::t) -> ty::t {
829 match resolve_type(self,
831 typ, resolve_nested_tvar | resolve_ivar) {
832 Ok(new_type) => new_type,
837 pub fn resolve_type_vars_in_trait_ref_if_possible(&self,
838 trait_ref: &ty::TraitRef)
840 // make up a dummy type just to reuse/abuse the resolve machinery
841 let dummy0 = ty::mk_trait(self.tcx,
843 trait_ref.substs.clone(),
844 ty::region_existential_bound(ty::ReStatic));
845 let dummy1 = self.resolve_type_vars_if_possible(dummy0);
846 match ty::get(dummy1).sty {
847 ty::ty_trait(box ty::TyTrait { ref def_id, ref substs, .. }) => {
850 substs: (*substs).clone(),
855 format!("resolve_type_vars_if_possible() yielded {} \
856 when supplied with {}",
857 self.ty_to_string(dummy0),
858 self.ty_to_string(dummy1)).as_slice());
863 // [Note-Type-error-reporting]
864 // An invariant is that anytime the expected or actual type is ty_err (the special
865 // error type, meaning that an error occurred when typechecking this expression),
866 // this is a derived error. The error cascaded from another error (that was already
867 // reported), so it's not useful to display it to the user.
868 // The following four methods -- type_error_message_str, type_error_message_str_with_expected,
869 // type_error_message, and report_mismatched_types -- implement this logic.
870 // They check if either the actual or expected type is ty_err, and don't print the error
871 // in this case. The typechecker should only ever report type errors involving mismatched
872 // types using one of these four methods, and should not call span_err directly for such
874 pub fn type_error_message_str(&self,
876 mk_msg: |Option<String>, String| -> String,
878 err: Option<&ty::type_err>) {
879 self.type_error_message_str_with_expected(sp, mk_msg, None, actual_ty, err)
882 pub fn type_error_message_str_with_expected(&self,
884 mk_msg: |Option<String>,
887 expected_ty: Option<ty::t>,
889 err: Option<&ty::type_err>) {
890 debug!("hi! expected_ty = {:?}, actual_ty = {}", expected_ty, actual_ty);
892 let error_str = err.map_or("".to_string(), |t_err| {
893 format!(" ({})", ty::type_err_to_str(self.tcx, t_err))
895 let resolved_expected = expected_ty.map(|e_ty| {
896 self.resolve_type_vars_if_possible(e_ty)
898 if !resolved_expected.map_or(false, |e| { ty::type_is_error(e) }) {
899 match resolved_expected {
905 mk_msg(None, actual_ty),
906 error_str).as_slice())
909 self.tcx.sess.span_err(sp,
911 mk_msg(Some(self.ty_to_string(e)), actual_ty),
912 error_str).as_slice());
915 for err in err.iter() {
916 ty::note_and_explain_type_err(self.tcx, *err)
921 pub fn type_error_message(&self,
923 mk_msg: |String| -> String,
925 err: Option<&ty::type_err>) {
926 let actual_ty = self.resolve_type_vars_if_possible(actual_ty);
928 // Don't report an error if actual type is ty_err.
929 if ty::type_is_error(actual_ty) {
933 self.type_error_message_str(sp, |_e, a| { mk_msg(a) }, self.ty_to_string(actual_ty), err);
936 pub fn report_mismatched_types(&self,
940 err: &ty::type_err) {
941 let resolved_expected =
942 self.resolve_type_vars_if_possible(e);
943 let mk_msg = match ty::get(resolved_expected).sty {
944 // Don't report an error if expected is ty_err
945 ty::ty_err => return,
947 // if I leave out : String, it infers &str and complains
949 format!("mismatched types: expected `{}`, found `{}`",
950 self.ty_to_string(resolved_expected),
955 self.type_error_message(sp, mk_msg, a, Some(err));
958 pub fn replace_late_bound_regions_with_fresh_regions(&self,
962 HashMap<ty::BoundRegion,
965 replace_late_bound_regions_in_fn_sig(self.tcx, fsig, |br| {
966 let rvar = self.next_region_var(
967 BoundRegionInFnType(trace.origin.span(), br));
968 debug!("Bound region {} maps to {:?}",
969 bound_region_to_string(self.tcx, "", false, br),
977 pub fn fold_regions_in_sig(tcx: &ty::ctxt,
979 fldr: |r: ty::Region| -> ty::Region)
981 ty_fold::RegionFolder::regions(tcx, fldr).fold_sig(fn_sig)
985 pub fn span(&self) -> Span {
990 impl Repr for TypeTrace {
991 fn repr(&self, tcx: &ty::ctxt) -> String {
992 format!("TypeTrace({})", self.origin.repr(tcx))
997 pub fn span(&self) -> Span {
999 MethodCompatCheck(span) => span,
1000 ExprAssignable(span) => span,
1002 RelateTraitRefs(span) => span,
1003 RelateSelfType(span) => span,
1004 RelateOutputImplTypes(span) => span,
1005 MatchExpressionArm(match_span, _) => match_span,
1006 IfExpression(span) => span,
1007 IfExpressionWithNoElse(span) => span
1012 impl Repr for TypeOrigin {
1013 fn repr(&self, tcx: &ty::ctxt) -> String {
1015 MethodCompatCheck(a) => {
1016 format!("MethodCompatCheck({})", a.repr(tcx))
1018 ExprAssignable(a) => {
1019 format!("ExprAssignable({})", a.repr(tcx))
1021 Misc(a) => format!("Misc({})", a.repr(tcx)),
1022 RelateTraitRefs(a) => {
1023 format!("RelateTraitRefs({})", a.repr(tcx))
1025 RelateSelfType(a) => {
1026 format!("RelateSelfType({})", a.repr(tcx))
1028 RelateOutputImplTypes(a) => {
1029 format!("RelateOutputImplTypes({})", a.repr(tcx))
1031 MatchExpressionArm(a, b) => {
1032 format!("MatchExpressionArm({}, {})", a.repr(tcx), b.repr(tcx))
1034 IfExpression(a) => {
1035 format!("IfExpression({})", a.repr(tcx))
1037 IfExpressionWithNoElse(a) => {
1038 format!("IfExpressionWithNoElse({})", a.repr(tcx))
1044 impl SubregionOrigin {
1045 pub fn span(&self) -> Span {
1047 Subtype(ref a) => a.span(),
1048 InfStackClosure(a) => a,
1049 InvokeClosure(a) => a,
1050 DerefPointer(a) => a,
1051 FreeVariable(a, _) => a,
1052 ProcCapture(a, _) => a,
1054 RelateObjectBound(a) => a,
1055 RelateProcBound(a, _, _) => a,
1056 RelateParamBound(a, _, _) => a,
1057 RelateRegionParamBound(a) => a,
1058 RelateDefaultParamBound(a, _) => a,
1060 ReborrowUpvar(a, _) => a,
1061 ReferenceOutlivesReferent(_, a) => a,
1062 ExprTypeIsNotInScope(_, a) => a,
1063 BindingTypeIsNotValidAtDecl(a) => a,
1073 impl Repr for SubregionOrigin {
1074 fn repr(&self, tcx: &ty::ctxt) -> String {
1077 format!("Subtype({})", a.repr(tcx))
1079 InfStackClosure(a) => {
1080 format!("InfStackClosure({})", a.repr(tcx))
1082 InvokeClosure(a) => {
1083 format!("InvokeClosure({})", a.repr(tcx))
1085 DerefPointer(a) => {
1086 format!("DerefPointer({})", a.repr(tcx))
1088 FreeVariable(a, b) => {
1089 format!("FreeVariable({}, {})", a.repr(tcx), b)
1091 ProcCapture(a, b) => {
1092 format!("ProcCapture({}, {})", a.repr(tcx), b)
1095 format!("IndexSlice({})", a.repr(tcx))
1097 RelateObjectBound(a) => {
1098 format!("RelateObjectBound({})", a.repr(tcx))
1100 RelateProcBound(a, b, c) => {
1101 format!("RelateProcBound({},{},{})",
1106 RelateParamBound(a, b, c) => {
1107 format!("RelateParamBound({},{},{})",
1112 RelateRegionParamBound(a) => {
1113 format!("RelateRegionParamBound({})",
1116 RelateDefaultParamBound(a, b) => {
1117 format!("RelateDefaultParamBound({},{})",
1121 Reborrow(a) => format!("Reborrow({})", a.repr(tcx)),
1122 ReborrowUpvar(a, b) => {
1123 format!("ReborrowUpvar({},{:?})", a.repr(tcx), b)
1125 ReferenceOutlivesReferent(_, a) => {
1126 format!("ReferenceOutlivesReferent({})", a.repr(tcx))
1128 ExprTypeIsNotInScope(a, b) => {
1129 format!("ExprTypeIsNotInScope({}, {})",
1133 BindingTypeIsNotValidAtDecl(a) => {
1134 format!("BindingTypeIsNotValidAtDecl({})", a.repr(tcx))
1136 CallRcvr(a) => format!("CallRcvr({})", a.repr(tcx)),
1137 CallArg(a) => format!("CallArg({})", a.repr(tcx)),
1138 CallReturn(a) => format!("CallReturn({})", a.repr(tcx)),
1139 AddrOf(a) => format!("AddrOf({})", a.repr(tcx)),
1140 AutoBorrow(a) => format!("AutoBorrow({})", a.repr(tcx)),
1145 impl RegionVariableOrigin {
1146 pub fn span(&self) -> Span {
1148 MiscVariable(a) => a,
1149 PatternRegion(a) => a,
1150 AddrOfRegion(a) => a,
1151 AddrOfSlice(a) => a,
1153 Coercion(ref a) => a.span(),
1154 EarlyBoundRegion(a, _) => a,
1155 LateBoundRegion(a, _) => a,
1156 BoundRegionInFnType(a, _) => a,
1157 BoundRegionInCoherence(_) => codemap::DUMMY_SP,
1158 UpvarRegion(_, a) => a
1163 impl Repr for RegionVariableOrigin {
1164 fn repr(&self, tcx: &ty::ctxt) -> String {
1166 MiscVariable(a) => {
1167 format!("MiscVariable({})", a.repr(tcx))
1169 PatternRegion(a) => {
1170 format!("PatternRegion({})", a.repr(tcx))
1172 AddrOfRegion(a) => {
1173 format!("AddrOfRegion({})", a.repr(tcx))
1175 AddrOfSlice(a) => format!("AddrOfSlice({})", a.repr(tcx)),
1176 Autoref(a) => format!("Autoref({})", a.repr(tcx)),
1177 Coercion(ref a) => format!("Coercion({})", a.repr(tcx)),
1178 EarlyBoundRegion(a, b) => {
1179 format!("EarlyBoundRegion({},{})", a.repr(tcx), b.repr(tcx))
1181 LateBoundRegion(a, b) => {
1182 format!("LateBoundRegion({},{})", a.repr(tcx), b.repr(tcx))
1184 BoundRegionInFnType(a, b) => {
1185 format!("bound_regionInFnType({},{})", a.repr(tcx),
1188 BoundRegionInCoherence(a) => {
1189 format!("bound_regionInCoherence({})", a.repr(tcx))
1191 UpvarRegion(a, b) => {
1192 format!("UpvarRegion({}, {})", a.repr(tcx), b.repr(tcx))