rust-lang / rust-lang/rust

Building with optimizations can skip monomorphizing some types, making errors disappear

Open
#157,654 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-MIR A-mir-opt A-monomorphization C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This program (original example due to @scottmcm) errors in debug builds but compiles successfully in release builds:

#![crate_type = "lib"]

#[unsafe(no_mangle)]
pub fn f() {
    g::<u16>();
}

pub fn g<T>() -> std::mem::MaybeUninit<[T; 1 << 62]> {
    std::mem::MaybeUninit::uninit()
}

The debug build error is:

error: values of the type `[u16; 4611686018427387904]` are too big for the target architecture
 --> src/lib.rs:8:1
  |
8 | pub fn g<T>() -> std::mem::MaybeUninit<[T; 1 << 62]> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In a release build, we're optimizing away this function call so the bad type never reaches codegen.

Related issues: #157047, https://github.com/rust-lang/rust/issues/122828.

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.

Research direction

Reproduce the example in src/lib.rs with both debug and release builds, confirming that the invalid array type is diagnosed only without optimizations. Trace the compiler's monomorphization and code-generation handling for the optimized-away call; done when the invalid type cannot evade the required diagnostic in release builds.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.