static-assertions icon indicating copy to clipboard operation
static-assertions copied to clipboard

How errors looks? (info in README.md)

Open xoac opened this issue 8 years ago • 4 comments

Hi, I wanted to use static assert this way: May header

 fn static_asserts() {
    const x: usize = mem::size_of::<Header>(); 
    const_assert_eq!(10, x);
}

And get error:

error[E0080]: constant evaluation error
  --> src/header/mod.rs:41:5
   |
41 |     const_assert_eq!(10, x);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

I start looking for reason. I was thinking it's because of may fault. Then I found my Header was wrong size. But I was thought that I am using library wrong way.. There should be section in README.md to know how compile error looks.

xoac avatar Feb 19 '18 09:02 xoac

Maybe there would be a way to add string error to that? I know that http://nalgebra.org/ has a excellent compile time errors.

xoac avatar Feb 19 '18 10:02 xoac

Aren't nalgebra's compile-time errors done via trait constraints?

Can you provide examples of their errors and possibly how they're emitted?

nvzqz avatar Feb 19 '18 17:02 nvzqz

Yes, you are right compile time errors are done via trait constanints. Here is an example output.

error[E0277]: the trait bound `na::constraint::ShapeConstraint: na::constraint::SameNumberOfRows<na::U4, na::U1>` is not satisfied
  --> src/lib.rs:35:40
   |
35 |         let sec = (training_set_output - w) * w.transpose();
   |                                        ^ the trait `na::constraint::SameNumberOfRows<na::U4, na::U1>` is not implemented for `na::constraint::ShapeConstraint`
   |
   = help: the following implementations were found:
             <na::constraint::ShapeConstraint as na::constraint::SameNumberOfRows<D, na::Dynamic>>
             <na::constraint::ShapeConstraint as na::constraint::SameNumberOfRows<D, D>>
             <na::constraint::ShapeConstraint as na::constraint::SameNumberOfRows<na::Dynamic, D>>
   = note: required because of the requirements on the impl of `std::ops::Sub<na::Matrix<f64, na::U1, na::U4, na::MatrixArray<f64, na::U1, na::U4>>>` for `na::Matrix<f64, na::U4, na::U1, na::MatrixArray<f64, na::U4, na::U1>>`

error[E0277]: the trait bound `na::constraint::ShapeConstraint: na::constraint::SameNumberOfColumns<na::U1, na::U4>` is not satisfied

But why don't use this syntax: const_assert!(label; 0 != 0, "0 is equal 0"); and under macro use compile_error?

xoac avatar Feb 20 '18 13:02 xoac

There's no way of making #[cfg] work with constant expressions. As far as I know, compile_error! can only be conditionally used with #[cfg].

nvzqz avatar Feb 22 '18 00:02 nvzqz