rust-lang / rust-lang/rust

GVN makes some post-monomorphisation errors optimisation-dependent

Open
#156,836 8 comments 0 reactions 1 assignee View on GitHub

@cjgillot is already working on this.

Since May 30, 2026.

A-const-eval A-mir-opt A-mir-opt-GVN C-bug I-prioritize regression-untriaged T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

I tried this code (https://rust.godbolt.org/z/EdW33qf15):

use std::marker::PhantomData;

pub fn f<T>() -> bool {
    struct Panic<T>(PhantomData<T>);

    impl<T> Panic<T> {
        const PANIC: bool = panic!();
    }

    Panic::<T>::PANIC
}

I expected to see this happen: I expect the panic to be a post-monomorphisation error, i. e. compilation should succeed because f is never monomorphised. This should be independent of the optimisation level.

Instead, this happened: This errors with -C opt-level=3, but not with -C opt-level=0.

This seems to be caused by a change in the GVN MIR opt between 1.74 and 1.75; the error can be reproduced by -Z mir-enable-passes=+GVN on 1.75 (but not 1.74).

I originally noticed this with an inline const block, which shows the same change between 1.74 and 1.75 with GVN enabled (https://rust.godbolt.org/z/xxGTjjvYn):

pub fn f<T>() -> bool {
    const { panic!() }
}
Version it worked on

It most recently worked on: Rust 1.76 (Rust 1.74 with -Z mir-enable-passes=+GVN)

Version with regression

It regressed in Rust 1.75 with -Z mir-enable-passes=+GVN.

GVN was enabled by default for 1.77 for opt-level != 0:
https://github.com/rust-lang/rust/commit/a03c9728168b5adb172c1f43951a7613d7f2a50a#diff-6202eda9e5f3c8b8a291cf165920ea8484c372002e485eafa920b319f7cb4dd3L112-R112

rustc --version --verbose:

rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6
Backtrace
Backtrace

<backtrace>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.