nvzqz/static-assertions

Feature suggestion: testability

Amomum opened this issue · 4 comments

Since all of the assertions right now will cause compilation failure, it's hard to write unit tests that check them.

For instance, if I'm writing a library and I want check that my preconditions are asserted correctly.

On the top of my head I would suggest some kind of option (may be detecting #[cfg(test)]?) that changes all compile-time assertions to run-time assertions; than it would be possible to catch panics and test them at runtime.

Isn't this just assert!()?

@jhpratt

Isn't this just assert!()?

No, that's not what I mean. Normal asserts are for runtime checks, but const_asserts are for compile-time checks. If I use const_assert, how can I check if it's correctly written?

Well, I can try to compile it with incorrect code and see that it doesn't compile - that is okay for simple cases that can be tested by hand. But what if my const condition is complex? And what if I want to test it in CI? How shall I do that? Detecting compilation failure is not very pretty imho.

nvzqz commented

Sorry for my silence on this issue.

I'd rather limit the complexity of this crate and not add the option to turn compile errors into runtime errors. If you want to create a PR with code that does this, then I'll review it and could be convinced.

There's not a lot of great ways to test compile errors outside of doing ```compile_fail doc tests, like what this crate does. I thought I saw a crate that wraps this, but I can't seem to find it.

Regarding prettiness of errors, once panic! or assert! becomes usable in const, I can greatly improve some of the error reporting of these assertions.

@nvzqz fair enough. Unfortunately I almost certainly won't be able to make such a PR any time soon, my rust skills are almost nil (and I was speaking from C++ land).

So feel free to put this issue on hold or closing it.