Rollup merge of #59166 - seanmonstar:trait-alias-import, r=alexreg

resolve: collect trait aliases along with traits

It seems trait aliases weren't being collected as `TraitCandidates` in resolve, this should change that. (I can't compile the full compiler locally, so relying on CI...)

Fixes https://github.com/rust-lang/rust/issues/56485

r? @alexreg
This commit is contained in:
Mazdak Farrokhzad 2019-04-02 18:25:12 +02:00 committed by GitHub
commit a2f3f0cdb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 192 additions and 37 deletions

View file

@ -0,0 +1,13 @@
#![feature(trait_alias)]
pub trait Hello {
fn hello(&self);
}
pub struct Hi;
impl Hello for Hi {
fn hello(&self) {}
}
pub trait Greet = Hello;

View file

@ -0,0 +1,14 @@
// run-pass
// aux-build:trait_alias.rs
#![feature(trait_alias)]
extern crate trait_alias;
// Import only the alias, not the real trait.
use trait_alias::{Greet, Hi};
fn main() {
let hi = Hi;
hi.hello(); // From `Hello`, via `Greet` alias.
}

View file

@ -0,0 +1,38 @@
#![feature(trait_alias)]
mod inner {
pub trait Foo {
fn foo(&self);
}
pub struct Qux;
impl Foo for Qux {
fn foo(&self) {}
}
pub trait Bar = Foo;
}
mod two {
pub trait A {
fn foo();
}
impl A for u8 {
fn foo() {}
}
}
// Import only the alias, not the `Foo` trait.
use inner::{Bar, Qux};
// Declaring an alias also brings in aliased methods.
trait Two = two::A;
fn main() {
let q = Qux;
q.foo(); // From Bar.
u8::foo(); // From A.
}

View file

@ -0,0 +1,24 @@
#![feature(trait_alias)]
mod inner {
pub trait A { fn foo(&self); }
pub trait B { fn foo(&self); }
impl A for u8 {
fn foo(&self) {}
}
impl B for u8 {
fn foo(&self) {}
}
pub trait C = A + B;
}
use inner::C;
fn main() {
let t = 1u8;
t.foo(); //~ ERROR E0034
inner::A::foo(&t); // ok
}

View file

@ -0,0 +1,20 @@
error[E0034]: multiple applicable items in scope
--> $DIR/trait-alias-ambiguous.rs:21:7
|
LL | t.foo();
| ^^^ multiple `foo` found
|
note: candidate #1 is defined in an impl of the trait `inner::A` for the type `u8`
--> $DIR/trait-alias-ambiguous.rs:8:9
|
LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
note: candidate #2 is defined in an impl of the trait `inner::B` for the type `u8`
--> $DIR/trait-alias-ambiguous.rs:11:9
|
LL | fn foo(&self) {}
| ^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0034`.