Skip to content

Possibly invalid borrowing actually allowed #4856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alexcrichton opened this issue Feb 9, 2013 · 2 comments
Closed

Possibly invalid borrowing actually allowed #4856

alexcrichton opened this issue Feb 9, 2013 · 2 comments
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system

Comments

@alexcrichton
Copy link
Member

For something like this program:

use core::hashmap::linear::LinearSet;

struct Foo {
  n: LinearSet<int>,
}

impl Foo {
  fn foo(&mut self, fun: fn(&int)) {
    for self.n.each |f| {
      fun(f);
    }
  }
}

fn bar(f: &mut Foo) {
  do f.foo |a| {
    f.n.insert(*a);
  }
}

fn main() {
  let mut f = Foo { n: LinearSet::new() };
  bar(&mut f);
}

The program both compiles and runs just fine. I thought that this should be an error, though. In the Foo::foo method, the field n is cast to an immutable borrowed pointer for the duration of the iteration, and it then yields a pointer to inside itself to fun. There are no restrictions on fun, however, so if fun does something like insert into n, it might invalidate the pointer due to something like resizing.

Should this actually be allowed or disallowed? I was hoping it would be disallowed because it seems like a bug to me. I also don't want to see pure come back though...

@nikomatsakis
Copy link
Contributor

Yeah this should be illegal. Not sure what's going on here, I'll take a look.

nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Feb 13, 2013
and then adjust code to match. rs=unsound (will review post-landing)
@nikomatsakis
Copy link
Contributor

Fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

2 participants