Conditional compilation of compiler attributes?


#1

Is it possible to use conditional compilation to include or exclude compiler attributes? For example, something like:

#[cfg(feature = "no-inline")]
#[inline(never)]
fn foo() {}

(in my case, I’m trying to turn off inlining so I can get more granular instrumentation of my code)


#2

Yes, you should be able to use cfg_attr:

#![cfg_attr(feature = "no-inline", inline(never))]
fn foo() {}

#3

Awesome, thanks! Also, it’s #[cfg_attr()], not #![cfg_attr()] (or at least, the latter worked for me while the former gave me errors: “an inner attribute is not permitted in this context”).


#4

Ah, yes, one applies to the whole file/program and one applies to a single item. I copied a global one and forgot to change it.


#5

Ah, gotcha.