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