Repository navigation
Smart asserts #2367
Description
Activity
This would be great. And maybe a good (re-)starter project for an intern (where's the "E-intern" label?)
Or it could print out a stack trace showing the values of all the locals.
I'd expect this as part of the 'note' facility, if we ever implement that independently. Or if we do it in a library, then a form of assert packaged as a syntax extension that adds notes (or note-objects, whatever their equivalent is) for each subexpression in its condition.
Python has a library for this. Here is a demo, which is rather impressive: http://pytest.org/latest/example/reportingdemo.html
Nominating for production ready.
accepted for far-future. (jclements asserted this is harder than you might think, and we agreed.)
ah, I was unaware that was a milestone things could be nominated for. makes sense :p
Triage: this is presumably still hard, but we now have the possibility of
assert!(foo < bar, "Expected foo = %? < bar = %?", foo, bar);(i.e. formats and prints that message if the assertion fails) which is better than nothing.No change.
Triage bump. No change.
Triage bump. No change.
/tmp $ cat hi.rs fn main() { let x = 4; let y = 10; let z = 20; assert!(x + y > z); } /tmp $ rustc hi.rs; and ./hi thread '<main>' panicked at 'assertion failed: x + y > z', hi.rs:6 /tmp $ rustc -vV rustc 1.0.0-nightly (2b01a37ec 2015-02-21) (built 2015-02-21) binary: rustc commit-hash: 2b01a37ec38db9301239f0c0abcf3c695055b0ff commit-date: 2015-02-21 build-date: 2015-02-21 host: x86_64-apple-darwin release: 1.0.0-nightlyWe now have two independent implementations: gifnksm/power-assert-rs and manuel-woelker/rust-passert. Both unconditionally stringifies subexpressions before the conditional however, so it cannot easily replace Rust's own
assert*!macros.A true replacement will work for all types, using
fmt::Debugfor appropriate types and omitting it for other types. This is something that needs something along the lines of default implementations. This is something that we may well be getting in the not-so-distant future, but until we do I don’t think progress is possible on this.Do we actually want this? It'll probably lead to huge code bloat.
This can accidentally leak sensitive information, too.
Rather, we should ensure all stdlib asserts are helpful, like in #29984
What about only adding them in debug mode?
Triage; no change.
Closing in favor of out of tree solutions for now. A change to incorporate one of those into
stdprobably merits an RFC at this point.- added a commit that references this issue
on Sep 22, 2022 - added a commit that references this issue
on May 1, 2025
It would be great if when asserts failed they printed something more useful than the offending line. Currently they print something like:
It'd be way nicer if they printed the values of variables that they use and the results of the functions they call: