[Pre-RFC] Cfg Attribute Alias

It would be nice to stay within the current syntax, although I imagine it wouldn’t be too hard to do a non-breaking change to allow this.

Since we would probably restrict aliases to only alias specific attributes then maybe something like this could work:

#![alias(foobar = cfg(any(foo, bar)))]
#![alias(Data = derive(Eq, PartialEq, Clone, Copy, Debug))]
#![alias(Serde = cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

//usage
#[cfg(foobar)] // foobar is a cfg alias so it can only be used within a cfg
fn only_called_when_foo_or_bar_are_on() {
  //...
}

#[derive(Data)] // Data is a derive Alias so it can only be used within a derive
struct Foo {
  //...
}

#[cfg_attr(Serde)] // Serde is a cfg_attr alias so it can only be used within a cfg_attr
struct CanSerialize {
  //...
}

Alternatively we could also alias the entire attribute and do:

#![alias(foobar = cfg(any(foo, bar)))]

#[foobar] // foobar aliases cfg(any(foo, bar)) so no need to re-wrap it a #[cfg(...)]
fn only_called_when_foo_or_bar_are_on() {
  //...
}

This last option however feels weird to me because now foobar looks like a custom attribute rather than a simple alias.

1 Like