Skip to content

lifetime on argument of returned function's is misinterpreted #110716

@zylthinking

Description

@zylthinking

I tried this code:

#![allow(unused_mut)]
#![allow(unused_variables)]

#[cfg(all())]
fn main() {
    fn only_accept_lifetime_of<'a, T: 'a>(_r: &'a T) -> fn(&'a mut T) {
        fn x<'b, T: 'b>(_: &'b mut T) {}
        x::<'a, T>
    }

    let mut a = 1;
    {
        let mut b = &mut a;
        let f = only_accept_lifetime_of(b);

        {
            let mut c = &mut b;
            f(c);

            {
                let mut d = 9;
                f(&mut d);
            }

            c;
        }
    }
}

only_accept_lifetime_of returns a function which early bound to a specified lifetime.

In the code above, f should only accept argument with same life time as b;

And, the following code compiles, which is a surprise:

&mut d must terminate at the end of the block containing d, c however, has a bigger lifetime, which definitely does not equal with '&mut d'.

But both f(c) and f(&mut d) are accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions