]> git.lizzy.rs Git - rust.git/commit
Implement default associated type inheritance.
authorAaron Turon <aturon@mozilla.com>
Tue, 29 Dec 2015 21:37:34 +0000 (13:37 -0800)
committerAaron Turon <aturon@mozilla.com>
Mon, 14 Mar 2016 22:04:37 +0000 (15:04 -0700)
commitb7e5112e88b8e73a5bff5a84f37f1d2a608e821c
treee53a74d0b0998fc2df36fbead6d2369cdb79842f
parent5dedbdaea4254a78c58c322f636ecd9175cb53fa
Implement default associated type inheritance.

This commit leverages the specialization graph infrastructure to allow
specializing trait implementations to leave off associated types for
which their parents have provided defaults.

It also modifies the type projection code to avoid projecting associated
types unless either (1) all input types are fully known or (2) the
available associated type is "final", i.e. not marked `default`.
This restriction is required for soundness, due to examples like:

```rust
trait Foo {
    type Assoc;
}

impl<T> Foo for T {
    default type Assoc = ();
}

impl Foo for u8 {
    type Assoc = String;
}

fn generic<T>() -> <T as Foo>::Assoc {
    () //~ ERROR
}

fn main() {
    let s: String = generic::<u8>();
    println!("{}", s); // bad news
}
```
src/librustc/middle/traits/project.rs
src/librustc_typeck/check/mod.rs