2014-09-12 09:53:35 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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 19:23:16 -06:00
|
|
|
//! Trait Resolution. See the Book for more.
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::SelectionError::*;
|
|
|
|
pub use self::FulfillmentErrorCode::*;
|
|
|
|
pub use self::Vtable::*;
|
|
|
|
pub use self::ObligationCauseCode::*;
|
|
|
|
|
2015-04-18 10:23:14 -05:00
|
|
|
use middle::free_region::FreeRegionMap;
|
2014-09-12 09:53:35 -05:00
|
|
|
use middle::subst;
|
2015-06-23 18:54:32 -05:00
|
|
|
use middle::ty::{self, HasTypeFlags, Ty};
|
2015-02-13 18:51:43 -06:00
|
|
|
use middle::ty_fold::TypeFoldable;
|
2015-02-20 05:21:46 -06:00
|
|
|
use middle::infer::{self, fixup_err_to_string, InferCtxt};
|
2014-12-17 13:16:28 -06:00
|
|
|
use std::rc::Rc;
|
2014-09-12 09:53:35 -05:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap::{Span, DUMMY_SP};
|
|
|
|
|
2014-12-06 10:39:25 -06:00
|
|
|
pub use self::error_reporting::report_fulfillment_errors;
|
2015-03-20 05:48:40 -05:00
|
|
|
pub use self::error_reporting::report_overflow_error;
|
2015-04-14 18:57:29 -05:00
|
|
|
pub use self::error_reporting::report_selection_error;
|
2015-01-02 03:01:30 -06:00
|
|
|
pub use self::error_reporting::suggest_new_overflow_limit;
|
Fix orphan checking (cc #19470). (This is not a complete fix of #19470 because of the backwards compatibility feature gate.)
This is a [breaking-change]. The new rules require that, for an impl of a trait defined
in some other crate, two conditions must hold:
1. Some type must be local.
2. Every type parameter must appear "under" some local type.
Here are some examples that are legal:
```rust
struct MyStruct<T> { ... }
// Here `T` appears "under' `MyStruct`.
impl<T> Clone for MyStruct<T> { }
// Here `T` appears "under' `MyStruct` as well. Note that it also appears
// elsewhere.
impl<T> Iterator<T> for MyStruct<T> { }
```
Here is an illegal example:
```rust
// Here `U` does not appear "under" `MyStruct` or any other local type.
// We call `U` "uncovered".
impl<T,U> Iterator<U> for MyStruct<T> { }
```
There are a couple of ways to rewrite this last example so that it is
legal:
1. In some cases, the uncovered type parameter (here, `U`) should be converted
into an associated type. This is however a non-local change that requires access
to the original trait. Also, associated types are not fully baked.
2. Add `U` as a type parameter of `MyStruct`:
```rust
struct MyStruct<T,U> { ... }
impl<T,U> Iterator<U> for MyStruct<T,U> { }
```
3. Create a newtype wrapper for `U`
```rust
impl<T,U> Iterator<Wrapper<U>> for MyStruct<T,U> { }
```
Because associated types are not fully baked, which in the case of the
`Hash` trait makes adhering to this rule impossible, you can
temporarily disable this rule in your crate by using
`#![feature(old_orphan_check)]`. Note that the `old_orphan_check`
feature will be removed before 1.0 is released.
2014-12-26 02:30:51 -06:00
|
|
|
pub use self::coherence::orphan_check;
|
2015-02-12 11:42:02 -06:00
|
|
|
pub use self::coherence::overlapping_impls;
|
Fix orphan checking (cc #19470). (This is not a complete fix of #19470 because of the backwards compatibility feature gate.)
This is a [breaking-change]. The new rules require that, for an impl of a trait defined
in some other crate, two conditions must hold:
1. Some type must be local.
2. Every type parameter must appear "under" some local type.
Here are some examples that are legal:
```rust
struct MyStruct<T> { ... }
// Here `T` appears "under' `MyStruct`.
impl<T> Clone for MyStruct<T> { }
// Here `T` appears "under' `MyStruct` as well. Note that it also appears
// elsewhere.
impl<T> Iterator<T> for MyStruct<T> { }
```
Here is an illegal example:
```rust
// Here `U` does not appear "under" `MyStruct` or any other local type.
// We call `U` "uncovered".
impl<T,U> Iterator<U> for MyStruct<T> { }
```
There are a couple of ways to rewrite this last example so that it is
legal:
1. In some cases, the uncovered type parameter (here, `U`) should be converted
into an associated type. This is however a non-local change that requires access
to the original trait. Also, associated types are not fully baked.
2. Add `U` as a type parameter of `MyStruct`:
```rust
struct MyStruct<T,U> { ... }
impl<T,U> Iterator<U> for MyStruct<T,U> { }
```
3. Create a newtype wrapper for `U`
```rust
impl<T,U> Iterator<Wrapper<U>> for MyStruct<T,U> { }
```
Because associated types are not fully baked, which in the case of the
`Hash` trait makes adhering to this rule impossible, you can
temporarily disable this rule in your crate by using
`#![feature(old_orphan_check)]`. Note that the `old_orphan_check`
feature will be removed before 1.0 is released.
2014-12-26 02:30:51 -06:00
|
|
|
pub use self::coherence::OrphanCheckErr;
|
Add a (somewhat hacky) cache to the tcx that tracks "global" trait refs
that are known to have been satisfied *somewhere*. This means that if
one fn finds that `SomeType: Foo`, then every other fn can just consider
that to hold.
Unfortunately, there are some complications:
1. If `SomeType: Foo` includes dependent conditions, those conditions
may trigger an error. This error will be repored in the first fn
where `SomeType: Foo` is evaluated, but not in the other fns, which
can lead to uneven error reporting (which is sometimes confusing).
2. This kind of caching can be unsound in the presence of
unsatisfiable where clauses. For example, suppose that the first fn
has a where-clause like `i32: Bar<u32>`, which in fact does not
hold. This will "fool" trait resolution into thinking that `i32:
Bar<u32>` holds. This is ok currently, because it means that the
first fn can never be calle (since its where clauses cannot be
satisfied), but if the first fn's successful resolution is cached, it
can allow other fns to compile that should not. This problem is fixed
in the next commit.
2015-06-09 16:02:18 -05:00
|
|
|
pub use self::fulfill::{FulfillmentContext, FulfilledPredicates, RegionObligation};
|
2014-12-17 13:16:28 -06:00
|
|
|
pub use self::project::MismatchedProjectionTypes;
|
2014-12-30 16:42:02 -06:00
|
|
|
pub use self::project::normalize;
|
|
|
|
pub use self::project::Normalized;
|
2014-12-15 20:11:09 -06:00
|
|
|
pub use self::object_safety::is_object_safe;
|
|
|
|
pub use self::object_safety::object_safety_violations;
|
|
|
|
pub use self::object_safety::ObjectSafetyViolation;
|
|
|
|
pub use self::object_safety::MethodViolationCode;
|
2015-03-18 14:26:38 -05:00
|
|
|
pub use self::object_safety::is_vtable_safe_method;
|
2014-09-12 09:53:35 -05:00
|
|
|
pub use self::select::SelectionContext;
|
2014-09-17 15:12:02 -05:00
|
|
|
pub use self::select::SelectionCache;
|
2014-10-17 07:51:43 -05:00
|
|
|
pub use self::select::{MethodMatchResult, MethodMatched, MethodAmbiguous, MethodDidNotMatch};
|
|
|
|
pub use self::select::{MethodMatchedData}; // intentionally don't export variants
|
2014-12-07 10:10:48 -06:00
|
|
|
pub use self::util::elaborate_predicates;
|
2014-12-20 08:15:52 -06:00
|
|
|
pub use self::util::get_vtable_index_of_object_method;
|
2014-12-17 13:16:28 -06:00
|
|
|
pub use self::util::trait_ref_for_builtin_bound;
|
2015-04-14 18:57:29 -05:00
|
|
|
pub use self::util::predicate_for_trait_def;
|
2014-09-12 09:53:35 -05:00
|
|
|
pub use self::util::supertraits;
|
|
|
|
pub use self::util::Supertraits;
|
2015-03-26 14:51:11 -05:00
|
|
|
pub use self::util::supertrait_def_ids;
|
|
|
|
pub use self::util::SupertraitDefIds;
|
2014-12-06 00:30:41 -06:00
|
|
|
pub use self::util::transitive_bounds;
|
2014-12-20 08:15:52 -06:00
|
|
|
pub use self::util::upcast;
|
2014-09-12 09:53:35 -05:00
|
|
|
|
|
|
|
mod coherence;
|
2014-12-06 10:39:25 -06:00
|
|
|
mod error_reporting;
|
2014-09-12 09:53:35 -05:00
|
|
|
mod fulfill;
|
2014-12-17 13:16:28 -06:00
|
|
|
mod project;
|
2014-12-15 20:11:09 -06:00
|
|
|
mod object_safety;
|
2014-09-12 09:53:35 -05:00
|
|
|
mod select;
|
|
|
|
mod util;
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// An `Obligation` represents some trait reference (e.g. `int:Eq`) for
|
|
|
|
/// which the vtable must be found. The process of finding a vtable is
|
|
|
|
/// called "resolving" the `Obligation`. This process consists of
|
|
|
|
/// either identifying an `impl` (e.g., `impl Eq for int`) that
|
|
|
|
/// provides the required vtable, or else finding a bound that is in
|
|
|
|
/// scope. The eventual result is usually a `Selection` (defined below).
|
2015-02-11 11:12:57 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2014-12-04 23:03:03 -06:00
|
|
|
pub struct Obligation<'tcx, T> {
|
2014-09-29 14:11:30 -05:00
|
|
|
pub cause: ObligationCause<'tcx>,
|
2015-03-25 19:06:52 -05:00
|
|
|
pub recursion_depth: usize,
|
2014-12-17 15:00:34 -06:00
|
|
|
pub predicate: T,
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>;
|
2014-12-17 13:16:28 -06:00
|
|
|
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
|
2014-12-04 23:03:03 -06:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Why did we incur this obligation? Used for error reporting.
|
2015-02-11 11:12:57 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub struct ObligationCause<'tcx> {
|
2014-09-12 09:53:35 -05:00
|
|
|
pub span: Span,
|
2014-12-05 00:59:17 -06:00
|
|
|
|
2014-12-11 03:35:51 -06:00
|
|
|
// The id of the fn body that triggered this obligation. This is
|
|
|
|
// used for region obligations to determine the precise
|
|
|
|
// environment in which the region obligation should be evaluated
|
|
|
|
// (in particular, closures can add new assumptions). See the
|
|
|
|
// field `region_obligations` of the `FulfillmentContext` for more
|
|
|
|
// information.
|
2014-12-06 00:30:41 -06:00
|
|
|
pub body_id: ast::NodeId,
|
2014-12-05 00:59:17 -06:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub code: ObligationCauseCode<'tcx>
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 11:12:57 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub enum ObligationCauseCode<'tcx> {
|
2014-09-12 09:53:35 -05:00
|
|
|
/// Not well classified or should be obvious from span.
|
|
|
|
MiscObligation,
|
|
|
|
|
|
|
|
/// In an impl of trait X for type Y, type Y must
|
|
|
|
/// also implement all supertraits of X.
|
|
|
|
ItemObligation(ast::DefId),
|
|
|
|
|
|
|
|
/// Obligation incurred due to an object cast.
|
2014-09-29 14:11:30 -05:00
|
|
|
ObjectCastObligation(/* Object type */ Ty<'tcx>),
|
2014-09-12 09:53:35 -05:00
|
|
|
|
|
|
|
/// Various cases where expressions must be sized/copy/etc:
|
|
|
|
AssignmentLhsSized, // L = X implies that L is Sized
|
|
|
|
StructInitializerSized, // S { ... } must be Sized
|
|
|
|
VariableType(ast::NodeId), // Type of each variable must be Sized
|
2014-10-18 15:12:02 -05:00
|
|
|
ReturnType, // Return type must be Sized
|
2014-09-12 09:53:35 -05:00
|
|
|
RepeatVec, // [T,..n] --> T must be Copy
|
|
|
|
|
2014-09-22 13:47:06 -05:00
|
|
|
// Captures of variable the given id by a closure (span is the
|
|
|
|
// span of the closure)
|
2014-12-07 10:10:48 -06:00
|
|
|
ClosureCapture(ast::NodeId, Span, ty::BuiltinBound),
|
2014-09-22 19:12:15 -05:00
|
|
|
|
|
|
|
// Types of fields (other than the last) in a struct must be sized.
|
|
|
|
FieldSized,
|
2014-11-07 15:26:26 -06:00
|
|
|
|
2014-12-06 10:39:25 -06:00
|
|
|
// static items must have `Sync` type
|
|
|
|
SharedStatic,
|
|
|
|
|
2015-01-14 15:43:17 -06:00
|
|
|
|
2014-12-17 13:16:28 -06:00
|
|
|
BuiltinDerivedObligation(DerivedObligationCause<'tcx>),
|
2014-12-06 10:39:25 -06:00
|
|
|
|
2014-12-17 13:16:28 -06:00
|
|
|
ImplDerivedObligation(DerivedObligationCause<'tcx>),
|
2015-01-14 15:43:17 -06:00
|
|
|
|
|
|
|
CompareImplMethodObligation,
|
2014-12-17 13:16:28 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 11:12:57 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2014-12-17 13:16:28 -06:00
|
|
|
pub struct DerivedObligationCause<'tcx> {
|
2014-12-30 07:59:33 -06:00
|
|
|
/// The trait reference of the parent obligation that led to the
|
|
|
|
/// current obligation. Note that only trait obligations lead to
|
|
|
|
/// derived obligations, so we just store the trait reference here
|
|
|
|
/// directly.
|
2014-12-17 13:16:28 -06:00
|
|
|
parent_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
|
|
|
|
/// The parent trait had this cause
|
|
|
|
parent_code: Rc<ObligationCauseCode<'tcx>>
|
2014-09-22 13:47:06 -05:00
|
|
|
}
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2015-06-02 18:14:45 -05:00
|
|
|
pub type Obligations<'tcx, O> = Vec<Obligation<'tcx, O>>;
|
|
|
|
pub type PredicateObligations<'tcx> = Vec<PredicateObligation<'tcx>>;
|
|
|
|
pub type TraitObligations<'tcx> = Vec<TraitObligation<'tcx>>;
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
pub type Selection<'tcx> = Vtable<'tcx, PredicateObligation<'tcx>>;
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Clone,Debug)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub enum SelectionError<'tcx> {
|
2014-09-12 09:53:35 -05:00
|
|
|
Unimplemented,
|
2014-12-17 13:16:28 -06:00
|
|
|
OutputTypeParameterMismatch(ty::PolyTraitRef<'tcx>,
|
|
|
|
ty::PolyTraitRef<'tcx>,
|
2014-12-11 12:37:37 -06:00
|
|
|
ty::type_err<'tcx>),
|
2015-04-14 18:57:29 -05:00
|
|
|
TraitNotObjectSafe(ast::DefId),
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub struct FulfillmentError<'tcx> {
|
2014-12-07 10:10:48 -06:00
|
|
|
pub obligation: PredicateObligation<'tcx>,
|
2014-09-29 14:11:30 -05:00
|
|
|
pub code: FulfillmentErrorCode<'tcx>
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Clone)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub enum FulfillmentErrorCode<'tcx> {
|
|
|
|
CodeSelectionError(SelectionError<'tcx>),
|
2014-12-17 13:16:28 -06:00
|
|
|
CodeProjectionError(MismatchedProjectionTypes<'tcx>),
|
2014-09-11 00:07:49 -05:00
|
|
|
CodeAmbiguity,
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// When performing resolution, it is typically the case that there
|
|
|
|
/// can be one of three outcomes:
|
|
|
|
///
|
|
|
|
/// - `Ok(Some(r))`: success occurred with result `r`
|
|
|
|
/// - `Ok(None)`: could not definitely determine anything, usually due
|
|
|
|
/// to inconclusive type inference.
|
|
|
|
/// - `Err(e)`: error `e` occurred
|
2014-09-29 14:11:30 -05:00
|
|
|
pub type SelectionResult<'tcx, T> = Result<Option<T>, SelectionError<'tcx>>;
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Given the successful resolution of an obligation, the `Vtable`
|
|
|
|
/// indicates where the vtable comes from. Note that while we call this
|
|
|
|
/// a "vtable", it does not necessarily indicate dynamic dispatch at
|
|
|
|
/// runtime. `Vtable` instances just tell the compiler where to find
|
|
|
|
/// methods, but in generic code those methods are typically statically
|
|
|
|
/// dispatched -- only when an object is constructed is a `Vtable`
|
|
|
|
/// instance reified into an actual vtable.
|
|
|
|
///
|
|
|
|
/// For example, the vtable may be tied to a specific impl (case A),
|
|
|
|
/// or it may be relative to some bound that is in scope (case B).
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// impl<T:Clone> Clone<T> for Option<T> { ... } // Impl_1
|
|
|
|
/// impl<T:Clone> Clone<T> for Box<T> { ... } // Impl_2
|
|
|
|
/// impl Clone for int { ... } // Impl_3
|
|
|
|
///
|
|
|
|
/// fn foo<T:Clone>(concrete: Option<Box<int>>,
|
|
|
|
/// param: T,
|
|
|
|
/// mixed: Option<T>) {
|
|
|
|
///
|
|
|
|
/// // Case A: Vtable points at a specific impl. Only possible when
|
|
|
|
/// // type is concretely known. If the impl itself has bounded
|
|
|
|
/// // type parameters, Vtable will carry resolutions for those as well:
|
|
|
|
/// concrete.clone(); // Vtable(Impl_1, [Vtable(Impl_2, [Vtable(Impl_3)])])
|
|
|
|
///
|
|
|
|
/// // Case B: Vtable must be provided by caller. This applies when
|
|
|
|
/// // type is a type parameter.
|
2014-12-27 03:22:29 -06:00
|
|
|
/// param.clone(); // VtableParam
|
2014-11-24 19:06:06 -06:00
|
|
|
///
|
|
|
|
/// // Case C: A mix of cases A and B.
|
2014-12-27 03:22:29 -06:00
|
|
|
/// mixed.clone(); // Vtable(Impl_1, [VtableParam])
|
2014-11-24 19:06:06 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### The type parameter `N`
|
|
|
|
///
|
|
|
|
/// See explanation on `VtableImplData`.
|
2015-06-18 00:51:23 -05:00
|
|
|
#[derive(Clone)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub enum Vtable<'tcx, N> {
|
2014-09-12 09:53:35 -05:00
|
|
|
/// Vtable identifying a particular impl.
|
2014-09-29 14:11:30 -05:00
|
|
|
VtableImpl(VtableImplData<'tcx, N>),
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2015-01-24 07:17:24 -06:00
|
|
|
/// Vtable for default trait implementations
|
2015-02-07 07:46:08 -06:00
|
|
|
/// This carries the information and nested obligations with regards
|
|
|
|
/// to a default implementation for a trait `Trait`. The nested obligations
|
|
|
|
/// ensure the trait implementation holds for all the constituent types.
|
2015-02-07 07:24:34 -06:00
|
|
|
VtableDefaultImpl(VtableDefaultImplData<N>),
|
2015-01-24 07:17:24 -06:00
|
|
|
|
2014-09-12 09:53:35 -05:00
|
|
|
/// Successful resolution to an obligation provided by the caller
|
2015-01-08 20:41:42 -06:00
|
|
|
/// for some type parameter. The `Vec<N>` represents the
|
|
|
|
/// obligations incurred from normalizing the where-clause (if
|
|
|
|
/// any).
|
|
|
|
VtableParam(Vec<N>),
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2014-12-23 04:26:34 -06:00
|
|
|
/// Virtual calls through an object
|
|
|
|
VtableObject(VtableObjectData<'tcx>),
|
|
|
|
|
2014-09-12 09:53:35 -05:00
|
|
|
/// Successful resolution for a builtin trait.
|
2014-10-09 16:19:50 -05:00
|
|
|
VtableBuiltin(VtableBuiltinData<N>),
|
2014-12-01 08:23:40 -06:00
|
|
|
|
2015-01-24 14:15:08 -06:00
|
|
|
/// Vtable automatically generated for a closure. The def ID is the ID
|
|
|
|
/// of the closure expression. This is a `VtableImpl` in spirit, but the
|
|
|
|
/// impl is generated by the compiler and does not appear in the source.
|
2015-06-09 16:09:37 -05:00
|
|
|
VtableClosure(VtableClosureData<'tcx, N>),
|
2014-12-01 08:23:40 -06:00
|
|
|
|
|
|
|
/// Same as above, but for a fn pointer type with the given signature.
|
|
|
|
VtableFnPointer(ty::Ty<'tcx>),
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Identifies a particular impl in the source, along with a set of
|
|
|
|
/// substitutions from the impl's type/lifetime parameters. The
|
|
|
|
/// `nested` vector corresponds to the nested obligations attached to
|
|
|
|
/// the impl's type parameters.
|
|
|
|
///
|
|
|
|
/// The type parameter `N` indicates the type used for "nested
|
|
|
|
/// obligations" that are required by the impl. During type check, this
|
|
|
|
/// is `Obligation`, as one might expect. During trans, however, this
|
|
|
|
/// is `()`, because trans only requires a shallow resolution of an
|
|
|
|
/// impl, and nested obligations are satisfied later.
|
2015-02-11 11:12:57 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub struct VtableImplData<'tcx, N> {
|
2014-09-12 09:53:35 -05:00
|
|
|
pub impl_def_id: ast::DefId,
|
2014-09-29 14:11:30 -05:00
|
|
|
pub substs: subst::Substs<'tcx>,
|
2015-06-02 18:14:45 -05:00
|
|
|
pub nested: Vec<N>
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2015-06-09 16:09:37 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
|
|
pub struct VtableClosureData<'tcx, N> {
|
|
|
|
pub closure_def_id: ast::DefId,
|
|
|
|
pub substs: subst::Substs<'tcx>,
|
|
|
|
/// Nested obligations. This can be non-empty if the closure
|
|
|
|
/// signature contains associated types.
|
|
|
|
pub nested: Vec<N>
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
#[derive(Clone)]
|
2015-02-07 07:24:34 -06:00
|
|
|
pub struct VtableDefaultImplData<N> {
|
2015-02-02 05:14:01 -06:00
|
|
|
pub trait_def_id: ast::DefId,
|
2015-02-04 04:51:17 -06:00
|
|
|
pub nested: Vec<N>
|
2015-02-02 05:14:01 -06:00
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
#[derive(Clone)]
|
2014-10-09 16:19:50 -05:00
|
|
|
pub struct VtableBuiltinData<N> {
|
2015-06-02 18:14:45 -05:00
|
|
|
pub nested: Vec<N>
|
2014-10-09 16:19:50 -05:00
|
|
|
}
|
|
|
|
|
2014-12-23 04:26:34 -06:00
|
|
|
/// A vtable for some object-safe trait `Foo` automatically derived
|
|
|
|
/// for the object type `Foo`.
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(PartialEq,Eq,Clone)]
|
2014-12-23 04:26:34 -06:00
|
|
|
pub struct VtableObjectData<'tcx> {
|
2015-03-03 07:01:13 -06:00
|
|
|
/// the object type `Foo`.
|
2014-12-23 04:26:34 -06:00
|
|
|
pub object_ty: Ty<'tcx>,
|
2015-03-03 07:01:13 -06:00
|
|
|
|
|
|
|
/// `Foo` upcast to the obligation trait. This will be some supertrait of `Foo`.
|
|
|
|
pub upcast_trait_ref: ty::PolyTraitRef<'tcx>,
|
2014-12-23 04:26:34 -06:00
|
|
|
}
|
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
/// Creates predicate obligations from the generic bounds.
|
2015-06-16 17:39:20 -05:00
|
|
|
pub fn predicates_for_generics<'tcx>(cause: ObligationCause<'tcx>,
|
2015-02-11 09:28:52 -06:00
|
|
|
generic_bounds: &ty::InstantiatedPredicates<'tcx>)
|
2014-12-07 10:10:48 -06:00
|
|
|
-> PredicateObligations<'tcx>
|
2014-09-12 09:53:35 -05:00
|
|
|
{
|
2015-06-16 17:39:20 -05:00
|
|
|
util::predicates_for_generics(cause, 0, generic_bounds)
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-18 08:26:10 -06:00
|
|
|
/// Determines whether the type `ty` is known to meet `bound` and
|
|
|
|
/// returns true if so. Returns false if `ty` either does not meet
|
|
|
|
/// `bound` or is not known to meet bound (note that this is
|
|
|
|
/// conservative towards *no impl*, which is the opposite of the
|
|
|
|
/// `evaluate` methods).
|
2015-06-09 13:33:28 -05:00
|
|
|
pub fn type_known_to_meet_builtin_bound<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
bound: ty::BuiltinBound,
|
|
|
|
span: Span)
|
|
|
|
-> bool
|
2014-12-18 08:26:10 -06:00
|
|
|
{
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("type_known_to_meet_builtin_bound(ty={:?}, bound={:?})",
|
|
|
|
ty,
|
2014-12-18 08:26:10 -06:00
|
|
|
bound);
|
|
|
|
|
2015-06-27 19:37:22 -05:00
|
|
|
let mut fulfill_cx = FulfillmentContext::new(false);
|
2014-12-18 08:26:10 -06:00
|
|
|
|
2015-01-02 03:01:30 -06:00
|
|
|
// We can use a dummy node-id here because we won't pay any mind
|
|
|
|
// to region obligations that arise (there shouldn't really be any
|
|
|
|
// anyhow).
|
|
|
|
let cause = ObligationCause::misc(span, ast::DUMMY_NODE_ID);
|
2014-12-18 08:26:10 -06:00
|
|
|
|
2014-12-27 03:22:29 -06:00
|
|
|
fulfill_cx.register_builtin_bound(infcx, ty, bound, cause);
|
2014-12-18 08:26:10 -06:00
|
|
|
|
|
|
|
// Note: we only assume something is `Copy` if we can
|
|
|
|
// *definitively* show that it implements `Copy`. Otherwise,
|
|
|
|
// assume it is move; linear is always ok.
|
2015-06-30 04:18:03 -05:00
|
|
|
match fulfill_cx.select_all_or_error(infcx) {
|
2015-06-09 13:33:28 -05:00
|
|
|
Ok(()) => {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("type_known_to_meet_builtin_bound: ty={:?} bound={:?} success",
|
|
|
|
ty,
|
2015-06-09 13:33:28 -05:00
|
|
|
bound);
|
2015-01-02 03:01:30 -06:00
|
|
|
true
|
|
|
|
}
|
2015-06-09 13:33:28 -05:00
|
|
|
Err(e) => {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("type_known_to_meet_builtin_bound: ty={:?} bound={:?} errors={:?}",
|
|
|
|
ty,
|
2015-06-09 13:33:28 -05:00
|
|
|
bound,
|
2015-06-18 12:25:05 -05:00
|
|
|
e);
|
2015-01-02 03:01:30 -06:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 14:23:41 -05:00
|
|
|
// FIXME: this is gonna need to be removed ...
|
2015-02-20 05:21:46 -06:00
|
|
|
/// Normalizes the parameter environment, reporting errors if they occur.
|
2015-01-26 13:20:38 -06:00
|
|
|
pub fn normalize_param_env_or_error<'a,'tcx>(unnormalized_env: ty::ParameterEnvironment<'a,'tcx>,
|
|
|
|
cause: ObligationCause<'tcx>)
|
|
|
|
-> ty::ParameterEnvironment<'a,'tcx>
|
|
|
|
{
|
2015-02-20 05:21:46 -06:00
|
|
|
// I'm not wild about reporting errors here; I'd prefer to
|
|
|
|
// have the errors get reported at a defined place (e.g.,
|
|
|
|
// during typeck). Instead I have all parameter
|
|
|
|
// environments, in effect, going through this function
|
|
|
|
// and hence potentially reporting errors. This ensurse of
|
|
|
|
// course that we never forget to normalize (the
|
|
|
|
// alternative seemed like it would involve a lot of
|
|
|
|
// manual invocations of this fn -- and then we'd have to
|
|
|
|
// deal with the errors at each of those sites).
|
|
|
|
//
|
|
|
|
// In any case, in practice, typeck constructs all the
|
|
|
|
// parameter environments once for every fn as it goes,
|
|
|
|
// and errors will get reported then; so after typeck we
|
|
|
|
// can be sure that no errors should occur.
|
|
|
|
|
|
|
|
let tcx = unnormalized_env.tcx;
|
|
|
|
let span = cause.span;
|
|
|
|
let body_id = cause.body_id;
|
|
|
|
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("normalize_param_env_or_error(unnormalized_env={:?})",
|
|
|
|
unnormalized_env);
|
2015-02-20 05:21:46 -06:00
|
|
|
|
2015-05-11 16:02:56 -05:00
|
|
|
let predicates: Vec<_> =
|
|
|
|
util::elaborate_predicates(tcx, unnormalized_env.caller_bounds.clone())
|
|
|
|
.filter(|p| !p.is_global()) // (*)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// (*) Any predicate like `i32: Trait<u32>` or whatever doesn't
|
|
|
|
// need to be in the *environment* to be proven, so screen those
|
|
|
|
// out. This is important for the soundness of inter-fn
|
|
|
|
// caching. Note though that we should probably check that these
|
|
|
|
// predicates hold at the point where the environment is
|
|
|
|
// constructed, but I am not currently doing so out of laziness.
|
|
|
|
// -nmatsakis
|
|
|
|
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("normalize_param_env_or_error: elaborated-predicates={:?}",
|
|
|
|
predicates);
|
2015-05-11 16:02:56 -05:00
|
|
|
|
|
|
|
let elaborated_env = unnormalized_env.with_caller_bounds(predicates);
|
|
|
|
|
2015-06-26 14:23:41 -05:00
|
|
|
let infcx = infer::new_infer_ctxt(tcx, &tcx.tables, Some(elaborated_env), false);
|
2015-06-30 04:18:03 -05:00
|
|
|
let predicates = match fully_normalize(&infcx, cause,
|
2015-06-24 15:40:54 -05:00
|
|
|
&infcx.parameter_environment.caller_bounds) {
|
2015-02-20 05:21:46 -06:00
|
|
|
Ok(predicates) => predicates,
|
2015-01-26 13:20:38 -06:00
|
|
|
Err(errors) => {
|
|
|
|
report_fulfillment_errors(&infcx, &errors);
|
2015-06-24 15:40:54 -05:00
|
|
|
return infcx.parameter_environment; // an unnormalized env is better than nothing
|
2015-01-26 13:20:38 -06:00
|
|
|
}
|
2015-02-20 05:21:46 -06:00
|
|
|
};
|
2015-01-26 13:20:38 -06:00
|
|
|
|
2015-04-18 10:23:14 -05:00
|
|
|
let free_regions = FreeRegionMap::new();
|
|
|
|
infcx.resolve_regions_and_report_errors(&free_regions, body_id);
|
2015-02-20 05:21:46 -06:00
|
|
|
let predicates = match infcx.fully_resolve(&predicates) {
|
|
|
|
Ok(predicates) => predicates,
|
|
|
|
Err(fixup_err) => {
|
|
|
|
// If we encounter a fixup error, it means that some type
|
|
|
|
// variable wound up unconstrained. I actually don't know
|
|
|
|
// if this can happen, and I certainly don't expect it to
|
|
|
|
// happen often, but if it did happen it probably
|
|
|
|
// represents a legitimate failure due to some kind of
|
|
|
|
// unconstrained variable, and it seems better not to ICE,
|
|
|
|
// all things considered.
|
|
|
|
let err_msg = fixup_err_to_string(fixup_err);
|
|
|
|
tcx.sess.span_err(span, &err_msg);
|
2015-06-24 15:40:54 -05:00
|
|
|
return infcx.parameter_environment; // an unnormalized env is better than nothing
|
2015-02-20 05:21:46 -06:00
|
|
|
}
|
|
|
|
};
|
2015-01-26 13:20:38 -06:00
|
|
|
|
2015-06-24 15:40:54 -05:00
|
|
|
infcx.parameter_environment.with_caller_bounds(predicates)
|
2015-01-26 13:20:38 -06:00
|
|
|
}
|
|
|
|
|
2015-02-13 18:51:43 -06:00
|
|
|
pub fn fully_normalize<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
value: &T)
|
|
|
|
-> Result<T, Vec<FulfillmentError<'tcx>>>
|
2015-06-23 18:54:32 -05:00
|
|
|
where T : TypeFoldable<'tcx> + HasTypeFlags
|
2015-02-13 18:51:43 -06:00
|
|
|
{
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("normalize_param_env(value={:?})", value);
|
2015-02-13 18:51:43 -06:00
|
|
|
|
2015-06-30 04:18:03 -05:00
|
|
|
let mut selcx = &mut SelectionContext::new(infcx);
|
2015-07-01 15:08:25 -05:00
|
|
|
// FIXME (@jroesch) ISSUE 26721
|
2015-06-30 04:39:47 -05:00
|
|
|
// I'm not sure if this is a bug or not, needs further investigation.
|
|
|
|
// It appears that by reusing the fulfillment_cx here we incur more
|
|
|
|
// obligations and later trip an asssertion on regionck.rs line 337.
|
|
|
|
//
|
|
|
|
// The two possibilities I see is:
|
|
|
|
// - normalization is not actually fully happening and we
|
|
|
|
// have a bug else where
|
|
|
|
// - we are adding a duplicate bound into the list causing
|
|
|
|
// its size to change.
|
|
|
|
//
|
|
|
|
// I think we should probably land this refactor and then come
|
2015-06-27 19:37:22 -05:00
|
|
|
// back to this is a follow-up patch.
|
|
|
|
let mut fulfill_cx = FulfillmentContext::new(false);
|
2015-06-27 19:37:13 -05:00
|
|
|
|
2015-02-13 18:51:43 -06:00
|
|
|
let Normalized { value: normalized_value, obligations } =
|
|
|
|
project::normalize(selcx, cause, value);
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("normalize_param_env: normalized_value={:?} obligations={:?}",
|
|
|
|
normalized_value,
|
|
|
|
obligations);
|
2015-02-13 18:51:43 -06:00
|
|
|
for obligation in obligations {
|
|
|
|
fulfill_cx.register_predicate_obligation(selcx.infcx(), obligation);
|
|
|
|
}
|
2015-07-01 15:08:25 -05:00
|
|
|
|
2015-06-30 04:18:03 -05:00
|
|
|
try!(fulfill_cx.select_all_or_error(infcx));
|
2015-02-13 18:51:43 -06:00
|
|
|
let resolved_value = infcx.resolve_type_vars_if_possible(&normalized_value);
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("normalize_param_env: resolved_value={:?}", resolved_value);
|
2015-02-13 18:51:43 -06:00
|
|
|
Ok(resolved_value)
|
|
|
|
}
|
|
|
|
|
2014-12-04 23:03:03 -06:00
|
|
|
impl<'tcx,O> Obligation<'tcx,O> {
|
|
|
|
pub fn new(cause: ObligationCause<'tcx>,
|
|
|
|
trait_ref: O)
|
|
|
|
-> Obligation<'tcx, O>
|
|
|
|
{
|
2014-09-12 09:53:35 -05:00
|
|
|
Obligation { cause: cause,
|
|
|
|
recursion_depth: 0,
|
2014-12-17 15:00:34 -06:00
|
|
|
predicate: trait_ref }
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-30 16:42:02 -06:00
|
|
|
fn with_depth(cause: ObligationCause<'tcx>,
|
2015-03-25 19:06:52 -05:00
|
|
|
recursion_depth: usize,
|
2014-12-30 16:42:02 -06:00
|
|
|
trait_ref: O)
|
|
|
|
-> Obligation<'tcx, O>
|
|
|
|
{
|
|
|
|
Obligation { cause: cause,
|
|
|
|
recursion_depth: recursion_depth,
|
|
|
|
predicate: trait_ref }
|
|
|
|
}
|
|
|
|
|
2014-12-06 00:30:41 -06:00
|
|
|
pub fn misc(span: Span, body_id: ast::NodeId, trait_ref: O) -> Obligation<'tcx, O> {
|
|
|
|
Obligation::new(ObligationCause::misc(span, body_id), trait_ref)
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
2014-12-07 10:10:48 -06:00
|
|
|
|
|
|
|
pub fn with<P>(&self, value: P) -> Obligation<'tcx,P> {
|
|
|
|
Obligation { cause: self.cause.clone(),
|
|
|
|
recursion_depth: self.recursion_depth,
|
2014-12-17 15:00:34 -06:00
|
|
|
predicate: value }
|
2014-12-07 10:10:48 -06:00
|
|
|
}
|
2014-12-04 23:03:03 -06:00
|
|
|
}
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> ObligationCause<'tcx> {
|
2014-12-05 00:59:17 -06:00
|
|
|
pub fn new(span: Span,
|
2014-12-06 00:30:41 -06:00
|
|
|
body_id: ast::NodeId,
|
2014-12-05 00:59:17 -06:00
|
|
|
code: ObligationCauseCode<'tcx>)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> ObligationCause<'tcx> {
|
2014-12-06 00:30:41 -06:00
|
|
|
ObligationCause { span: span, body_id: body_id, code: code }
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:30:41 -06:00
|
|
|
pub fn misc(span: Span, body_id: ast::NodeId) -> ObligationCause<'tcx> {
|
|
|
|
ObligationCause { span: span, body_id: body_id, code: MiscObligation }
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
2014-09-22 13:47:06 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn dummy() -> ObligationCause<'tcx> {
|
2014-12-06 00:30:41 -06:00
|
|
|
ObligationCause { span: DUMMY_SP, body_id: 0, code: MiscObligation }
|
2014-09-22 13:47:06 -05:00
|
|
|
}
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx, N> Vtable<'tcx, N> {
|
2015-06-02 18:14:45 -05:00
|
|
|
pub fn nested_obligations(self) -> Vec<N> {
|
|
|
|
match self {
|
|
|
|
VtableImpl(i) => i.nested,
|
|
|
|
VtableParam(n) => n,
|
|
|
|
VtableBuiltin(i) => i.nested,
|
|
|
|
VtableDefaultImpl(d) => d.nested,
|
2015-06-09 16:09:37 -05:00
|
|
|
VtableClosure(c) => c.nested,
|
|
|
|
VtableObject(_) | VtableFnPointer(..) => vec![]
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-02 18:14:45 -05:00
|
|
|
pub fn map<M, F>(self, f: F) -> Vtable<'tcx, M> where F: FnMut(N) -> M {
|
2014-09-12 09:53:35 -05:00
|
|
|
match self {
|
2015-06-02 18:14:45 -05:00
|
|
|
VtableImpl(i) => VtableImpl(VtableImplData {
|
|
|
|
impl_def_id: i.impl_def_id,
|
|
|
|
substs: i.substs,
|
|
|
|
nested: i.nested.into_iter().map(f).collect()
|
|
|
|
}),
|
|
|
|
VtableParam(n) => VtableParam(n.into_iter().map(f).collect()),
|
|
|
|
VtableBuiltin(i) => VtableBuiltin(VtableBuiltinData {
|
|
|
|
nested: i.nested.into_iter().map(f).collect()
|
|
|
|
}),
|
|
|
|
VtableObject(o) => VtableObject(o),
|
|
|
|
VtableDefaultImpl(d) => VtableDefaultImpl(VtableDefaultImplData {
|
|
|
|
trait_def_id: d.trait_def_id,
|
|
|
|
nested: d.nested.into_iter().map(f).collect()
|
|
|
|
}),
|
|
|
|
VtableFnPointer(f) => VtableFnPointer(f),
|
2015-06-09 16:09:37 -05:00
|
|
|
VtableClosure(c) => VtableClosure(VtableClosureData {
|
|
|
|
closure_def_id: c.closure_def_id,
|
|
|
|
substs: c.substs,
|
|
|
|
nested: c.nested.into_iter().map(f).collect()
|
|
|
|
})
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> FulfillmentError<'tcx> {
|
2014-12-07 10:10:48 -06:00
|
|
|
fn new(obligation: PredicateObligation<'tcx>,
|
2014-12-04 23:03:03 -06:00
|
|
|
code: FulfillmentErrorCode<'tcx>)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> FulfillmentError<'tcx>
|
2014-09-12 09:53:35 -05:00
|
|
|
{
|
|
|
|
FulfillmentError { obligation: obligation, code: code }
|
|
|
|
}
|
|
|
|
}
|
2014-12-17 13:16:28 -06:00
|
|
|
|
|
|
|
impl<'tcx> TraitObligation<'tcx> {
|
2015-03-26 14:51:11 -05:00
|
|
|
fn self_ty(&self) -> ty::Binder<Ty<'tcx>> {
|
|
|
|
ty::Binder(self.predicate.skip_binder().self_ty())
|
2014-12-17 13:16:28 -06:00
|
|
|
}
|
|
|
|
}
|