Conversation
Kixunil
left a comment
There was a problem hiding this comment.
Other than + vs ? this is good.
internals/src/macros.rs
Outdated
There was a problem hiding this comment.
This needs to be ?, not +. If you somehow want to support multiple messages then this syntax would look better: $(; $(mesage:expr),+ $(,)?)?
There was a problem hiding this comment.
Drats, that was just a mistake - I'm sick and brain fried right now, you might have noticed. I'm enjoying coding though. Please excuse me.
internals/src/lib.rs
Outdated
There was a problem hiding this comment.
Actually, this message should say "platforms that have usize larger than 64 bits are not supported" so that it's clear it's a compatibility problem for it to fail.
There was a problem hiding this comment.
Used as suggested, thanks.
356aa0d to
8c74534
Compare
internals/src/macros.rs
Outdated
There was a problem hiding this comment.
Oh, actually, this has no separator for message so it'd become "failedplatforms". I think that rather than adding it to the string above we should put additional ": ", after $(,.
There was a problem hiding this comment.
Done, and tested with
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn const_assert() {
const_assert!(1 == 0; "this is the message");
}
}Which outputs:
---- macros::tests::const_assert stdout ----
thread 'macros::tests::const_assert' panicked at internals/src/macros.rs:212:9:
assertion 1 == 0 failed: this is the messageThere was a problem hiding this comment.
This made me realize our macro is misleading. It's not actually a const assert unless you assign it into a const. It needs to expand to const _: () = if ...
…rtibility of `u32` to `usize` c427d8b bitcoin: Compile time assert on index size (Tobin C. Harding) 49a6acc internals: Remove double parenthesis in const_assert (Tobin C. Harding) 2300b28 units: Remove compile time pointer width check (Tobin C. Harding) Pull request description: 3 patches in preparation for other size related work, this PR does not touch the `ToU64` issue which will be handled separately. - Patch 1: Don't check pointer width in `units` because its not consensus code - Patch 2: Modify internal macro `const_assert` - Patch 3: Use index size to enforce not building on a 16 bit machine ACKs for top commit: Kixunil: ACK c427d8b though I think the last commit was kinda a waste of time and it should have been adding the trait instead or leave it for later. apoelstra: ACK c427d8b successfully ran local tests; unsure if we want to merg this or wait for #3215 Tree-SHA512: 823df5b6a5af3265bce2422c00d287f45816faeb5f965685650ac974a1bd441cf548e25ac2962591732ff221bee91a55703da936382eb166c014ca5d4129edf8
8c74534 to
d5eb992
Compare
d5eb992 to
b4abb23
Compare
|
Now adds a module so we can use |
internals/src/macros.rs
Outdated
There was a problem hiding this comment.
This is broken because previously it expanded to const _, now it doesn't.
There was a problem hiding this comment.
Oh, nice. Now we can get rid of the module again.
Now that we can panic in const context we can improve the `const_assert` macro by adding a message string. Original idea by Kix: rust-bitcoin#2972 (comment)
b4abb23 to
3c7c8c4
Compare
| const _: [(); 0 - !$x as usize] = []; | ||
| }; | ||
| ($x:expr $(; $message:expr)?) => { | ||
| const _: bool = { |
There was a problem hiding this comment.
Why? It could've been just ().
There was a problem hiding this comment.
Good to call this out so that if we later dig into this, we at least can see that we noticed it and thought it was funny at the time.
But it really makes no difference at all. I suspect it could have also been ! if you want the simplest possible type.
There was a problem hiding this comment.
It'd have to always panic to be !.
There was a problem hiding this comment.
Seemed logical to me that if one is asserting an expression then the expression is a bool. I agree with the observation that it is not technically required.
There was a problem hiding this comment.
FTR none of the asserts in std evaluates to bool for a simple reason: the return value would be always true. Same goes here except you can't even compile a const that's not true, that's why it carries no information which is better represented by () type.
It was correctly pointed out during review of rust-bitcoin#3215 (when we made `const_assert` panic) that using a `bool` added no additional information. Remove the `bool` and just use unit.
c71b23d Remove bool from cont_assert (Tobin C. Harding) Pull request description: It was correctly pointed out during review of #3215 (when we made `const_assert` panic) that using a `bool` added no additional information. Remove the `bool` and just use unit. ACKs for top commit: apoelstra: ACK c71b23d successfully ran local tests; lol sure Kixunil: ACK c71b23d Tree-SHA512: be9f4f10ee7d626a082b7ae9f257b79d500824ed3c1f7327391b2ad4d67e60d7da47a14fa7ef8f99d1ea8157967b4658518cbcf1c1bfcf1d8888455f3eb96437
Now that we can panic in const context we can improve the
const_assertmacro by adding a message string.Original idea by Kix:
#2972 (comment)