Skip to content
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

Unsound behavior when returning &-pointers #9069

Closed
alexcrichton opened this issue Sep 9, 2013 · 1 comment
Closed

Unsound behavior when returning &-pointers #9069

alexcrichton opened this issue Sep 9, 2013 · 1 comment

Comments

@alexcrichton
Copy link
Member

This code:

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

impl<'self> Iterator<&'self int> for A<'self> {
    fn next(&mut self) -> Option<&'self int> {
        *self.cur += 1;
        return Some(&*self.cur);
    }
}

fn main() {
    let mut a = 3;
    let mut a = A { cur: &mut a };
    let b = a.next();
    println!("{:?}", b);
    let c = a.next();
    println!("{:?}", b);
    println!("{:?}", c);
}

Runs with:

$ rustc foo.rs
warning: no debug symbols in executable (-arch x86_64)
$ ./foo
Some(&4)
Some(&5)
Some(&5)

I thought something like this should be a compiler error? It seems to me that the lifetime of b would conflict with attempting to re-borrow a as mut.

cc @nikomatsakis

@blake2-ppc discovered this in a bug in a test he wrote for #9062

@nikomatsakis
Copy link
Contributor

Dup #8624

flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 30, 2022
Rustup

r? `@ghost`

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants