Skip to content

Const in trait impl block not evaluated unless used, skipping panics. #91877

Closed
@XrXr

Description

@XrXr

I found a surprising behavior of constant evaluation and I'm not sure if it's intended.
I feel this is a small usability issue with panics in const context for implementing
compile time assertions.

I tried this code(playground):

trait ConstPanic {
    const EVAL: ();
}

impl ConstPanic for i32 {
    // Does not seem to be evaluated unless used.
    // Program compiles, unexpectedly.
    const EVAL: () = panic!("oh no!");
}

fn main() {
    // Uncommenting the following gives the compile time panic
    // I expect to see. It also gives "warning: path statement with no effect"
    // In this case, it clearly has a compile time effect.
    //i32::EVAL;
}

I expected to see this happen: The program fails to compile

Instead, this happened: The program compiles and runs.

Meta

rustc --version --verbose:

rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: x86_64-apple-darwin
release: 1.57.0
LLVM version: 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions