Skip to content

const functions can assert! but cannot assert_eq! #119826

@oconnor663

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?

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-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions