Skip to content

Unsoundness: can put borrowed pointer in @Trait, and have it outlive its lifetime #7175

Closed
@bill-myers

Description

@bill-myers

Not totally sure what the root issue is, I guess it is that borrowck doesn't realize that the lifetime of "x as @mut Trait" needs to be limited to the lifetime of x.

Or perhaps it shouldn't be allowed at all to implement traits without lifetime parameters for structs with lifetime parameters.

trait R
{
    fn set_v(&mut self, v: int);
}

struct A<'self>
{
    v: &'self mut int
}

impl<'self> R for A<'self>
{
    fn set_v(&mut self, v: int)
    {
        *self.v = v;
    }
}

fn boom() -> @mut R
{
    let mut i = 3;
    let a = @mut A {v: &mut i};
    let f: @mut R = a as @mut R;
    f
}

pub fn main()
{
    let mut f = boom();
    f.set_v(66); // write to random place on stack
}

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