Skip to content

Improve const_assert#3215

Merged
apoelstra merged 1 commit intorust-bitcoin:masterfrom
tcharding:08-22-const-assert
Aug 31, 2024
Merged

Improve const_assert#3215
apoelstra merged 1 commit intorust-bitcoin:masterfrom
tcharding:08-22-const-assert

Conversation

@tcharding
Copy link
Copy Markdown
Member

Now that we can panic in const context we can improve the const_assert macro by adding a message string.

Original idea by Kix:

#2972 (comment)

@github-actions github-actions bot added the C-internals PRs modifying the internals crate label Aug 22, 2024
Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than + vs ? this is good.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be ?, not +. If you somehow want to support multiple messages then this syntax would look better: $(; $(mesage:expr),+ $(,)?)?

Copy link
Copy Markdown
Member Author

@tcharding tcharding Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used as suggested, thanks.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 $(,.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 message

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ...

apoelstra added a commit that referenced this pull request Aug 24, 2024
…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
@github-actions github-actions bot added the C-bitcoin PRs modifying the bitcoin crate label Aug 29, 2024
@tcharding
Copy link
Copy Markdown
Member Author

Now adds a module so we can use const_asset in item context. (I couldn't get double parenthesis to work.)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is broken because previously it expanded to const _, now it doesn't.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
@tcharding tcharding requested a review from Kixunil August 29, 2024 20:34
Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 3c7c8c4 in the sense that it does what it's supposed to with the only tiny issue being that the bool looks weird but not broken in any other way I can think of.

const _: [(); 0 - !$x as usize] = [];
};
($x:expr $(; $message:expr)?) => {
const _: bool = {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It could've been just ().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd have to always panic to be !.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 3c7c8c4 successfully ran local tests

@apoelstra apoelstra merged commit 753961f into rust-bitcoin:master Aug 31, 2024
tcharding added a commit to tcharding/rust-bitcoin that referenced this pull request Sep 1, 2024
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.
apoelstra added a commit that referenced this pull request Sep 2, 2024
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
@tcharding tcharding deleted the 08-22-const-assert branch September 5, 2024 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-bitcoin PRs modifying the bitcoin crate C-internals PRs modifying the internals crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants