Skip to content

Diagnostic refers to enum discriminant as x.0 #150144

@SimonSapin

Description

@SimonSapin

Code

// in upstream library crate1

#[non_exhaustive]
pub enum Meow {
    A(u32, String),
}

// in a downstream crate

use crate1::Meow;

pub fn f(x: Meow) -> impl FnOnce() {
    || {
        match x {
            Meow::A(a, b) => {
                drop((a, b));
            }
            _ => unreachable!(),
        }
    }
}

Current output

error[E0373]: closure may outlive the current function, but it borrows `x.0`, which is owned by the current function
  --> src/main.rs:8:5
   |
 8 |     || {
   |     ^^ may outlive borrowed value `x.0`
 9 |         match x {
   |               - `x.0` is borrowed here
   |
note: closure is returned here
  --> src/main.rs:8:5
   |
 8 | /     || {
 9 | |         match x {
10 | |             Meow::A(a, b) => {
11 | |                 drop((a, b));
...  |
15 | |     }
   | |_____^
help: to force the closure to take ownership of `x.0` (and any other referenced variables), use the `move` keyword
   |
 8 |     move || {
   |     ++++

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

Desired output

I’m not sure. Maybe refer to “the discriminant of x” instead of x.0?

Rationale and extra context

This code compiles without error on Stable 1.92.0.

The diagnostic in current Nightly refers to x.0. At first glance I’m not sure what that means, and if I write it in code rustc agrees: error[E0609]: no field `0` on type `Meow`

This test case is from rust-lang/reference#1837 (comment) which refers to #138961. (That PR landed recently so it’s not part of 1.92.0.) There we can read:

The breaking change

During closure capture analysis, matching an enum against a constructor is considered to require inspecting a discriminant if the enum has more than one variant. Notably, this is the case even if all the other variants happen to be uninhabited.

So it sounds like x.0 really means “the discriminant of x

Rust Version

rustc 1.94.0-nightly (fcf67da03 2025-12-18)
binary: rustc
commit-hash: fcf67da039f42e3905cf6f69e33304299c45149f
commit-date: 2025-12-18
host: x86_64-unknown-linux-gnu
release: 1.94.0-nightly
LLVM version: 21.1.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions