2016-04-13 00:34:29 -07:00
|
|
|
use aster::AstBuilder;
|
|
|
|
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ptr::P;
|
|
|
|
use syntax::visit;
|
|
|
|
|
2016-06-30 19:53:57 -07:00
|
|
|
use internals::ast::Item;
|
|
|
|
use internals::attr;
|
2016-05-20 22:03:20 -07: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".
|
|
|
|
pub fn without_defaults(generics: &ast::Generics) -> ast::Generics {
|
|
|
|
ast::Generics {
|
2016-05-02 07:08:43 +02:00
|
|
|
ty_params: generics.ty_params.iter().map(|ty_param| {
|
2016-04-13 23:51:04 -07:00
|
|
|
ast::TyParam {
|
|
|
|
default: None,
|
|
|
|
.. ty_param.clone()
|
2016-05-02 07:08:43 +02:00
|
|
|
}}).collect(),
|
2016-04-13 23:51:04 -07:00
|
|
|
.. generics.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:03:20 -07:00
|
|
|
pub fn with_where_predicates(
|
|
|
|
builder: &AstBuilder,
|
|
|
|
generics: &ast::Generics,
|
2016-06-04 14:24:30 -07:00
|
|
|
predicates: &[ast::WherePredicate],
|
2016-05-20 22:03:20 -07:00
|
|
|
) -> ast::Generics {
|
|
|
|
builder.from_generics(generics.clone())
|
2016-06-04 14:24:30 -07:00
|
|
|
.with_predicates(predicates.to_vec())
|
2016-05-20 22:03:20 -07:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_where_predicates_from_fields<F>(
|
2016-04-13 00:34:29 -07:00
|
|
|
builder: &AstBuilder,
|
2016-06-14 23:37:20 -07:00
|
|
|
item: &Item,
|
2016-04-13 00:34:29 -07:00
|
|
|
generics: &ast::Generics,
|
2016-05-20 22:03:20 -07:00
|
|
|
from_field: F,
|
2016-06-14 23:37:20 -07:00
|
|
|
) -> ast::Generics
|
2016-06-19 20:15:49 -07:00
|
|
|
where F: Fn(&attr::Field) -> Option<&[ast::WherePredicate]>,
|
2016-05-20 22:03:20 -07:00
|
|
|
{
|
2016-06-14 23:37:20 -07:00
|
|
|
builder.from_generics(generics.clone())
|
2016-05-20 22:03:20 -07:00
|
|
|
.with_predicates(
|
2016-06-14 23:37:20 -07:00
|
|
|
item.body.all_fields()
|
|
|
|
.flat_map(|field| from_field(&field.attrs))
|
2016-06-04 14:24:30 -07:00
|
|
|
.flat_map(|predicates| predicates.to_vec()))
|
2016-06-14 23:37:20 -07:00
|
|
|
.build()
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_bound<F>(
|
|
|
|
builder: &AstBuilder,
|
2016-06-14 23:37:20 -07:00
|
|
|
item: &Item,
|
2016-05-20 22:03:20 -07:00
|
|
|
generics: &ast::Generics,
|
|
|
|
filter: F,
|
2016-04-24 10:59:57 -07:00
|
|
|
bound: &ast::Path,
|
2016-06-14 23:37:20 -07:00
|
|
|
) -> ast::Generics
|
2016-06-19 20:15:49 -07:00
|
|
|
where F: Fn(&attr::Field) -> bool,
|
2016-05-20 22:03:20 -07:00
|
|
|
{
|
2016-06-14 23:37:20 -07:00
|
|
|
builder.from_generics(generics.clone())
|
2016-04-13 00:34:29 -07:00
|
|
|
.with_predicates(
|
2016-06-14 23:37:20 -07:00
|
|
|
item.body.all_fields()
|
|
|
|
.filter(|&field| filter(&field.attrs))
|
|
|
|
.map(|field| &field.ty)
|
2016-06-04 16:12:01 -07:00
|
|
|
.filter(|ty| !contains_recursion(ty, item.ident))
|
2016-04-13 00:34:29 -07:00
|
|
|
.map(|ty| strip_reference(ty))
|
|
|
|
.map(|ty| builder.where_predicate()
|
|
|
|
// the type that is being bounded e.g. T
|
|
|
|
.bound().build(ty.clone())
|
|
|
|
// the bound e.g. Serialize
|
2016-04-24 10:59:57 -07:00
|
|
|
.bound().trait_(bound.clone()).build()
|
2016-04-13 00:34:29 -07:00
|
|
|
.build()))
|
2016-06-14 23:37:20 -07:00
|
|
|
.build()
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
|
|
|
|
2016-06-04 16:12:01 -07:00
|
|
|
// We do not attempt to generate any bounds based on field types that are
|
|
|
|
// directly recursive, as in:
|
|
|
|
//
|
|
|
|
// struct Test<D> {
|
|
|
|
// next: Box<Test<D>>,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// This does not catch field types that are mutually recursive with some other
|
2016-06-05 10:47:40 -07:00
|
|
|
// type. For those, we require bounds to be specified by a `bound` attribute if
|
2016-06-04 16:12:01 -07:00
|
|
|
// the inferred ones are not correct.
|
|
|
|
//
|
|
|
|
// struct Test<D> {
|
2016-06-05 10:47:40 -07:00
|
|
|
// #[serde(bound="D: Serialize + Deserialize")]
|
2016-06-04 16:12:01 -07:00
|
|
|
// next: Box<Other<D>>,
|
|
|
|
// }
|
|
|
|
// struct Other<D> {
|
2016-06-05 10:47:40 -07:00
|
|
|
// #[serde(bound="D: Serialize + Deserialize")]
|
2016-06-04 16:12:01 -07:00
|
|
|
// next: Box<Test<D>>,
|
|
|
|
// }
|
|
|
|
fn contains_recursion(ty: &ast::Ty, ident: ast::Ident) -> bool {
|
|
|
|
struct FindRecursion {
|
|
|
|
ident: ast::Ident,
|
|
|
|
found_recursion: bool,
|
|
|
|
}
|
|
|
|
impl<'v> visit::Visitor<'v> for FindRecursion {
|
|
|
|
fn visit_path(&mut self, path: &'v ast::Path, _id: ast::NodeId) {
|
|
|
|
if !path.global
|
|
|
|
&& path.segments.len() == 1
|
|
|
|
&& path.segments[0].identifier == self.ident {
|
|
|
|
self.found_recursion = true;
|
|
|
|
} else {
|
|
|
|
visit::walk_path(self, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut visitor = FindRecursion {
|
|
|
|
ident: ident,
|
|
|
|
found_recursion: false,
|
|
|
|
};
|
|
|
|
visit::walk_ty(&mut visitor, ty);
|
|
|
|
visitor.found_recursion
|
|
|
|
}
|
|
|
|
|
2016-04-13 00:34:29 -07:00
|
|
|
// This is required to handle types that use both a reference and a value of
|
|
|
|
// the same type, as in:
|
|
|
|
//
|
|
|
|
// enum Test<'a, T> where T: 'a {
|
|
|
|
// Lifetime(&'a T),
|
|
|
|
// NoLifetime(T),
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Preserving references, we would generate an impl like:
|
|
|
|
//
|
|
|
|
// impl<'a, T> Serialize for Test<'a, T>
|
|
|
|
// where &'a T: Serialize,
|
|
|
|
// T: Serialize { ... }
|
|
|
|
//
|
|
|
|
// And taking a reference to one of the elements would fail with:
|
|
|
|
//
|
|
|
|
// error: cannot infer an appropriate lifetime for pattern due
|
|
|
|
// to conflicting requirements [E0495]
|
|
|
|
// Test::NoLifetime(ref v) => { ... }
|
|
|
|
// ^~~~~
|
|
|
|
//
|
|
|
|
// Instead, we strip references before adding `T: Serialize` bounds in order to
|
|
|
|
// generate:
|
|
|
|
//
|
|
|
|
// impl<'a, T> Serialize for Test<'a, T>
|
|
|
|
// where T: Serialize { ... }
|
2016-05-18 23:46:06 -07:00
|
|
|
fn strip_reference(mut ty: &P<ast::Ty>) -> &P<ast::Ty> {
|
|
|
|
while let ast::TyKind::Rptr(_, ref mut_ty) = ty.node {
|
|
|
|
ty = &mut_ty.ty;
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
2016-05-18 23:46:06 -07:00
|
|
|
ty
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|