1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use syn::{
parse_macro_input, parse_quote, Attribute, Block, Error, Expr, ExprLit, ImplItem, ImplItemFn,
Item, ItemFn, ItemImpl, ItemMod, Lit, Meta, Result, Signature, Type,
};
/// Keep the function body under `#[cfg(..)]`, or replace it with `unimplemented!()` under `#[cfg(not(..))]`.
///
/// # Examples
///
/// `#[cfg_or_panic]` can be used on functions, `mod`, and `impl` blocks.
///
/// ## Function
/// ```should_panic
/// # use cfg_or_panic::cfg_or_panic;
/// #[cfg_or_panic(foo)]
/// fn foo() {
/// println!("foo");
/// }
/// # fn main() { foo(); }
/// ```
///
/// ## `mod`
/// ```should_panic
/// # use cfg_or_panic::cfg_or_panic;
/// #[cfg_or_panic(foo)]
/// mod foo {
/// pub fn foo() {
/// println!("foo");
/// }
/// }
/// # fn main() { foo::foo(); }
/// ```
///
/// ## `impl`
/// ```should_panic
/// # use cfg_or_panic::cfg_or_panic;
/// struct Foo(String);
///
/// #[cfg_or_panic(foo)]
/// impl Foo {
/// fn foo(&self) {
/// println!("foo: {}", self.0);
/// }
/// }
/// # fn main() { Foo("bar".to_owned()).foo(); }
/// ```
///
/// ## Dummy return type
/// For the functions returning an `impl Trait`, you may have to specify a dummy return type for the panic branch.
/// This can be done by adding `#[panic_return = "dummy::return::Type"]` to the function.
/// ```should_panic
/// # use cfg_or_panic::cfg_or_panic;
/// #[cfg_or_panic(foo)]
/// #[panic_return = "std::iter::Empty<_>"]
/// fn my_iter() -> impl Iterator<Item = i32> {
/// (0..10).into_iter()
/// }
/// # fn main() { my_iter().count(); }
/// ```
#[proc_macro_attribute]
pub fn cfg_or_panic(args: TokenStream, input: TokenStream) -> TokenStream {
let expander = Expander::new(args);
let mut item = parse_macro_input!(input as Item);
if let Err(e) = expander.expand_item(&mut item) {
return e.to_compile_error().into();
}
item.into_token_stream().into()
}
struct Expander {
args: TokenStream2,
}
impl Expander {
fn new(args: impl Into<TokenStream2>) -> Self {
Self { args: args.into() }
}
fn expand_item(&self, item: &mut Item) -> Result<()> {
match item {
Item::Fn(item_fn) => self.expand_fn(item_fn),
Item::Impl(item_impl) => self.expand_impl(item_impl),
Item::Mod(item_mod) => self.expand_mod(item_mod),
_ => Err(Error::new_spanned(
item,
"`#[cfg_or_panic]` can only be used on functions, `mod`, and `impl` blocks",
)),
}
}
fn expand_mod(&self, item_mod: &mut ItemMod) -> Result<()> {
let Some((_, content)) = &mut item_mod.content else {
return Ok(());
};
for item in content {
self.expand_item(item).ok();
}
Ok(())
}
fn expand_impl(&self, item_impl: &mut ItemImpl) -> Result<()> {
for item in &mut item_impl.items {
#[allow(clippy::single_match)]
match item {
ImplItem::Fn(impl_item_fn) => self.expand_impl_fn(impl_item_fn)?,
_ => {}
}
}
Ok(())
}
fn expand_fn(&self, f: &mut ItemFn) -> Result<()> {
self.expand_fn_inner(&f.sig, &mut f.block, &mut f.attrs)
}
fn expand_impl_fn(&self, f: &mut ImplItemFn) -> Result<()> {
self.expand_fn_inner(&f.sig, &mut f.block, &mut f.attrs)
}
fn expand_fn_inner(
&self,
sig: &Signature,
fn_block: &mut Block,
fn_attrs: &mut Vec<Attribute>,
) -> Result<()> {
let name = &sig.ident;
let args = &self.args;
let return_ty = {
let mut return_ty = None;
let mut new_fn_attrs = Vec::new();
// TODO: use `extract_if` when stable
for fn_attr in fn_attrs.drain(..) {
if let Some(ty) = extract_panic_return_attr(&fn_attr) {
return_ty = Some(ty?);
} else {
new_fn_attrs.push(fn_attr);
}
}
*fn_attrs = new_fn_attrs;
return_ty
};
let unimplemented = if sig.constness.is_some() {
// const functions do not support formatting
quote!(
unimplemented!();
)
} else {
quote!(
unimplemented!(
"function `{}` unimplemented under `#[cfg(not({}))]`",
stringify!(#name),
stringify!(#args)
);
)
};
let may_with_ret_ty = if let Some(ty) = return_ty {
quote!(
#[allow(unreachable_code, clippy::diverging_sub_expression)]
{
let __ret: #ty = #unimplemented;
return __ret;
}
)
} else {
unimplemented
};
let block = std::mem::replace(fn_block, parse_quote!({}));
*fn_block = parse_quote!({
#[cfg(not(#args))]
#may_with_ret_ty
#[cfg(#args)]
#block
});
let attr = parse_quote!(
#[cfg_attr(not(#args), allow(unused_variables))]
);
fn_attrs.push(attr);
Ok(())
}
}
fn extract_panic_return_attr(attr: &Attribute) -> Option<Result<Type>> {
let Meta::NameValue(name_value) = &attr.meta else {
return None;
};
if name_value.path.get_ident()? != "panic_return" {
return None;
}
Some(parse_panic_return_attr(name_value.value.clone()))
}
fn parse_panic_return_attr(value_expr: Expr) -> Result<Type> {
let Expr::Lit(ExprLit {
lit: Lit::Str(lit_str),
..
}) = value_expr
else {
return Err(Error::new_spanned(value_expr, "expected a string literal"));
};
syn::parse_str(&lit_str.value())
}