Skip to content

Box<Any> does not fulfil 'static. #15423

Closed
@smosher

Description

@smosher
$ ⮀rustc --version
rustc 0.11.0-pre-nightly (4c39962 2014-06-22 00:01:34 +0000)
host: x86_64-unknown-linux-gnu

Test case:

use std::any::Any;
use std::any::AnyRefExt;

struct Object { val: Box<Any> }

impl Object {
    fn new_int() -> Object { Object { val: box 5 as Box<Any> } }
    fn new_vec() -> Object {
        let mut v: Vec<Object> = vec!();
        v.push( Object::new_int() );
        Object { val: box v as Box<Any> }
    }
}

fn main() {
    let t = Object::new_vec();
    let v = t.val.as_ref::<Vec<Object>>();
}

Produces the error:

golf.rs:17:10: 17:39 error: instantiating a type parameter with an incompatible type `collections::vec::Vec<Object>`, which does not fulfill `'static`
golf.rs:17      let v = t.val.as_ref::<Vec<Object>>();

(In general, any attempts to .as_ref::<T>() where T contains Box<Any> will fail.)

I find this confusing, since box v as Box<Any> on line 11 should produce more or less the same error. Yet if you compile this with line 17 commented out, there are no errors (there are warnings about dead code/unused variables.)

Issue #7268 looks similar, but I can't convince myself that these two are the same. Line 17 doesn't ask for anything line 11 doesn't provide.

Metadata

Metadata

Assignees

No one assigned

    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