Skip to content

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

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-destructorsArea: Destructors (`Drop`, …)A-fmtArea: `core::fmt`A-temporary-lifetime-extensionArea: temporary lifetime extensionC-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.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamT-libs-api[DEPRECATED; DO NOT USE]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