`ensure!` doesn't accept inline const blocks
ensure! doesn't accept inline const blocks, even though plain Rust and assert! both do:
ensure!(const { 1 + 1 } == 3);
ensure!(3 == const { 1 + 1 });
ensure!(const { false });All three give:
error: no rules expected keyword `const`
--> src/lib.rs:4:33
|
4 | fn a() -> Result<()> { ensure!(const { 1 + 1 } == 3); Ok(()) }
| ^^^^^ no rules expected this token in macro callIn #298 you added the const block rule to __parse_ensure with a note that it's mostly useless because of rust-lang/rust#86730, and that it would be good to have the case there to revisit in the future.
The parse rules do handle all three. What actually fails is one step later, __fancy_ensure matching $lhs:expr and $rhs:expr, and for the bare condition __fallback_ensure matching $cond:expr.
The thing I didn't expect is that the matcher only rejects a const block when it's the first token of the fragment. ensure!(true && const { false }) compiles today for that reason. So putting the block behind a token tree group is enough to get it past the matcher, and no edition bump is needed.
The tradeoff is that the const block then shows up parenthesized in the message:
Condition failed: `(const { 1 + 1 }) == 3` (2 vs 3)Happy to leave this alone if that isn't worth it to you, since const blocks in ensure! are pretty rare.
Source: dtolnay/anyhow