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
/* This file incorporates work covered by the following copyright and
 * permission notice:
 *   Copyright 2016 The serde Developers. See
 *   https://github.com/serde-rs/serde/blob/3f28a9324042950afa80354722aeeee1a55cbfa3/README.md#license.
 *
 *   Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 *   http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 *   <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
 *   option. This file may not be copied, modified, or distributed
 *   except according to those terms.
 */

use ast;
use attr;
use std::collections::HashSet;
use syn::{self, visit, GenericParam};

// use internals::ast::Item;
// use internals::attr;

/// Remove the default from every type parameter because in the generated `impl`s
/// they look like associated types: "error: associated type bindings are not
/// allowed here".
pub fn without_defaults(generics: &syn::Generics) -> syn::Generics {
    syn::Generics {
        params: generics
            .params
            .iter()
            .map(|generic_param| match *generic_param {
                GenericParam::Type(ref ty_param) => syn::GenericParam::Type(syn::TypeParam {
                    default: None,
                    ..ty_param.clone()
                }),
                ref param => param.clone(),
            })
            .collect(),
        ..generics.clone()
    }
}

pub fn with_where_predicates(
    generics: &syn::Generics,
    predicates: &[syn::WherePredicate],
) -> syn::Generics {
    let mut cloned = generics.clone();
    cloned
        .make_where_clause()
        .predicates
        .extend(predicates.iter().cloned());
    cloned
}

pub fn with_where_predicates_from_fields<F>(
    item: &ast::Input,
    generics: &syn::Generics,
    from_field: F,
) -> syn::Generics
where
    F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
{
    let mut cloned = generics.clone();
    {
        let fields = item.body.all_fields();
        let field_where_predicates = fields
            .iter()
            .flat_map(|field| from_field(&field.attrs))
            .flat_map(|predicates| predicates.to_vec());

        cloned
            .make_where_clause()
            .predicates
            .extend(field_where_predicates);
    }
    cloned
}

/// Puts the given bound on any generic type parameters that are used in fields
/// for which filter returns true.
///
/// For example, the following structure needs the bound `A: Debug, B: Debug`.
///
/// ```ignore
/// struct S<'b, A, B: 'b, C> {
///     a: A,
///     b: Option<&'b B>
///     #[derivative(Debug="ignore")]
///     c: C,
/// }
/// ```
pub fn with_bound<F>(
    item: &ast::Input,
    generics: &syn::Generics,
    filter: F,
    bound: &syn::Path,
) -> syn::Generics
where
    F: Fn(&attr::Field) -> bool,
{
    #[derive(Debug)]
    struct FindTyParams {
        /// Set of all generic type parameters on the current struct (A, B, C in
        /// the example). Initialized up front.
        all_ty_params: HashSet<syn::Ident>,
        /// Set of generic type parameters used in fields for which filter
        /// returns true (A and B in the example). Filled in as the visitor sees
        /// them.
        relevant_ty_params: HashSet<syn::Ident>,
    }
    impl<'ast> visit::Visit<'ast> for FindTyParams {
        fn visit_path(&mut self, path: &'ast syn::Path) {
            if is_phantom_data(path) {
                // Hardcoded exception, because `PhantomData<T>` implements
                // most traits whether or not `T` implements it.
                return;
            }
            if path.leading_colon.is_none() && path.segments.len() == 1 {
                let id = &path.segments[0].ident;
                if self.all_ty_params.contains(id) {
                    self.relevant_ty_params.insert(id.clone());
                }
            }
            visit::visit_path(self, path);
        }
    }

    let all_ty_params: HashSet<_> = generics
        .type_params()
        .map(|ty_param| ty_param.ident.clone())
        .collect();

    let relevant_tys = item
        .body
        .all_fields()
        .into_iter()
        .filter(|field| {
            if let syn::Type::Path(syn::TypePath { ref path, .. }) = *field.ty {
                !is_phantom_data(path)
            } else {
                true
            }
        })
        .filter(|field| filter(&field.attrs))
        .map(|field| &field.ty);

    let mut visitor = FindTyParams {
        all_ty_params,
        relevant_ty_params: HashSet::new(),
    };
    for ty in relevant_tys {
        visit::visit_type(&mut visitor, ty);
    }

    let mut cloned = generics.clone();
    {
        let relevant_where_predicates = generics
            .type_params()
            .map(|ty_param| &ty_param.ident)
            .filter(|id| visitor.relevant_ty_params.contains(id))
            .map(|id| -> syn::WherePredicate { parse_quote!( #id : #bound ) });

        cloned
            .make_where_clause()
            .predicates
            .extend(relevant_where_predicates);
    }
    cloned
}

#[allow(clippy::match_like_matches_macro)] // needs rustc 1.42
fn is_phantom_data(path: &syn::Path) -> bool {
    match path.segments.last() {
        Some(path) if path.ident == "PhantomData" => true,
        _ => false,
    }
}