Skip to content

Regression: format_args!() no longer lifetime-extends some temporaries. #145422

@theemathas

Description

@theemathas

Note that unlike the other similar issues, this bug does not involve if let, and therefore will not be fixed by #145342.

I tried this code:

#[derive(Debug)]
struct Thing;

#[derive(Debug)]
struct Ref<'a>(&'a Thing);
impl Drop for Ref<'_> {
    fn drop(&mut self) {}
}

fn new_thing() -> Thing {
    Thing
}

fn new_ref(x: &Thing) -> Ref<'_> {
    Ref(x)
}

pub fn foo() {
    let _x = format_args!("{:?}, {:?}", 1, new_ref(&new_thing()));
}

The code compiled in version 1.88.0. However, in version 1.89.0, I get the following compile error:

error[E0716]: temporary value dropped while borrowed
  --> src/lib.rs:19:53
   |
19 |     let _x = format_args!("{:?}, {:?}", 1, new_ref(&new_thing()));
   |                                            ---------^^^^^^^^^^^- - temporary value is freed at the end of this statement
   |                                            |        |
   |                                            |        creates a temporary value which is freed while still in use
   |                                            a temporary with access to the borrow is created here ...
20 | }
   | - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `Ref`
   |
   = note: consider using a `let` binding to create a longer lived value

For more information about this error, try `rustc --explain E0716`.

I'm not sure how important this is though, since as far as I can tell, there is no way to actually be able to use the value of _x in version 1.88.0.

Presumably regressed in #140748

@rustbot labels +regression-from-stable-to-stable +F-super_let +A-destructors

Meta

Regression from stable rust version 1.88.0 to 1.89.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-destructorsArea: Destructors (`Drop`, …)C-bugCategory: This is a bug.F-super_letit's super, let's go!I-lang-radarItems that are on lang's radar and will need eventual work or consideration.I-prioritizeIssue: Indicates that prioritization has been requested for this issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions