Skip to content

Regression when dropping temporary value in match in older editions #145374

@madsmtm

Description

@madsmtm

Compiling the following code in older editions (edition 2024 works):

// rustc +nightly-2025-08-10 src/main.rs
use std::fmt;

pub struct ItemIdentifier;

impl ItemIdentifier {
    fn nserror() -> Self {
        todo!()
    }

    pub fn path(&self) -> impl fmt::Display + '_ {
        0
    }
}

struct Ty {
    pointee: Box<Self>,
}

impl Ty {
    fn is_static_object(&self) -> bool {
        true
    }

    fn behind_pointer(&self) -> impl fmt::Display + '_ {
        0
    }
}

fn foo(ty: &Ty, f: &mut fmt::Formatter<'_>) {
    let _res = match ty {
        Ty { pointee } if pointee.is_static_object() => {
            write!(
                f,
                "{}, {}",
                pointee.behind_pointer(),
                ItemIdentifier::path(&ItemIdentifier::nserror()),
            )
        }
        _ => todo!(),
    };
}

fn main() {}

(Reduced from https://github.com/madsmtm/objc2/tree/master/crates/header-translator, this is as small as I managed to make it, it seems like both pointee and ItemIdentifier are required?).

Fails compiling with:

error[E0716]: temporary value dropped while borrowed
  --> src/main.rs:36:39
   |
36 |                 ItemIdentifier::path(&ItemIdentifier::nserror()),
   |                 ----------------------^^^^^^^^^^^^^^^^^^^^^^^^^-
   |                 |                     |
   |                 |                     creates a temporary value which is freed while still in use
   |                 a temporary with access to the borrow is created here ...
37 |             )
38 |         }
   |         -
   |         |
   |         temporary value is freed at the end of this statement
   |         ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `impl std::fmt::Display + '_`
   |
   = note: consider using a `let` binding to create a longer lived value

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

A cargo bisect-rustc reveals that this was introduced in #143376 (nightly-2025-08-09 works, nightly-2025-08-10 fails), it is unclear to me if this breakage was intentional or not?

CC @dianne @est31

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions