Skip to content

'static reference to local variable that never goes out of scope? #565

Description

@joshlf

Is the following sound? In particular, is it sound to synthesize a 'static reference to a local variable if we can prove that that variable will never go out of scope at runtime because the local scope will never finish executing?

fn with_static<T: 'static, F: FnOnce(&'static T)>(t: T, f: F) {
    let tp: *const T = &t;
    // SAFETY: The `loop {}` below ensures that `t` never goes
    // out of scope.
    let tr: &'static T = unsafe { &*tp };
    f(tr);
    loop {}
    drop(t)
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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