2017-04-13 17:34:42 -07:00
|
|
|
// Copyright 2017 Serde Developers
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2016-07-21 22:51:28 -07:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2018-04-12 22:39:26 -07:00
|
|
|
use syn;
|
2018-04-12 22:58:24 -07:00
|
|
|
use syn::punctuated::{Pair, Punctuated};
|
2018-04-12 22:39:26 -07:00
|
|
|
use syn::visit::{self, Visit};
|
2016-04-13 00:34:29 -07:00
|
|
|
|
2018-04-12 22:58:24 -07:00
|
|
|
use internals::ast::{Container, Data};
|
2016-06-30 19:53:57 -07:00
|
|
|
use internals::attr;
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2018-03-31 23:46:25 +02:00
|
|
|
use proc_macro2::Span;
|
2017-02-20 13:18:38 -08:00
|
|
|
|
2016-04-13 23:51:04 -07:00
|
|
|
// Remove the default from every type parameter because in the generated impls
|
|
|
|
// they look like associated types: "error: associated type bindings are not
|
|
|
|
// allowed here".
|
2016-09-10 21:53:14 -07:00
|
|
|
pub fn without_defaults(generics: &syn::Generics) -> syn::Generics {
|
|
|
|
syn::Generics {
|
2018-01-08 21:49:09 -08:00
|
|
|
params: generics
|
|
|
|
.params
|
2017-02-12 21:59:04 -08:00
|
|
|
.iter()
|
2018-01-08 21:49:09 -08:00
|
|
|
.map(|param| match *param {
|
2018-04-12 22:58:24 -07:00
|
|
|
syn::GenericParam::Type(ref param) => syn::GenericParam::Type(syn::TypeParam {
|
|
|
|
eq_token: None,
|
|
|
|
default: None,
|
|
|
|
..param.clone()
|
|
|
|
}),
|
2018-01-08 21:49:09 -08:00
|
|
|
_ => param.clone(),
|
2017-12-23 20:13:08 -08:00
|
|
|
})
|
2017-02-12 21:59:04 -08:00
|
|
|
.collect(),
|
|
|
|
..generics.clone()
|
2016-04-13 23:51:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
pub fn with_where_predicates(
|
|
|
|
generics: &syn::Generics,
|
|
|
|
predicates: &[syn::WherePredicate],
|
|
|
|
) -> syn::Generics {
|
2017-02-20 13:18:38 -08:00
|
|
|
let mut generics = generics.clone();
|
2018-04-12 22:58:24 -07:00
|
|
|
generics
|
|
|
|
.make_where_clause()
|
2017-04-13 12:28:23 -07:00
|
|
|
.predicates
|
2018-01-08 21:49:09 -08:00
|
|
|
.extend(predicates.into_iter().cloned());
|
2017-02-20 13:18:38 -08:00
|
|
|
generics
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
2018-04-12 22:48:31 -07:00
|
|
|
pub fn with_where_predicates_from_fields(
|
2017-04-14 15:52:56 -07:00
|
|
|
cont: &Container,
|
2017-04-13 12:28:23 -07:00
|
|
|
generics: &syn::Generics,
|
2018-04-12 22:48:31 -07:00
|
|
|
from_field: fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
|
|
|
|
) -> syn::Generics {
|
2018-05-19 17:33:30 -07:00
|
|
|
let predicates = cont
|
|
|
|
.data
|
2018-04-12 18:27:19 -07:00
|
|
|
.all_fields()
|
|
|
|
.flat_map(|field| from_field(&field.attrs))
|
|
|
|
.flat_map(|predicates| predicates.to_vec());
|
|
|
|
|
2017-02-20 13:18:38 -08:00
|
|
|
let mut generics = generics.clone();
|
2018-05-08 10:07:17 -07:00
|
|
|
generics.make_where_clause().predicates.extend(predicates);
|
|
|
|
generics
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_where_predicates_from_variants(
|
|
|
|
cont: &Container,
|
|
|
|
generics: &syn::Generics,
|
|
|
|
from_variant: fn(&attr::Variant) -> Option<&[syn::WherePredicate]>,
|
|
|
|
) -> syn::Generics {
|
|
|
|
let variants = match cont.data {
|
|
|
|
Data::Enum(ref variants) => variants,
|
|
|
|
Data::Struct(_, _) => {
|
|
|
|
return generics.clone();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let predicates = variants
|
|
|
|
.iter()
|
|
|
|
.flat_map(|variant| from_variant(&variant.attrs))
|
|
|
|
.flat_map(|predicates| predicates.to_vec());
|
|
|
|
|
|
|
|
let mut generics = generics.clone();
|
2018-04-12 22:58:24 -07:00
|
|
|
generics.make_where_clause().predicates.extend(predicates);
|
2017-02-20 13:18:38 -08:00
|
|
|
generics
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
2016-07-21 22:51:28 -07:00
|
|
|
// Puts the given bound on any generic type parameters that are used in fields
|
|
|
|
// for which filter returns true.
|
|
|
|
//
|
|
|
|
// For example, the following struct needs the bound `A: Serialize, B: Serialize`.
|
|
|
|
//
|
|
|
|
// struct S<'b, A, B: 'b, C> {
|
|
|
|
// a: A,
|
|
|
|
// b: Option<&'b B>
|
|
|
|
// #[serde(skip_serializing)]
|
|
|
|
// c: C,
|
|
|
|
// }
|
2018-04-12 22:48:31 -07:00
|
|
|
pub fn with_bound(
|
2017-04-14 15:52:56 -07:00
|
|
|
cont: &Container,
|
2017-04-13 12:28:23 -07:00
|
|
|
generics: &syn::Generics,
|
2018-04-12 22:48:31 -07:00
|
|
|
filter: fn(&attr::Field, Option<&attr::Variant>) -> bool,
|
2017-04-13 12:28:23 -07:00
|
|
|
bound: &syn::Path,
|
2018-04-12 22:48:31 -07:00
|
|
|
) -> syn::Generics {
|
2018-04-12 22:39:26 -07:00
|
|
|
struct FindTyParams<'ast> {
|
2016-07-21 22:51:28 -07:00
|
|
|
// Set of all generic type parameters on the current struct (A, B, C in
|
|
|
|
// the example). Initialized up front.
|
2018-04-12 22:04:34 -07:00
|
|
|
all_type_params: HashSet<syn::Ident>,
|
2018-04-12 22:39:26 -07:00
|
|
|
|
2016-07-21 22:51:28 -07:00
|
|
|
// 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.
|
2018-04-12 22:04:34 -07:00
|
|
|
relevant_type_params: HashSet<syn::Ident>,
|
2018-04-12 22:39:26 -07:00
|
|
|
|
|
|
|
// Fields whose type is an associated type of one of the generic type
|
|
|
|
// parameters.
|
|
|
|
associated_type_usage: Vec<&'ast syn::TypePath>,
|
2016-06-04 16:12:01 -07:00
|
|
|
}
|
2018-04-12 22:39:26 -07:00
|
|
|
impl<'ast> Visit<'ast> for FindTyParams<'ast> {
|
|
|
|
fn visit_field(&mut self, field: &'ast syn::Field) {
|
|
|
|
if let syn::Type::Path(ref ty) = field.ty {
|
|
|
|
if let Some(Pair::Punctuated(ref t, _)) = ty.path.segments.first() {
|
|
|
|
if self.all_type_params.contains(&t.ident) {
|
|
|
|
self.associated_type_usage.push(ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.visit_type(&field.ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_path(&mut self, path: &'ast syn::Path) {
|
2016-07-22 09:05:36 -07:00
|
|
|
if let Some(seg) = path.segments.last() {
|
2018-01-08 21:49:09 -08:00
|
|
|
if seg.into_value().ident == "PhantomData" {
|
2016-07-22 09:05:36 -07:00
|
|
|
// Hardcoded exception, because PhantomData<T> implements
|
|
|
|
// Serialize and Deserialize whether or not T implements it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-01-08 21:49:09 -08:00
|
|
|
if path.leading_colon.is_none() && path.segments.len() == 1 {
|
2018-05-20 19:34:52 -07:00
|
|
|
let id = &path.segments[0].ident;
|
|
|
|
if self.all_type_params.contains(id) {
|
|
|
|
self.relevant_type_params.insert(id.clone());
|
2016-07-21 22:51:28 -07:00
|
|
|
}
|
2016-06-04 16:12:01 -07:00
|
|
|
}
|
2018-01-08 21:49:09 -08:00
|
|
|
visit::visit_path(self, path);
|
2016-06-04 16:12:01 -07:00
|
|
|
}
|
2017-11-05 12:18:39 -08:00
|
|
|
|
|
|
|
// Type parameter should not be considered used by a macro path.
|
|
|
|
//
|
|
|
|
// struct TypeMacro<T> {
|
|
|
|
// mac: T!(),
|
|
|
|
// marker: PhantomData<T>,
|
|
|
|
// }
|
2018-04-12 22:39:26 -07:00
|
|
|
fn visit_macro(&mut self, _mac: &'ast syn::Macro) {}
|
2016-06-04 16:12:01 -07:00
|
|
|
}
|
|
|
|
|
2018-05-20 19:34:52 -07:00
|
|
|
let all_type_params = generics.type_params().map(|param| param.ident.clone()).collect();
|
2016-06-04 16:12:01 -07:00
|
|
|
|
2016-07-21 22:51:28 -07:00
|
|
|
let mut visitor = FindTyParams {
|
2018-04-12 22:04:34 -07:00
|
|
|
all_type_params: all_type_params,
|
|
|
|
relevant_type_params: HashSet::new(),
|
2018-04-12 22:39:26 -07:00
|
|
|
associated_type_usage: Vec::new(),
|
2016-07-21 22:51:28 -07:00
|
|
|
};
|
2018-01-08 21:49:09 -08:00
|
|
|
match cont.data {
|
|
|
|
Data::Enum(ref variants) => for variant in variants.iter() {
|
2017-12-23 20:13:08 -08:00
|
|
|
let relevant_fields = variant
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.filter(|field| filter(&field.attrs, Some(&variant.attrs)));
|
|
|
|
for field in relevant_fields {
|
2018-04-12 22:39:26 -07:00
|
|
|
visitor.visit_field(field.original);
|
2017-08-07 17:22:26 -07:00
|
|
|
}
|
2017-12-23 20:13:08 -08:00
|
|
|
},
|
2018-01-08 21:49:09 -08:00
|
|
|
Data::Struct(_, ref fields) => {
|
2017-08-07 17:22:26 -07:00
|
|
|
for field in fields.iter().filter(|field| filter(&field.attrs, None)) {
|
2018-04-12 22:39:26 -07:00
|
|
|
visitor.visit_field(field.original);
|
2017-08-07 17:22:26 -07:00
|
|
|
}
|
|
|
|
}
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
2016-07-21 22:51:28 -07:00
|
|
|
|
2018-04-12 22:39:26 -07:00
|
|
|
let relevant_type_params = visitor.relevant_type_params;
|
|
|
|
let associated_type_usage = visitor.associated_type_usage;
|
2018-04-12 22:58:24 -07:00
|
|
|
let new_predicates = generics
|
|
|
|
.type_params()
|
2018-05-20 19:34:52 -07:00
|
|
|
.map(|param| param.ident.clone())
|
2018-04-12 22:39:26 -07:00
|
|
|
.filter(|id| relevant_type_params.contains(id))
|
2018-04-12 22:58:24 -07:00
|
|
|
.map(|id| syn::TypePath {
|
|
|
|
qself: None,
|
|
|
|
path: id.into(),
|
|
|
|
})
|
2018-04-12 22:39:26 -07:00
|
|
|
.chain(associated_type_usage.into_iter().cloned())
|
|
|
|
.map(|bounded_ty| {
|
2018-01-08 21:49:09 -08:00
|
|
|
syn::WherePredicate::Type(syn::PredicateType {
|
|
|
|
lifetimes: None,
|
2017-12-23 20:13:08 -08:00
|
|
|
// the type parameter that is being bounded e.g. T
|
2018-04-12 22:39:26 -07:00
|
|
|
bounded_ty: syn::Type::Path(bounded_ty),
|
2018-01-08 21:49:09 -08:00
|
|
|
colon_token: Default::default(),
|
2017-12-23 20:13:08 -08:00
|
|
|
// the bound e.g. Serialize
|
2018-04-12 22:58:24 -07:00
|
|
|
bounds: vec![syn::TypeParamBound::Trait(syn::TraitBound {
|
|
|
|
paren_token: None,
|
|
|
|
modifier: syn::TraitBoundModifier::None,
|
|
|
|
lifetimes: None,
|
|
|
|
path: bound.clone(),
|
|
|
|
})].into_iter()
|
|
|
|
.collect(),
|
2017-12-23 20:13:08 -08:00
|
|
|
})
|
|
|
|
});
|
2017-02-20 13:18:38 -08:00
|
|
|
|
|
|
|
let mut generics = generics.clone();
|
2018-04-12 22:58:24 -07:00
|
|
|
generics
|
|
|
|
.make_where_clause()
|
2018-01-08 21:49:09 -08:00
|
|
|
.predicates
|
|
|
|
.extend(new_predicates);
|
2017-02-20 13:18:38 -08:00
|
|
|
generics
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
2017-02-20 13:18:38 -08:00
|
|
|
|
2017-04-18 14:23:21 -07:00
|
|
|
pub fn with_self_bound(
|
|
|
|
cont: &Container,
|
|
|
|
generics: &syn::Generics,
|
|
|
|
bound: &syn::Path,
|
|
|
|
) -> syn::Generics {
|
2017-02-20 17:03:38 -08:00
|
|
|
let mut generics = generics.clone();
|
2018-04-12 22:58:24 -07:00
|
|
|
generics
|
|
|
|
.make_where_clause()
|
2017-04-13 12:28:23 -07:00
|
|
|
.predicates
|
2018-01-08 21:49:09 -08:00
|
|
|
.push(syn::WherePredicate::Type(syn::PredicateType {
|
|
|
|
lifetimes: None,
|
|
|
|
// the type that is being bounded e.g. MyStruct<'a, T>
|
|
|
|
bounded_ty: type_of_item(cont),
|
|
|
|
colon_token: Default::default(),
|
|
|
|
// the bound e.g. Default
|
2018-04-12 22:58:24 -07:00
|
|
|
bounds: vec![syn::TypeParamBound::Trait(syn::TraitBound {
|
|
|
|
paren_token: None,
|
|
|
|
modifier: syn::TraitBoundModifier::None,
|
|
|
|
lifetimes: None,
|
|
|
|
path: bound.clone(),
|
|
|
|
})].into_iter()
|
|
|
|
.collect(),
|
2018-01-08 21:49:09 -08:00
|
|
|
}));
|
2017-02-20 17:03:38 -08:00
|
|
|
generics
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
pub fn with_lifetime_bound(generics: &syn::Generics, lifetime: &str) -> syn::Generics {
|
2018-03-31 23:46:25 +02:00
|
|
|
let bound = syn::Lifetime::new(lifetime, Span::call_site());
|
2018-01-08 21:49:09 -08:00
|
|
|
let def = syn::LifetimeDef {
|
|
|
|
attrs: Vec::new(),
|
2018-05-20 19:34:52 -07:00
|
|
|
lifetime: bound.clone(),
|
2018-01-08 21:49:09 -08:00
|
|
|
colon_token: None,
|
|
|
|
bounds: Punctuated::new(),
|
|
|
|
};
|
2017-02-20 13:18:38 -08:00
|
|
|
|
2018-01-08 21:49:09 -08:00
|
|
|
let params = Some(syn::GenericParam::Lifetime(def))
|
|
|
|
.into_iter()
|
2018-04-12 22:58:24 -07:00
|
|
|
.chain(generics.params.iter().cloned().map(|mut param| {
|
|
|
|
match param {
|
|
|
|
syn::GenericParam::Lifetime(ref mut param) => {
|
2018-05-20 19:34:52 -07:00
|
|
|
param.bounds.push(bound.clone());
|
2018-01-08 21:49:09 -08:00
|
|
|
}
|
2018-04-12 22:58:24 -07:00
|
|
|
syn::GenericParam::Type(ref mut param) => {
|
2018-05-20 19:34:52 -07:00
|
|
|
param.bounds.push(syn::TypeParamBound::Lifetime(bound.clone()));
|
2018-04-12 22:58:24 -07:00
|
|
|
}
|
|
|
|
syn::GenericParam::Const(_) => {}
|
|
|
|
}
|
|
|
|
param
|
|
|
|
}))
|
2018-01-08 21:49:09 -08:00
|
|
|
.collect();
|
2017-02-20 13:18:38 -08:00
|
|
|
|
2018-01-08 21:49:09 -08:00
|
|
|
syn::Generics {
|
|
|
|
params: params,
|
|
|
|
..generics.clone()
|
2017-02-20 13:18:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:49:09 -08:00
|
|
|
fn type_of_item(cont: &Container) -> syn::Type {
|
|
|
|
syn::Type::Path(syn::TypePath {
|
|
|
|
qself: None,
|
|
|
|
path: syn::Path {
|
|
|
|
leading_colon: None,
|
2018-04-12 22:58:24 -07:00
|
|
|
segments: vec![syn::PathSegment {
|
2018-05-20 19:34:52 -07:00
|
|
|
ident: cont.ident.clone(),
|
2018-04-12 22:58:24 -07:00
|
|
|
arguments: syn::PathArguments::AngleBracketed(
|
|
|
|
syn::AngleBracketedGenericArguments {
|
|
|
|
colon2_token: None,
|
|
|
|
lt_token: Default::default(),
|
2018-05-19 17:33:30 -07:00
|
|
|
args: cont
|
|
|
|
.generics
|
2018-04-12 22:58:24 -07:00
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.map(|param| match *param {
|
|
|
|
syn::GenericParam::Type(ref param) => {
|
|
|
|
syn::GenericArgument::Type(syn::Type::Path(syn::TypePath {
|
|
|
|
qself: None,
|
2018-05-20 19:34:52 -07:00
|
|
|
path: param.ident.clone().into(),
|
2018-04-12 22:58:24 -07:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
syn::GenericParam::Lifetime(ref param) => {
|
2018-05-20 19:34:52 -07:00
|
|
|
syn::GenericArgument::Lifetime(param.lifetime.clone())
|
2018-04-12 22:58:24 -07:00
|
|
|
}
|
|
|
|
syn::GenericParam::Const(_) => {
|
|
|
|
panic!("Serde does not support const generics yet");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
gt_token: Default::default(),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}].into_iter()
|
|
|
|
.collect(),
|
2017-04-13 12:28:23 -07:00
|
|
|
},
|
2018-01-08 21:49:09 -08:00
|
|
|
})
|
2017-02-20 17:03:38 -08:00
|
|
|
}
|