Include a Debug rendering of lhs and rhs in `ensure!` messages
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Tolnay · committed by David Tolnay · parent 6ca7da9 · Browse files · 5 files·+487 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+487 added−3 removed
- tests:+5 added−3 removed
- other:+482 added−0 removed
Provenance
- Commit
a8717ab- Tree
2c5f31c- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
6ca7da9
5 files changed+487 added−3 removed
added:src/ensure.rs+471 added
1+use crate::{anyhow, Error};2+use core::fmt::Debug;3+4+#[doc(hidden)]5+pub trait BothDebug {6+ fn __dispatch_ensure(self, msg: &'static str) -> Error;7+}8+9+impl<A, B> BothDebug for (A, B)10+where11+ A: Debug,12+ B: Debug,13+{14+ fn __dispatch_ensure(self, msg: &'static str) -> Error {15+ anyhow!("{} ({:?} vs {:?})", msg, self.0, self.1)16+ }17+}18+19+#[doc(hidden)]20+pub trait NotBothDebug {21+ fn __dispatch_ensure(self, msg: &'static str) -> Error;22+}23+24+impl<A, B> NotBothDebug for &(A, B) {25+ fn __dispatch_ensure(self, msg: &'static str) -> Error {26+ Error::msg(msg)27+ }28+}29+30+#[doc(hidden)]31+#[macro_export]32+macro_rules! __parse_ensure {33+ (atom () $bail:tt {($($rhs:tt)+) ($($lhs:tt)+) $op:tt} $(,)?) => {34+ $crate::__fancy_ensure!($($lhs)+, $op, $($rhs)+)35+ };36+37+ // low precedence control flow constructs38+39+ (0 $stack:tt $bail:tt $parse:tt return $($rest:tt)*) => {40+ $crate::__fallback_ensure!($bail)41+ };42+43+ (0 $stack:tt $bail:tt $parse:tt break $($rest:tt)*) => {44+ $crate::__fallback_ensure!($bail)45+ };46+47+ (0 $stack:tt $bail:tt $parse:tt continue $($rest:tt)*) => {48+ $crate::__fallback_ensure!($bail)49+ };50+51+ (0 $stack:tt $bail:tt $parse:tt yield $($rest:tt)*) => {52+ $crate::__fallback_ensure!($bail)53+ };54+55+ (0 $stack:tt $bail:tt $parse:tt move $($rest:tt)*) => {56+ $crate::__fallback_ensure!($bail)57+ };58+59+ // unary operators60+61+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} * $($rest:tt)*) => {62+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* *) $($parse)*} $($rest)*)63+ };64+65+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ! $($rest:tt)*) => {66+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* !) $($parse)*} $($rest)*)67+ };68+69+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} - $($rest:tt)*) => {70+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* -) $($parse)*} $($rest)*)71+ };72+73+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} let $(|)? $($pat:pat)|+ = $($rest:tt)*) => {74+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* let $($pat)|+ =) $($parse)*} $($rest)*)75+ };76+77+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $life:lifetime : $($rest:tt)*) => {78+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* $life :) $($parse)*} $($rest)*)79+ };80+81+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} &mut $($rest:tt)*) => {82+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* &mut) $($parse)*} $($rest)*)83+ };84+85+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} & $($rest:tt)*) => {86+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* &) $($parse)*} $($rest)*)87+ };88+89+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} &&mut $($rest:tt)*) => {90+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* &&mut) $($parse)*} $($rest)*)91+ };92+93+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} && $($rest:tt)*) => {94+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* &&) $($parse)*} $($rest)*)95+ };96+97+ // control flow constructs98+99+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} if $($rest:tt)*) => {100+ $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* if) $($parse)*} $($rest)*)101+ };102+103+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} match $($rest:tt)*) => {104+ $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* match) $($parse)*} $($rest)*)105+ };106+107+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} while $($rest:tt)*) => {108+ $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* while) $($parse)*} $($rest)*)109+ };110+111+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} for $(|)? $($pat:pat)|+ in $($rest:tt)*) => {112+ $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* for $($pat)|+ in) $($parse)*} $($rest)*)113+ };114+115+ (atom (cond $stack:tt) $bail:tt {($($buf:tt)*) $($parse:tt)*} {$($block:tt)*} $($rest:tt)*) => {116+ $crate::__parse_ensure!(cond $stack $bail {($($buf)* {$($block)*}) $($parse)*} $($rest)*)117+ };118+119+ (cond $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} else if $($rest:tt)*) => {120+ $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* else if) $($parse)*} $($rest)*)121+ };122+123+ (cond $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} else {$($block:tt)*} $($rest:tt)*) => {124+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* else {$($block)*}) $($parse)*} $($rest)*)125+ };126+127+ (cond $stack:tt $bail:tt $parse:tt $($rest:tt)*) => {128+ $crate::__parse_ensure!(atom $stack $bail $parse $($rest)*)129+ };130+131+ // atomic expressions132+133+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ($($paren:tt)*) $($rest:tt)*) => {134+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* ($($paren)*)) $($parse)*} $($rest)*)135+ };136+137+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} [$($array:tt)*] $($rest:tt)*) => {138+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* [$($array)*]) $($parse)*} $($rest)*)139+ };140+141+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} {$($block:tt)*} $($rest:tt)*) => {142+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* {$($block)*}) $($parse)*} $($rest)*)143+ };144+145+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} loop {$($block:tt)*} $($rest:tt)*) => {146+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* loop {$($block)*}) $($parse)*} $($rest)*)147+ };148+149+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} async {$($block:tt)*} $($rest:tt)*) => {150+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* async {$($block)*}) $($parse)*} $($rest)*)151+ };152+153+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} async move {$($block:tt)*} $($rest:tt)*) => {154+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* async move {$($block)*}) $($parse)*} $($rest)*)155+ };156+157+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} unsafe {$($block:tt)*} $($rest:tt)*) => {158+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* unsafe {$($block)*}) $($parse)*} $($rest)*)159+ };160+161+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $lit:literal $($rest:tt)*) => {162+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* $lit) $($parse)*} $($rest)*)163+ };164+165+ // path expressions166+167+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} :: $($rest:tt)*) => {168+ $crate::__parse_ensure!(path $stack $bail {($($buf)* ::) $($parse)*} $($rest)*)169+ };170+171+ (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $ident:ident $($rest:tt)*) => {172+ $crate::__parse_ensure!(component $stack $bail {($($buf)* $ident) $($parse)*} $($rest)*)173+ };174+175+ (path $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $ident:ident $($rest:tt)*) => {176+ $crate::__parse_ensure!(component $stack $bail {($($buf)* $ident) $($parse)*} $($rest)*)177+ };178+179+ (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} :: < $($rest:tt)*) => {180+ $crate::__parse_ensure!(generic (component $stack) $bail {($($buf)* :: <) $($parse)*} $($rest)*)181+ };182+183+ (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} :: << $($rest:tt)*) => {184+ $crate::__parse_ensure!(generic (component $stack) $bail {($($buf)* :: <) $($parse)*} < $($rest)*)185+ };186+187+ (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} :: $($rest:tt)*) => {188+ $crate::__parse_ensure!(path $stack $bail {($($buf)* ::) $($parse)*} $($rest)*)189+ };190+191+ // macro invocations192+193+ (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ! ($($mac:tt)*) $($rest:tt)*) => {194+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* ! ($($mac)*)) $($parse)*} $($rest)*)195+ };196+197+ (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ! [$($mac:tt)*] $($rest:tt)*) => {198+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* ! [$($mac)*]) $($parse)*} $($rest)*)199+ };200+201+ (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ! {$($mac:tt)*} $($rest:tt)*) => {202+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* ! {$($mac)*}) $($parse)*} $($rest)*)203+ };204+205+ (component $stack:tt $bail:tt $parse:tt $($rest:tt)*) => {206+ $crate::__parse_ensure!(atom $stack $bail $parse $($rest)*)207+ };208+209+ // trailer expressions210+211+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ($($call:tt)*) $($rest:tt)*) => {212+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* ($($call)*)) $($parse)*} $($rest)*)213+ };214+215+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} [$($index:tt)*] $($rest:tt)*) => {216+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* [$($index)*]) $($parse)*} $($rest)*)217+ };218+219+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} {$($init:tt)*} $($rest:tt)*) => {220+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* {$($init)*}) $($parse)*} $($rest)*)221+ };222+223+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ? $($rest:tt)*) => {224+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* ?) $($parse)*} $($rest)*)225+ };226+227+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} . $ident:ident :: < $($rest:tt)*) => {228+ $crate::__parse_ensure!(generic (atom $stack) $bail {($($buf)* . $ident :: <) $($parse)*} $($rest)*)229+ };230+231+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} . $ident:ident :: << $($rest:tt)*) => {232+ $crate::__parse_ensure!(generic (atom $stack) $bail {($($buf)* . $ident :: <) $($parse)*} < $($rest)*)233+ };234+235+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} . $ident:ident $($rest:tt)*) => {236+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* . $ident) $($parse)*} $($rest)*)237+ };238+239+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} . $lit:tt $($rest:tt)*) => {240+ $crate::__parse_ensure!(atom $stack $bail {($($buf)* . $lit) $($parse)*} $($rest)*)241+ };242+243+ // angle bracketed generic arguments244+245+ (generic ($pop:ident $stack:tt) $bail:tt {($($buf:tt)*) $($parse:tt)*} > $($rest:tt)*) => {246+ $crate::__parse_ensure!($pop $stack $bail {($($buf)* >) $($parse)*} $($rest)*)247+ };248+249+ (generic $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $lit:literal $($rest:tt)*) => {250+ $crate::__parse_ensure!(arglist $stack $bail {($($buf)* $lit) $($parse)*} $($rest)*)251+ };252+253+ (generic $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} {$($block:tt)*} $($rest:tt)*) => {254+ $crate::__parse_ensure!(arglist $stack $bail {($($buf)* {$($block)*}) $($parse)*} $($rest)*)255+ };256+257+ (generic $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $life:lifetime $($rest:tt)*) => {258+ $crate::__parse_ensure!(arglist $stack $bail {($($buf)* $life) $($parse)*} $($rest)*)259+ };260+261+ (generic $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $ty:ty , $($rest:tt)*) => {262+ $crate::__parse_ensure!(generic $stack $bail {($($buf)* $ty ,) $($parse)*} $($rest)*)263+ };264+265+ (generic ($pop:ident $stack:tt) $bail:tt {($($buf:tt)*) $($parse:tt)*} $ty:ty > $($rest:tt)*) => {266+ $crate::__parse_ensure!($pop $stack $bail {($($buf)* $ty >) $($parse)*} $($rest)*)267+ };268+269+ (arglist $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} , $($rest:tt)*) => {270+ $crate::__parse_ensure!(generic $stack $bail {($($buf)* ,) $($parse)*} $($rest)*)271+ };272+273+ (arglist ($pop:ident $stack:tt) $bail:tt {($($buf:tt)*) $($parse:tt)*} > $($rest:tt)*) => {274+ $crate::__parse_ensure!($pop $stack $bail {($($buf)* >) $($parse)*} $($rest)*)275+ };276+277+ // high precedence binary operators278+279+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} + $($rest:tt)*) => {280+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* +) $($parse)*} $($rest)*)281+ };282+283+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} - $($rest:tt)*) => {284+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* -) $($parse)*} $($rest)*)285+ };286+287+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} * $($rest:tt)*) => {288+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* *) $($parse)*} $($rest)*)289+ };290+291+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} / $($rest:tt)*) => {292+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* /) $($parse)*} $($rest)*)293+ };294+295+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} % $($rest:tt)*) => {296+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* %) $($parse)*} $($rest)*)297+ };298+299+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ^ $($rest:tt)*) => {300+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* ^) $($parse)*} $($rest)*)301+ };302+303+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} & $($rest:tt)*) => {304+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* &) $($parse)*} $($rest)*)305+ };306+307+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} | $($rest:tt)*) => {308+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* |) $($parse)*} $($rest)*)309+ };310+311+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} << $($rest:tt)*) => {312+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* <<) $($parse)*} $($rest)*)313+ };314+315+ (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} >> $($rest:tt)*) => {316+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* >>) $($parse)*} $($rest)*)317+ };318+319+ // comparison binary operators320+321+ (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} == $($rest:tt)*) => {322+ $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) ==} $($rest)*)323+ };324+325+ (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} == $($rest:tt)*) => {326+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* ==) $($parse)*} $($rest)*)327+ };328+329+ (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} <= $($rest:tt)*) => {330+ $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) <=} $($rest)*)331+ };332+333+ (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} <= $($rest:tt)*) => {334+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* <=) $($parse)*} $($rest)*)335+ };336+337+ (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} < $($rest:tt)*) => {338+ $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) <} $($rest)*)339+ };340+341+ (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} < $($rest:tt)*) => {342+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* <) $($parse)*} $($rest)*)343+ };344+345+ (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} != $($rest:tt)*) => {346+ $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) !=} $($rest)*)347+ };348+349+ (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} != $($rest:tt)*) => {350+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* !=) $($parse)*} $($rest)*)351+ };352+353+ (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} >= $($rest:tt)*) => {354+ $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) >=} $($rest)*)355+ };356+357+ (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} >= $($rest:tt)*) => {358+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* >=) $($parse)*} $($rest)*)359+ };360+361+ (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} > $($rest:tt)*) => {362+ $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) >} $($rest)*)363+ };364+365+ (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} > $($rest:tt)*) => {366+ $crate::__parse_ensure!(0 $stack $bail {($($buf)* >) $($parse)*} $($rest)*)367+ };368+369+ // low precedence binary operators370+371+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} && $($rest:tt)*) => {372+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* &&) $($parse)*} $($rest)*)373+ };374+375+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} || $($rest:tt)*) => {376+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* ||) $($parse)*} $($rest)*)377+ };378+379+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} = $($rest:tt)*) => {380+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* =) $($parse)*} $($rest)*)381+ };382+383+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} += $($rest:tt)*) => {384+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* +=) $($parse)*} $($rest)*)385+ };386+387+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} -= $($rest:tt)*) => {388+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* -=) $($parse)*} $($rest)*)389+ };390+391+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} *= $($rest:tt)*) => {392+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* *=) $($parse)*} $($rest)*)393+ };394+395+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} /= $($rest:tt)*) => {396+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* /=) $($parse)*} $($rest)*)397+ };398+399+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} %= $($rest:tt)*) => {400+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* %=) $($parse)*} $($rest)*)401+ };402+403+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} ^= $($rest:tt)*) => {404+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* ^=) $($parse)*} $($rest)*)405+ };406+407+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} &= $($rest:tt)*) => {408+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* &=) $($parse)*} $($rest)*)409+ };410+411+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} |= $($rest:tt)*) => {412+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* |=) $($parse)*} $($rest)*)413+ };414+415+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} <<= $($rest:tt)*) => {416+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* <<=) $($parse)*} $($rest)*)417+ };418+419+ (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} >>= $($rest:tt)*) => {420+ $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* >>=) $($parse)*} $($rest)*)421+ };422+423+ // unrecognized expression424+425+ ($state:tt $stack:tt $bail:tt $($rest:tt)*) => {426+ $crate::__fallback_ensure!($bail)427+ };428+}429+430+#[doc(hidden)]431+#[macro_export]432+macro_rules! __fancy_ensure {433+ ($lhs:expr, $op:tt, $rhs:expr) => {434+ match (&$lhs, &$rhs) {435+ (lhs, rhs) => {436+ if !(lhs $op rhs) {437+ #[allow(unused_imports)]438+ use $crate::private::{BothDebug, NotBothDebug};439+ return Err((lhs, rhs).__dispatch_ensure(concat!("Condition failed: `", stringify!($lhs), " ", stringify!($op), " ", stringify!($rhs), "`")));440+ }441+ }442+ }443+ };444+}445+446+#[doc(hidden)]447+#[macro_export]448+macro_rules! __fallback_ensure {449+ (($cond:expr $(,)?)) => {450+ if !$cond {451+ return $crate::private::Err($crate::Error::msg(452+ $crate::private::concat!("Condition failed: `", $crate::private::stringify!($cond), "`")453+ ));454+ }455+ };456+ (($cond:expr, $msg:literal $(,)?)) => {457+ if !$cond {458+ return $crate::private::Err($crate::anyhow!($msg));459+ }460+ };461+ (($cond:expr, $err:expr $(,)?)) => {462+ if !$cond {463+ return $crate::private::Err($crate::anyhow!($err));464+ }465+ };466+ (($cond:expr, $fmt:expr, $($arg:tt)*)) => {467+ if !$cond {468+ return $crate::private::Err($crate::anyhow!($fmt, $($arg)*));469+ }470+ };471+}modified:src/lib.rs+2 added
235 unmodified lines236 #[macro_use]237 mod backtrace;238 mod chain;239 mod context;240+mod ensure;241 mod error;242 mod fmt;243 mod kind;244 mod macros;363 unmodified lines608 use crate::Error;609 use alloc::fmt;610 use core::fmt::Arguments;611 612+ pub use crate::ensure::{BothDebug, NotBothDebug};613 pub use alloc::format;614 pub use core::result::Result::Err;615 pub use core::{concat, format_args, stringify};616 26 unmodified linesmodified:src/macros.rs+9 added
131 unmodified lines132 /// ensure!(depth <= MAX_DEPTH, ScienceError::RecursionLimitExceeded);133 /// # Ok(())134 /// # }135 /// ```136+#[cfg(doc)]137 #[macro_export]138 macro_rules! ensure {139 ($cond:expr $(,)?) => {140 if !$cond {18 unmodified lines159 }160 };161 }162 163+#[cfg(not(doc))]164+#[macro_export]165+macro_rules! ensure {166+ ($($tt:tt)*) => {167+ $crate::__parse_ensure!(0 () ($($tt)*) {()} $($tt)*)168+ };169+}170+171 /// Construct an ad-hoc error from a string or existing non-`anyhow` error172 /// value.173 ///174 /// This evaluates to an [`Error`][crate::Error]. It can take either just a41 unmodified linesmodified:tests/ui/empty-ensure.stderr+4 added-2 removed
1-error: unexpected end of macro invocation1+error: no rules expected the token `)`2 --> tests/ui/empty-ensure.rs:4:53 |4 4 | ensure!();5- | ^^^^^^^^^ missing tokens in macro arguments5+ | ^^^^^^^^^ no rules expected this token in macro call6+ |7+ = note: this error originates in the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)modified:tests/test_macros.rs+1 added-1 removed
45 unmodified lines46 Ok(())47 };48 assert_eq!(49 f().unwrap_err().to_string(),50- "Condition failed: `v + v == 1`",50+ "Condition failed: `v + v == 1` (2 vs 1)",51 );52 }53 54 #[test]25 unmodified lines