Skip to content

Unexpected "life is too short" error #16339

Closed
@kvark

Description

@kvark

The repro code:

#![feature(macro_rules)]

#[macro_export]
macro_rules! entity {
    ($($name:ident : $component:ty,)*) => {
        type IdType = u32;
        #[deriving(Clone, PartialEq, Show)]
        pub struct Id<S>(IdType);

        /// A collection of pointers to components
        pub struct Entity {
            $(
            pub $name: Option<Id<$component>>,
            )*
        }
        impl Entity {
            pub fn new() -> Entity {
                Entity {
                    $(
                    $name: None,
                    )*
                }
            }
        }

        /// A collection of component arrays
        pub struct DataHub {
            $(
            pub $name: Vec<$component>,
            )*
        }

        /// Component change() wrapper
        pub struct Changer<'a> {
            entity: &'a Entity,
            hub: &'a mut DataHub,
        }
        impl <'a> Changer<'a> {
            $(
            pub fn $name(&mut self) -> Option<&mut $component> {
                self.entity.$name.map(|Id(id)| self.hub.$name.get_mut(id as uint))
            }
            )*
        }
    }
}

pub mod test {
    type DummyComponent = int;
    entity! {
        dummy : DummyComponent,
    }
}

fn main() {
}

Rust complains:

<anon>:41:48: 41:62 error: lifetime of `self` is too short to guarantee its contents can be safely reborrowed
<anon>:41                 self.entity.$name.map(|Id(id)| self.hub.$name.get_mut(id as uint))
                                                         ^~~~~~~~~~~~~~
<anon>:4:1: 46:2 note: in expansion of entity!
<anon>:50:5: 52:6 note: expansion site
<anon>:40:64: 42:14 note: `self` would have to be valid for the anonymous lifetime #1 defined on the block at 40:63...
<anon>:40             pub fn $name(&mut self) -> Option<&mut $component> {
<anon>:41                 self.entity.$name.map(|Id(id)| self.hub.$name.get_mut(id as uint))
<anon>:42             }
<anon>:41:17: 41:83 note: ...but `self` is only valid for the method call at 41:16
<anon>:41                 self.entity.$name.map(|Id(id)| self.hub.$name.get_mut(id as uint))
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:4:1: 46:2 note: in expansion of entity!

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