reference icon indicating copy to clipboard operation
reference copied to clipboard

list of place expressions is incomplete

Open lolbinarycat opened this issue 1 year ago • 3 comments

several control flow expressions are actually place expressions:

fn main() {
    let arr = [10, 20];
    let arr_ref = &loop { break arr[0]; };
    dbg!(arr_ref);
}

i've tested if and loop, doubtless there are many more.

lolbinarycat avatar Oct 05 '24 23:10 lolbinarycat

Can you please explain why it seems like control flow expressions are place expressions?

ehuss avatar Oct 05 '24 23:10 ehuss

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b6ab9f2513e411fc96a792bea657b20e shows what is happening here with errors:

  • break is a value expression context, so it converts the place expression to a value expression by copy or move - it works in the above because i32 is Copy, in the playground it errors because *x is not movable and String is not Copy
  • loop is a value expression but because & $expr takes a place expression (and not specifically an assignee expression), the result is promoted to a temporary - this is allowed in example because temporary lifetime extension let's it live for the rest of the function, and errors in the playground because you can't return a reference to a temporary.

If loop{break $place} was a place expression, then the playground would compile (replace the loop {break *x} with a place-propagating expression like (*x) to demonstrate this)

chorman0773 avatar Oct 05 '24 23:10 chorman0773

yeah i see that. the actual problem is that the rules for temporary lifetime extension are obfuscated and incomprehensible.

and also that rust-analyzer doesn't catch these errors, leading me to believe they are valid.

lolbinarycat avatar Oct 09 '24 01:10 lolbinarycat

I'm going to close as I believe this has been answered. Please let us know if something is overlooked.

ehuss avatar Mar 20 '25 02:03 ehuss