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:
commit
a2f3f0cdb3
9 changed files with 192 additions and 37 deletions
13
src/test/run-pass/traits/auxiliary/trait_alias.rs
Normal file
13
src/test/run-pass/traits/auxiliary/trait_alias.rs
Normal 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;
|
||||
14
src/test/run-pass/traits/trait-alias-import-cross-crate.rs
Normal file
14
src/test/run-pass/traits/trait-alias-import-cross-crate.rs
Normal 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.
|
||||
}
|
||||
38
src/test/run-pass/traits/trait-alias-import.rs
Normal file
38
src/test/run-pass/traits/trait-alias-import.rs
Normal 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.
|
||||
}
|
||||
24
src/test/ui/traits/trait-alias-ambiguous.rs
Normal file
24
src/test/ui/traits/trait-alias-ambiguous.rs
Normal 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
|
||||
}
|
||||
20
src/test/ui/traits/trait-alias-ambiguous.stderr
Normal file
20
src/test/ui/traits/trait-alias-ambiguous.stderr
Normal 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`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue