Closed RReverser closed 5 years ago
I wonder if we could also implement some of these sorts of sizing checks as compile-time assertions without affecting binary size, and thus move some key sanity assumptions earlier in the QA cycle (that is, theoretically get caught on cargo check
rather than just cargo test
).
@ZackPierce That should be definitely possible as all involved checks are just using const funcs.
I wonder if we could also implement some of these sorts of sizing checks as compile-time assertions without affecting binary size, and thus move some key sanity assumptions earlier in the QA cycle (that is, theoretically get caught on
cargo check
rather than justcargo test
).
It would be nicer to get the errors as early as possible, but the code size isn't really a concern, since the tests are only compiled when running cargo test
and are not compiled for cargo build
.
If the appropriate static assertions could be placed in the regular compile path (that is, not in a compile-for-test-only module) and had no effects on code size or runtime, then it seems like a good idea to get that extra level of compiler-help for development applied as broadly as reasonable.
Rust doesn't have static assertions though. You could have #[test]
s with const fn
but I don't think that would catch anything earlier, Rust would have to also add #[const_test]
or something. Am I missing something?
@fitzgen Yeah but there are plenty of crates that internally e.g. convert boolean to 0
or -1
and then use that as size of array type, or just do integer overflow and so on, basically forcing a compilation error.
But yeah, for now cargo test
seems enough IMO.
@fitzgen Yeah but there are plenty of crates that internally e.g. convert boolean to
0
or-1
and then use that as size of array type, or just do integer overflow and so on, basically forcing a compilation error.
Oh, wow! That is a clever trick! Would be happy to merge a PR that added this :-p
Agreed, tests (and this timely PR) seem good enough for now; was looking for ways to reduce the odds of such a mismatch escaping us in the future. For reference, one might use https://github.com/nvzqz/static-assertions-rs or similar.
@ZackPierce Yeah, that was one of the crates I was referring to. They implement static assertions using 0
or -1
approach: https://github.com/nvzqz/static-assertions-rs/blob/master/src/const_assert.rs#L74-L75
was looking for ways to reduce the odds of such a mismatch escaping us in the future
Well for that tests just have to be executed at all (in particular, on CI). I've raised #72 to track that.
Fixes #69.