Skip to content

run rustfmt on libsyntax_ext/deriving/generic folder #34110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
860 changes: 459 additions & 401 deletions src/libsyntax_ext/deriving/generic/mod.rs

Large diffs are not rendered by default.

148 changes: 79 additions & 69 deletions src/libsyntax_ext/deriving/generic/ty.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ use syntax::ast;
use syntax::ast::{Expr, Generics, Ident, SelfKind};
use syntax::ext::base::ExtCtxt;
use syntax::ext::build::AstBuilder;
use syntax::codemap::{Span,respan};
use syntax::codemap::{Span, respan};
use syntax::ptr::P;

/// The types of pointers
@@ -35,20 +35,20 @@ pub enum PtrTy<'a> {
/// for type parameters and a lifetime.
#[derive(Clone, Eq, PartialEq)]
pub struct Path<'a> {
pub path: Vec<&'a str> ,
pub path: Vec<&'a str>,
pub lifetime: Option<&'a str>,
pub params: Vec<Box<Ty<'a>>>,
pub global: bool,
}

impl<'a> Path<'a> {
pub fn new<'r>(path: Vec<&'r str> ) -> Path<'r> {
pub fn new<'r>(path: Vec<&'r str>) -> Path<'r> {
Path::new_(path, None, Vec::new(), true)
}
pub fn new_local<'r>(path: &'r str) -> Path<'r> {
Path::new_(vec!( path ), None, Vec::new(), false)
Path::new_(vec![path], None, Vec::new(), false)
}
pub fn new_<'r>(path: Vec<&'r str> ,
pub fn new_<'r>(path: Vec<&'r str>,
lifetime: Option<&'r str>,
params: Vec<Box<Ty<'r>>>,
global: bool)
@@ -57,7 +57,7 @@ impl<'a> Path<'a> {
path: path,
lifetime: lifetime,
params: params,
global: global
global: global,
}
}

@@ -93,7 +93,7 @@ pub enum Ty<'a> {
/// parameter, and things like `i32`
Literal(Path<'a>),
/// includes unit
Tuple(Vec<Ty<'a>> )
Tuple(Vec<Ty<'a>>),
}

pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
@@ -118,14 +118,14 @@ pub fn nil_ty<'r>() -> Ty<'r> {
fn mk_lifetime(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
match *lt {
Some(ref s) => Some(cx.lifetime(span, cx.ident_of(*s).name)),
None => None
None => None,
}
}

fn mk_lifetimes(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
match *lt {
Some(ref s) => vec!(cx.lifetime(span, cx.ident_of(*s).name)),
None => vec!()
Some(ref s) => vec![cx.lifetime(span, cx.ident_of(*s).name)],
None => vec![],
}
}

@@ -144,17 +144,17 @@ impl<'a> Ty<'a> {
let lt = mk_lifetime(cx, span, lt);
cx.ty_rptr(span, raw_ty, lt, mutbl)
}
Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl)
Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl),
}
}
Literal(ref p) => { p.to_ty(cx, span, self_ty, self_generics) }
Self_ => {
cx.ty_path(self.to_path(cx, span, self_ty, self_generics))
}
Literal(ref p) => p.to_ty(cx, span, self_ty, self_generics),
Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
Tuple(ref fields) => {
let ty = ast::TyKind::Tup(fields.iter()
.map(|f| f.to_ty(cx, span, self_ty, self_generics))
.collect());
.map(|f| {
f.to_ty(cx, span, self_ty, self_generics)
})
.collect());
cx.ty(span, ty)
}
}
@@ -168,20 +168,25 @@ impl<'a> Ty<'a> {
-> ast::Path {
match *self {
Self_ => {
let self_params = self_generics.ty_params.iter().map(|ty_param| {
cx.ty_ident(span, ty_param.ident)
}).collect();
let lifetimes = self_generics.lifetimes.iter()
.map(|d| d.lifetime)
.collect();
let self_params = self_generics.ty_params
.iter()
.map(|ty_param| cx.ty_ident(span, ty_param.ident))
.collect();
let lifetimes = self_generics.lifetimes
.iter()
.map(|d| d.lifetime)
.collect();

cx.path_all(span, false, vec![self_ty], lifetimes, self_params, Vec::new())
}
Literal(ref p) => {
p.to_path(cx, span, self_ty, self_generics)
cx.path_all(span,
false,
vec![self_ty],
lifetimes,
self_params,
Vec::new())
}
Ptr(..) => { cx.span_bug(span, "pointer in a path in generic `derive`") }
Tuple(..) => { cx.span_bug(span, "tuple in a path in generic `derive`") }
Literal(ref p) => p.to_path(cx, span, self_ty, self_generics),
Ptr(..) => cx.span_bug(span, "pointer in a path in generic `derive`"),
Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
}
}
}
@@ -194,16 +199,16 @@ fn mk_ty_param(cx: &ExtCtxt,
self_ident: Ident,
self_generics: &Generics)
-> ast::TyParam {
let bounds =
bounds.iter().map(|b| {
let path = b.to_path(cx, span, self_ident, self_generics);
cx.typarambound(path)
}).collect();
let bounds = bounds.iter()
.map(|b| {
let path = b.to_path(cx, span, self_ident, self_generics);
cx.typarambound(path)
})
.collect();
cx.typaram(span, cx.ident_of(name), bounds, None)
}

fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam>)
-> Generics {
fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam>) -> Generics {
Generics {
lifetimes: lifetimes,
ty_params: P::from_vec(ty_params),
@@ -224,7 +229,8 @@ pub struct LifetimeBounds<'a> {
impl<'a> LifetimeBounds<'a> {
pub fn empty() -> LifetimeBounds<'a> {
LifetimeBounds {
lifetimes: Vec::new(), bounds: Vec::new()
lifetimes: Vec::new(),
bounds: Vec::new(),
}
}
pub fn to_generics(&self,
@@ -233,46 +239,50 @@ impl<'a> LifetimeBounds<'a> {
self_ty: Ident,
self_generics: &Generics)
-> Generics {
let lifetimes = self.lifetimes.iter().map(|&(ref lt, ref bounds)| {
let bounds =
bounds.iter().map(
|b| cx.lifetime(span, cx.ident_of(*b).name)).collect();
cx.lifetime_def(span, cx.ident_of(*lt).name, bounds)
}).collect();
let ty_params = self.bounds.iter().map(|t| {
match *t {
(ref name, ref bounds) => {
mk_ty_param(cx,
span,
*name,
bounds,
self_ty,
self_generics)
}
}
}).collect();
let lifetimes = self.lifetimes
.iter()
.map(|&(ref lt, ref bounds)| {
let bounds = bounds.iter()
.map(|b| {
cx.lifetime(span, cx.ident_of(*b).name)
})
.collect();
cx.lifetime_def(span, cx.ident_of(*lt).name, bounds)
})
.collect();
let ty_params = self.bounds
.iter()
.map(|t| {
match *t {
(ref name, ref bounds) => {
mk_ty_param(cx, span, *name, bounds, self_ty, self_generics)
}
}
})
.collect();
mk_generics(lifetimes, ty_params)
}
}

pub fn get_explicit_self(cx: &ExtCtxt, span: Span, self_ptr: &Option<PtrTy>)
-> (P<Expr>, ast::ExplicitSelf) {
pub fn get_explicit_self(cx: &ExtCtxt,
span: Span,
self_ptr: &Option<PtrTy>)
-> (P<Expr>, ast::ExplicitSelf) {
// this constructs a fresh `self` path
let self_path = cx.expr_self(span);
match *self_ptr {
None => {
(self_path, respan(span, SelfKind::Value(ast::Mutability::Immutable)))
}
None => (self_path, respan(span, SelfKind::Value(ast::Mutability::Immutable))),
Some(ref ptr) => {
let self_ty = respan(
span,
match *ptr {
Borrowed(ref lt, mutbl) => {
let lt = lt.map(|s| cx.lifetime(span, cx.ident_of(s).name));
SelfKind::Region(lt, mutbl)
}
Raw(_) => cx.span_bug(span, "attempted to use *self in deriving definition")
});
let self_ty = respan(span,
match *ptr {
Borrowed(ref lt, mutbl) => {
let lt = lt.map(|s| {
cx.lifetime(span, cx.ident_of(s).name)
});
SelfKind::Region(lt, mutbl)
}
Raw(_) => cx.span_bug(span, "attempted to use *self in deriving definition"),
});
let self_expr = cx.expr_deref(span, self_path);
(self_expr, self_ty)
}