[go: up one dir, main page]

cfg_eval 0.1.2

`#[cfg_eval]` in stable Rust 🙃
Documentation
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#![doc = include_str!("../README.md")]
// Templated by `cargo-generate` using https://github.com/danielhenrymantilla/proc-macro-template
#![allow(nonstandard_style, unused_imports)]
#![forbid(unsafe_code)]

use ::core::{
    mem,
    ops::Not as _,
};
use ::proc_macro::{
    TokenStream,
};
use ::proc_macro2::{
    Span,
    TokenStream as TokenStream2,
    TokenTree as TT,
};
use ::quote::{
    format_ident,
    quote,
    quote_spanned,
    ToTokens,
};
use ::syn::{*,
    parse::{Parse, Parser, ParseStream},
    punctuated::Punctuated,
    Result, // Explicitly shadow it
    spanned::Spanned,
};

/// [`#[::core::prelude::v1::cfg_eval]`](
/// https://doc.rust-lang.org/1.70.0/core/prelude/v1/attr.cfg_eval.html)
/// in stable Rust.
///
///   - Note: this macro, by default, only works on `struct`, `enum`, and
///     `union` definitions (_i.e._, on `#[derive]` input).
///
///     Enable `features = ["items"]` to get support for arbitary items.
///
/// ## Example
///
/// ```rust
/// use ::macro_rules_attribute::apply;
///
/// #[macro_use]
/// extern crate cfg_eval;
///
/// fn main()
/// {
///     let output_without_cfg_eval = {
///         #[apply(stringify!)]
///         enum Foo {
///             Bar,
///
///             #[cfg(FALSE)]
///             NonExisting,
///         }
///     };
///     // This is usually not great.
///     assert!(output_without_cfg_eval.contains("NonExisting"));
///
///     let output_with_cfg_eval = {
///         #[cfg_eval]
///         #[apply(stringify!)]
///         enum Foo {
///             Bar,
///
///             #[cfg(FALSE)]
///             NonExisting,
///         }
///     };
///     assert_eq!(output_with_cfg_eval, stringify! {
///         enum Foo {
///             Bar,
///         }
///     });
/// }
/// ```
///
/// ## How it works
///
/// The way this is achieved is by taking advantage of **`#[derive(SomeDerive)]`
/// having `#[cfg_eval]` semantics built in**.
///
///   - This means that if you have the luxury of hesitating between offering a
///     derive macro, or an attribute macro, and need `#[cfg_eval]` semantics,
///     you'd be better off using a derive than `#[cfg_eval]`!
///
///   - but the reality is that certain macros do want to modify their input,
///     which rules out implementing it as a `#[derive]`.
///
/// #### Illustration
///
/// With that knowledge, let's see a step-by-step description of what the macro
/// is doing.
///
/// Consider, for instance, the following snippet:
///
/// ```rust
/// # #[cfg(any())] macro_rules! ignore {
/// #[cfg_eval]
/// #[other attrs...]
/// #[cfg_attr(all(), for_instance)]
/// struct Foo {
///     x: i32,
///     #[cfg(any())]
///     y: u8,
/// }
/// # }
/// ```
///
///   - Remember: `all()` is `cfg`-speak for `true`, and `any()`, for `false`.
///
/// **What `#[cfg_eval]` does**, then, on this snippet, is emitting the
/// following:
///
/// ```rust
/// # #[cfg(any())] macro_rules! ignore {
/// #[derive(RemoveExterminate)] // 👈
/// #[exterminate]               // 👈
/// #[other attrs...]
/// #[cfg_attr(all(), for_instance)]
/// struct Foo {
///     x: i32,
///     #[cfg(any())]
///     y: u8,
/// }
/// # }
/// ```
///
/// With the added macros doing what their names indicate. If this is not clear
/// enough, then feel free to read the following more detailed section:
///
/// <details class="custom"><summary><span class="summary-box"><span>Click to show</span></span></summary>
///
/// The `#[derive(RemoveExterminate)]` invocation leads to the following two
/// snippets of code, as per the rules of `#[derive()]`s (_i.e._, that the
/// annotated item be emitted independently of what the macro emits):
///
///   - _independently of what `RemoveExterminate` does_, we have:
///
///     ```rust
///     # #[cfg(any())] macro_rules! ignore {
///     #[exterminate] // 👈
///     #[other attrs...]
///     #[cfg_attr(all(), for_instance)]
///     struct Foo {
///         x: i32,
///         #[cfg(any())]
///         y: u8,
///     }
///     # }
///     ```
///
///     which then, thus, calls `#[exterminate]`.
///
///     What `#[exterminate]` does, as hinted to the attentive reader by its
///     otherwise totally unconspicuous name, is removing the annotated item:
///
///     ```rust
///     # #[cfg(any())] macro_rules! ignore {
///     /* nothing here! */
///     # }
///     ```
///
///   - _independently of the previous bullet_, `#[derive(RemoveExterminate)]`
///     gets called, and we get:
///
///     ```rust
///     # #[cfg(any())] macro_rules! ignore {
///     //! Pseudo-code!
///
///     RemoveExterminate! {
///         // Note: thanks to `#[derive]` Magicâ„¢, the following tokens have
///         // been `#[cfg_eval]`-cleaned up! // ----+
///         #[exterminate]                        // |
///         #[other attrs...]                     // |
///         #[for_instance] // <---------------------+
///         struct Foo {                          // |
///             x: i32,                           // |
///         /* removed! <----------------------------+
///             #[cfg(any())]
///             y: u8,
///          */
///         }
///     }
///     # }
///     ```
///
///     From here, this `RemoveExterminate` macro knows it has been invoked on an item
///     doomed to die. It can thus, **contrary to usual derives, reëmit the item
///     it receives, modifying it at leisure**.
///
///     What it actually does, then, is reëmitting it almost as-is, but for that
///     pesky `#[exterminate]` which is no longer useful to us.
///
///     ```rust
///     # #[cfg(any())] macro_rules! ignore {
///     #[other attrs...]
///     #[for_instance]
///     struct Foo {
///         x: i32,
///     }
///     # }
///     ```
///
/// Which matches the expected `#[cfg_eval]` semantics on that original input:
///
/// ```rust
/// # #[cfg(any())] macro_rules! ignore {
/// #[cfg_eval]
/// #[other attrs...]
/// #[cfg_attr(all(), for_instance)]
/// struct Foo {
///     x: i32,
///     #[cfg(any())]
///     y: u8,
/// }
/// # }
/// ```
///
/// ___
///
/// </details>
///
/// ## Reëxporting this macro: the `crate = ::some::path` optional attribute arg
///
/// The following section is only meaningful to macro authors wishing to
/// reëxport <code>#[[cfg_eval]]</code> / reüse it from further downstream code.
///
/// <details class="custom"><summary><span class="summary-box"><span>Click to show</span></span></summary>
///
/// Given the above implementation, it should be no surprise that
/// <code>#[[cfg_eval]]</code> needs to refer to sibling helper macros in a
/// path-robust manner.
///
/// By default, it uses `::cfg_eval::*` paths, expecting there to be an external
/// `::cfg_eval` crate with the items of this very crate (like proc-macros are
/// known to do, given lack of `$crate` for non-function-like proc-macros), as
/// **a direct dependency**.
///
/// This means that if you have your own macro which, internally (or publicly),
/// needs <code>#[[cfg_eval]]</code>, then **your downstream dependents, which
/// are likely not to depend on `::cfg_eval` _directly_**, will be unable to
/// make it work.
///
/// The solution, to this, is an optional `crate = ::some::path` attribute arg:
///
/// ```rust
/// # // for the test
/// # extern crate std as cfg_eval;
/// # #[cfg(any())]
/// # extern crate cfg_eval;
/// #
/// # extern crate self as your_crate;
/// #
/// #[doc(hidden)] /** Not part of the public API */
/// pub mod __internal {
///     # #[cfg(any())] // for the test
///     pub use ::cfg_eval;
///     # pub extern crate cfg_eval;
/// }
///
/// #[macro_export]
/// macro_rules! example {( $input:item ) => (
///     #[$crate::__internal::cfg_eval::cfg_eval(
///         // Add this:
///         crate = $crate::__internal::cfg_eval // 👈
///     )]
///     $input
/// )}
///
/// // so that downstream users can write:
/// ::your_crate::example! {
///     struct Foo { /* … */ }
/// }
/// # fn main() {}
/// ```
///
/// ___
///
/// </details>
///
/// [cfg_eval]: [macro@cfg_eval]
#[proc_macro_attribute] pub
fn cfg_eval(
    args: TokenStream,
    input: TokenStream,
) -> TokenStream
{
    cfg_eval_impl(args.into(), input.into())
    //  .map(|ret| { println!("{}", ret); ret })
        .unwrap_or_else(|err| {
            let mut errors =
                err .into_iter()
                    .map(|err| Error::new(
                        err.span(),
                        format_args!("`#[cfg_eval::cfg_eval]`: {}", err),
                    ))
            ;
            let mut err = errors.next().unwrap();
            errors.for_each(|cur| err.combine(cur));
            err.to_compile_error()
        })
        .into()
}

fn cfg_eval_impl(
    args: TokenStream2,
    input: TokenStream2,
) -> Result<TokenStream2>
{
    // Handle an optional `crate = some::path` arg
    let krate: Option<(Span, TokenStream2)> = Parser::parse2(
        |input: ParseStream<'_>| Result::<_>::Ok({
            let krate: Option<Token![crate]> = input.parse()?;
            if let Some(krate) = krate {
                let _: Token![=] = input.parse()?;
                let path: TokenStream2 = input.parse()?;
                let mut _span = krate.span();
                // not worth it, I think.
                // let path = path.into_iter().map(|tt| { _span = tt.span(); tt }).collect();
                Some((_span, path))
            } else {
                None
            }
        }),
        args,
    )?;

    let (span, krate) = krate.unwrap_or_else(|| (
        Span::mixed_site(),
        quote_spanned!(Span::mixed_site() =>
            ::cfg_eval
        ),
    ));

    let attrs_to_add = quote_spanned!(span=>
        #[::core::prelude::v1::derive(
            #krate::à¶žRemoveExterminate
        )]
        #[#krate::à¶ždalek_exterminate]
    );

    if cfg!(feature = "items") {
        return Ok(quote_spanned!(Span::mixed_site()=>
            #attrs_to_add
            enum à¶ž { à¶ž = { #input } }
        ));
    }

    Ok(quote_spanned!(Span::mixed_site()=>
        #attrs_to_add
        #input
    ))
}

#[doc(hidden)] /** Not part of the public API */
#[proc_macro_derive(à¶žRemoveExterminate)] pub
fn __(input: TokenStream)
  -> TokenStream
{
    if cfg!(not(feature = "items")) {
        // The only thing left for us to do is removing the
        // `#` and `[ …exterminate ]`
        return input.into_iter().skip(2).collect();
    }

    // From:
    // ```rs
    // #[…exterminate]
    // enum à¶ž {
    //     à¶ž = { #input }
    // }
    // ```
    // to:
    // ```rs
    // #input
    // ```
    let mut tts = input.into_iter();

    // Remove `#[…] enum ඞ`
    tts.by_ref().take(4).for_each(drop);
    // Remove the `{}` in `{ ඞ = … }`
    if let Some(::proc_macro::TokenTree::Group(g)) = tts.next() {
        tts = g.stream().into_iter();
    }
    // Remove `à¶ž =`
    tts.by_ref().take(2).for_each(drop);
    // Remove the `{}` in `{ #input }`
    if let Some(::proc_macro::TokenTree::Group(g)) = tts.next() {
        tts = g.stream().into_iter();
    }

    tts.collect()
}

#[doc(hidden)] /** Not part of the public API */
#[proc_macro_attribute] pub
fn à¶ždalek_exterminate(_: TokenStream, _: TokenStream)
  -> TokenStream
{
    <_>::default()
}