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

temporary value dropped while borrowed #132578

Open
vickenty opened this issue Nov 3, 2024 · 2 comments
Open

temporary value dropped while borrowed #132578

vickenty opened this issue Nov 3, 2024 · 2 comments
Labels
C-bug Category: This is a bug. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-const-eval Working group: Const evaluation

Comments

@vickenty
Copy link
Contributor

vickenty commented Nov 3, 2024

Code

I tried this code:

pub const fn concat(_x: &[u8], _y: &[u8]) -> [u8; 1024] {
    [0u8; 1024]
}

const _: &[u8] = &concat(
    if b"".len() > 0 {
        &concat(b"", b"")
    } else {
        b""
    },
    b""
);

I expected to see this happen: compiles

Instead, this happened: fails to compile with the following error:

error[E0716]: temporary value dropped while borrowed
 --> <source>:7:10
  |
5 | const _: &[u8] = &concat(
  |                   ------ borrow later used by call
6 |     if b"".len() > 0 {
7 |         &concat(b"", b"")
  |          ^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
8 |     } else {
  |     - temporary value is freed at the end of this statement
  |
  = note: consider using a `let` binding to create a longer lived value

Version it worked on

It most recently worked on: 1.78.0

Version with regression

rustc --version --verbose:

rustc 1.79.0 (129f3b996 2024-06-10)

@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged

@vickenty vickenty added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Nov 3, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-untriaged Untriaged performance or correctness regression. labels Nov 3, 2024
@vickenty
Copy link
Contributor Author

vickenty commented Nov 3, 2024

This error can be worked around by adding the freshly stabilized const {} block around the second call to concat, but I think this should not be required, given it worked fine before.

@saethlin saethlin added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-const-eval Working group: Const evaluation and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 3, 2024
@saethlin
Copy link
Member

saethlin commented Nov 3, 2024

This is #121557. In particular, see this comment (that GitHub has hidden so the page load might be weird):
#121557 (comment)
You might be interested in the RFC that motivated this: https://rust-lang.github.io/rfcs/3027-infallible-promotion.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-const-eval Working group: Const evaluation
Projects
None yet
Development

No branches or pull requests

3 participants