rust-lang / rust-lang/rust

const functions can assert! but cannot assert_eq!

Open
#119,826 4 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-eval A-trait-system C-feature-request T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This is probably a well-known limitation, but I couldn't find any other issues for it with a quick search. Apologies if it's a duplicate.

// compiles
pub const fn foo() {
    assert!(1 == 1);
}

// does not compile
pub const fn bar() {
    assert_eq!(1, 1);
}
error[E0015]: cannot call non-const fn `core::panicking::assert_failed::<i32, i32>` in constant functions
 --> src/lib.rs:6:5
  |
6 |     assert_eq!(1, 1);
  |     ^^^^^^^^^^^^^^^^
  |

Playground link

Can core::panicking::assert_failed be made const?

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

Start with the const-function reproduction in src/lib.rs and the reported error for core::panicking::assert_failed. Trace how assert! and assert_eq! expand, then determine whether assert_failed can be const; done means the shown assert_eq! example compiles in a const fn without the reported E0015 error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.