2018-08-30 14:18:55 +02:00
|
|
|
//@ run-pass
|
2014-10-30 15:41:07 +13:00
|
|
|
// Test calling methods on an impl for a bare trait.
|
|
|
|
|
2015-01-01 12:29:48 +13:00
|
|
|
//@ aux-build:traitimpl.rs
|
2015-03-22 13:13:15 -07:00
|
|
|
|
2015-01-01 12:29:48 +13:00
|
|
|
extern crate traitimpl;
|
|
|
|
use traitimpl::Bar;
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
static mut COUNT: usize = 1;
|
2014-10-30 15:41:07 +13:00
|
|
|
|
2015-02-18 15:58:07 -08:00
|
|
|
trait T {
|
2024-02-07 10:42:01 +08:00
|
|
|
fn t(&self) {} //~ WARN method `t` is never used
|
2015-02-12 10:29:52 -05:00
|
|
|
}
|
2014-10-30 15:41:07 +13:00
|
|
|
|
2019-05-28 14:47:21 -04:00
|
|
|
impl<'a> dyn T+'a {
|
2014-10-30 15:41:07 +13:00
|
|
|
fn foo(&self) {
|
|
|
|
unsafe { COUNT *= 2; }
|
|
|
|
}
|
|
|
|
fn bar() {
|
|
|
|
unsafe { COUNT *= 3; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
impl T for isize {}
|
2014-10-30 15:41:07 +13:00
|
|
|
|
2015-01-01 12:29:48 +13:00
|
|
|
struct Foo;
|
|
|
|
impl<'a> Bar<'a> for Foo {}
|
|
|
|
|
2014-10-30 15:41:07 +13:00
|
|
|
fn main() {
|
2019-05-28 14:47:21 -04:00
|
|
|
let x: &dyn T = &42;
|
2014-10-30 15:41:07 +13:00
|
|
|
|
|
|
|
x.foo();
|
2021-03-07 18:47:39 +03:00
|
|
|
<dyn T>::foo(x);
|
|
|
|
<dyn T>::bar();
|
2014-10-30 15:41:07 +13:00
|
|
|
|
2015-06-07 21:00:38 +03:00
|
|
|
unsafe { assert_eq!(COUNT, 12); }
|
2015-01-01 12:29:48 +13:00
|
|
|
|
|
|
|
// Cross-crait case
|
2019-05-28 14:47:21 -04:00
|
|
|
let x: &dyn Bar = &Foo;
|
2015-01-01 12:29:48 +13:00
|
|
|
x.bar();
|
2014-10-30 15:41:07 +13:00
|
|
|
}
|