Skip to content

Trait object (e.g. impl Any) inherent methods can't be used in another crate #19056

Closed
@japaric

Description

@japaric

STR

// any.rs
pub trait Any: 'static {}

impl<T> Any for T where T: 'static {}

impl Any {
    pub fn is<T: 'static>(&self) -> bool { true }
}

#[test]
fn is() {
    let b: Box<bool> = box true;
    let a: Box<Any> = b as Box<Any>;
    assert!(a.is::<bool>());

    let b: &bool = &true;
    let a: &Any = b as &Any;
    assert!(a.is::<bool>());
}
// app.rs
extern crate any;

use any::Any;

fn main() {
    let b: Box<bool> = box true;
    let a: Box<Any> = b as Box<Any>;
    assert!(a.is::<bool>());

    let b: &bool = &true;
    let a: &Any = b as &Any;
    assert!(a.is::<bool>());
}
$ rustc --test any.rs && ./any
running 1 test
test is ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
$ rustc --crate-type=lib any.rs && rustc -L . app.rs
app.rs:9:15: 9:27 error: type `Box<any::Any+'static>` does not implement any method in scope named `is`
app.rs:9     assert!(a.is::<bool>());
                       ^~~~~~~~~~~~
app.rs:13:15: 13:27 error: type `&any::Any+'static` does not implement any method in scope named `is`
app.rs:13     assert!(a.is::<bool>());
                        ^~~~~~~~~~~~
error: aborting due to 2 previous errors

Version

88c743d


cc @nick29581

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions