Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart asserts #2367

Closed
jesse99 opened this issue May 8, 2012 · 18 comments
Closed

Smart asserts #2367

jesse99 opened this issue May 8, 2012 · 18 comments
Labels
A-codegen Area: Code generation A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one. E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot.

Comments

@jesse99
Copy link
Contributor

jesse99 commented May 8, 2012

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
@catamorphism
Copy link
Contributor

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

@jruderman
Copy link
Contributor

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

@graydon
Copy link
Contributor

graydon commented May 8, 2012

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.

@sanxiyn
Copy link
Member

sanxiyn commented Apr 23, 2013

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

@emberian
Copy link
Member

Nominating for production ready.

@pnkfelix
Copy link
Member

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

@emberian
Copy link
Member

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

@huonw
Copy link
Member

huonw commented Sep 3, 2013

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.

@huonw
Copy link
Member

huonw commented Jan 12, 2014

No change.

@treeman
Copy link
Contributor

treeman commented Sep 15, 2014

Triage bump. No change.

@frewsxcv
Copy link
Member

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

@lifthrasiir
Copy link
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.

@chris-morgan
Copy link
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.

@tbu-
Copy link
Contributor

tbu- commented Oct 19, 2015

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

@Manishearth
Copy link
Member

This can accidentally leak sensitive information, too.

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

@ticki
Copy link
Contributor

ticki commented Dec 22, 2015

What about only adding them in debug mode?

@steveklabnik
Copy link
Member

Triage; no change.

@aturon
Copy link
Member

aturon commented Apr 15, 2017

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.

@aturon aturon closed this as completed Apr 15, 2017
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 22, 2022
use ui_test regex capabilities

We can have more precise patterns now, so use them for some of the tests where that makes sense. :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one. E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot.
Projects
None yet
Development

No branches or pull requests