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

allow attaching strings to assert messages #2761

Closed
bblum opened this issue Jun 30, 2012 · 7 comments
Closed

allow attaching strings to assert messages #2761

bblum opened this issue Jun 30, 2012 · 7 comments
Labels
A-grammar Area: The grammar of Rust A-syntaxext Area: Syntax extensions E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@bblum
Copy link
Contributor

bblum commented Jun 30, 2012

A great C idiom is:

assert(condition && "Failure message");

In rust, I keep writing:

if !condition { fail "Failure message"; }

I'd be pretty happy with an #assert macro that took a condition and a failure message and generated appropriate code (easy), or perhaps changing the grammar of assert itself (less feasible).

@brson
Copy link
Contributor

brson commented Jun 30, 2012

I like the macro idea. The error message could be optional. At that point we could remove assert from the language. If we used bang for macros then we write assert ! condition or assert ! (condition, "message").

@bblum
Copy link
Contributor Author

bblum commented Jun 30, 2012

whoa, are you sure it's a good idea to have people write "!condition" to mean "condition must hold"? c.c

@brson
Copy link
Contributor

brson commented Jun 30, 2012

I did think it was a good idea, but it is clearly not.

@eholk
Copy link
Contributor

eholk commented Jun 30, 2012

I think we can already write this with the existing macro system. Does this work?

#macro[
    [#assert[cond], if !(cond) { fail }],
    [#assert[cond, message], if !(cond) { fail message }]
];

@bblum
Copy link
Contributor Author

bblum commented Jun 30, 2012

It should still also print the file and line number, and ideally also the containing function, in either case (with message or not).

(this is me being demanding without knowing how to do macros on my own.)

Oh yeah, also the macro hygiene issue. It should be wrapped in whatever rust's equivalent of C's "do { .. } while (0)" is.

@eholk
Copy link
Contributor

eholk commented Jun 30, 2012

This does most of what you want. Unfortunately, it shows you the line number where the macro was defined, rather than where the macro was invoked. There's a case to be made that this is wrong behavior for #line, or maybe we need #invocation_line instead.

fn macros() {
    #macro[
        [#assert[cond],
         (#assert[cond, "%s", #stringify(cond)])],
        [#assert[cond, message, args, ...],
         {
             if !(cond) { fail #fmt("Assertion failed at %s:%u: %s",
                                    #file(), #line(),
                                    /* #stringify(cond) */
                                   #fmt(message, args, ...)) }
         }]
    ];
}

fn main() {
    #assert(true);
    #assert("123" == "4 5 6",
            "This was supposed to go wrong.");
    #assert("1" == "2");
    #assert("123" == "3 4 5", "This was supposed to go wrong");
}

@burg
Copy link

burg commented Oct 3, 2012

This macro has been added to Servo, btw. I was looking for an NS_ASSERT equivalent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-grammar Area: The grammar of Rust A-syntaxext Area: Syntax extensions E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

4 participants