Implement new orphan rule that requires that impls of remote traits meet the following two criteria:

- the self type includes some local type; and,
- type parameters in the self type must be constrained by a local type.

A type parameter is called *constrained* if it appears in some type-parameter of a local type.

Here are some examples that are accepted. In all of these examples, I
assume that `Foo` is a trait defined in another crate. If `Foo` were
defined in the local crate, then all the examples would be legal.

- `impl Foo for LocalType`
- `impl<T> Foo<T> for LocalType` -- T does not appear in Self, so it is OK
- `impl<T> Foo<T> for LocalType<T>` -- T here is constrained by LocalType
- `impl<T> Foo<T> for (LocalType<T>, T)` -- T here is constrained by LocalType

Here are some illegal examples (again, these examples assume that
`Foo` is not local to the current crate):

- `impl Foo for int` -- the Self type is not local
- `impl<T> Foo for T` -- T appears in Self unconstrained by a local type
- `impl<T> Foo for (LocalType, T)` -- T appears in Self unconstrained by a local type

This is a [breaking-change]. For the time being, you can opt out of
the new rules by placing `#[old_orphan_check]` on the trait (and
enabling the feature gate where the trait is defined). Longer term,
you should restructure your traits to avoid the problem. Usually this
means changing the order of parameters so that the "central" type
parameter is in the `Self` position.

As an example of that refactoring, consider the `BorrowFrom` trait:

```rust
pub trait BorrowFrom<Sized? Owned> for Sized? {
    fn borrow_from(owned: &Owned) -> &Self;
}
```

As defined, this trait is commonly implemented for custom pointer
types, such as `Arc`. Those impls follow the pattern:

```rust
impl<T> BorrowFrom<Arc<T>> for T {...}
```

Unfortunately, this impl is illegal because the self type `T` is not
local to the current crate. Therefore, we are going to change the order of the parameters,
so that `BorrowFrom` becomes `Borrow`:

```rust
pub trait Borrow<Sized? Borrowed> for Sized? {
    fn borrow_from(owned: &Self) -> &Borrowed;
}
```

Now the `Arc` impl is written:

```rust
impl<T> Borrow<T> for Arc<T> { ... }
```

This impl is legal because the self type (`Arc<T>`) is local.
This commit is contained in:
Niko Matsakis 2015-01-04 20:35:06 -05:00
parent 260e46115b
commit 6e68fd09ed
18 changed files with 69 additions and 54 deletions

View file

@ -14,6 +14,6 @@ extern crate "coherence-lib" as lib;
use lib::Remote;
impl<T> Remote for int { }
//~^ ERROR cannot provide an extension implementation
//~^ ERROR E0117
fn main() { }

View file

@ -15,6 +15,6 @@ use lib::Remote1;
pub struct BigInt;
impl Remote1<BigInt> for int { }
impl Remote1<BigInt> for int { } //~ ERROR E0117
fn main() { }

View file

@ -16,6 +16,6 @@ use lib::Remote1;
pub struct BigInt;
impl<T> Remote1<BigInt> for T { }
//~^ ERROR type parameter `T` must also appear
//~^ ERROR type parameter `T` is not constrained
fn main() { }

View file

@ -15,6 +15,6 @@ use lib::Remote1;
pub struct BigInt;
impl Remote1<BigInt> for Vec<int> { }
impl Remote1<BigInt> for Vec<int> { } //~ ERROR E0117
fn main() { }

View file

@ -16,7 +16,7 @@ extern crate trait_impl_conflict;
use trait_impl_conflict::Foo;
impl<A> Foo for A {
//~^ ERROR E0117
//~^ ERROR type parameter `A` is not constrained
//~^^ ERROR E0119
}

View file

@ -13,6 +13,6 @@
extern crate "coherence-lib" as lib;
use lib::Remote;
impl<T> Remote for T { } //~ ERROR E0117
impl<T> Remote for T { } //~ ERROR type parameter `T` is not constrained
fn main() { }

View file

@ -18,7 +18,7 @@ struct TheType;
impl TheTrait<uint> for int { } //~ ERROR E0117
impl TheTrait<TheType> for int { }
impl TheTrait<TheType> for int { } //~ ERROR E0117
impl TheTrait<int> for TheType { }

View file

@ -16,6 +16,6 @@ use lib::Remote;
struct Foo;
impl<T> Remote for lib::Pair<T,Foo> { }
//~^ ERROR type parameter `T` must also appear
//~^ ERROR type parameter `T` is not constrained
fn main() { }

View file

@ -16,6 +16,6 @@ use lib::{Remote, Pair};
struct Local<T>(T);
impl<T,U> Remote for Pair<T,Local<U>> { }
//~^ ERROR type parameter `T` must also appear
//~^ ERROR type parameter `T` is not constrained
fn main() { }

View file

@ -10,7 +10,7 @@
impl Drop for int {
//~^ ERROR the Drop trait may only be implemented on structures
//~^^ ERROR cannot provide an extension implementation
//~^^ ERROR E0117
fn drop(&mut self) {
println!("kaboom");
}

View file

@ -16,6 +16,5 @@ use lib::Remote1;
struct Foo<T>(T);
impl<T,U> Remote1<U> for Foo<T> { }
//~^ ERROR type parameter `U` must also appear
fn main() { }