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
// Copyright (c) 2021 René Kijewski <rene.[SURNAME]@fu-berlin.de>
// All rights reserved.
//
// This software and the accompanying materials are made available under
// the terms of the ISC License which is available in the project root as LICENSE-ISC, AND/OR
// the terms of the MIT License which is available in the project root as LICENSE-MIT, AND/OR
// the terms of the Apache License, Version 2.0 which is available in the project root as LICENSE-APACHE.
//
// You have to accept AT LEAST one of the aforementioned licenses to use, copy, modify, and/or distribute this software.
// At your will you may redistribute the software under the terms of only one, two, or all three of the aforementioned licenses.

#![forbid(unsafe_code)]
#![deny(missing_docs)]

//! ## `impl Trait` not allowed outside of function and method return types
//!
//! **… but it is now!**
//!
//! This library gives you one macro, and one macro only: [`#[desugar_impl]`][macro@desugar_impl].
//!
//! Annotate any struct, enum, or union with [`#[desugar_impl]`][macro@desugar_impl]
//! to allow the use of `field_name: impl SomeTrait` in their declaration. E.g.
//!
//! ```
//! #[desugar_impl::desugar_impl]
//! struct Test {
//!     a: impl Clone + PartialOrd,
//!     b: impl Clone + PartialOrd,
//!     c: impl Copy,
//! }
//! ```
//!
//! desugars to
//!
//! ```
//! struct Test<Ty1, Ty2, Ty3>
//! where
//!     Ty1: Clone + PartialOrd,
//!     Ty2: Clone + PartialOrd,
//!     Ty3: Copy,
//! {
//!     a: Ty1,
//!     b: Ty2,
//!     c: Ty3,
//! }
//! ```
//!
//! You can still place any `#[derive(…)]` macros just below `#[desugar_impl]` any they see work
//! with the desugared code.

use std::iter::FromIterator;

use proc_macro::{Span, TokenStream};
use quote::quote;
use syn::punctuated::Punctuated;
use syn::token::{Colon, Where};
use syn::{
    parse_macro_input, Data, DataEnum, DataStruct, DataUnion, DeriveInput, Field, Fields,
    FieldsNamed, FieldsUnnamed, GenericParam, Ident, Path, PathArguments, PathSegment,
    PredicateType, Type, TypeImplTrait, TypeParam, TypePath, WhereClause, WherePredicate,
};

/// Desugar `impl Trait` fields in a struct, enum, or union declaration.
///
/// Please see the library documentation for an explanation: [desugar_impl](index.html).
#[proc_macro_attribute]
pub fn desugar_impl(_: TokenStream, item: TokenStream) -> TokenStream {
    let mut ast = parse_macro_input!(item as DeriveInput);
    let mut ty_index = 1;

    let ast_generics = &mut ast.generics;
    let ast_data = &mut ast.data;

    let mut convert_fields = |fields: &mut Punctuated<_, _>| {
        for Field { ty, .. } in fields {
            if let Type::ImplTrait(TypeImplTrait { bounds, .. }) = ty {
                let type_ident = format!("Ty{}", ty_index);
                ty_index += 1;
                let type_ident = Ident::new(&type_ident, Span::call_site().into());
                let type_path = Type::Path(TypePath {
                    qself: None,
                    path: Path {
                        leading_colon: None,
                        segments: Punctuated::from_iter([PathSegment {
                            ident: type_ident.clone(),
                            arguments: PathArguments::None,
                        }]),
                    },
                });

                let predicate = WherePredicate::Type(PredicateType {
                    lifetimes: None,
                    bounded_ty: type_path.clone(),
                    colon_token: Colon::default(),
                    bounds: bounds.clone(),
                });
                match &mut ast_generics.where_clause {
                    Some(where_clause) => {
                        where_clause.predicates.push(predicate);
                    }
                    where_clause @ None => {
                        *where_clause = Some(WhereClause {
                            where_token: Where::default(),
                            predicates: Punctuated::from_iter([predicate]),
                        });
                    }
                }

                ast_generics.params.push(GenericParam::Type(TypeParam {
                    attrs: Vec::new(),
                    ident: type_ident,
                    colon_token: None,
                    bounds: Default::default(),
                    eq_token: None,
                    default: None,
                }));

                *ty = type_path;
            }
        }
    };

    let mut convert_some_fields = |fields: &mut Fields| match fields {
        Fields::Named(FieldsNamed { named: fields, .. })
        | Fields::Unnamed(FieldsUnnamed {
            unnamed: fields, ..
        }) => {
            convert_fields(fields);
        }
        Fields::Unit => {}
    };

    match ast_data {
        Data::Struct(DataStruct { fields, .. }) => {
            convert_some_fields(fields);
        }
        Data::Union(DataUnion {
            fields: FieldsNamed { named: fields, .. },
            ..
        }) => {
            convert_fields(fields);
        }
        Data::Enum(DataEnum { variants, .. }) => {
            for variant in variants {
                convert_some_fields(&mut variant.fields);
            }
        }
    }

    TokenStream::from(quote! { #ast })
}