Skip to content

const functions can assert! but cannot assert_eq! #119826

Open

Description

@oconnor663

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?

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jan 10, 2024
  2. krtab commented on Jan 11, 2024

    @krtab
    Contributor

    Hi!

    I believe this is due to the fact that assert_eq (and other assert_* macros) end up calling core::panicking::assert_fail_inner. Which in turn uses formatting macros (println! with a {:?} placeholder) that are currently not usable in a const context which is known but I couldn't find any issue tracking progress on this front.

  3. saethlin commented on Jan 27, 2024

    @saethlin
    Member

    Yeah, I think this is blocked on const traits. assert_eq! can only be used when the Debug impls or the tail are guaranteed to work in const, and we don't have any way to express that yet.

  4. added
    T-libs-api[DEPRECATED; DO NOT USE]
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jan 27, 2024
  5. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    and removed on Dec 1, 2024
  6. Randl commented on Jul 8, 2025

    @Randl
    Contributor

    We could use const_panic meanwhile?

    /// Helper macro for panicking in a `const fn`.
    /// Invoke as:
    /// ```rust,ignore (just an example)
    /// core::macros::const_panic!("boring message", "flavored message {a} {b:?}", a: u32 = foo.len(), b: Something = bar);
    /// ```
    /// where the first message will be printed in const-eval,
    /// and the second message will be printed at runtime.
    // All uses of this macro are FIXME(const-hack).
    #[unstable(feature = "panic_internals", issue = "none")]
    #[doc(hidden)]
    pub macro const_panic {

  7. oli-obk commented on Jul 8, 2025

    @oli-obk
    Contributor

    We don't need hacks, we can start marking Debug as const_trait and making impls const as we go. Then we need to figure out how to make more Formatter methods const

  8. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-trait-systemArea: Trait systemC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions