Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request static asserts #2790

Open
Tracked by #84
elichai opened this issue Aug 16, 2019 · 5 comments
Open
Tracked by #84

Feature Request static asserts #2790

elichai opened this issue Aug 16, 2019 · 5 comments
Labels
A-assertions Proposals relating to assertions. A-const-eval Proposals relating to compile time evaluation (CTFE). T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@elichai
Copy link

elichai commented Aug 16, 2019

I think asserts are a pretty common thing, especially in unsafe rust (checking sizes/alignments etc.)

Now with anonymous consts we can already do things like:

use std::mem::*;
#[macro_export]
macro_rules! static_assert {
    ($condition:expr) => {
        const _: &() = &[()][1 - ($condition) as usize];
    }
}

static_assert!(size_of::<usize>() == 8);
static_assert!(size_of::<*const u8>() == 8);
static_assert!(align_of::<*const u8>() >= align_of::<u128>());
static_assert!(5>3);

I propose to add macros like this to the core library, with the whole assert/assert_eq/assert_ne facade.

I think these would be really useful

@memoryruins
Copy link

It is definitely useful! Check out the static_assertions crate, which offers similar macros for compile-time asserts. It might be worth seeing if there is anything left to explore in that crate before putting the macros into core. The Rust Internals forum is often a good place for discussions like these. cc @nvzqz

@matthiaskrgr
Copy link
Member

@nvzqz
Copy link
Contributor

nvzqz commented Aug 16, 2019

@memoryruins it looks like you're not the only one this week suggesting this: rust-lang/rust#61347 (comment).

@ExoticMatter
Copy link

ExoticMatter commented Aug 19, 2019

rust-lang/rust#51999 allows panicking in constants, and rust-lang/rust#49146 will allow conditionals in constants.
Once rust-lang/rust#49146 is implemented you will be able to improve the error messages:

#![feature(const_fn, const_panic)]

#[doc(hidden)]
pub use core as core_;

#[macro_export]
macro_rules! static_assert {
    ($cond:expr) => {
        $crate::static_assert!($cond, concat!("assertion failed: ", stringify!($cond)));
    };
    ($cond:expr, $($t:tt)+) => {
        #[forbid(const_err)]
        const _: () = {
            if !$cond {
                $crate::core_::panic!($($t)+)
            }
        };
    };
}

//static_assert!(1 + 1 == 3); // not actually possible yet

The error messages are noisy but more descriptive than an integer overflow error:

error: any use of this value will cause an error
  --> src/lib.rs:22:1
   |
14 | /         const _: () = {
15 | |             //if !$cond {
16 | |                 $crate::core_::panic!($($t)+)
17 | |             //}
18 | |         };
   | |__________-
...
22 |   static_assert!(1 + 1 == 3);
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |   |
   |   the evaluated program panicked at 'assertion failed: 1 + 1 == 3', src/lib.rs:22:1
   |   in this macro invocation
   |
note: lint level defined here
  --> src/lib.rs:13:18
   |
13 |         #[forbid(const_err)]
   |                  ^^^^^^^^^
...
22 | static_assert!(1 + 1 == 3);
   | --------------------------- in this macro invocation
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

@Mark-Simulacrum Mark-Simulacrum transferred this issue from rust-lang/rust Oct 18, 2019
@jonas-schievink jonas-schievink added A-assertions Proposals relating to assertions. A-const-eval Proposals relating to compile time evaluation (CTFE). T-libs-api Relevant to the library API team, which will review and decide on the RFC. labels Nov 17, 2019
@est31
Copy link
Member

est31 commented Sep 19, 2022

Evaluation in a const context works for assert already since 1.57.0: rust-lang/rust#89508

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assertions Proposals relating to assertions. A-const-eval Proposals relating to compile time evaluation (CTFE). T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

7 participants