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

if_test!() macro #8130

Closed
bblum opened this issue Jul 30, 2013 · 9 comments
Closed

if_test!() macro #8130

bblum opened this issue Jul 30, 2013 · 9 comments
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-syntaxext Area: Syntax extensions

Comments

@bblum
Copy link
Contributor

bblum commented Jul 30, 2013

Often I find myself writing code such as:

#[cfg(test)]
fn assert_something_expensive() { ... }
#[cfg(not(test))]
fn assert_something_expensive() { return; }

Sometimes this is logic that is not just an assert, in the case of rt::comm::block_on(). I wish for a macro so this could be done more concisely without needing to define clunky new function items.

@emberian
Copy link
Member

#7869

@huonw
Copy link
Member

huonw commented Jul 31, 2013

It seems like this might be useful as a general if_cfg!():

if_cfg!(test {
   ...
} else {
  ....
})

if_cfg!(target_word_size="64" { } else { })
if_cfg!(not(test) { ... })

@jbclements
Copy link
Contributor

Processing the cfgs before doing macro expansion makes this a bit harder, but how about something like this?:

#[cfg(test)]
#[macro_escape]
mod anonymous20130731 {
    macro_rules! if_test(($t:expr) => ($t))
}

#[cfg(not(test))]
#[macro_escape]
mod anonymous20130731 {
    macro_rules! if_test(($t:expr) => (()))
}


fn f() {
    if_test!(std::io::println("I am in testing mode."));
    std::io::println("The function f is all done.");
}

fn main () {
    f();
}

#[test] fn t1 () {
    f();
}

@jbclements
Copy link
Contributor

@huonw points out that
a) it should be possible to write a macro that expands into something that uses #[cfg...] flags, and that
b) it's probably not actually simpler.

@jbclements
Copy link
Contributor

... and also that the exsting "debug!" macro should cover this issue's request. @bblum , do you think this issue should not be closed?

@SimonSapin
Copy link
Contributor

@jbclements , debug! only does logging, it’s unrelated to this.

Until something more pretty is added, I worked around this with:

#[cfg(not(test))]
static CFG_TEST: bool = false;

#[cfg(test)]
static CFG_TEST: bool = true;

… and using if CFG_TEST { assert!(…) }, counting on the compiler to optimize the test away.

@huonw
Copy link
Member

huonw commented Aug 1, 2013

I just opened #8188 which implements cfg!, which can be used like:

if cfg!(test) {
    assert!(something_expensive());
}

Is this an appropriate solution?

@SimonSapin
Copy link
Contributor

@huonw looks good, thanks!

@bblum
Copy link
Contributor Author

bblum commented Aug 1, 2013

Yeah that's actually better than i was hoping for. Brilliant.

@bors bors closed this as completed in 2460170 Aug 2, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-syntaxext Area: Syntax extensions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants