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

Privately uninhabited types can affect how pattern matches are borrow-checked #137999

Closed
meithecatte opened this issue Mar 4, 2025 · 0 comments · Fixed by #138001
Closed

Privately uninhabited types can affect how pattern matches are borrow-checked #137999

meithecatte opened this issue Mar 4, 2025 · 0 comments · Fixed by #138001
Assignees
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@meithecatte
Copy link
Contributor

meithecatte commented Mar 4, 2025

The following code compiles:

mod m {
    enum Void {}
    
    pub struct Internal {
        _v: Void,
    }

    pub enum Test {
        A(u32, u32),
        B(Internal),
    }
}

use m::Test;

pub fn meow(x: &mut Test) {
    let r1: &mut u32 = match x {
        Test::A(a, _) => a,
        _ => todo!(),
    };

    let r2: &mut u32 = match x {
        Test::A(_, b) => b,
        _ => todo!(),
    };

    let _ = *r1;
    let _ = *r2;
}

However, it only compiles because the _v private field is uninhabited – changing its type to () makes it fail compilation with the following error:

error[E0503]: cannot use `*x` because it was mutably borrowed
  --> src/lib.rs:22:30
   |
18 |         Test::A(a, _) => a,
   |                 - `x.0` is borrowed here
...
22 |     let r2: &mut u32 = match x {
   |                              ^ use of borrowed `x.0`
...
27 |     let _ = *r1;
   |             --- borrow later used here

As discussed on Zulip, the compiler makes efforts to not make it possible to observe a type being privately uninhabited, and so this is a bug.

Meta

rustc --version --verbose:

rustc 1.87.0-nightly (f4a216d28 2025-03-02)
binary: rustc
commit-hash: f4a216d28ee635afce685b4206e713579f66e130
commit-date: 2025-03-02
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

As I discovered this bug while reading rustc sources, I am working on a PR to fix this.

@rustbot claim

@meithecatte meithecatte added the C-bug Category: This is a bug. label Mar 4, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 4, 2025
@jieyouxu jieyouxu added A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 4, 2025
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 4, 2025
…=<try>

mir_build: consider privacy when checking for irrefutable patterns

This PR fixes rust-lang#137999.

Note that, since this makes the compiler reject code that was previously accepted, it will probably need a crater run.

I include a commit that factors out a common code pattern into a helper function, purely because the fact that this was repeated all over the place was bothering me. Let me know if I should split that into a separate PR instead.
@bors bors closed this as completed in 2ab69b8 Mar 20, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 20, 2025
Rollup merge of rust-lang#138001 - meithecatte:privately-uninhabited, r=Nadrieril

mir_build: consider privacy when checking for irrefutable patterns

This PR fixes rust-lang#137999.

Note that, since this makes the compiler reject code that was previously accepted, it will probably need a crater run.

I include a commit that factors out a common code pattern into a helper function, purely because the fact that this was repeated all over the place was bothering me. Let me know if I should split that into a separate PR instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants