If one of the rust macro arguments takes a boolean constant as argument and expands if/then/else code
base on that boolean constant, does the expanded code annd condition check get optimized based on the macro argument?
For example , if I had the following macro definition and invocaton as below.
Will the compiled code still have code to check if true ? Or will it be optimized out?
macro_rules! condcode {
( $x:expr ) => {
{
if $x {
something if true;
} else {
something if false;
}
}
};
}
condcode!(true);
Your example macro with true as an argument will cause the compiler to optimize away the conditional branch 100% of the time (unless you disable optimizations) ¹.
As already mentioned before, this has nothing to do with macros. All runtime code is optimized equally by LLVM, whether or not it originates from a macro expansion.