2014-09-12 10:54:08 -04: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.
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
//! See `doc.rs` for high-level documentation
|
2014-09-12 10:54:08 -04:00
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
use super::SelectionContext;
|
2014-12-05 01:59:17 -05:00
|
|
|
use super::{Obligation, ObligationCause};
|
2014-09-12 10:54:08 -04:00
|
|
|
use super::util;
|
|
|
|
|
|
|
|
use middle::subst::Subst;
|
2014-09-13 21:09:25 +03:00
|
|
|
use middle::ty::{mod, Ty};
|
2014-12-14 07:17:23 -05:00
|
|
|
use middle::infer::InferCtxt;
|
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 03:30:51 -05:00
|
|
|
use std::collections::HashSet;
|
2014-12-12 11:28:35 -05:00
|
|
|
use std::rc::Rc;
|
2014-09-12 10:54:08 -04:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap::DUMMY_SP;
|
|
|
|
use util::ppaux::Repr;
|
|
|
|
|
|
|
|
pub fn impl_can_satisfy(infcx: &InferCtxt,
|
|
|
|
impl1_def_id: ast::DefId,
|
|
|
|
impl2_def_id: ast::DefId)
|
|
|
|
-> bool
|
|
|
|
{
|
2014-10-09 17:19:50 -04:00
|
|
|
debug!("impl_can_satisfy(\
|
|
|
|
impl1_def_id={}, \
|
|
|
|
impl2_def_id={})",
|
|
|
|
impl1_def_id.repr(infcx.tcx),
|
|
|
|
impl2_def_id.repr(infcx.tcx));
|
|
|
|
|
2014-09-12 10:54:08 -04:00
|
|
|
// `impl1` provides an implementation of `Foo<X,Y> for Z`.
|
|
|
|
let impl1_substs =
|
|
|
|
util::fresh_substs_for_impl(infcx, DUMMY_SP, impl1_def_id);
|
2014-10-09 17:19:50 -04:00
|
|
|
let impl1_trait_ref =
|
2014-12-14 07:17:23 -05:00
|
|
|
(*ty::impl_trait_ref(infcx.tcx, impl1_def_id).unwrap()).subst(infcx.tcx, &impl1_substs);
|
2014-09-12 10:54:08 -04:00
|
|
|
|
|
|
|
// Determine whether `impl2` can provide an implementation for those
|
|
|
|
// same types.
|
|
|
|
let param_env = ty::empty_parameter_environment();
|
2014-11-03 14:48:03 -05:00
|
|
|
let mut selcx = SelectionContext::intercrate(infcx, ¶m_env, infcx.tcx);
|
2014-12-12 11:28:35 -05:00
|
|
|
let obligation = Obligation::new(ObligationCause::dummy(),
|
2014-12-17 14:16:28 -05:00
|
|
|
ty::Binder(ty::TraitPredicate {
|
|
|
|
trait_ref: Rc::new(impl1_trait_ref),
|
|
|
|
}));
|
2014-11-15 17:25:05 -05:00
|
|
|
debug!("impl_can_satisfy(obligation={})", obligation.repr(infcx.tcx));
|
2014-10-09 17:19:50 -04:00
|
|
|
selcx.evaluate_impl(impl2_def_id, &obligation)
|
2014-09-12 10:54:08 -04:00
|
|
|
}
|
|
|
|
|
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 03:30:51 -05:00
|
|
|
#[allow(missing_copy_implementations)]
|
|
|
|
pub enum OrphanCheckErr {
|
|
|
|
NoLocalInputType,
|
|
|
|
UncoveredTypeParameter(ty::ParamTy),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks the coherence orphan rules. `impl_def_id` should be the
|
|
|
|
/// def-id of a trait impl. To pass, either the trait must be local, or else
|
|
|
|
/// two conditions must be satisfied:
|
|
|
|
///
|
|
|
|
/// 1. At least one of the input types must involve a local type.
|
|
|
|
/// 2. All type parameters must be covered by a local type.
|
|
|
|
pub fn orphan_check(tcx: &ty::ctxt,
|
|
|
|
impl_def_id: ast::DefId)
|
|
|
|
-> Result<(), OrphanCheckErr>
|
2014-09-12 10:54:08 -04:00
|
|
|
{
|
|
|
|
debug!("impl_is_local({})", impl_def_id.repr(tcx));
|
|
|
|
|
|
|
|
// We only except this routine to be invoked on implementations
|
|
|
|
// of a trait, not inherent implementations.
|
|
|
|
let trait_ref = ty::impl_trait_ref(tcx, impl_def_id).unwrap();
|
|
|
|
debug!("trait_ref={}", trait_ref.repr(tcx));
|
|
|
|
|
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 03:30:51 -05:00
|
|
|
// If the *trait* is local to the crate, ok.
|
2014-12-14 07:17:23 -05:00
|
|
|
if trait_ref.def_id.krate == ast::LOCAL_CRATE {
|
2014-09-12 10:54:08 -04:00
|
|
|
debug!("trait {} is local to current crate",
|
2014-12-14 07:17:23 -05:00
|
|
|
trait_ref.def_id.repr(tcx));
|
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 03:30:51 -05:00
|
|
|
return Ok(());
|
2014-09-12 10:54:08 -04:00
|
|
|
}
|
|
|
|
|
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 03:30:51 -05:00
|
|
|
// Check condition 1: at least one type must be local.
|
|
|
|
if !trait_ref.input_types().iter().any(|&t| ty_reaches_local(tcx, t)) {
|
|
|
|
return Err(OrphanCheckErr::NoLocalInputType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check condition 2: type parameters must be "covered" by a local type.
|
|
|
|
let covered_params: HashSet<_> =
|
|
|
|
trait_ref.input_types().iter()
|
|
|
|
.flat_map(|&t| type_parameters_covered_by_ty(tcx, t).into_iter())
|
|
|
|
.collect();
|
|
|
|
let all_params: HashSet<_> =
|
|
|
|
trait_ref.input_types().iter()
|
|
|
|
.flat_map(|&t| type_parameters_reachable_from_ty(t).into_iter())
|
|
|
|
.collect();
|
|
|
|
for ¶m in all_params.difference(&covered_params) {
|
|
|
|
return Err(OrphanCheckErr::UncoveredTypeParameter(param));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ty_reaches_local<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
ty.walk().any(|t| ty_is_local_constructor(tcx, t))
|
2014-09-12 10:54:08 -04:00
|
|
|
}
|
|
|
|
|
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 03:30:51 -05:00
|
|
|
fn ty_is_local_constructor<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
debug!("ty_is_local_constructor({})", ty.repr(tcx));
|
2014-09-12 10:54:08 -04:00
|
|
|
|
2014-10-31 10:51:16 +02:00
|
|
|
match ty.sty {
|
2014-09-12 10:54:08 -04:00
|
|
|
ty::ty_bool |
|
|
|
|
ty::ty_char |
|
|
|
|
ty::ty_int(..) |
|
|
|
|
ty::ty_uint(..) |
|
|
|
|
ty::ty_float(..) |
|
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 03:30:51 -05:00
|
|
|
ty::ty_str(..) |
|
2014-09-12 10:54:08 -04:00
|
|
|
ty::ty_bare_fn(..) |
|
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 03:30:51 -05:00
|
|
|
ty::ty_closure(..) |
|
|
|
|
ty::ty_vec(..) |
|
|
|
|
ty::ty_ptr(..) |
|
|
|
|
ty::ty_rptr(..) |
|
|
|
|
ty::ty_tup(..) |
|
|
|
|
ty::ty_param(..) |
|
|
|
|
ty::ty_projection(..) => {
|
2014-09-12 10:54:08 -04:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
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 03:30:51 -05:00
|
|
|
ty::ty_enum(def_id, _) |
|
|
|
|
ty::ty_struct(def_id, _) => {
|
|
|
|
def_id.krate == ast::LOCAL_CRATE
|
2014-09-12 10:54:08 -04:00
|
|
|
}
|
|
|
|
|
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 03:30:51 -05:00
|
|
|
ty::ty_uniq(_) => { // treat ~T like Box<T>
|
|
|
|
let krate = tcx.lang_items.owned_box().map(|d| d.krate);
|
|
|
|
krate == Some(ast::LOCAL_CRATE)
|
2014-09-12 10:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_trait(ref tt) => {
|
2014-12-17 14:16:28 -05:00
|
|
|
tt.principal_def_id().krate == ast::LOCAL_CRATE
|
2014-09-12 10:54:08 -04:00
|
|
|
}
|
|
|
|
|
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 03:30:51 -05:00
|
|
|
ty::ty_unboxed_closure(..) |
|
2014-09-12 10:54:08 -04:00
|
|
|
ty::ty_infer(..) |
|
|
|
|
ty::ty_open(..) |
|
|
|
|
ty::ty_err => {
|
|
|
|
tcx.sess.bug(
|
|
|
|
format!("ty_is_local invoked on unexpected type: {}",
|
2014-12-10 19:46:38 -08:00
|
|
|
ty.repr(tcx))[])
|
2014-09-12 10:54:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 03:30:51 -05:00
|
|
|
|
|
|
|
fn type_parameters_covered_by_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
-> HashSet<ty::ParamTy>
|
|
|
|
{
|
|
|
|
if ty_is_local_constructor(tcx, ty) {
|
|
|
|
type_parameters_reachable_from_ty(ty)
|
|
|
|
} else {
|
|
|
|
ty.walk_children().flat_map(|t| type_parameters_covered_by_ty(tcx, t).into_iter()).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// All type parameters reachable from `ty`
|
|
|
|
fn type_parameters_reachable_from_ty<'tcx>(ty: Ty<'tcx>) -> HashSet<ty::ParamTy> {
|
|
|
|
ty.walk()
|
|
|
|
.filter_map(|t| {
|
|
|
|
match t.sty {
|
|
|
|
ty::ty_param(ref param_ty) => Some(param_ty.clone()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|