2014-02-10 15:36:31 +01:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-02-09 20:23:16 -05:00
|
|
|
//! See the Book for more information.
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2014-11-17 15:25:37 -05:00
|
|
|
pub use self::LateBoundRegionConversionTime::*;
|
|
|
|
pub use self::RegionVariableOrigin::*;
|
|
|
|
pub use self::SubregionOrigin::*;
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::ValuePairs::*;
|
2016-03-22 17:30:57 +02:00
|
|
|
pub use ty::IntVarValue;
|
2014-12-12 06:13:42 -05:00
|
|
|
pub use self::freshen::TypeFreshener;
|
2017-11-05 06:34:22 -05:00
|
|
|
pub use self::region_constraints::{GenericKind, VerifyBound};
|
2013-01-29 16:26:16 -08:00
|
|
|
|
2016-03-29 12:54:26 +03:00
|
|
|
use hir::def_id::DefId;
|
2017-04-10 00:00:08 -07:00
|
|
|
use middle::free_region::{FreeRegionMap, RegionRelations};
|
2017-08-31 21:37:38 +03:00
|
|
|
use middle::region;
|
2016-11-13 19:42:15 -07:00
|
|
|
use middle::lang_items;
|
2016-05-11 04:14:41 +03:00
|
|
|
use mir::tcx::LvalueTy;
|
2016-10-09 11:36:12 -07:00
|
|
|
use ty::subst::{Kind, Subst, Substs};
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::{TyVid, IntVid, FloatVid};
|
2017-05-15 17:58:58 -04:00
|
|
|
use ty::{self, Ty, TyCtxt};
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
|
2016-07-16 19:38:17 +03:00
|
|
|
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
|
2017-05-24 09:43:20 -04:00
|
|
|
use ty::relate::RelateResult;
|
2016-11-07 13:25:06 -05:00
|
|
|
use traits::{self, ObligationCause, PredicateObligations, Reveal};
|
2015-04-07 06:12:21 -04:00
|
|
|
use rustc_data_structures::unify::{self, UnificationTable};
|
2017-11-05 07:22:39 -05:00
|
|
|
use std::cell::{Cell, RefCell, Ref, RefMut};
|
2015-03-19 14:49:28 -07:00
|
|
|
use std::fmt;
|
2013-01-08 19:37:25 -08:00
|
|
|
use syntax::ast;
|
2016-06-21 18:08:13 -04:00
|
|
|
use errors::DiagnosticBuilder;
|
|
|
|
use syntax_pos::{self, Span, DUMMY_SP};
|
2017-11-01 13:41:07 -04:00
|
|
|
use util::nodemap::{NodeMap, FxHashMap};
|
2016-12-23 20:48:21 -07:00
|
|
|
use arena::DroplessArena;
|
2017-07-01 15:41:49 +02:00
|
|
|
|
2015-03-22 15:11:56 -04:00
|
|
|
use self::combine::CombineFields;
|
2016-05-21 08:16:07 -04:00
|
|
|
use self::higher_ranked::HrMatchResult;
|
2017-11-05 07:01:48 -05:00
|
|
|
use self::region_constraints::{RegionConstraintCollector, RegionSnapshot};
|
2017-11-05 05:48:16 -05:00
|
|
|
use self::lexical_region_resolve::LexicalRegionResolutions;
|
2016-11-28 10:08:08 -08:00
|
|
|
use self::type_variable::TypeVariableOrigin;
|
2015-04-07 06:12:21 -04:00
|
|
|
use self::unify_key::ToType;
|
2014-10-09 17:19:50 -04:00
|
|
|
|
2017-05-24 09:43:20 -04:00
|
|
|
pub mod at;
|
2016-07-25 13:08:48 -07:00
|
|
|
mod combine;
|
|
|
|
mod equate;
|
2014-07-22 07:46:36 -04:00
|
|
|
pub mod error_reporting;
|
2016-11-08 16:18:44 -05:00
|
|
|
mod fudge;
|
2016-07-25 13:08:48 -07:00
|
|
|
mod glb;
|
2014-12-12 14:55:07 -05:00
|
|
|
mod higher_ranked;
|
2013-01-29 16:26:16 -08:00
|
|
|
pub mod lattice;
|
2016-07-25 13:08:48 -07:00
|
|
|
mod lub;
|
2017-11-05 06:34:22 -05:00
|
|
|
pub mod region_constraints;
|
2017-11-15 16:58:14 -05:00
|
|
|
mod lexical_region_resolve;
|
2017-11-04 07:23:21 -04:00
|
|
|
mod outlives;
|
2013-01-29 16:26:16 -08:00
|
|
|
pub mod resolve;
|
2014-12-12 06:13:42 -05:00
|
|
|
mod freshen;
|
2016-07-25 13:08:48 -07:00
|
|
|
mod sub;
|
2014-07-22 07:46:36 -04:00
|
|
|
pub mod type_variable;
|
2015-04-07 06:12:21 -04:00
|
|
|
pub mod unify_key;
|
2013-01-29 16:26:16 -08:00
|
|
|
|
2017-11-04 07:36:00 -04:00
|
|
|
pub use self::outlives::env::OutlivesEnvironment;
|
|
|
|
|
2016-05-21 08:16:07 -04:00
|
|
|
#[must_use]
|
2016-03-29 20:06:42 -07:00
|
|
|
pub struct InferOk<'tcx, T> {
|
|
|
|
pub value: T,
|
|
|
|
pub obligations: PredicateObligations<'tcx>,
|
|
|
|
}
|
|
|
|
pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
|
|
|
|
|
2013-01-29 16:26:16 -08:00
|
|
|
pub type Bound<T> = Option<T>;
|
2015-03-22 15:11:56 -04:00
|
|
|
pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
|
2015-07-10 19:16:35 -07:00
|
|
|
pub type FixupResult<T> = Result<T, FixupError>; // "fixup result"
|
2013-01-29 16:26:16 -08:00
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2017-06-09 10:55:16 +03:00
|
|
|
/// During type-checking/inference of a body, `in_progress_tables`
|
|
|
|
/// contains a reference to the tables being built up, which are
|
|
|
|
/// used for reading closure kinds/signatures as they are inferred,
|
|
|
|
/// and for error reporting logic to read arbitrary node types.
|
|
|
|
pub in_progress_tables: Option<&'a RefCell<ty::TypeckTables<'tcx>>>,
|
2015-06-25 13:08:10 -07:00
|
|
|
|
2016-05-21 08:18:52 -04:00
|
|
|
// Cache for projections. This cache is snapshotted along with the
|
|
|
|
// infcx.
|
|
|
|
//
|
|
|
|
// Public so that `traits::project` can use it.
|
|
|
|
pub projection_cache: RefCell<traits::ProjectionCache<'tcx>>,
|
|
|
|
|
2014-09-13 21:09:25 +03:00
|
|
|
// We instantiate UnificationTable with bounds<Ty> because the
|
2012-11-29 16:41:39 -08:00
|
|
|
// types that might instantiate a general type variable have an
|
|
|
|
// order, represented by its upper and lower bounds.
|
2016-11-28 10:08:08 -08:00
|
|
|
pub type_variables: RefCell<type_variable::TypeVariableTable<'tcx>>,
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2013-01-22 07:02:40 -08:00
|
|
|
// Map from integral variable to the kind of integer it represents
|
2015-01-21 20:25:24 -05:00
|
|
|
int_unification_table: RefCell<UnificationTable<ty::IntVid>>,
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2013-01-22 07:02:40 -08:00
|
|
|
// Map from floating variable to the kind of float it represents
|
2015-01-21 20:25:24 -05:00
|
|
|
float_unification_table: RefCell<UnificationTable<ty::FloatVid>>,
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2017-11-05 07:22:39 -05:00
|
|
|
// Tracks the set of region variables and the constraints between
|
|
|
|
// them. This is initially `Some(_)` but when
|
|
|
|
// `resolve_regions_and_report_errors` is invoked, this gets set
|
|
|
|
// to `None` -- further attempts to perform unification etc may
|
|
|
|
// fail if new region constraints would've been added.
|
|
|
|
region_constraints: RefCell<Option<RegionConstraintCollector<'tcx>>>,
|
2015-06-24 13:40:54 -07:00
|
|
|
|
2017-11-05 05:48:16 -05:00
|
|
|
// Once region inference is done, the values for each variable.
|
|
|
|
lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
/// Caches the results of trait selection. This cache is used
|
|
|
|
/// for things that have to do with the parameters in scope.
|
|
|
|
pub selection_cache: traits::SelectionCache<'tcx>,
|
|
|
|
|
|
|
|
/// Caches the results of trait evaluation.
|
|
|
|
pub evaluation_cache: traits::EvaluationCache<'tcx>,
|
|
|
|
|
2015-09-24 19:58:00 +03:00
|
|
|
// the set of predicates on which errors have been reported, to
|
|
|
|
// avoid reporting the same error twice.
|
2017-05-08 19:45:27 +03:00
|
|
|
pub reported_trait_errors: RefCell<FxHashMap<Span, Vec<ty::Predicate<'tcx>>>>,
|
2015-09-24 19:58:00 +03:00
|
|
|
|
2016-03-15 04:49:10 -04:00
|
|
|
// When an error occurs, we want to avoid reporting "derived"
|
|
|
|
// errors that are due to this original failure. Normally, we
|
|
|
|
// handle this with the `err_count_on_creation` count, which
|
|
|
|
// basically just tracks how many errors were reported when we
|
|
|
|
// started type-checking a fn and checks to see if any new errors
|
|
|
|
// have been reported since then. Not great, but it works.
|
|
|
|
//
|
|
|
|
// However, when errors originated in other passes -- notably
|
|
|
|
// resolve -- this heuristic breaks down. Therefore, we have this
|
|
|
|
// auxiliary flag that one can set whenever one creates a
|
|
|
|
// type-error that is due to an error in a prior pass.
|
|
|
|
//
|
|
|
|
// Don't read this flag directly, call `is_tainted_by_errors()`
|
|
|
|
// and `set_tainted_by_errors()`.
|
|
|
|
tainted_by_errors_flag: Cell<bool>,
|
|
|
|
|
|
|
|
// Track how many errors were reported when this infcx is created.
|
|
|
|
// If the number of errors increases, that's also a sign (line
|
|
|
|
// `tained_by_errors`) to avoid reporting certain kinds of errors.
|
2015-06-25 13:08:10 -07:00
|
|
|
err_count_on_creation: usize,
|
2016-05-25 21:12:35 +03:00
|
|
|
|
2017-04-15 23:51:58 +03:00
|
|
|
// This flag is true while there is an active snapshot.
|
|
|
|
in_snapshot: Cell<bool>,
|
2017-11-01 13:41:07 -04:00
|
|
|
|
|
|
|
// A set of constraints that regionck must validate. Each
|
|
|
|
// constraint has the form `T:'a`, meaning "some type `T` must
|
|
|
|
// outlive the lifetime 'a". These constraints derive from
|
|
|
|
// instantiated type parameters. So if you had a struct defined
|
|
|
|
// like
|
|
|
|
//
|
|
|
|
// struct Foo<T:'static> { ... }
|
|
|
|
//
|
|
|
|
// then in some expression `let x = Foo { ... }` it will
|
|
|
|
// instantiate the type parameter `T` with a fresh type `$0`. At
|
|
|
|
// the same time, it will record a region obligation of
|
|
|
|
// `$0:'static`. This will get checked later by regionck. (We
|
|
|
|
// can't generally check these things right away because we have
|
|
|
|
// to wait until types are resolved.)
|
|
|
|
//
|
|
|
|
// These are stored in a map keyed to the id of the innermost
|
|
|
|
// enclosing fn body / static initializer expression. This is
|
|
|
|
// because the location where the obligation was incurred can be
|
|
|
|
// relevant with respect to which sublifetime assumptions are in
|
|
|
|
// place. The reason that we store under the fn-id, and not
|
|
|
|
// something more fine-grained, is so that it is easier for
|
|
|
|
// regionck to be sure that it has found *all* the region
|
|
|
|
// obligations (otherwise, it's easy to fail to walk to a
|
|
|
|
// particular node-id).
|
2017-11-03 05:31:19 -04:00
|
|
|
//
|
|
|
|
// Before running `resolve_regions_and_report_errors`, the creator
|
|
|
|
// of the inference context is expected to invoke
|
|
|
|
// `process_region_obligations` (defined in `self::region_obligations`)
|
|
|
|
// for each body-id in this map, which will process the
|
|
|
|
// obligations within. This is expected to be done 'late enough'
|
|
|
|
// that all type inference variables have been bound and so forth.
|
2017-11-01 13:41:07 -04:00
|
|
|
region_obligations: RefCell<NodeMap<Vec<RegionObligation<'tcx>>>>,
|
2013-01-08 14:00:45 -08:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2014-12-12 14:55:07 -05:00
|
|
|
/// A map returned by `skolemize_late_bound_regions()` indicating the skolemized
|
2014-12-12 06:37:42 -05:00
|
|
|
/// region that each late-bound region was replaced with.
|
2017-04-20 04:45:53 -04:00
|
|
|
pub type SkolemizationMap<'tcx> = FxHashMap<ty::BoundRegion, ty::Region<'tcx>>;
|
2014-12-12 06:37:42 -05:00
|
|
|
|
2017-02-27 22:09:13 +03:00
|
|
|
/// See `error_reporting` module for more details
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub enum ValuePairs<'tcx> {
|
2015-09-06 21:51:58 +03:00
|
|
|
Types(ExpectedFound<Ty<'tcx>>),
|
|
|
|
TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
|
|
|
|
PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The trace designates the path through inference that we took to
|
|
|
|
/// encounter an error or subtyping constraint.
|
|
|
|
///
|
2017-02-27 22:09:13 +03:00
|
|
|
/// See `error_reporting` module for more details.
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct TypeTrace<'tcx> {
|
2016-11-07 13:25:06 -05:00
|
|
|
cause: ObligationCause<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
values: ValuePairs<'tcx>,
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The origin of a `r1 <= r2` constraint.
|
|
|
|
///
|
2017-02-27 22:09:13 +03:00
|
|
|
/// See `error_reporting` module for more details
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub enum SubregionOrigin<'tcx> {
|
2013-05-23 21:37:37 -04:00
|
|
|
// Arose from a subtyping relation
|
2014-09-29 22:11:30 +03:00
|
|
|
Subtype(TypeTrace<'tcx>),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2013-06-25 12:04:50 -04:00
|
|
|
// Stack-allocated closures cannot outlive innermost loop
|
|
|
|
// or function so as to ensure we only require finite stack
|
2013-08-31 18:13:04 +02:00
|
|
|
InfStackClosure(Span),
|
2013-06-25 12:04:50 -04:00
|
|
|
|
2013-05-23 21:37:37 -04:00
|
|
|
// Invocation of closure must be within its lifetime
|
2013-08-31 18:13:04 +02:00
|
|
|
InvokeClosure(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2014-01-07 18:49:13 -08:00
|
|
|
// Dereference of reference must be within its lifetime
|
2013-08-31 18:13:04 +02:00
|
|
|
DerefPointer(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// Closure bound must not outlive captured free variables
|
2014-02-07 14:43:48 -05:00
|
|
|
FreeVariable(Span, ast::NodeId),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// Index into slice must be within its lifetime
|
2013-08-31 18:13:04 +02:00
|
|
|
IndexSlice(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// When casting `&'a T` to an `&'b Trait` object,
|
|
|
|
// relating `'a` to `'b`
|
2013-08-31 18:13:04 +02:00
|
|
|
RelateObjectBound(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2014-11-15 17:25:05 -05:00
|
|
|
// Some type parameter was instantiated with the given type,
|
2014-08-27 21:46:52 -04:00
|
|
|
// and that type must outlive some region.
|
2014-09-29 22:11:30 +03:00
|
|
|
RelateParamBound(Span, Ty<'tcx>),
|
2014-08-27 21:46:52 -04:00
|
|
|
|
|
|
|
// The given region parameter was instantiated with a region
|
|
|
|
// that must outlive some other region.
|
|
|
|
RelateRegionParamBound(Span),
|
|
|
|
|
|
|
|
// A bound placed on type parameters that states that must outlive
|
|
|
|
// the moment of their instantiation.
|
2014-09-29 22:11:30 +03:00
|
|
|
RelateDefaultParamBound(Span, Ty<'tcx>),
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2014-01-07 18:49:13 -08:00
|
|
|
// Creating a pointer `b` to contents of another reference
|
2013-08-31 18:13:04 +02:00
|
|
|
Reborrow(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2014-02-07 14:43:48 -05:00
|
|
|
// Creating a pointer `b` to contents of an upvar
|
|
|
|
ReborrowUpvar(Span, ty::UpvarId),
|
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
// Data with type `Ty<'tcx>` was borrowed
|
|
|
|
DataBorrowed(Ty<'tcx>, Span),
|
|
|
|
|
2013-05-23 21:37:37 -04:00
|
|
|
// (&'a &'b T) where a >= b
|
2014-09-29 22:11:30 +03:00
|
|
|
ReferenceOutlivesReferent(Ty<'tcx>, Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
// Type or region parameters must be in scope.
|
|
|
|
ParameterInScope(ParameterOrigin, Span),
|
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
// The type T of an expression E must outlive the lifetime for E.
|
2014-09-29 22:11:30 +03:00
|
|
|
ExprTypeIsNotInScope(Ty<'tcx>, Span),
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2013-05-23 21:37:37 -04:00
|
|
|
// A `ref b` whose region does not enclose the decl site
|
2013-08-31 18:13:04 +02:00
|
|
|
BindingTypeIsNotValidAtDecl(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// Regions appearing in a method receiver must outlive method call
|
2013-08-31 18:13:04 +02:00
|
|
|
CallRcvr(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// Regions appearing in a function argument must outlive func call
|
2013-08-31 18:13:04 +02:00
|
|
|
CallArg(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// Region in return type of invoked fn must enclose call
|
2013-08-31 18:13:04 +02:00
|
|
|
CallReturn(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2015-02-12 12:48:54 -05:00
|
|
|
// Operands must be in scope
|
|
|
|
Operand(Span),
|
|
|
|
|
2013-05-23 21:37:37 -04:00
|
|
|
// Region resulting from a `&` expr must enclose the `&` expr
|
2013-08-31 18:13:04 +02:00
|
|
|
AddrOf(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// An auto-borrow that does not enclose the expr where it occurs
|
2013-08-31 18:13:04 +02:00
|
|
|
AutoBorrow(Span),
|
2014-10-27 12:55:16 +01:00
|
|
|
|
|
|
|
// Region constraint arriving from destructor safety
|
|
|
|
SafeDestructor(Span),
|
2016-10-05 10:17:14 -04:00
|
|
|
|
|
|
|
// Comparing the signature and requirements of an impl method against
|
|
|
|
// the containing trait.
|
|
|
|
CompareImplMethodObligation {
|
|
|
|
span: Span,
|
|
|
|
item_name: ast::Name,
|
|
|
|
impl_item_def_id: DefId,
|
|
|
|
trait_item_def_id: DefId,
|
|
|
|
},
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
/// Places that type/region parameters can appear.
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum ParameterOrigin {
|
|
|
|
Path, // foo::bar
|
|
|
|
MethodCall, // foo.bar() <-- parameters on impl providing bar()
|
|
|
|
OverloadedOperator, // a + b when overloaded
|
|
|
|
OverloadedDeref, // *a when overloaded
|
|
|
|
}
|
|
|
|
|
2014-11-05 22:06:04 -05:00
|
|
|
/// Times when we replace late-bound regions with variables:
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2014-11-05 22:06:04 -05:00
|
|
|
pub enum LateBoundRegionConversionTime {
|
|
|
|
/// when a fn is called
|
|
|
|
FnCall,
|
|
|
|
|
2014-11-15 16:22:22 -05:00
|
|
|
/// when two higher-ranked types are compared
|
|
|
|
HigherRankedType,
|
2014-12-17 14:16:28 -05:00
|
|
|
|
|
|
|
/// when projecting an associated type
|
2017-08-19 13:41:10 -07:00
|
|
|
AssocTypeProjection(DefId),
|
2014-11-05 22:06:04 -05:00
|
|
|
}
|
|
|
|
|
2013-05-23 21:37:37 -04:00
|
|
|
/// Reasons to create a region inference variable
|
|
|
|
///
|
2017-02-27 22:09:13 +03:00
|
|
|
/// See `error_reporting` module for more details
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2015-03-10 07:02:27 -04:00
|
|
|
pub enum RegionVariableOrigin {
|
2013-05-23 21:37:37 -04:00
|
|
|
// Region variables created for ill-categorized reasons,
|
|
|
|
// mostly indicates places in need of refactoring
|
2013-08-31 18:13:04 +02:00
|
|
|
MiscVariable(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// Regions created by a `&P` or `[...]` pattern
|
2013-08-31 18:13:04 +02:00
|
|
|
PatternRegion(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// Regions created by `&` operator
|
2013-08-31 18:13:04 +02:00
|
|
|
AddrOfRegion(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// Regions created as part of an autoref of a method receiver
|
2013-08-31 18:13:04 +02:00
|
|
|
Autoref(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
|
|
|
// Regions created as part of an automatic coercion
|
2015-03-10 07:02:27 -04:00
|
|
|
Coercion(Span),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2014-03-07 08:43:39 +01:00
|
|
|
// Region variables created as the values for early-bound regions
|
2017-07-29 17:19:57 +03:00
|
|
|
EarlyBoundRegion(Span, ast::Name),
|
2014-03-07 08:43:39 +01:00
|
|
|
|
2013-05-23 21:37:37 -04:00
|
|
|
// Region variables created for bound regions
|
|
|
|
// in a function or method that is called
|
2014-11-05 22:06:04 -05:00
|
|
|
LateBoundRegion(Span, ty::BoundRegion, LateBoundRegionConversionTime),
|
2013-05-23 21:37:37 -04:00
|
|
|
|
2014-02-07 14:43:48 -05:00
|
|
|
UpvarRegion(ty::UpvarId, Span),
|
|
|
|
|
2014-03-07 08:43:39 +01:00
|
|
|
BoundRegionInCoherence(ast::Name),
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2015-07-10 19:16:35 -07:00
|
|
|
pub enum FixupError {
|
|
|
|
UnresolvedIntTy(IntVid),
|
|
|
|
UnresolvedFloatTy(FloatVid),
|
|
|
|
UnresolvedTy(TyVid)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2017-11-01 13:41:07 -04:00
|
|
|
/// See the `region_obligations` field for more information.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RegionObligation<'tcx> {
|
|
|
|
pub sub_region: ty::Region<'tcx>,
|
|
|
|
pub sup_type: Ty<'tcx>,
|
|
|
|
pub cause: ObligationCause<'tcx>,
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
impl fmt::Display for FixupError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::FixupError::*;
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
UnresolvedIntTy(_) => {
|
|
|
|
write!(f, "cannot determine the type of this integer; \
|
|
|
|
add a suffix to specify the type explicitly")
|
|
|
|
}
|
|
|
|
UnresolvedFloatTy(_) => {
|
|
|
|
write!(f, "cannot determine the type of this number; \
|
|
|
|
add a suffix to specify the type explicitly")
|
|
|
|
}
|
|
|
|
UnresolvedTy(_) => write!(f, "unconstrained type")
|
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:55:16 +03:00
|
|
|
/// Helper type of a temporary returned by tcx.infer_ctxt().
|
2016-05-11 04:14:41 +03:00
|
|
|
/// Necessary because we can't write the following bound:
|
|
|
|
/// F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(InferCtxt<'b, 'gcx, 'tcx>).
|
|
|
|
pub struct InferCtxtBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
global_tcx: TyCtxt<'a, 'gcx, 'gcx>,
|
2016-12-23 20:48:21 -07:00
|
|
|
arena: DroplessArena,
|
2017-01-25 16:24:00 -05:00
|
|
|
fresh_tables: Option<RefCell<ty::TypeckTables<'tcx>>>,
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'gcx> {
|
2017-06-09 10:55:16 +03:00
|
|
|
pub fn infer_ctxt(self) -> InferCtxtBuilder<'a, 'gcx, 'tcx> {
|
2016-05-11 04:14:41 +03:00
|
|
|
InferCtxtBuilder {
|
|
|
|
global_tcx: self,
|
2016-12-23 20:48:21 -07:00
|
|
|
arena: DroplessArena::new(),
|
2017-06-09 10:55:16 +03:00
|
|
|
fresh_tables: None,
|
2017-05-08 19:45:27 +03:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> {
|
2017-06-09 10:55:16 +03:00
|
|
|
/// Used only by `rustc_typeck` during body type-checking/inference,
|
|
|
|
/// will initialize `in_progress_tables` with fresh `TypeckTables`.
|
2017-08-04 09:49:40 +02:00
|
|
|
pub fn with_fresh_in_progress_tables(mut self, table_owner: DefId) -> Self {
|
2017-08-11 11:56:26 +02:00
|
|
|
self.fresh_tables = Some(RefCell::new(ty::TypeckTables::empty(Some(table_owner))));
|
2017-06-09 10:55:16 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub fn enter<F, R>(&'tcx mut self, f: F) -> R
|
|
|
|
where F: for<'b> FnOnce(InferCtxt<'b, 'gcx, 'tcx>) -> R
|
2016-03-25 05:22:44 +02:00
|
|
|
{
|
2016-05-11 04:14:41 +03:00
|
|
|
let InferCtxtBuilder {
|
|
|
|
global_tcx,
|
2016-12-23 20:48:21 -07:00
|
|
|
ref arena,
|
2017-01-06 21:54:24 +02:00
|
|
|
ref fresh_tables,
|
2016-05-11 04:14:41 +03:00
|
|
|
} = *self;
|
2017-06-09 10:55:16 +03:00
|
|
|
let in_progress_tables = fresh_tables.as_ref();
|
2016-12-23 20:48:21 -07:00
|
|
|
global_tcx.enter_local(arena, |tcx| f(InferCtxt {
|
2017-06-09 10:55:16 +03:00
|
|
|
tcx,
|
|
|
|
in_progress_tables,
|
2016-05-21 08:18:52 -04:00
|
|
|
projection_cache: RefCell::new(traits::ProjectionCache::new()),
|
2016-03-11 02:31:38 +02:00
|
|
|
type_variables: RefCell::new(type_variable::TypeVariableTable::new()),
|
|
|
|
int_unification_table: RefCell::new(UnificationTable::new()),
|
|
|
|
float_unification_table: RefCell::new(UnificationTable::new()),
|
2017-11-05 07:22:39 -05:00
|
|
|
region_constraints: RefCell::new(Some(RegionConstraintCollector::new())),
|
2017-11-05 05:48:16 -05:00
|
|
|
lexical_region_resolutions: RefCell::new(None),
|
2016-04-29 06:00:23 +03:00
|
|
|
selection_cache: traits::SelectionCache::new(),
|
|
|
|
evaluation_cache: traits::EvaluationCache::new(),
|
2017-05-08 19:45:27 +03:00
|
|
|
reported_trait_errors: RefCell::new(FxHashMap()),
|
2016-05-11 04:14:41 +03:00
|
|
|
tainted_by_errors_flag: Cell::new(false),
|
2016-05-25 21:12:35 +03:00
|
|
|
err_count_on_creation: tcx.sess.err_count(),
|
2017-04-15 23:51:58 +03:00
|
|
|
in_snapshot: Cell::new(false),
|
2017-11-01 13:41:07 -04:00
|
|
|
region_obligations: RefCell::new(NodeMap()),
|
2016-05-11 04:14:41 +03:00
|
|
|
}))
|
2016-03-11 02:31:38 +02:00
|
|
|
}
|
2015-06-27 22:04:15 -07:00
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
impl<T> ExpectedFound<T> {
|
2017-03-09 21:47:09 -05:00
|
|
|
pub fn new(a_is_expected: bool, a: T, b: T) -> Self {
|
2016-03-17 00:15:31 +02:00
|
|
|
if a_is_expected {
|
|
|
|
ExpectedFound {expected: a, found: b}
|
|
|
|
} else {
|
|
|
|
ExpectedFound {expected: b, found: a}
|
|
|
|
}
|
2016-02-26 10:51:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
impl<'tcx, T> InferOk<'tcx, T> {
|
2016-04-20 19:51:56 -04:00
|
|
|
pub fn unit(self) -> InferOk<'tcx, ()> {
|
2016-03-17 00:15:31 +02:00
|
|
|
InferOk { value: (), obligations: self.obligations }
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 10:11:59 -05:00
|
|
|
#[must_use = "once you start a snapshot, you should always consume it"]
|
2017-05-17 00:54:04 +03:00
|
|
|
pub struct CombinedSnapshot<'a, 'tcx:'a> {
|
2016-05-21 08:18:52 -04:00
|
|
|
projection_cache_snapshot: traits::ProjectionCacheSnapshot,
|
2014-07-22 07:46:36 -04:00
|
|
|
type_snapshot: type_variable::Snapshot,
|
|
|
|
int_snapshot: unify::Snapshot<ty::IntVid>,
|
|
|
|
float_snapshot: unify::Snapshot<ty::FloatVid>,
|
2017-11-05 06:55:40 -05:00
|
|
|
region_constraints_snapshot: RegionSnapshot,
|
2017-04-15 23:51:58 +03:00
|
|
|
was_in_snapshot: bool,
|
2017-05-17 00:54:04 +03:00
|
|
|
_in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
/// Helper trait for shortening the lifetimes inside a
|
|
|
|
/// value for post-type-checking normalization.
|
|
|
|
pub trait TransNormalize<'gcx>: TypeFoldable<'gcx> {
|
2017-05-23 04:19:47 -04:00
|
|
|
fn trans_normalize<'a, 'tcx>(&self,
|
|
|
|
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>)
|
|
|
|
-> Self;
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! items { ($($item:item)+) => ($($item)+) }
|
|
|
|
macro_rules! impl_trans_normalize {
|
|
|
|
($lt_gcx:tt, $($ty:ty),+) => {
|
|
|
|
items!($(impl<$lt_gcx> TransNormalize<$lt_gcx> for $ty {
|
|
|
|
fn trans_normalize<'a, 'tcx>(&self,
|
2017-05-23 04:19:47 -04:00
|
|
|
infcx: &InferCtxt<'a, $lt_gcx, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>)
|
2016-05-11 04:14:41 +03:00
|
|
|
-> Self {
|
2017-05-23 04:19:47 -04:00
|
|
|
infcx.normalize_projections_in(param_env, self)
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
})+);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_trans_normalize!('gcx,
|
|
|
|
Ty<'gcx>,
|
2017-08-07 08:08:53 +03:00
|
|
|
&'gcx ty::Const<'gcx>,
|
2016-05-11 04:14:41 +03:00
|
|
|
&'gcx Substs<'gcx>,
|
|
|
|
ty::FnSig<'gcx>,
|
2017-02-13 10:51:06 +02:00
|
|
|
ty::PolyFnSig<'gcx>,
|
2016-05-11 04:14:41 +03:00
|
|
|
ty::ClosureSubsts<'gcx>,
|
2016-10-13 00:08:11 +03:00
|
|
|
ty::PolyTraitRef<'gcx>,
|
|
|
|
ty::ExistentialTraitRef<'gcx>
|
2016-05-11 04:14:41 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
impl<'gcx> TransNormalize<'gcx> for LvalueTy<'gcx> {
|
2017-05-23 04:19:47 -04:00
|
|
|
fn trans_normalize<'a, 'tcx>(&self,
|
|
|
|
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>)
|
|
|
|
-> Self {
|
2016-05-11 04:14:41 +03:00
|
|
|
match *self {
|
2017-05-23 04:19:47 -04:00
|
|
|
LvalueTy::Ty { ty } => LvalueTy::Ty { ty: ty.trans_normalize(infcx, param_env) },
|
2016-05-11 04:14:41 +03:00
|
|
|
LvalueTy::Downcast { adt_def, substs, variant_index } => {
|
|
|
|
LvalueTy::Downcast {
|
2017-07-03 11:19:51 -07:00
|
|
|
adt_def,
|
2017-05-23 04:19:47 -04:00
|
|
|
substs: substs.trans_normalize(infcx, param_env),
|
2017-07-03 11:19:51 -07:00
|
|
|
variant_index,
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 12:47:09 -08:00
|
|
|
// NOTE: Callable from trans only!
|
2016-05-03 05:23:22 +03:00
|
|
|
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
|
2016-10-13 00:08:11 +03:00
|
|
|
/// Currently, higher-ranked type bounds inhibit normalization. Therefore,
|
|
|
|
/// each time we erase them in translation, we need to normalize
|
|
|
|
/// the contents.
|
|
|
|
pub fn erase_late_bound_regions_and_normalize<T>(self, value: &ty::Binder<T>)
|
|
|
|
-> T
|
|
|
|
where T: TransNormalize<'tcx>
|
|
|
|
{
|
|
|
|
assert!(!value.needs_subst());
|
|
|
|
let value = self.erase_late_bound_regions(value);
|
2017-10-16 19:39:32 +01:00
|
|
|
self.fully_normalize_associated_types_in(&value)
|
2016-10-13 00:08:11 +03:00
|
|
|
}
|
|
|
|
|
2017-05-23 04:19:47 -04:00
|
|
|
/// Fully normalizes any associated types in `value`, using an
|
|
|
|
/// empty environment and `Reveal::All` mode (therefore, suitable
|
|
|
|
/// only for monomorphized code during trans, basically).
|
2017-10-16 19:39:32 +01:00
|
|
|
pub fn fully_normalize_associated_types_in<T>(self, value: &T) -> T
|
2016-05-11 04:14:41 +03:00
|
|
|
where T: TransNormalize<'tcx>
|
2016-03-11 02:33:20 +02:00
|
|
|
{
|
2017-10-16 19:39:32 +01:00
|
|
|
debug!("fully_normalize_associated_types_in(t={:?})", value);
|
2015-06-27 22:04:15 -07:00
|
|
|
|
2017-05-23 04:19:47 -04:00
|
|
|
let param_env = ty::ParamEnv::empty(Reveal::All);
|
2016-03-11 02:33:20 +02:00
|
|
|
let value = self.erase_regions(value);
|
2015-06-27 22:04:15 -07:00
|
|
|
|
2017-08-07 08:08:53 +03:00
|
|
|
if !value.has_projections() {
|
2016-03-11 02:33:20 +02:00
|
|
|
return value;
|
|
|
|
}
|
2015-06-27 22:04:15 -07:00
|
|
|
|
2017-06-09 10:55:16 +03:00
|
|
|
self.infer_ctxt().enter(|infcx| {
|
2017-05-23 04:19:47 -04:00
|
|
|
value.trans_normalize(&infcx, param_env)
|
2016-04-29 06:00:23 +03:00
|
|
|
})
|
|
|
|
}
|
2016-05-17 02:26:18 +03:00
|
|
|
|
2017-05-17 08:01:04 -04:00
|
|
|
/// Does a best-effort to normalize any associated types in
|
|
|
|
/// `value`; this includes revealing specializable types, so this
|
|
|
|
/// should be not be used during type-checking, but only during
|
|
|
|
/// optimization and code generation.
|
2016-05-17 02:26:18 +03:00
|
|
|
pub fn normalize_associated_type_in_env<T>(
|
2017-05-15 17:57:30 -04:00
|
|
|
self, value: &T, env: ty::ParamEnv<'tcx>
|
2016-05-17 02:26:18 +03:00
|
|
|
) -> T
|
|
|
|
where T: TransNormalize<'tcx>
|
|
|
|
{
|
|
|
|
debug!("normalize_associated_type_in_env(t={:?})", value);
|
|
|
|
|
|
|
|
let value = self.erase_regions(value);
|
|
|
|
|
2017-08-07 08:08:53 +03:00
|
|
|
if !value.has_projections() {
|
2016-05-17 02:26:18 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:55:16 +03:00
|
|
|
self.infer_ctxt().enter(|infcx| {
|
2017-05-23 04:19:47 -04:00
|
|
|
value.trans_normalize(&infcx, env.reveal_all())
|
2016-05-17 02:26:18 +03:00
|
|
|
})
|
|
|
|
}
|
2016-04-29 06:00:23 +03:00
|
|
|
}
|
2015-06-27 22:04:15 -07:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
|
2017-05-23 04:19:47 -04:00
|
|
|
fn normalize_projections_in<T>(&self, param_env: ty::ParamEnv<'tcx>, value: &T) -> T::Lifted
|
2016-05-11 04:14:41 +03:00
|
|
|
where T: TypeFoldable<'tcx> + ty::Lift<'gcx>
|
2016-04-29 06:00:23 +03:00
|
|
|
{
|
|
|
|
let mut selcx = traits::SelectionContext::new(self);
|
|
|
|
let cause = traits::ObligationCause::dummy();
|
|
|
|
let traits::Normalized { value: result, obligations } =
|
2017-05-23 04:19:47 -04:00
|
|
|
traits::normalize(&mut selcx, param_env, cause, value);
|
2015-06-27 22:04:15 -07:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
debug!("normalize_projections_in: result={:?} obligations={:?}",
|
|
|
|
result, obligations);
|
2015-06-27 22:04:15 -07:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
let mut fulfill_cx = traits::FulfillmentContext::new();
|
2015-06-27 22:04:15 -07:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
for obligation in obligations {
|
|
|
|
fulfill_cx.register_predicate_obligation(self, obligation);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.drain_fulfillment_cx_or_panic(DUMMY_SP, &mut fulfill_cx, &result)
|
2016-03-11 02:33:20 +02:00
|
|
|
}
|
2015-06-27 22:04:15 -07:00
|
|
|
|
2016-08-08 09:40:12 -04:00
|
|
|
/// Finishes processes any obligations that remain in the
|
|
|
|
/// fulfillment context, and then returns the result with all type
|
|
|
|
/// variables removed and regions erased. Because this is intended
|
|
|
|
/// for use after type-check has completed, if any errors occur,
|
|
|
|
/// it will panic. It is used during normalization and other cases
|
|
|
|
/// where processing the obligations in `fulfill_cx` may cause
|
|
|
|
/// type inference variables that appear in `result` to be
|
|
|
|
/// unified, and hence we need to process those obligations to get
|
|
|
|
/// the complete picture of the type.
|
2016-05-11 08:48:12 +03:00
|
|
|
pub fn drain_fulfillment_cx_or_panic<T>(&self,
|
|
|
|
span: Span,
|
|
|
|
fulfill_cx: &mut traits::FulfillmentContext<'tcx>,
|
|
|
|
result: &T)
|
|
|
|
-> T::Lifted
|
|
|
|
where T: TypeFoldable<'tcx> + ty::Lift<'gcx>
|
|
|
|
{
|
2016-05-21 05:41:40 -04:00
|
|
|
debug!("drain_fulfillment_cx_or_panic()");
|
|
|
|
|
2016-08-08 09:40:12 -04:00
|
|
|
// In principle, we only need to do this so long as `result`
|
|
|
|
// contains unbound type parameters. It could be a slight
|
|
|
|
// optimization to stop iterating early.
|
|
|
|
match fulfill_cx.select_all_or_error(self) {
|
|
|
|
Ok(()) => { }
|
2016-05-11 08:48:12 +03:00
|
|
|
Err(errors) => {
|
2016-08-08 09:40:12 -04:00
|
|
|
span_bug!(span, "Encountered errors `{:?}` resolving bounds after type-checking",
|
|
|
|
errors);
|
2016-05-11 08:48:12 +03:00
|
|
|
}
|
2016-08-08 09:40:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = self.resolve_type_vars_if_possible(result);
|
|
|
|
let result = self.tcx.erase_regions(&result);
|
2016-05-11 04:14:41 +03:00
|
|
|
|
2016-08-08 09:40:12 -04:00
|
|
|
match self.tcx.lift_to_global(&result) {
|
|
|
|
Some(result) => result,
|
2016-05-11 08:48:12 +03:00
|
|
|
None => {
|
2016-08-08 09:40:12 -04:00
|
|
|
span_bug!(span, "Uninferred types/regions in `{:?}`", result);
|
2016-05-11 08:48:12 +03:00
|
|
|
}
|
2015-06-27 22:04:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:51:58 +03:00
|
|
|
pub fn is_in_snapshot(&self) -> bool {
|
|
|
|
self.in_snapshot.get()
|
|
|
|
}
|
|
|
|
|
2014-12-12 06:13:42 -05:00
|
|
|
pub fn freshen<T:TypeFoldable<'tcx>>(&self, t: T) -> T {
|
|
|
|
t.fold_with(&mut self.freshener())
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
|
|
|
|
2014-09-13 21:09:25 +03:00
|
|
|
pub fn type_var_diverges(&'a self, ty: Ty) -> bool {
|
2014-10-31 10:51:16 +02:00
|
|
|
match ty.sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyInfer(ty::TyVar(vid)) => self.type_variables.borrow().var_diverges(vid),
|
2014-10-24 21:14:37 +02:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'gcx, 'tcx> {
|
2014-12-12 06:13:42 -05:00
|
|
|
freshen::TypeFreshener::new(self)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2014-12-15 09:45:28 -05:00
|
|
|
pub fn type_is_unconstrained_numeric(&'a self, ty: Ty) -> UnconstrainedNumeric {
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::error::UnconstrainedNumeric::Neither;
|
|
|
|
use ty::error::UnconstrainedNumeric::{UnconstrainedInt, UnconstrainedFloat};
|
2014-12-15 09:45:28 -05:00
|
|
|
match ty.sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyInfer(ty::IntVar(vid)) => {
|
2015-02-16 05:07:22 -05:00
|
|
|
if self.int_unification_table.borrow_mut().has_value(vid) {
|
|
|
|
Neither
|
|
|
|
} else {
|
|
|
|
UnconstrainedInt
|
2014-12-15 09:45:28 -05:00
|
|
|
}
|
|
|
|
},
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyInfer(ty::FloatVar(vid)) => {
|
2015-02-16 05:07:22 -05:00
|
|
|
if self.float_unification_table.borrow_mut().has_value(vid) {
|
|
|
|
Neither
|
|
|
|
} else {
|
|
|
|
UnconstrainedFloat
|
2014-12-15 09:45:28 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => Neither,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 12:15:48 -07:00
|
|
|
/// Returns a type variable's default fallback if any exists. A default
|
|
|
|
/// must be attached to the variable when created, if it is created
|
|
|
|
/// without a default, this will return None.
|
|
|
|
///
|
2015-07-16 11:26:02 -07:00
|
|
|
/// This code does not apply to integral or floating point variables,
|
|
|
|
/// only to use declared defaults.
|
|
|
|
///
|
2015-07-09 12:15:48 -07:00
|
|
|
/// See `new_ty_var_with_default` to create a type variable with a default.
|
|
|
|
/// See `type_variable::Default` for details about what a default entails.
|
|
|
|
pub fn default(&self, ty: Ty<'tcx>) -> Option<type_variable::Default<'tcx>> {
|
|
|
|
match ty.sty {
|
|
|
|
ty::TyInfer(ty::TyVar(vid)) => self.type_variables.borrow().default(vid),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:44:23 -04:00
|
|
|
pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {
|
2015-07-07 15:50:02 -07:00
|
|
|
let mut variables = Vec::new();
|
|
|
|
|
|
|
|
let unbound_ty_vars = self.type_variables
|
2016-03-03 10:43:52 +01:00
|
|
|
.borrow_mut()
|
2015-07-07 15:50:02 -07:00
|
|
|
.unsolved_variables()
|
2015-07-09 12:15:48 -07:00
|
|
|
.into_iter()
|
|
|
|
.map(|t| self.tcx.mk_var(t));
|
2015-07-07 15:50:02 -07:00
|
|
|
|
|
|
|
let unbound_int_vars = self.int_unification_table
|
|
|
|
.borrow_mut()
|
|
|
|
.unsolved_variables()
|
2015-07-09 12:15:48 -07:00
|
|
|
.into_iter()
|
|
|
|
.map(|v| self.tcx.mk_int_var(v));
|
2015-07-07 15:50:02 -07:00
|
|
|
|
|
|
|
let unbound_float_vars = self.float_unification_table
|
|
|
|
.borrow_mut()
|
|
|
|
.unsolved_variables()
|
2015-07-09 12:15:48 -07:00
|
|
|
.into_iter()
|
|
|
|
.map(|v| self.tcx.mk_float_var(v));
|
2015-07-07 15:50:02 -07:00
|
|
|
|
|
|
|
variables.extend(unbound_ty_vars);
|
|
|
|
variables.extend(unbound_int_vars);
|
|
|
|
variables.extend(unbound_float_vars);
|
2015-07-12 20:33:17 -07:00
|
|
|
|
2015-07-07 15:50:02 -07:00
|
|
|
return variables;
|
|
|
|
}
|
|
|
|
|
2017-05-23 04:19:47 -04:00
|
|
|
fn combine_fields(&'a self, trace: TypeTrace<'tcx>, param_env: ty::ParamEnv<'tcx>)
|
2016-04-29 06:00:23 +03:00
|
|
|
-> CombineFields<'a, 'gcx, 'tcx> {
|
2016-03-29 20:06:42 -07:00
|
|
|
CombineFields {
|
|
|
|
infcx: self,
|
2017-07-03 11:19:51 -07:00
|
|
|
trace,
|
2016-03-29 20:06:42 -07:00
|
|
|
cause: None,
|
2017-05-23 04:19:47 -04:00
|
|
|
param_env,
|
2016-03-29 20:06:42 -07:00
|
|
|
obligations: PredicateObligations::new(),
|
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2017-04-15 23:51:58 +03:00
|
|
|
// Clear the "currently in a snapshot" flag, invoke the closure,
|
2016-09-13 18:31:26 -04:00
|
|
|
// then restore the flag to its original value. This flag is a
|
|
|
|
// debugging measure designed to detect cases where we start a
|
2017-04-15 23:51:58 +03:00
|
|
|
// snapshot, create type variables, and register obligations
|
|
|
|
// which may involve those type variables in the fulfillment cx,
|
|
|
|
// potentially leaving "dangling type variables" behind.
|
|
|
|
// In such cases, an assertion will fail when attempting to
|
|
|
|
// register obligations, within a snapshot. Very useful, much
|
|
|
|
// better than grovelling through megabytes of RUST_LOG output.
|
2016-09-13 18:31:26 -04:00
|
|
|
//
|
2017-04-15 23:51:58 +03:00
|
|
|
// HOWEVER, in some cases the flag is unhelpful. In particular, we
|
2016-09-13 18:31:26 -04:00
|
|
|
// sometimes create a "mini-fulfilment-cx" in which we enroll
|
|
|
|
// obligations. As long as this fulfillment cx is fully drained
|
|
|
|
// before we return, this is not a problem, as there won't be any
|
|
|
|
// escaping obligations in the main cx. In those cases, you can
|
|
|
|
// use this function.
|
2017-04-15 23:51:58 +03:00
|
|
|
pub fn save_and_restore_in_snapshot_flag<F, R>(&self, func: F) -> R
|
2016-09-13 18:31:26 -04:00
|
|
|
where F: FnOnce(&Self) -> R
|
|
|
|
{
|
2017-04-15 23:51:58 +03:00
|
|
|
let flag = self.in_snapshot.get();
|
|
|
|
self.in_snapshot.set(false);
|
2016-09-13 18:31:26 -04:00
|
|
|
let result = func(self);
|
2017-04-15 23:51:58 +03:00
|
|
|
self.in_snapshot.set(flag);
|
2016-09-13 18:31:26 -04:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2017-05-17 00:54:04 +03:00
|
|
|
fn start_snapshot<'b>(&'b self) -> CombinedSnapshot<'b, 'tcx> {
|
2016-05-21 05:41:40 -04:00
|
|
|
debug!("start_snapshot()");
|
|
|
|
|
2017-04-15 23:51:58 +03:00
|
|
|
let in_snapshot = self.in_snapshot.get();
|
|
|
|
self.in_snapshot.set(true);
|
2016-05-25 21:12:35 +03:00
|
|
|
|
2014-06-20 06:35:06 -04:00
|
|
|
CombinedSnapshot {
|
2016-05-21 08:18:52 -04:00
|
|
|
projection_cache_snapshot: self.projection_cache.borrow_mut().snapshot(),
|
2014-07-22 07:46:36 -04:00
|
|
|
type_snapshot: self.type_variables.borrow_mut().snapshot(),
|
2014-06-20 06:35:06 -04:00
|
|
|
int_snapshot: self.int_unification_table.borrow_mut().snapshot(),
|
|
|
|
float_snapshot: self.float_unification_table.borrow_mut().snapshot(),
|
2017-11-05 07:22:39 -05:00
|
|
|
region_constraints_snapshot: self.borrow_region_constraints().start_snapshot(),
|
2017-04-15 23:51:58 +03:00
|
|
|
was_in_snapshot: in_snapshot,
|
2017-05-17 00:54:04 +03:00
|
|
|
// Borrow tables "in progress" (i.e. during typeck)
|
|
|
|
// to ban writes from within a snapshot to them.
|
2017-06-09 10:55:16 +03:00
|
|
|
_in_progress_tables: self.in_progress_tables.map(|tables| {
|
|
|
|
tables.borrow()
|
|
|
|
})
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 10:23:35 -04:00
|
|
|
fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot) {
|
|
|
|
debug!("rollback_to(cause={})", cause);
|
2016-05-21 08:18:52 -04:00
|
|
|
let CombinedSnapshot { projection_cache_snapshot,
|
|
|
|
type_snapshot,
|
2014-06-20 06:35:06 -04:00
|
|
|
int_snapshot,
|
|
|
|
float_snapshot,
|
2017-11-05 06:55:40 -05:00
|
|
|
region_constraints_snapshot,
|
2017-05-17 00:54:04 +03:00
|
|
|
was_in_snapshot,
|
|
|
|
_in_progress_tables } = snapshot;
|
2016-05-25 21:12:35 +03:00
|
|
|
|
2017-04-15 23:51:58 +03:00
|
|
|
self.in_snapshot.set(was_in_snapshot);
|
2014-06-20 06:35:06 -04:00
|
|
|
|
2016-05-21 08:18:52 -04:00
|
|
|
self.projection_cache
|
|
|
|
.borrow_mut()
|
|
|
|
.rollback_to(projection_cache_snapshot);
|
2014-07-22 07:46:36 -04:00
|
|
|
self.type_variables
|
2014-06-20 06:35:06 -04:00
|
|
|
.borrow_mut()
|
2014-07-22 07:40:51 -04:00
|
|
|
.rollback_to(type_snapshot);
|
2014-06-20 06:35:06 -04:00
|
|
|
self.int_unification_table
|
|
|
|
.borrow_mut()
|
2014-07-22 07:40:51 -04:00
|
|
|
.rollback_to(int_snapshot);
|
2014-06-20 06:35:06 -04:00
|
|
|
self.float_unification_table
|
|
|
|
.borrow_mut()
|
2014-07-22 07:40:51 -04:00
|
|
|
.rollback_to(float_snapshot);
|
2017-11-05 07:22:39 -05:00
|
|
|
self.borrow_region_constraints()
|
2017-11-05 06:55:40 -05:00
|
|
|
.rollback_to(region_constraints_snapshot);
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 06:35:06 -04:00
|
|
|
fn commit_from(&self, snapshot: CombinedSnapshot) {
|
2016-05-21 05:41:40 -04:00
|
|
|
debug!("commit_from()");
|
2016-05-21 08:18:52 -04:00
|
|
|
let CombinedSnapshot { projection_cache_snapshot,
|
|
|
|
type_snapshot,
|
2014-06-20 06:35:06 -04:00
|
|
|
int_snapshot,
|
|
|
|
float_snapshot,
|
2017-11-05 06:55:40 -05:00
|
|
|
region_constraints_snapshot,
|
2017-05-17 00:54:04 +03:00
|
|
|
was_in_snapshot,
|
|
|
|
_in_progress_tables } = snapshot;
|
2016-05-25 21:12:35 +03:00
|
|
|
|
2017-04-15 23:51:58 +03:00
|
|
|
self.in_snapshot.set(was_in_snapshot);
|
2014-06-20 06:35:06 -04:00
|
|
|
|
2016-05-21 08:18:52 -04:00
|
|
|
self.projection_cache
|
|
|
|
.borrow_mut()
|
|
|
|
.commit(projection_cache_snapshot);
|
2014-07-22 07:46:36 -04:00
|
|
|
self.type_variables
|
2014-06-20 06:35:06 -04:00
|
|
|
.borrow_mut()
|
|
|
|
.commit(type_snapshot);
|
|
|
|
self.int_unification_table
|
|
|
|
.borrow_mut()
|
|
|
|
.commit(int_snapshot);
|
|
|
|
self.float_unification_table
|
|
|
|
.borrow_mut()
|
|
|
|
.commit(float_snapshot);
|
2017-11-05 07:22:39 -05:00
|
|
|
self.borrow_region_constraints()
|
2017-11-05 06:55:40 -05:00
|
|
|
.commit(region_constraints_snapshot);
|
2014-06-20 06:35:06 -04:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2014-06-20 06:35:06 -04:00
|
|
|
/// Execute `f` and commit the bindings
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fn commit_unconditionally<R, F>(&self, f: F) -> R where
|
|
|
|
F: FnOnce() -> R,
|
|
|
|
{
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("commit()");
|
2014-06-20 06:35:06 -04:00
|
|
|
let snapshot = self.start_snapshot();
|
|
|
|
let r = f();
|
|
|
|
self.commit_from(snapshot);
|
|
|
|
r
|
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2015-03-10 07:02:27 -04:00
|
|
|
/// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E> where
|
2015-03-10 07:02:27 -04:00
|
|
|
F: FnOnce(&CombinedSnapshot) -> Result<T, E>
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2015-03-10 07:02:27 -04:00
|
|
|
debug!("commit_if_ok()");
|
|
|
|
let snapshot = self.start_snapshot();
|
|
|
|
let r = f(&snapshot);
|
|
|
|
debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok());
|
|
|
|
match r {
|
|
|
|
Ok(_) => { self.commit_from(snapshot); }
|
2015-07-24 10:23:35 -04:00
|
|
|
Err(_) => { self.rollback_to("commit_if_ok -- error", snapshot); }
|
2015-03-10 07:02:27 -04:00
|
|
|
}
|
|
|
|
r
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2016-05-01 17:19:11 +03:00
|
|
|
// Execute `f` in a snapshot, and commit the bindings it creates
|
|
|
|
pub fn in_snapshot<T, F>(&self, f: F) -> T where
|
|
|
|
F: FnOnce(&CombinedSnapshot) -> T
|
|
|
|
{
|
|
|
|
debug!("in_snapshot()");
|
|
|
|
let snapshot = self.start_snapshot();
|
|
|
|
let r = f(&snapshot);
|
|
|
|
self.commit_from(snapshot);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2012-11-29 16:41:39 -08:00
|
|
|
/// Execute `f` then unroll any bindings it creates
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fn probe<R, F>(&self, f: F) -> R where
|
2014-12-14 07:17:23 -05:00
|
|
|
F: FnOnce(&CombinedSnapshot) -> R,
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("probe()");
|
2014-06-20 06:35:06 -04:00
|
|
|
let snapshot = self.start_snapshot();
|
2014-12-14 07:17:23 -05:00
|
|
|
let r = f(&snapshot);
|
2015-07-24 10:23:35 -04:00
|
|
|
self.rollback_to("probe", snapshot);
|
2014-06-20 06:35:06 -04:00
|
|
|
r
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
|
|
|
|
pub fn add_given(&self,
|
2017-05-11 15:05:00 +03:00
|
|
|
sub: ty::Region<'tcx>,
|
2014-08-27 21:46:52 -04:00
|
|
|
sup: ty::RegionVid)
|
|
|
|
{
|
2017-11-05 07:22:39 -05:00
|
|
|
self.borrow_region_constraints().add_given(sub, sup);
|
2014-08-27 21:46:52 -04:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2017-05-24 09:43:20 -04:00
|
|
|
pub fn can_sub<T>(&self,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
a: T,
|
|
|
|
b: T)
|
|
|
|
-> UnitResult<'tcx>
|
|
|
|
where T: at::ToTrace<'tcx>
|
2016-03-17 00:15:31 +02:00
|
|
|
{
|
2017-05-24 09:43:20 -04:00
|
|
|
let origin = &ObligationCause::dummy();
|
2016-03-17 00:15:31 +02:00
|
|
|
self.probe(|_| {
|
2017-05-24 09:43:20 -04:00
|
|
|
self.at(origin, param_env).sub(a, b).map(|InferOk { obligations: _, .. }| {
|
generalize type variables too
When we are generalizing a super/sub-type, we have to replace type
variables with a fresh variable (and not just region variables). So if
we know that `Box<?T> <: ?U`, for example, we instantiate `?U` with
`Box<?V>` and then relate `Box<?T>` to `Box<?V>` (and hence require that
`?T <: ?V`).
This change has some complex interactions, however:
First, the occurs check must be updated to detect constraints like `?T
<: ?U` and `?U <: Box<?T>`. If we're not careful, we'll create a
never-ending sequence of new variables. To address this, we add a second
unification set into `type_variables` that tracks type variables related
through **either** equality **or** subtyping, and use that during the
occurs-check.
Second, the "fudge regions if ok" code was expecting no new type
variables to be created. It must be updated to create new type variables
outside of the probe. This is relatively straight-forward under the new
scheme, since type variables are now independent from one another, and
any relations are moderated by pending subtype obliations and so forth.
This part would be tricky to backport though.
cc #18653
cc #40951
2017-04-11 17:17:58 -04:00
|
|
|
// Ignore obligations, since we are unrolling
|
|
|
|
// everything anyway.
|
2016-10-04 02:19:40 +03:00
|
|
|
})
|
2016-03-17 00:15:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-24 09:43:20 -04:00
|
|
|
pub fn can_eq<T>(&self,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
a: T,
|
|
|
|
b: T)
|
|
|
|
-> UnitResult<'tcx>
|
|
|
|
where T: at::ToTrace<'tcx>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2017-05-24 09:43:20 -04:00
|
|
|
let origin = &ObligationCause::dummy();
|
|
|
|
self.probe(|_| {
|
|
|
|
self.at(origin, param_env).eq(a, b).map(|InferOk { obligations: _, .. }| {
|
|
|
|
// Ignore obligations, since we are unrolling
|
|
|
|
// everything anyway.
|
|
|
|
})
|
2014-10-09 17:19:50 -04:00
|
|
|
})
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
pub fn sub_regions(&self,
|
|
|
|
origin: SubregionOrigin<'tcx>,
|
2017-04-20 04:45:53 -04:00
|
|
|
a: ty::Region<'tcx>,
|
|
|
|
b: ty::Region<'tcx>) {
|
2016-03-17 00:15:31 +02:00
|
|
|
debug!("sub_regions({:?} <: {:?})", a, b);
|
2017-11-05 07:22:39 -05:00
|
|
|
self.borrow_region_constraints().make_subregion(origin, a, b);
|
2014-12-14 07:17:23 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 05:34:34 -05:00
|
|
|
pub fn equality_predicate(&self,
|
2016-11-07 13:25:06 -05:00
|
|
|
cause: &ObligationCause<'tcx>,
|
2017-05-23 04:19:47 -04:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2014-12-13 05:34:34 -05:00
|
|
|
predicate: &ty::PolyEquatePredicate<'tcx>)
|
2016-03-29 20:06:42 -07:00
|
|
|
-> InferResult<'tcx, ()>
|
|
|
|
{
|
2015-03-10 07:02:27 -04:00
|
|
|
self.commit_if_ok(|snapshot| {
|
2014-12-13 05:34:34 -05:00
|
|
|
let (ty::EquatePredicate(a, b), skol_map) =
|
|
|
|
self.skolemize_late_bound_regions(predicate, snapshot);
|
2016-11-07 13:25:06 -05:00
|
|
|
let cause_span = cause.span;
|
2017-05-24 09:43:20 -04:00
|
|
|
let eqty_ok = self.at(cause, param_env).eq(b, a)?;
|
2016-11-07 13:25:06 -05:00
|
|
|
self.leak_check(false, cause_span, &skol_map, snapshot)?;
|
2016-04-20 19:51:56 -04:00
|
|
|
self.pop_skolemized(skol_map, snapshot);
|
|
|
|
Ok(eqty_ok.unit())
|
2014-12-13 05:34:34 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-09 21:47:09 -05:00
|
|
|
pub fn subtype_predicate(&self,
|
|
|
|
cause: &ObligationCause<'tcx>,
|
2017-05-23 04:19:47 -04:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-03-09 21:47:09 -05:00
|
|
|
predicate: &ty::PolySubtypePredicate<'tcx>)
|
|
|
|
-> Option<InferResult<'tcx, ()>>
|
|
|
|
{
|
|
|
|
// Subtle: it's ok to skip the binder here and resolve because
|
|
|
|
// `shallow_resolve` just ignores anything that is not a type
|
|
|
|
// variable, and because type variable's can't (at present, at
|
|
|
|
// least) capture any of the things bound by this binder.
|
|
|
|
//
|
|
|
|
// Really, there is no *particular* reason to do this
|
|
|
|
// `shallow_resolve` here except as a
|
|
|
|
// micro-optimization. Naturally I could not
|
|
|
|
// resist. -nmatsakis
|
|
|
|
let two_unbound_type_vars = {
|
|
|
|
let a = self.shallow_resolve(predicate.skip_binder().a);
|
|
|
|
let b = self.shallow_resolve(predicate.skip_binder().b);
|
|
|
|
a.is_ty_var() && b.is_ty_var()
|
|
|
|
};
|
|
|
|
|
|
|
|
if two_unbound_type_vars {
|
|
|
|
// Two unbound type variables? Can't make progress.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(self.commit_if_ok(|snapshot| {
|
|
|
|
let (ty::SubtypePredicate { a_is_expected, a, b}, skol_map) =
|
|
|
|
self.skolemize_late_bound_regions(predicate, snapshot);
|
|
|
|
|
|
|
|
let cause_span = cause.span;
|
2017-05-24 09:43:20 -04:00
|
|
|
let ok = self.at(cause, param_env).sub_exp(a_is_expected, a, b)?;
|
2017-03-09 21:47:09 -05:00
|
|
|
self.leak_check(false, cause_span, &skol_map, snapshot)?;
|
|
|
|
self.pop_skolemized(skol_map, snapshot);
|
|
|
|
Ok(ok.unit())
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:34:34 -05:00
|
|
|
pub fn region_outlives_predicate(&self,
|
2016-10-05 10:17:14 -04:00
|
|
|
cause: &traits::ObligationCause<'tcx>,
|
2016-08-25 23:58:52 +03:00
|
|
|
predicate: &ty::PolyRegionOutlivesPredicate<'tcx>)
|
2016-03-29 20:06:42 -07:00
|
|
|
-> UnitResult<'tcx>
|
|
|
|
{
|
2015-03-10 07:02:27 -04:00
|
|
|
self.commit_if_ok(|snapshot| {
|
2014-12-13 05:34:34 -05:00
|
|
|
let (ty::OutlivesPredicate(r_a, r_b), skol_map) =
|
|
|
|
self.skolemize_late_bound_regions(predicate, snapshot);
|
2016-10-19 10:45:49 -04:00
|
|
|
let origin =
|
|
|
|
SubregionOrigin::from_obligation_cause(cause,
|
|
|
|
|| RelateRegionParamBound(cause.span));
|
2016-03-17 00:15:31 +02:00
|
|
|
self.sub_regions(origin, r_b, r_a); // `b : a` ==> `a <= b`
|
2016-10-05 10:17:14 -04:00
|
|
|
self.leak_check(false, cause.span, &skol_map, snapshot)?;
|
2016-04-20 19:51:56 -04:00
|
|
|
Ok(self.pop_skolemized(skol_map, snapshot))
|
2014-12-13 05:34:34 -05:00
|
|
|
})
|
2014-12-12 06:37:42 -05:00
|
|
|
}
|
|
|
|
|
2016-11-28 10:08:08 -08:00
|
|
|
pub fn next_ty_var_id(&self, diverging: bool, origin: TypeVariableOrigin) -> TyVid {
|
2014-07-22 07:46:36 -04:00
|
|
|
self.type_variables
|
2014-06-20 06:35:06 -04:00
|
|
|
.borrow_mut()
|
2016-11-28 10:08:08 -08:00
|
|
|
.new_var(diverging, origin, None)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2016-11-28 10:08:08 -08:00
|
|
|
pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
|
|
|
|
self.tcx.mk_var(self.next_ty_var_id(false, origin))
|
2014-10-24 21:14:37 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 10:08:08 -08:00
|
|
|
pub fn next_diverging_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
|
|
|
|
self.tcx.mk_var(self.next_ty_var_id(true, origin))
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:29:57 -08:00
|
|
|
pub fn next_int_var_id(&self) -> IntVid {
|
2014-06-20 06:35:06 -04:00
|
|
|
self.int_unification_table
|
|
|
|
.borrow_mut()
|
|
|
|
.new_key(None)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:29:57 -08:00
|
|
|
pub fn next_float_var_id(&self) -> FloatVid {
|
2014-06-20 06:35:06 -04:00
|
|
|
self.float_unification_table
|
|
|
|
.borrow_mut()
|
|
|
|
.new_key(None)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2016-08-25 23:58:52 +03:00
|
|
|
pub fn next_region_var(&self, origin: RegionVariableOrigin)
|
2017-04-20 04:45:53 -04:00
|
|
|
-> ty::Region<'tcx> {
|
2017-11-05 07:22:39 -05:00
|
|
|
self.tcx.mk_region(ty::ReVar(self.borrow_region_constraints().new_region_var(origin)))
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2016-08-17 03:56:18 +03:00
|
|
|
/// Create a region inference variable for the given
|
|
|
|
/// region parameter definition.
|
|
|
|
pub fn region_var_for_def(&self,
|
|
|
|
span: Span,
|
|
|
|
def: &ty::RegionParameterDef)
|
2017-04-20 04:45:53 -04:00
|
|
|
-> ty::Region<'tcx> {
|
2017-07-29 17:19:57 +03:00
|
|
|
self.next_region_var(EarlyBoundRegion(span, def.name))
|
2016-08-17 03:56:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a type inference variable for the given
|
|
|
|
/// type parameter definition. The substitutions are
|
|
|
|
/// for actual parameters that may be referred to by
|
|
|
|
/// the default of this type parameter, if it exists.
|
|
|
|
/// E.g. `struct Foo<A, B, C = (A, B)>(...);` when
|
|
|
|
/// used in a path such as `Foo::<T, U>::new()` will
|
|
|
|
/// use an inference variable for `C` with `[T, U]`
|
|
|
|
/// as the substitutions for the default, `(T, U)`.
|
|
|
|
pub fn type_var_for_def(&self,
|
|
|
|
span: Span,
|
2017-01-25 22:01:11 +02:00
|
|
|
def: &ty::TypeParameterDef,
|
2016-10-09 11:36:12 -07:00
|
|
|
substs: &[Kind<'tcx>])
|
2016-08-17 03:56:18 +03:00
|
|
|
-> Ty<'tcx> {
|
2017-01-25 22:01:11 +02:00
|
|
|
let default = if def.has_default {
|
2017-04-24 15:20:46 +03:00
|
|
|
let default = self.tcx.type_of(def.def_id);
|
2017-01-25 22:01:11 +02:00
|
|
|
Some(type_variable::Default {
|
2016-08-17 03:56:18 +03:00
|
|
|
ty: default.subst_spanned(self.tcx, substs, Some(span)),
|
|
|
|
origin_span: span,
|
2017-01-25 22:01:11 +02:00
|
|
|
def_id: def.def_id
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2016-08-17 03:56:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
let ty_var_id = self.type_variables
|
|
|
|
.borrow_mut()
|
2016-11-28 10:08:08 -08:00
|
|
|
.new_var(false,
|
|
|
|
TypeVariableOrigin::TypeParameterDefinition(span, def.name),
|
|
|
|
default);
|
2016-08-17 03:56:18 +03:00
|
|
|
|
|
|
|
self.tcx.mk_var(ty_var_id)
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Given a set of generics defined on a type or impl, returns a substitution mapping each
|
|
|
|
/// type/region parameter to a fresh inference variable.
|
2016-08-08 23:39:49 +03:00
|
|
|
pub fn fresh_substs_for_item(&self,
|
|
|
|
span: Span,
|
|
|
|
def_id: DefId)
|
|
|
|
-> &'tcx Substs<'tcx> {
|
|
|
|
Substs::for_item(self.tcx, def_id, |def, _| {
|
2016-08-17 03:56:18 +03:00
|
|
|
self.region_var_for_def(span, def)
|
|
|
|
}, |def, substs| {
|
|
|
|
self.type_var_for_def(span, def, substs)
|
2016-08-08 23:39:49 +03:00
|
|
|
})
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:49:10 -04:00
|
|
|
/// True if errors have been reported since this infcx was
|
|
|
|
/// created. This is sometimes used as a heuristic to skip
|
|
|
|
/// reporting errors that often occur as a result of earlier
|
|
|
|
/// errors, but where it's hard to be 100% sure (e.g., unresolved
|
|
|
|
/// inference variables, regionck errors).
|
|
|
|
pub fn is_tainted_by_errors(&self) -> bool {
|
|
|
|
debug!("is_tainted_by_errors(err_count={}, err_count_on_creation={}, \
|
|
|
|
tainted_by_errors_flag={})",
|
|
|
|
self.tcx.sess.err_count(),
|
|
|
|
self.err_count_on_creation,
|
|
|
|
self.tainted_by_errors_flag.get());
|
|
|
|
|
|
|
|
if self.tcx.sess.err_count() > self.err_count_on_creation {
|
|
|
|
return true; // errors reported since this infcx was made
|
|
|
|
}
|
|
|
|
self.tainted_by_errors_flag.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the "tainted by errors" flag to true. We call this when we
|
|
|
|
/// observe an error from a prior pass.
|
|
|
|
pub fn set_tainted_by_errors(&self) {
|
|
|
|
debug!("set_tainted_by_errors()");
|
|
|
|
self.tainted_by_errors_flag.set(true)
|
2016-03-09 16:53:19 -05:00
|
|
|
}
|
|
|
|
|
2017-11-05 07:22:39 -05:00
|
|
|
/// Process the region constraints and report any errors that
|
|
|
|
/// result. After this, no more unification operations should be
|
|
|
|
/// done -- or the compiler will panic -- but it is legal to use
|
|
|
|
/// `resolve_type_vars_if_possible` as well as `fully_resolve`.
|
2015-04-18 11:23:14 -04:00
|
|
|
pub fn resolve_regions_and_report_errors(&self,
|
2017-04-10 00:00:08 -07:00
|
|
|
region_context: DefId,
|
2017-08-31 21:37:38 +03:00
|
|
|
region_map: ®ion::ScopeTree,
|
2017-04-10 00:00:08 -07:00
|
|
|
free_regions: &FreeRegionMap<'tcx>) {
|
2017-11-01 14:52:24 -04:00
|
|
|
assert!(self.is_tainted_by_errors() || self.region_obligations.borrow().is_empty(),
|
|
|
|
"region_obligations not empty: {:#?}",
|
|
|
|
self.region_obligations.borrow());
|
2017-11-01 13:41:07 -04:00
|
|
|
|
2017-11-05 14:37:55 -05:00
|
|
|
let region_rels = &RegionRelations::new(self.tcx,
|
|
|
|
region_context,
|
|
|
|
region_map,
|
|
|
|
free_regions);
|
|
|
|
let (var_origins, data) = self.region_constraints.borrow_mut()
|
|
|
|
.take()
|
|
|
|
.expect("regions already resolved")
|
|
|
|
.into_origins_and_data();
|
|
|
|
let (lexical_region_resolutions, errors) =
|
|
|
|
lexical_region_resolve::resolve(region_rels, var_origins, data);
|
2017-11-05 05:48:16 -05:00
|
|
|
|
|
|
|
let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
|
|
|
|
assert!(old_value.is_none());
|
2017-06-12 11:45:19 -07:00
|
|
|
|
2016-03-15 04:49:10 -04:00
|
|
|
if !self.is_tainted_by_errors() {
|
2016-03-09 16:53:19 -05:00
|
|
|
// As a heuristic, just skip reporting region errors
|
|
|
|
// altogether if other errors have been reported while
|
|
|
|
// this infcx was in use. This is totally hokey but
|
|
|
|
// otherwise we have a hard time separating legit region
|
|
|
|
// errors from silly ones.
|
2017-08-30 02:39:06 +03:00
|
|
|
self.report_region_errors(region_map, &errors); // see error_reporting module
|
2016-03-09 16:53:19 -05:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
|
2015-06-18 20:25:05 +03:00
|
|
|
self.resolve_type_vars_if_possible(&t).to_string()
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn tys_to_string(&self, ts: &[Ty<'tcx>]) -> String {
|
2014-06-21 03:39:03 -07:00
|
|
|
let tstrs: Vec<String> = ts.iter().map(|t| self.ty_to_string(*t)).collect();
|
2015-07-10 08:19:21 -04:00
|
|
|
format!("({})", tstrs.join(", "))
|
2013-07-18 17:20:58 -07:00
|
|
|
}
|
|
|
|
|
2015-04-21 18:59:58 +03:00
|
|
|
pub fn trait_ref_to_string(&self, t: &ty::TraitRef<'tcx>) -> String {
|
2015-06-18 20:25:05 +03:00
|
|
|
self.resolve_type_vars_if_possible(t).to_string()
|
2013-03-27 06:16:28 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn shallow_resolve(&self, typ: Ty<'tcx>) -> Ty<'tcx> {
|
2014-10-31 10:51:16 +02:00
|
|
|
match typ.sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyInfer(ty::TyVar(v)) => {
|
2014-12-01 12:27:27 -05:00
|
|
|
// Not entirely obvious: if `typ` is a type variable,
|
|
|
|
// it can be resolved to an int/float variable, which
|
|
|
|
// can then be recursively resolved, hence the
|
|
|
|
// recursion. Note though that we prevent type
|
|
|
|
// variables from unifying to other type variables
|
|
|
|
// directly (though they may be embedded
|
|
|
|
// structurally), and we prevent cycles in any case,
|
|
|
|
// so this recursion should always be of very limited
|
|
|
|
// depth.
|
2016-03-03 10:43:52 +01:00
|
|
|
self.type_variables.borrow_mut()
|
2014-10-09 17:19:50 -04:00
|
|
|
.probe(v)
|
2014-12-01 12:27:27 -05:00
|
|
|
.map(|t| self.shallow_resolve(t))
|
2014-10-09 17:19:50 -04:00
|
|
|
.unwrap_or(typ)
|
|
|
|
}
|
|
|
|
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyInfer(ty::IntVar(v)) => {
|
2015-02-15 15:16:45 -05:00
|
|
|
self.int_unification_table
|
|
|
|
.borrow_mut()
|
2015-03-10 15:32:25 -04:00
|
|
|
.probe(v)
|
|
|
|
.map(|v| v.to_type(self.tcx))
|
2014-10-09 17:19:50 -04:00
|
|
|
.unwrap_or(typ)
|
|
|
|
}
|
|
|
|
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyInfer(ty::FloatVar(v)) => {
|
2015-02-15 15:16:45 -05:00
|
|
|
self.float_unification_table
|
|
|
|
.borrow_mut()
|
2015-03-10 15:32:25 -04:00
|
|
|
.probe(v)
|
|
|
|
.map(|v| v.to_type(self.tcx))
|
2014-10-09 17:19:50 -04:00
|
|
|
.unwrap_or(typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
typ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 23:50:24 +03:00
|
|
|
pub fn resolve_type_vars_if_possible<T>(&self, value: &T) -> T
|
2015-11-18 09:38:57 +00:00
|
|
|
where T: TypeFoldable<'tcx>
|
2015-08-17 23:50:24 +03:00
|
|
|
{
|
2014-12-01 12:27:27 -05:00
|
|
|
/*!
|
|
|
|
* Where possible, replaces type/int/float variables in
|
|
|
|
* `value` with their final value. Note that region variables
|
|
|
|
* are unaffected. If a type variable has not been unified, it
|
|
|
|
* is left as is. This is an idempotent operation that does
|
|
|
|
* not affect inference state in any way and so you can do it
|
|
|
|
* at will.
|
|
|
|
*/
|
|
|
|
|
2015-08-17 23:50:24 +03:00
|
|
|
if !value.needs_infer() {
|
|
|
|
return value.clone(); // avoid duplicated subst-folding
|
|
|
|
}
|
2014-12-01 12:27:27 -05:00
|
|
|
let mut r = resolve::OpportunisticTypeResolver::new(self);
|
2014-12-01 10:11:59 -05:00
|
|
|
value.fold_with(&mut r)
|
2013-03-27 06:16:28 -04:00
|
|
|
}
|
|
|
|
|
2017-09-02 08:35:07 -04:00
|
|
|
/// Returns true if `T` contains unresolved type variables. In the
|
|
|
|
/// process of visiting `T`, this will resolve (where possible)
|
|
|
|
/// type variables in `T`, but it never constructs the final,
|
|
|
|
/// resolved type, so it's more efficient than
|
|
|
|
/// `resolve_type_vars_if_possible()`.
|
|
|
|
pub fn any_unresolved_type_vars<T>(&self, value: &T) -> bool
|
|
|
|
where T: TypeFoldable<'tcx>
|
|
|
|
{
|
|
|
|
let mut r = resolve::UnresolvedTypeFinder::new(self);
|
|
|
|
value.visit_with(&mut r)
|
|
|
|
}
|
|
|
|
|
2015-12-06 20:21:23 +02:00
|
|
|
pub fn resolve_type_and_region_vars_if_possible<T>(&self, value: &T) -> T
|
|
|
|
where T: TypeFoldable<'tcx>
|
|
|
|
{
|
|
|
|
let mut r = resolve::OpportunisticTypeAndRegionResolver::new(self);
|
|
|
|
value.fold_with(&mut r)
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:16:35 -07:00
|
|
|
pub fn fully_resolve<T:TypeFoldable<'tcx>>(&self, value: &T) -> FixupResult<T> {
|
2014-12-01 12:27:27 -05:00
|
|
|
/*!
|
|
|
|
* Attempts to resolve all type/region variables in
|
|
|
|
* `value`. Region inference must have been run already (e.g.,
|
|
|
|
* by calling `resolve_regions_and_report_errors`). If some
|
|
|
|
* variable was never unified, an `Err` results.
|
|
|
|
*
|
|
|
|
* This method is idempotent, but it not typically not invoked
|
|
|
|
* except during the writeback phase.
|
|
|
|
*/
|
|
|
|
|
|
|
|
resolve::fully_resolve(self, value)
|
|
|
|
}
|
|
|
|
|
2013-08-12 16:44:07 -07:00
|
|
|
// [Note-Type-error-reporting]
|
2015-06-11 16:21:46 -07:00
|
|
|
// An invariant is that anytime the expected or actual type is TyError (the special
|
2013-08-12 16:44:07 -07:00
|
|
|
// error type, meaning that an error occurred when typechecking this expression),
|
|
|
|
// this is a derived error. The error cascaded from another error (that was already
|
|
|
|
// reported), so it's not useful to display it to the user.
|
2016-07-08 22:51:29 +03:00
|
|
|
// The following methods implement this logic.
|
2015-06-11 16:21:46 -07:00
|
|
|
// They check if either the actual or expected type is TyError, and don't print the error
|
2013-08-12 16:44:07 -07:00
|
|
|
// in this case. The typechecker should only ever report type errors involving mismatched
|
2016-07-08 22:51:29 +03:00
|
|
|
// types using one of these methods, and should not call span_err directly for such
|
2013-08-12 16:44:07 -07:00
|
|
|
// errors.
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2016-07-21 16:07:08 +02:00
|
|
|
pub fn type_error_struct_with_diag<M>(&self,
|
|
|
|
sp: Span,
|
|
|
|
mk_diag: M,
|
|
|
|
actual_ty: Ty<'tcx>)
|
|
|
|
-> DiagnosticBuilder<'tcx>
|
|
|
|
where M: FnOnce(String) -> DiagnosticBuilder<'tcx>,
|
|
|
|
{
|
2014-12-01 10:11:59 -05:00
|
|
|
let actual_ty = self.resolve_type_vars_if_possible(&actual_ty);
|
2016-07-21 16:07:08 +02:00
|
|
|
debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty);
|
2013-04-17 19:36:59 -07:00
|
|
|
|
2015-06-11 16:21:46 -07:00
|
|
|
// Don't report an error if actual type is TyError.
|
2015-06-24 02:54:32 +03:00
|
|
|
if actual_ty.references_error() {
|
2015-12-23 19:27:20 +13:00
|
|
|
return self.tcx.sess.diagnostic().struct_dummy();
|
2013-04-17 19:36:59 -07:00
|
|
|
}
|
|
|
|
|
2016-07-21 16:07:08 +02:00
|
|
|
mk_diag(self.ty_to_string(actual_ty))
|
2013-04-17 19:36:59 -07:00
|
|
|
}
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
pub fn report_mismatched_types(&self,
|
2016-11-07 13:25:06 -05:00
|
|
|
cause: &ObligationCause<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
expected: Ty<'tcx>,
|
|
|
|
actual: Ty<'tcx>,
|
2016-12-02 16:36:03 -08:00
|
|
|
err: TypeError<'tcx>)
|
|
|
|
-> DiagnosticBuilder<'tcx> {
|
2016-11-07 13:25:06 -05:00
|
|
|
let trace = TypeTrace::types(cause, true, expected, actual);
|
2016-10-27 00:43:00 +02:00
|
|
|
self.report_and_explain_type_error(trace, &err)
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
|
|
|
|
2015-07-07 15:50:02 -07:00
|
|
|
pub fn report_conflicting_default_types(&self,
|
2015-07-12 21:43:13 -07:00
|
|
|
span: Span,
|
2016-11-07 13:25:06 -05:00
|
|
|
body_id: ast::NodeId,
|
2015-07-12 21:43:13 -07:00
|
|
|
expected: type_variable::Default<'tcx>,
|
|
|
|
actual: type_variable::Default<'tcx>) {
|
2015-07-07 15:50:02 -07:00
|
|
|
let trace = TypeTrace {
|
2016-11-07 13:25:06 -05:00
|
|
|
cause: ObligationCause::misc(span, body_id),
|
2015-09-06 21:51:58 +03:00
|
|
|
values: Types(ExpectedFound {
|
2015-07-12 20:33:17 -07:00
|
|
|
expected: expected.ty,
|
|
|
|
found: actual.ty
|
2015-07-07 15:50:02 -07:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2016-03-18 14:47:40 -04:00
|
|
|
self.report_and_explain_type_error(
|
|
|
|
trace,
|
2015-09-06 21:51:58 +03:00
|
|
|
&TypeError::TyParamDefaultMismatch(ExpectedFound {
|
2017-07-03 11:19:51 -07:00
|
|
|
expected,
|
2015-07-07 16:06:35 -07:00
|
|
|
found: actual
|
2016-03-18 14:47:40 -04:00
|
|
|
}))
|
|
|
|
.emit();
|
2015-07-07 15:50:02 -07:00
|
|
|
}
|
|
|
|
|
2014-11-12 14:11:22 -05:00
|
|
|
pub fn replace_late_bound_regions_with_fresh_var<T>(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
lbrct: LateBoundRegionConversionTime,
|
2014-12-12 11:28:35 -05:00
|
|
|
value: &ty::Binder<T>)
|
2017-04-20 04:45:53 -04:00
|
|
|
-> (T, FxHashMap<ty::BoundRegion, ty::Region<'tcx>>)
|
2015-06-18 08:51:23 +03:00
|
|
|
where T : TypeFoldable<'tcx>
|
2014-11-05 22:06:04 -05:00
|
|
|
{
|
2015-09-06 21:51:58 +03:00
|
|
|
self.tcx.replace_late_bound_regions(
|
2014-11-15 16:47:59 -05:00
|
|
|
value,
|
2015-01-07 14:05:32 -05:00
|
|
|
|br| self.next_region_var(LateBoundRegion(span, br, lbrct)))
|
2012-11-29 16:41:39 -08:00
|
|
|
}
|
2015-01-03 04:40:33 -05:00
|
|
|
|
2016-05-21 08:16:07 -04:00
|
|
|
/// Given a higher-ranked projection predicate like:
|
|
|
|
///
|
|
|
|
/// for<'a> <T as Fn<&'a u32>>::Output = &'a u32
|
|
|
|
///
|
|
|
|
/// and a target trait-ref like:
|
|
|
|
///
|
|
|
|
/// <T as Fn<&'x u32>>
|
|
|
|
///
|
|
|
|
/// find a substitution `S` for the higher-ranked regions (here,
|
|
|
|
/// `['a => 'x]`) such that the predicate matches the trait-ref,
|
|
|
|
/// and then return the value (here, `&'a u32`) but with the
|
|
|
|
/// substitution applied (hence, `&'x u32`).
|
|
|
|
///
|
|
|
|
/// See `higher_ranked_match` in `higher_ranked/mod.rs` for more
|
|
|
|
/// details.
|
|
|
|
pub fn match_poly_projection_predicate(&self,
|
2016-11-07 13:25:06 -05:00
|
|
|
cause: ObligationCause<'tcx>,
|
2017-05-23 04:19:47 -04:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2016-05-21 08:16:07 -04:00
|
|
|
match_a: ty::PolyProjectionPredicate<'tcx>,
|
|
|
|
match_b: ty::TraitRef<'tcx>)
|
2016-05-21 08:29:50 -04:00
|
|
|
-> InferResult<'tcx, HrMatchResult<Ty<'tcx>>>
|
2016-05-21 08:16:07 -04:00
|
|
|
{
|
2017-07-11 10:33:09 -04:00
|
|
|
let match_pair = match_a.map_bound(|p| (p.projection_ty.trait_ref(self.tcx), p.ty));
|
2016-05-21 08:16:07 -04:00
|
|
|
let trace = TypeTrace {
|
2017-07-03 11:19:51 -07:00
|
|
|
cause,
|
2017-07-11 10:33:09 -04:00
|
|
|
values: TraitRefs(ExpectedFound::new(true, match_pair.skip_binder().0, match_b))
|
2016-05-21 08:16:07 -04:00
|
|
|
};
|
|
|
|
|
2017-05-23 04:19:47 -04:00
|
|
|
let mut combine = self.combine_fields(trace, param_env);
|
2017-07-29 17:19:57 +03:00
|
|
|
let result = combine.higher_ranked_match(&match_pair, &match_b, true)?;
|
2016-05-21 08:29:50 -04:00
|
|
|
Ok(InferOk { value: result, obligations: combine.obligations })
|
2016-05-21 08:16:07 -04:00
|
|
|
}
|
|
|
|
|
2017-11-05 06:34:22 -05:00
|
|
|
/// See `verify_generic_bound` method in `region_constraints`
|
2015-01-03 04:40:33 -05:00
|
|
|
pub fn verify_generic_bound(&self,
|
|
|
|
origin: SubregionOrigin<'tcx>,
|
|
|
|
kind: GenericKind<'tcx>,
|
2017-04-20 04:45:53 -04:00
|
|
|
a: ty::Region<'tcx>,
|
2016-08-25 23:58:52 +03:00
|
|
|
bound: VerifyBound<'tcx>) {
|
2015-06-18 20:25:05 +03:00
|
|
|
debug!("verify_generic_bound({:?}, {:?} <: {:?})",
|
|
|
|
kind,
|
|
|
|
a,
|
2015-08-07 10:33:18 -04:00
|
|
|
bound);
|
2015-01-03 04:40:33 -05:00
|
|
|
|
2017-11-05 07:22:39 -05:00
|
|
|
self.borrow_region_constraints().verify_generic_bound(origin, kind, a, bound);
|
2015-01-03 04:40:33 -05:00
|
|
|
}
|
2015-01-05 21:50:01 -05:00
|
|
|
|
2017-05-23 04:19:47 -04:00
|
|
|
pub fn type_moves_by_default(&self,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
span: Span)
|
|
|
|
-> bool {
|
2015-06-30 02:18:03 -07:00
|
|
|
let ty = self.resolve_type_vars_if_possible(&ty);
|
2017-06-09 10:55:16 +03:00
|
|
|
// Even if the type may have no inference variables, during
|
|
|
|
// type-checking closure types are in local tables only.
|
|
|
|
if !self.in_progress_tables.is_some() || !ty.has_closure_types() {
|
|
|
|
if let Some((param_env, ty)) = self.tcx.lift_to_global(&(param_env, ty)) {
|
2017-05-23 04:19:47 -04:00
|
|
|
return ty.moves_by_default(self.tcx.global_tcx(), param_env, span);
|
2016-04-29 06:00:23 +03:00
|
|
|
}
|
2015-08-17 20:26:22 +03:00
|
|
|
}
|
2016-04-29 06:00:23 +03:00
|
|
|
|
2016-11-25 21:21:03 -07:00
|
|
|
let copy_def_id = self.tcx.require_lang_item(lang_items::CopyTraitLangItem);
|
2016-11-13 19:42:15 -07:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
// this can get called from typeck (by euv), and moves_by_default
|
|
|
|
// rightly refuses to work with inference variables, but
|
|
|
|
// moves_by_default has a cache, which we want to use in other
|
|
|
|
// cases.
|
2017-05-23 04:19:47 -04:00
|
|
|
!traits::type_known_to_meet_bound(self, param_env, ty, copy_def_id, span)
|
2015-06-30 02:18:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closure_kind(&self,
|
2015-08-16 06:32:28 -04:00
|
|
|
def_id: DefId)
|
2015-06-30 02:18:03 -07:00
|
|
|
-> Option<ty::ClosureKind>
|
|
|
|
{
|
2017-06-09 10:55:16 +03:00
|
|
|
if let Some(tables) = self.in_progress_tables {
|
2017-01-26 02:41:06 +02:00
|
|
|
if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
|
2017-08-07 15:50:13 +02:00
|
|
|
let hir_id = self.tcx.hir.node_to_hir_id(id);
|
2017-05-24 16:23:02 +01:00
|
|
|
return tables.borrow()
|
2017-08-10 16:10:04 +02:00
|
|
|
.closure_kinds()
|
|
|
|
.get(hir_id)
|
2017-05-24 16:23:02 +01:00
|
|
|
.cloned()
|
|
|
|
.map(|(kind, _)| kind);
|
2017-01-06 21:54:24 +02:00
|
|
|
}
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
2017-01-06 21:54:24 +02:00
|
|
|
|
|
|
|
// During typeck, ALL closures are local. But afterwards,
|
|
|
|
// during trans, we see closure ids from other traits.
|
|
|
|
// That may require loading the closure data out of the
|
|
|
|
// cstore.
|
|
|
|
Some(self.tcx.closure_kind(def_id))
|
2015-06-30 02:18:03 -07:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:12:29 +03:00
|
|
|
/// Obtain the signature of a function or closure.
|
|
|
|
/// For closures, unlike `tcx.fn_sig(def_id)`, this method will
|
|
|
|
/// work during the type-checking of the enclosing function and
|
|
|
|
/// return the closure signature in its partially inferred state.
|
|
|
|
pub fn fn_sig(&self, def_id: DefId) -> ty::PolyFnSig<'tcx> {
|
2017-06-09 10:55:16 +03:00
|
|
|
if let Some(tables) = self.in_progress_tables {
|
2017-01-26 02:41:06 +02:00
|
|
|
if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
|
2017-08-07 15:50:13 +02:00
|
|
|
let hir_id = self.tcx.hir.node_to_hir_id(id);
|
2017-08-10 16:10:04 +02:00
|
|
|
if let Some(&ty) = tables.borrow().closure_tys().get(hir_id) {
|
2017-02-13 23:26:32 +02:00
|
|
|
return ty;
|
2017-01-06 21:54:24 +02:00
|
|
|
}
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-30 02:18:03 -07:00
|
|
|
|
2017-05-13 13:12:29 +03:00
|
|
|
self.tcx.fn_sig(def_id)
|
2015-06-30 02:18:03 -07:00
|
|
|
}
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
|
|
pub fn generator_sig(&self, def_id: DefId) -> Option<ty::PolyGenSig<'tcx>> {
|
|
|
|
if let Some(tables) = self.in_progress_tables {
|
|
|
|
if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
|
2017-08-14 20:12:13 -07:00
|
|
|
let hir_id = self.tcx.hir.node_to_hir_id(id);
|
|
|
|
if let Some(&ty) = tables.borrow().generator_sigs().get(hir_id) {
|
2016-12-26 14:34:03 +01:00
|
|
|
return ty.map(|t| ty::Binder(t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.tcx.generator_sig(def_id)
|
|
|
|
}
|
2017-11-02 06:06:09 -04:00
|
|
|
|
|
|
|
/// Normalizes associated types in `value`, potentially returning
|
|
|
|
/// new obligations that must further be processed.
|
|
|
|
pub fn partially_normalize_associated_types_in<T>(&self,
|
2017-11-05 07:22:39 -05:00
|
|
|
span: Span,
|
|
|
|
body_id: ast::NodeId,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
value: &T)
|
|
|
|
-> InferOk<'tcx, T>
|
2017-11-02 06:06:09 -04:00
|
|
|
where T : TypeFoldable<'tcx>
|
|
|
|
{
|
|
|
|
debug!("partially_normalize_associated_types_in(value={:?})", value);
|
|
|
|
let mut selcx = traits::SelectionContext::new(self);
|
|
|
|
let cause = ObligationCause::misc(span, body_id);
|
|
|
|
let traits::Normalized { value, obligations } =
|
|
|
|
traits::normalize(&mut selcx, param_env, cause, value);
|
|
|
|
debug!("partially_normalize_associated_types_in: result={:?} predicates={:?}",
|
|
|
|
value,
|
|
|
|
obligations);
|
|
|
|
InferOk { value, obligations }
|
|
|
|
}
|
2017-11-05 07:22:39 -05:00
|
|
|
|
|
|
|
fn borrow_region_constraints(&self) -> RefMut<'_, RegionConstraintCollector<'tcx>> {
|
|
|
|
RefMut::map(
|
|
|
|
self.region_constraints.borrow_mut(),
|
|
|
|
|c| c.as_mut().expect("region constraints already solved"))
|
|
|
|
}
|
2013-05-02 14:32:37 -04:00
|
|
|
}
|
2012-11-29 16:41:39 -08:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TypeTrace<'tcx> {
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span(&self) -> Span {
|
2016-11-07 13:25:06 -05:00
|
|
|
self.cause.span
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
2014-11-15 20:30:33 -05:00
|
|
|
|
2016-11-07 13:25:06 -05:00
|
|
|
pub fn types(cause: &ObligationCause<'tcx>,
|
2015-01-16 05:48:15 +02:00
|
|
|
a_is_expected: bool,
|
|
|
|
a: Ty<'tcx>,
|
|
|
|
b: Ty<'tcx>)
|
|
|
|
-> TypeTrace<'tcx> {
|
|
|
|
TypeTrace {
|
2016-11-07 13:25:06 -05:00
|
|
|
cause: cause.clone(),
|
2016-03-17 00:15:31 +02:00
|
|
|
values: Types(ExpectedFound::new(a_is_expected, a, b))
|
2015-01-16 05:48:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn dummy(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> TypeTrace<'tcx> {
|
2014-11-15 20:30:33 -05:00
|
|
|
TypeTrace {
|
2016-11-07 13:25:06 -05:00
|
|
|
cause: ObligationCause::dummy(),
|
2015-09-06 21:51:58 +03:00
|
|
|
values: Types(ExpectedFound {
|
2014-12-25 07:20:48 -05:00
|
|
|
expected: tcx.types.err,
|
|
|
|
found: tcx.types.err,
|
2014-11-15 20:30:33 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 08:51:23 +03:00
|
|
|
impl<'tcx> fmt::Debug for TypeTrace<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-11-07 13:25:06 -05:00
|
|
|
write!(f, "TypeTrace({:?})", self.cause)
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> SubregionOrigin<'tcx> {
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span(&self) -> Span {
|
2013-05-23 21:37:37 -04:00
|
|
|
match *self {
|
2014-04-22 02:21:52 +03:00
|
|
|
Subtype(ref a) => a.span(),
|
2013-06-25 12:04:50 -04:00
|
|
|
InfStackClosure(a) => a,
|
2013-05-23 21:37:37 -04:00
|
|
|
InvokeClosure(a) => a,
|
|
|
|
DerefPointer(a) => a,
|
2014-02-07 14:43:48 -05:00
|
|
|
FreeVariable(a, _) => a,
|
2013-05-23 21:37:37 -04:00
|
|
|
IndexSlice(a) => a,
|
|
|
|
RelateObjectBound(a) => a,
|
2014-11-15 17:25:05 -05:00
|
|
|
RelateParamBound(a, _) => a,
|
2014-08-27 21:46:52 -04:00
|
|
|
RelateRegionParamBound(a) => a,
|
|
|
|
RelateDefaultParamBound(a, _) => a,
|
2013-05-23 21:37:37 -04:00
|
|
|
Reborrow(a) => a,
|
2014-02-07 14:43:48 -05:00
|
|
|
ReborrowUpvar(a, _) => a,
|
2015-08-07 10:28:51 -04:00
|
|
|
DataBorrowed(_, a) => a,
|
2013-05-23 21:37:37 -04:00
|
|
|
ReferenceOutlivesReferent(_, a) => a,
|
2015-08-07 10:28:51 -04:00
|
|
|
ParameterInScope(_, a) => a,
|
2014-08-27 21:46:52 -04:00
|
|
|
ExprTypeIsNotInScope(_, a) => a,
|
2013-05-23 21:37:37 -04:00
|
|
|
BindingTypeIsNotValidAtDecl(a) => a,
|
|
|
|
CallRcvr(a) => a,
|
|
|
|
CallArg(a) => a,
|
|
|
|
CallReturn(a) => a,
|
2015-02-12 12:48:54 -05:00
|
|
|
Operand(a) => a,
|
2013-05-23 21:37:37 -04:00
|
|
|
AddrOf(a) => a,
|
|
|
|
AutoBorrow(a) => a,
|
2014-10-27 12:55:16 +01:00
|
|
|
SafeDestructor(a) => a,
|
2016-10-05 10:17:14 -04:00
|
|
|
CompareImplMethodObligation { span, .. } => span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 10:45:49 -04:00
|
|
|
pub fn from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>,
|
|
|
|
default: F)
|
|
|
|
-> Self
|
2016-10-05 10:17:14 -04:00
|
|
|
where F: FnOnce() -> Self
|
|
|
|
{
|
|
|
|
match cause.code {
|
|
|
|
traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) =>
|
|
|
|
SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span),
|
|
|
|
|
|
|
|
traits::ObligationCauseCode::CompareImplMethodObligation { item_name,
|
|
|
|
impl_item_def_id,
|
2017-11-01 14:23:30 -04:00
|
|
|
trait_item_def_id, } =>
|
2016-10-05 10:17:14 -04:00
|
|
|
SubregionOrigin::CompareImplMethodObligation {
|
|
|
|
span: cause.span,
|
2017-07-03 11:19:51 -07:00
|
|
|
item_name,
|
|
|
|
impl_item_def_id,
|
|
|
|
trait_item_def_id,
|
2016-10-05 10:17:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_ => default(),
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 07:02:27 -04:00
|
|
|
impl RegionVariableOrigin {
|
2013-08-31 18:13:04 +02:00
|
|
|
pub fn span(&self) -> Span {
|
2013-05-23 21:37:37 -04:00
|
|
|
match *self {
|
|
|
|
MiscVariable(a) => a,
|
|
|
|
PatternRegion(a) => a,
|
|
|
|
AddrOfRegion(a) => a,
|
|
|
|
Autoref(a) => a,
|
2015-03-10 07:02:27 -04:00
|
|
|
Coercion(a) => a,
|
2017-01-06 14:35:23 -05:00
|
|
|
EarlyBoundRegion(a, ..) => a,
|
2016-08-26 19:23:42 +03:00
|
|
|
LateBoundRegion(a, ..) => a,
|
2016-06-21 18:08:13 -04:00
|
|
|
BoundRegionInCoherence(_) => syntax_pos::DUMMY_SP,
|
2014-02-07 14:43:48 -05:00
|
|
|
UpvarRegion(_, a) => a
|
2013-05-23 21:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 19:38:17 +03:00
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for ValuePairs<'tcx> {
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
match *self {
|
|
|
|
ValuePairs::Types(ref ef) => {
|
|
|
|
ValuePairs::Types(ef.fold_with(folder))
|
|
|
|
}
|
|
|
|
ValuePairs::TraitRefs(ref ef) => {
|
|
|
|
ValuePairs::TraitRefs(ef.fold_with(folder))
|
|
|
|
}
|
|
|
|
ValuePairs::PolyTraitRefs(ref ef) => {
|
|
|
|
ValuePairs::PolyTraitRefs(ef.fold_with(folder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
|
|
|
match *self {
|
|
|
|
ValuePairs::Types(ref ef) => ef.visit_with(visitor),
|
|
|
|
ValuePairs::TraitRefs(ref ef) => ef.visit_with(visitor),
|
|
|
|
ValuePairs::PolyTraitRefs(ref ef) => ef.visit_with(visitor),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeFoldable<'tcx> for TypeTrace<'tcx> {
|
|
|
|
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
|
|
|
|
TypeTrace {
|
2016-11-07 13:25:06 -05:00
|
|
|
cause: self.cause.fold_with(folder),
|
2016-07-16 19:38:17 +03:00
|
|
|
values: self.values.fold_with(folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
|
2016-11-07 13:25:06 -05:00
|
|
|
self.cause.visit_with(visitor) || self.values.visit_with(visitor)
|
2016-07-16 19:38:17 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-01 13:41:07 -04:00
|
|
|
|
|
|
|
impl<'tcx> fmt::Debug for RegionObligation<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "RegionObligation(sub_region={:?}, sup_type={:?})",
|
|
|
|
self.sub_region,
|
|
|
|
self.sup_type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|