Skip to content

Support const blocks in ensure! - #459

Open
FadeHack wants to merge 1 commit into
dtolnay:masterfrom
FadeHack:ensure-const-block
Open

Support const blocks in ensure!#459
FadeHack wants to merge 1 commit into
dtolnay:masterfrom
FadeHack:ensure-const-block

Conversation

@FadeHack

Copy link
Copy Markdown

Fixes #458.

The parse rules already recognized const blocks, they just passed the tokens along unwrapped so they ended up at __fancy_ensure's :expr matcher, which won't take a bare const block before the 2024 edition. Wrapping the block in a token tree group gets it past the matcher. Same trick in __fallback_ensure for a condition that starts with a const block and never reaches the comparison machinery.

The fallback arm goes last on purpose. With it first, ensure!() started reporting "while trying to match keyword const" instead of pointing at $cond:expr, which broke tests/ui/empty-ensure.rs. rustc falls through to later arms when an :expr fragment fails to parse, so putting it at the end keeps that diagnostic the way it was.

Tests cover the const block on the left, on the right, on both sides, nested inside a larger expression, inside an index expression, and the three bare condition forms.

One visible change is that the const block comes out parenthesized in the message:

Condition failed: `(const { 1 + 1 }) == 3` (2 vs 3)

I added clippy::nonminimal_bool to the allow list in tests/test_ensure.rs because one of the new cases is true && const { false }, which pedantic clippy wants to simplify.

Ran the test suite on nightly (with --cfg=anyhow_nightly_testing and -Zrandomize-layout), stable and 1.88.0, plus clippy pedantic and miri under strict provenance. Also checked the library still builds on 1.68.0.

The parse rules already recognized const blocks, but the tokens were
passed along unwrapped, so they ended up at __fancy_ensure's :expr
matcher which won't take a bare const block before the 2024 edition.
Putting the block behind a token tree group gets it past the matcher,
and the same trick handles a leading const block on the fallback path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ensure! doesn't accept inline const blocks

1 participant