Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

list of place expressions is incomplete #1645

Open
lolbinarycat opened this issue Oct 5, 2024 · 3 comments
Open

list of place expressions is incomplete #1645

lolbinarycat opened this issue Oct 5, 2024 · 3 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants