Skip to content

Smart asserts #2367

Description

@jesse99

It would be great if when asserts failed they printed something more useful than the offending line. Currently they print something like:

Assertion 'x < y + bar() failed', src/foo.rs:561

It'd be way nicer if they printed the values of variables that they use and the results of the functions they call:

Assertion 'x{10} < y{2} + bar(){4} failed', src/foo.rs:561

Activity

  1. catamorphism commented on May 8, 2012

    @catamorphism
    Contributor

    This would be great. And maybe a good (re-)starter project for an intern (where's the "E-intern" label?)

  2. jruderman commented on May 8, 2012

    @jruderman
    Contributor

    Or it could print out a stack trace showing the values of all the locals.

  3. graydon commented on May 8, 2012

    @graydon
    Contributor

    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.

  4. sanxiyn commented on Apr 23, 2013

    @sanxiyn
    Contributor

    Python has a library for this. Here is a demo, which is rather impressive: http://pytest.org/latest/example/reportingdemo.html

  5. emberian commented on Jun 25, 2013

    @emberian
    Contributor

    Nominating for production ready.

  6. pnkfelix commented on Jun 27, 2013

    @pnkfelix
    Contributor

    accepted for far-future. (jclements asserted this is harder than you might think, and we agreed.)

  7. emberian commented on Jun 27, 2013

    @emberian
    Contributor

    ah, I was unaware that was a milestone things could be nominated for. makes sense :p

  8. huonw commented on Sep 3, 2013

    @huonw
    Contributor

    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.

  9. huonw commented on Jan 12, 2014

    @huonw
    Contributor

    No change.

  10. treeman commented on Sep 15, 2014

    @treeman
    Contributor

    Triage bump. No change.

  11. frewsxcv commented on Feb 24, 2015

    @frewsxcv
    Contributor

    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-nightly
    
  12. lifthrasiir commented on Sep 8, 2015

    @lifthrasiir
    Contributor

    We 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.

  13. chris-morgan commented on Sep 9, 2015

    @chris-morgan
    Member

    A true replacement will work for all types, using fmt::Debug for 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.

  14. tbu- commented on Oct 19, 2015

    @tbu-
    Contributor

    Do we actually want this? It'll probably lead to huge code bloat.

  15. Manishearth commented on Nov 23, 2015

    @Manishearth
    Member

    This can accidentally leak sensitive information, too.

    Rather, we should ensure all stdlib asserts are helpful, like in #29984

  16. ticki commented on Dec 22, 2015

    @ticki
    Contributor

    What about only adding them in debug mode?

  17. steveklabnik commented on Feb 7, 2017

    @steveklabnik
    Contributor

    Triage; no change.

  18. aturon commented on Apr 15, 2017

    @aturon
    Contributor

    Closing in favor of out of tree solutions for now. A change to incorporate one of those into std probably merits an RFC at this point.

  19. added a commit that references this issue on Sep 22, 2022
  20. added a commit that references this issue on May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-syntaxextArea: Syntax extensionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions