Skip to content

list of place expressions is incomplete #1645

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
lolbinarycat opened this issue Oct 5, 2024 · 4 comments
Closed

list of place expressions is incomplete #1645

lolbinarycat opened this issue Oct 5, 2024 · 4 comments

Comments

@lolbinarycat
Copy link

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.

@ehuss
Copy link
Contributor

ehuss commented Oct 5, 2024

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

@chorman0773
Copy link
Contributor

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)

@lolbinarycat
Copy link
Author

lolbinarycat commented Oct 9, 2024

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.

@ehuss
Copy link
Contributor

ehuss commented Mar 20, 2025

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

@ehuss ehuss closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants