Skip to content

stable addresses for local variables, etc #15

Open
@nikomatsakis

Description

@nikomatsakis

When do we guarantee "stable" addresses? (Meaning that the integral value of a pointer remains the same). Note that addresses are visible to safe code via as conversions.

Some examples:

Local variables

let x = 22;
{ let y = &x; .. }
...
{ let z = &x; .. }

Are the integral values of y and z guaranteed to be equal? It might be useful if they were not, since a compiler could keep x in a register and spill it to memory in different spots on the stack.


Assuming the answer to this question is "yes", are locals still guaranteed to have a stable address when they are reallocated using StorageDead / StorageLive? For example:

let x = 22;
let y = &x as *const _ as usize;
x = 33;
let z = &x as *const _ as usize;
assert_eq!(y, z); // is this true?

Or:

let mut prev = None;
loop {
  let x = 22;
  let y = &x as *const _ as usize;
  if let Some(z) = prev {
    assert_eq!(y, z); // is this true?
  }
  prev = Some(y);
}

Edit by @digama0: moved question about const address stability to #406 , clarified question on killed locals

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-stable-addressTopic: Related to stable addressesS-pending-designStatus: Resolving this issue requires addressing some open design questions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions