Implement trait upcasting coercion type-checking.

This commit is contained in:
Charles Lew 2021-07-25 18:43:48 +08:00
parent ac354cf5ce
commit fb4e0a0972
10 changed files with 130 additions and 25 deletions

View file

@ -9,5 +9,5 @@ impl Bar for () {}
fn main() {
let bar: &dyn Bar = &();
let foo: &dyn Foo = bar;
//~^ ERROR trait upcasting is experimental [E0658]
//~^ ERROR trait upcasting coercion is experimental [E0658]
}

View file

@ -1,4 +1,4 @@
error[E0658]: trait upcasting is experimental
error[E0658]: trait upcasting coercion is experimental
--> $DIR/feature-gate-trait_upcasting.rs:11:25
|
LL | let foo: &dyn Foo = bar;

View file

@ -1,10 +1,10 @@
#![feature(box_syntax)]
struct Test {
func: Box<dyn FnMut() + 'static>
func: Box<dyn FnMut() + 'static>,
}
fn main() {
let closure: Box<dyn Fn() + 'static> = Box::new(|| ());
let test = box Test { func: closure }; //~ ERROR trait upcasting is experimental [E0658]
let test = box Test { func: closure }; //~ ERROR trait upcasting coercion is experimental [E0658]
}

View file

@ -1,4 +1,4 @@
error[E0658]: trait upcasting is experimental
error[E0658]: trait upcasting coercion is experimental
--> $DIR/issue-11515.rs:9:33
|
LL | let test = box Test { func: closure };

View file

@ -0,0 +1,13 @@
// run-pass
#![feature(box_syntax, trait_upcasting)]
#![allow(incomplete_features)]
struct Test {
func: Box<dyn FnMut() + 'static>,
}
fn main() {
let closure: Box<dyn Fn() + 'static> = Box::new(|| ());
let mut test = box Test { func: closure };
(test.func)();
}

View file

@ -0,0 +1,22 @@
// check-fail
#![feature(trait_upcasting)]
#![allow(incomplete_features)]
trait Bar<T> {
fn bar(&self, _: T) {}
}
trait Foo : Bar<i32> + Bar<u32> {
fn foo(&self, _: ()) {}
}
struct S;
impl Bar<i32> for S {}
impl Bar<u32> for S {}
impl Foo for S {}
fn main() {
let s: &dyn Foo = &S;
let t: &dyn Bar<_> = s; //~ ERROR mismatched types
}

View file

@ -0,0 +1,14 @@
error[E0308]: mismatched types
--> $DIR/multiple-occurence-ambiguousity.rs:21:26
|
LL | let t: &dyn Bar<_> = s;
| ----------- ^ expected trait `Bar`, found trait `Foo`
| |
| expected due to this
|
= note: expected reference `&dyn Bar<_>`
found reference `&dyn Foo`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.