dtolnay / paste

Macros for all your token pasting needs
Apache License 2.0
983 stars 53 forks source link

Support paste in cfg feature context #56

Closed frol closed 3 years ago

frol commented 3 years ago

I wonder if there is a way to express the following macro:

#[macro_export]
macro_rules! checked_feature {
    ($feature:ident, $current_protocol_version:expr) => {{
        paste! {
            #[cfg(feature = "protocol_feature_" $feature:snake)]
            let is_feature_enabled = PROTOCOL_FEATURES_TO_VERSION_MAPPING[&ProtocolFeature::$feature] >= $current_protocol_version;
            #[cfg(not(feature = "protocol_feature_" $feature:snake))]
            let is_feature_enabled = false;
            is_feature_enabled
        }
    }};
}

This gives me quite a cryptic message when use:

let q = checked_feature!(ForwardChunkParts, _protocol_version);
error: expected `,`, found `ForwardChunkParts`
   --> chain/chunks/src/lib.rs:654:34
    |
654 |                 checked_feature!(ForwardChunkParts, _protocol_version);
    |                 -----------------^^^^^^^^^^^^^^^^^--------------------
    |                 |                |
    |                 |                unexpected token
    |                 expected `,`

I have tried various combinations of stringify! and it seems that Rust just does not like anything but plain string literal there. I assume that the #[doc] support might be quite close to what I want here for the features.

Even if I use #[cfg(feature = stringify!($feature))] I get:

error: expected unsuffixed literal or identifier, found `stringify`
scalexm commented 3 years ago

I think we could go further and try to support paste inside arbitrary attributes.