]> git.lizzy.rs Git - rust.git/commit
Implement new orphan rule that requires that impls of remote traits meet the followin...
authorNiko Matsakis <niko@alum.mit.edu>
Mon, 5 Jan 2015 01:35:06 +0000 (20:35 -0500)
committerNiko Matsakis <niko@alum.mit.edu>
Mon, 5 Jan 2015 22:17:26 +0000 (17:17 -0500)
commit6e68fd09edc7ed37fd76f703247b5410cd338bfe
treeb7614a7f1a03a327a40a3bb8877339912b882cb9
parent260e46115b922d29ca98b3cbea190011b1d3e63f
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.
21 files changed:
src/libcore/borrow.rs
src/libcore/cmp.rs
src/librustc/lint/builtin.rs
src/librustc/middle/traits/coherence.rs
src/librustc_typeck/check/callee.rs
src/librustc_typeck/coherence/orphan.rs
src/libsyntax/feature_gate.rs
src/test/compile-fail/coherence-all-remote.rs
src/test/compile-fail/coherence-bigint-int.rs [new file with mode: 0644]
src/test/compile-fail/coherence-bigint-param.rs
src/test/compile-fail/coherence-bigint-vecint.rs [new file with mode: 0644]
src/test/compile-fail/coherence-cross-crate-conflict.rs
src/test/compile-fail/coherence-iterator-vec-any-elem.rs [deleted file]
src/test/compile-fail/coherence-lone-type-parameter.rs
src/test/compile-fail/coherence-orphan.rs
src/test/compile-fail/coherence-overlapping-pairs.rs
src/test/compile-fail/coherence-pair-covered-uncovered.rs
src/test/compile-fail/drop-on-non-struct.rs
src/test/run-pass/coherence-bigint-int.rs [deleted file]
src/test/run-pass/coherence-bigint-vecint.rs [deleted file]
src/test/run-pass/coherence-iterator-vec-any-elem.rs [new file with mode: 0644]