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
405
406
407
408
409
410
411
412
413
414
use {
    either::*,
    proc_macro::TokenStream,
    proc_macro2::{Span, TokenStream as TokenStream2},
    proc_macro_error::*,
    quote::{quote, ToTokens},
    std::{env, iter, path::PathBuf},
    syn::{
        parse::{Parse, ParseStream, Parser},
        parse_macro_input,
        punctuated::Punctuated,
        GenericParam, Generics, Ident, Item, ItemEnum, ItemImpl, ItemStruct, ItemTrait, ItemUnion,
        LifetimeDef, Path, Token, TraitBound, TraitBoundModifier, Type, TypePath, Visibility,
    },
};

#[proc_macro_error]
#[proc_macro_attribute]
pub fn dyn_upcast(attr: TokenStream, item: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item as Item);
    common(
        item,
        MacroInfo {
            macro_type: MacroType::Upcast,
            attr: attr.into(),
        },
    )
    .into()
}

#[proc_macro_error]
#[proc_macro_attribute]
pub fn dyn_cast(attr: TokenStream, item: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item as Item);
    common(
        item,
        MacroInfo {
            macro_type: MacroType::Cast,
            attr: attr.into(),
        },
    )
    .into()
}

#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq)]
enum MacroType {
    Upcast,
    Cast,
}
impl MacroType {
    fn name(&self) -> &'static str {
        match self {
            Self::Upcast { .. } => "#[dyn_upcast]",
            Self::Cast { .. } => "#[dyn_cast]",
        }
    }
}

struct MacroInfo {
    macro_type: MacroType,
    attr: TokenStream2,
}
impl MacroInfo {
    fn name(&self) -> &'static str {
        self.macro_type.name()
    }
    /// Returns `None` if we should cast to `Self`.
    fn trait_target(self) -> Option<impl Iterator<Item = Path>> {
        let name = self.name();
        let attr = self.attr;
        match self.macro_type {
            MacroType::Upcast => {
                if !attr.is_empty() {
                    abort!(
                        attr,
                        "{} doesn't take any arguments when used on a trait.",
                        name
                    );
                }
                None
            }
            MacroType::Cast => {
                let parser = Punctuated::<Path, Token![,]>::parse_terminated;
                let list = parser.parse2(attr).expect_or_abort("expected a comma separated list of paths to traits which this trait should support casting into.");
                if list.is_empty() {
                    abort!(list, "expected a comma separated list of paths to traits which this trait should support casting into.");
                }
                Some(list.into_iter())
            }
        }
    }
    /// Use when we don't know the source trait. This will require that users specify both
    /// the source and target traits (`Source => Target`) for casting and only the source
    /// (`Source`) when upcasting.
    fn full_cast_config(self) -> impl Iterator<Item = (Path, Path)> {
        let attr = self.attr;
        match self.macro_type {
            MacroType::Upcast => {
                let parser = Punctuated::<Path, Token![,]>::parse_terminated;
                let list = parser.parse2(attr)
                    .expect_or_abort("expected a comma separated list of paths to traits that this type implements and wants to support upcasting into.");
                if list.is_empty() {
                    abort!(list, "expected a comma separated list of paths to traits that this type implements and wants to support upcasting into.");
                }
                Left(list.into_iter().map(|path| (path.clone(), path)))
            }
            MacroType::Cast => {
                let list = syn::parse2::<SourceWithTargets>(attr)
                    .expect_or_abort("expected a source trait that this type implements followed by an `=>` and then a comma separated list of paths to traits that this type should support casting into.");
                let source = list.source;
                Right(
                    list.targets
                        .into_iter()
                        .map(move |target| (source.clone(), target)),
                )
            }
        }
    }
}

struct SourceWithTargets {
    source: Path,
    #[allow(dead_code)]
    arrow: Token![=>],
    targets: Punctuated<Path, Token![,]>,
}
impl Parse for SourceWithTargets {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let this = Self {
            source: input.parse()?,
            arrow: input.parse()?,
            targets: input.parse_terminated(Path::parse)?,
        };
        if this.targets.first().is_none() {
            Err(input.error("expected at least one path"))
        } else {
            Ok(this)
        }
    }
}

fn common(item: Item, info: MacroInfo) -> TokenStream2 {
    // If something goes wrong just output the input:
    set_dummy(item.to_token_stream());

    match item {
        Item::Trait(item) => {
            let targets = match info.trait_target() {
                Some(v) => Left(v),
                None => Right(iter::once({
                    let ident = &item.ident;
                    // Parameters without any bounds:
                    let params = item.generics.split_for_impl().1;
                    if item.generics.params.is_empty() {
                        Path::from(item.ident.clone())
                    } else {
                        syn::parse2::<Path>(quote! { #ident #params })
                            .expect("internal error: failed to generate a path to the defined trait")
                    }
                })),
            };
            add_dyn_cast_super_traits(item, targets)
        }
        Item::Enum(ItemEnum {
            ref ident,
            ref generics,
            ..
        })
        | Item::Struct(ItemStruct {
            ref ident,
            ref generics,
            ..
        })
        | Item::Union(ItemUnion {
            ref ident,
            ref generics,
            ..
        }) => {
            let extra = generate_dyn_cast_impl(
                TypePath {
                    qself: None,
                    path: ident.clone().into(),
                }
                .into(),
                generics,
                info.full_cast_config(),
            );
            // Keep the original item unmodified and just append to it:
            let mut stream = item.into_token_stream();
            stream.extend(extra);
            stream
        }
        Item::Impl(ItemImpl {
            ref generics,
            ref self_ty,
            trait_: Some(ref trait_),
            ..
        }) => {
            let targets = match info.trait_target() {
                Some(v) => Left(v),
                None => Right(iter::once(Path::from(trait_.1.clone())))
            };
            let extra = generate_dyn_cast_impl(
                (**self_ty).clone(),
                generics,
                targets.map(|target| (trait_.1.clone(), target))
            );
            // Keep the original item unmodified and just append to it:
            let mut stream = item.into_token_stream();
            stream.extend(extra);
            stream
        }
        other => abort!(
            other,
            "{} can only be used on trait, struct, enum or union definitions and on trait implementations.",
            info.name()
        ),
    }
}

fn add_dyn_cast_super_traits(
    mut trait_def: ItemTrait,
    targets: impl Iterator<Item = Path>,
) -> TokenStream2 {
    let my_crate = my_crate();
    let mut output = TokenStream2::new();
    for target in targets {
        // Try to generate a unique name for the config type:
        let config_name = {
            let target_name = target
                .segments
                .iter()
                .map(|path_seg| path_seg.ident.to_string())
                .collect::<Vec<_>>();
            let target_name = target_name.join("_");

            Ident::new(
                &format!("__{}To{}DynCastConfig", trait_def.ident, target_name),
                Span::call_site(),
            )
        };
        let config_path = {
            let trait_vis = &trait_def.vis;
            let source_ident = &trait_def.ident;

            let generics = &mut trait_def.generics;
            move_bounds_to_where_clause(generics);
            let where_clause = generics
                .where_clause
                .as_ref()
                .map(|where_clause| &where_clause.predicates);
            let params = &generics.params;
            let phantom_marker: Punctuated<TokenStream2, Token![,]> = params
                .iter()
                .map(|param| {
                    if let syn::GenericParam::Lifetime(_) = param {
                        quote!(::core::marker::PhantomData<&#param ()>)
                    } else {
                        quote!(::core::marker::PhantomData<#param>)
                    }
                })
                .collect();

            match trait_vis {
                Visibility::Public(_) | Visibility::Crate(_) => {
                    // Hide generated config types in private modules so that they aren't
                    // exposed to users of crates that makes use of this macro:
                    output.extend(quote! {
                        #[doc(hidden)]
                        mod #config_name {
                            #trait_vis struct Config<#params>(#phantom_marker) where #where_clause;
                        }
                        #my_crate::impl_dyn_cast_config!(
                            for<#params> #config_name::Config<#params> where {#where_clause} = #source_ident<#params> => #target
                        );
                    });
                    quote! { #config_name::Config<#params> }
                }
                Visibility::Restricted(_) | Visibility::Inherited => {
                    // It would be hard to modify the `Restricted` path to be valid
                    // in another module and since the type's visibility is restricted
                    // it won't be visible to users of the current crate anyway, so
                    // lets just generate it in the current module. (For `Inherited`
                    // visibility there is nothing to gain from putting the type
                    // inside another module as the type can't be less visible than
                    // it already is.)
                    output.extend(quote! {
                        #my_crate::create_dyn_cast_config!(
                            #[doc(hidden)]
                            #trait_vis #config_name<#params> where {#where_clause} = #source_ident<#params> => #target
                        );
                    });
                    quote! { #config_name<#params> }
                }
            }
        };
        trait_def.supertraits.push(
            TraitBound {
                paren_token: None,
                modifier: TraitBoundModifier::None,
                lifetimes: None,
                path: syn::parse2(quote! { #my_crate::DynCast<#config_path> })
                    .expect("internal error: failed to generate a supertrait bound"),
            }
            .into(),
        )
    }
    output.extend(trait_def.into_token_stream());
    output
}

fn generate_dyn_cast_impl(
    self_type: Type,
    generics: &Generics,
    config: impl Iterator<Item = (Path, Path)>,
) -> TokenStream2 {
    let mut generics = generics.clone();
    move_bounds_to_where_clause(&mut generics);
    let where_clause = generics
        .where_clause
        .as_ref()
        .map(|where_clause| &where_clause.predicates)
        .filter(|predicates| !predicates.is_empty());
    let params = &generics.params;

    let my_crate = my_crate();
    let mut output = TokenStream2::new();
    for (source, target) in config {
        if params.is_empty() && where_clause.is_none() {
            // This macro implementation is currently simpler and might allow for
            // foreign types (more relaxed regarding orphan rules) so we use it
            // when we can.
            output.extend(quote! {
                #my_crate::impl_dyn_cast!(#self_type as #source => #target);
            });
        } else {
            output.extend(quote! {
                #my_crate::impl_dyn_cast!(for<#params> #self_type as #source where {#where_clause} => #target);
            });
        }
    }
    output
}

/// Move any bounds in the parameters into the where clause.
///
/// For example `<T: Clone>` would be changed into `<T> where T: Clone`.
fn move_bounds_to_where_clause(generics: &mut Generics) {
    generics.make_where_clause();
    let where_clause = generics.where_clause.as_mut().unwrap();
    for outer_param in generics.params.iter_mut() {
        match outer_param {
            GenericParam::Type(param) => {
                let ident = &param.ident;
                let bounds = &param.bounds;
                if !bounds.is_empty() {
                    where_clause.predicates.push(
                        syn::parse2(quote! {#ident: #bounds})
                            .expect("internal error: failed to generate a type bound"),
                    );
                }
                *outer_param = GenericParam::Type(ident.clone().into());
            }
            GenericParam::Lifetime(param) => {
                let lifetime = &param.lifetime;
                let bounds = &param.bounds;
                if !bounds.is_empty() {
                    where_clause.predicates.push(
                        syn::parse2(quote! {#lifetime: #bounds})
                            .expect("internal error: failed to generate a lifetime bound"),
                    );
                }
                *outer_param =
                    GenericParam::Lifetime(LifetimeDef::new(param.lifetime.clone()).into());
            }
            GenericParam::Const(param) => {
                param.attrs.clear();
                param.eq_token.take();
                param.default.take();
            }
        }
    }
}

/// Get an identifier that resolves to the current crate. Can be used where `$crate`
/// would be used in a declarative macro.
fn my_crate() -> TokenStream2 {
    const ORIGINAL_NAME: &str = "cast_trait_object";

    let is_test = {
        // This is only true if we are compiling the parent directory:
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .and_then(|macro_crate| {
                let macro_crate = macro_crate.join("cast_trait_object");
                let current = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR")?);
                Some(macro_crate == PathBuf::from(current))
            })
    };
    if is_test.unwrap_or(false) {
        quote!(crate)
    } else {
        let name = proc_macro_crate::crate_name(ORIGINAL_NAME).unwrap_or_else(|e| {
            abort_call_site!(
                "expected `{}` to be present in `Cargo.toml`: {}",
                ORIGINAL_NAME,
                e
            );
            // ORIGINAL_NAME.to_string()
        });
        let ident = Ident::new(&name, Span::call_site());
        quote! { ::#ident }
    }
}