Support const blocks in ensure! - #459
Open
FadeHack wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #458.
The parse rules already recognized const blocks, they just passed the tokens along unwrapped so they ended up at
__fancy_ensure's:exprmatcher, 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_ensurefor 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 keywordconst" instead of pointing at$cond:expr, which broke tests/ui/empty-ensure.rs. rustc falls through to later arms when an:exprfragment 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:
I added
clippy::nonminimal_boolto the allow list in tests/test_ensure.rs because one of the new cases istrue && const { false }, which pedantic clippy wants to simplify.Ran the test suite on nightly (with
--cfg=anyhow_nightly_testingand-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.