From 5d36c74ea3c127b9fe77ad549e791871fd00af1e Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Mon, 6 Jun 2016 20:33:08 +0530 Subject: [PATCH] run rustfmt on libsyntax_ext/deriving/generic folder --- src/libsyntax_ext/deriving/generic/mod.rs | 860 ++++++++++++---------- src/libsyntax_ext/deriving/generic/ty.rs | 148 ++-- 2 files changed, 538 insertions(+), 470 deletions(-) diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index d6adec84e8440..714ce5a35adb2 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -192,16 +192,16 @@ use std::collections::HashSet; use std::vec; use syntax::abi::Abi; -use syntax::ast::{self, EnumDef, Expr, Ident, Generics, VariantData, BinOpKind, PatKind}; +use syntax::ast::{self, BinOpKind, EnumDef, Expr, Generics, Ident, PatKind, VariantData}; use syntax::attr; use syntax::attr::AttrMetaMethods; -use syntax::ext::base::{ExtCtxt, Annotatable}; +use syntax::ext::base::{Annotatable, ExtCtxt}; use syntax::ext::build::AstBuilder; -use syntax::codemap::{self, respan, DUMMY_SP}; +use syntax::codemap::{self, DUMMY_SP, respan}; use syntax::codemap::Span; use syntax::errors::Handler; use syntax::util::move_map::MoveMap; -use syntax::parse::token::{keywords, InternedString}; +use syntax::parse::token::{InternedString, keywords}; use syntax::ptr::P; use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty}; @@ -273,7 +273,7 @@ pub struct Substructure<'a> { pub self_args: &'a [P], /// verbatim access to any other arguments pub nonself_args: &'a [P], - pub fields: &'a SubstructureFields<'a> + pub fields: &'a SubstructureFields<'a>, } /// Summary of the relevant parts of a struct/enum field. @@ -338,7 +338,7 @@ pub type EnumNonMatchCollapsedFunc<'a> = Box]) -> P + 'a>; pub fn combine_substructure<'a>(f: CombineSubstructureFunc<'a>) - -> RefCell> { + -> RefCell> { RefCell::new(f) } @@ -388,23 +388,15 @@ impl<'a> TraitDef<'a> { cx: &mut ExtCtxt, mitem: &ast::MetaItem, item: &'a Annotatable, - push: &mut FnMut(Annotatable)) - { + push: &mut FnMut(Annotatable)) { match *item { Annotatable::Item(ref item) => { let newitem = match item.node { ast::ItemKind::Struct(ref struct_def, ref generics) => { - self.expand_struct_def(cx, - &struct_def, - item.ident, - generics) + self.expand_struct_def(cx, &struct_def, item.ident, generics) } ast::ItemKind::Enum(ref enum_def, ref generics) => { - self.expand_enum_def(cx, - enum_def, - &item.attrs, - item.ident, - generics) + self.expand_enum_def(cx, enum_def, &item.attrs, item.ident, generics) } _ => { cx.span_err(mitem.span, @@ -415,19 +407,25 @@ impl<'a> TraitDef<'a> { // Keep the lint attributes of the previous item to control how the // generated implementations are linted let mut attrs = newitem.attrs.clone(); - attrs.extend(item.attrs.iter().filter(|a| { - match &a.name()[..] { - "allow" | "warn" | "deny" | "forbid" | "stable" | "unstable" => true, - _ => false, - } - }).cloned()); - push(Annotatable::Item(P(ast::Item { - attrs: attrs, - ..(*newitem).clone() - }))) + attrs.extend(item.attrs + .iter() + .filter(|a| { + match &a.name()[..] { + "allow" | + "warn" | + "deny" | + "forbid" | + "stable" | + "unstable" => true, + _ => false, + } + }) + .cloned()); + push(Annotatable::Item(P(ast::Item { attrs: attrs, ..(*newitem).clone() }))) } _ => { - cx.span_err(mitem.span, "`derive` may only be applied to structs and enums"); + cx.span_err(mitem.span, + "`derive` may only be applied to structs and enums"); } } } @@ -468,7 +466,8 @@ impl<'a> TraitDef<'a> { type_ident: Ident, generics: &Generics, field_tys: Vec>, - methods: Vec) -> P { + methods: Vec) + -> P { let trait_path = self.path.to_path(cx, self.span, type_ident, generics); // Transform associated types from `deriving::ty::Ty` into `ast::ImplItem` @@ -480,16 +479,15 @@ impl<'a> TraitDef<'a> { vis: ast::Visibility::Inherited, defaultness: ast::Defaultness::Final, attrs: Vec::new(), - node: ast::ImplItemKind::Type(type_def.to_ty(cx, - self.span, - type_ident, - generics - )), + node: ast::ImplItemKind::Type(type_def.to_ty(cx, self.span, type_ident, generics)), } }); - let Generics { mut lifetimes, ty_params, mut where_clause } = - self.generics.to_generics(cx, self.span, type_ident, generics); + let Generics { mut lifetimes, ty_params, mut where_clause } = self.generics + .to_generics(cx, + self.span, + type_ident, + generics); let mut ty_params = ty_params.into_vec(); // Copy the lifetimes @@ -514,10 +512,7 @@ impl<'a> TraitDef<'a> { bounds.push((*declared_bound).clone()); } - cx.typaram(self.span, - ty_param.ident, - P::from_vec(bounds), - None) + cx.typaram(self.span, ty_param.ident, P::from_vec(bounds), None) })); // and similarly for where clauses @@ -535,7 +530,7 @@ impl<'a> TraitDef<'a> { ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate { span: self.span, lifetime: rb.lifetime, - bounds: rb.bounds.iter().cloned().collect() + bounds: rb.bounds.iter().cloned().collect(), }) } ast::WherePredicate::EqPredicate(ref we) => { @@ -543,7 +538,7 @@ impl<'a> TraitDef<'a> { id: ast::DUMMY_NODE_ID, span: self.span, path: we.path.clone(), - ty: we.ty.clone() + ty: we.ty.clone(), }) } } @@ -551,8 +546,8 @@ impl<'a> TraitDef<'a> { if !ty_params.is_empty() { let ty_param_names: Vec = ty_params.iter() - .map(|ty_param| ty_param.ident.name) - .collect(); + .map(|ty_param| ty_param.ident.name) + .collect(); let mut processed_field_types = HashSet::new(); for field_ty in field_tys { @@ -561,16 +556,22 @@ impl<'a> TraitDef<'a> { for ty in tys { // if we have already handled this type, skip it if let ast::TyKind::Path(_, ref p) = ty.node { - if p.segments.len() == 1 - && ty_param_names.contains(&p.segments[0].identifier.name) - || processed_field_types.contains(&p.segments) { + if p.segments.len() == 1 && + ty_param_names.contains(&p.segments[0].identifier.name) || + processed_field_types.contains(&p.segments) { continue; }; processed_field_types.insert(p.segments.clone()); } - let mut bounds: Vec<_> = self.additional_bounds.iter().map(|p| { - cx.typarambound(p.to_path(cx, self.span, type_ident, generics)) - }).collect(); + let mut bounds: Vec<_> = self.additional_bounds + .iter() + .map(|p| { + cx.typarambound(p.to_path(cx, + self.span, + type_ident, + generics)) + }) + .collect(); // require the current trait bounds.push(cx.typarambound(trait_path.clone())); @@ -591,40 +592,41 @@ impl<'a> TraitDef<'a> { let trait_generics = Generics { lifetimes: lifetimes, ty_params: P::from_vec(ty_params), - where_clause: where_clause + where_clause: where_clause, }; // Create the reference to the trait. let trait_ref = cx.trait_ref(trait_path); // Create the type parameters on the `self` path. - let self_ty_params = generics.ty_params.iter().map(|ty_param| { - cx.ty_ident(self.span, ty_param.ident) - }).collect(); + let self_ty_params = generics.ty_params + .iter() + .map(|ty_param| cx.ty_ident(self.span, ty_param.ident)) + .collect(); - let self_lifetimes: Vec = - generics.lifetimes - .iter() - .map(|ld| ld.lifetime) - .collect(); + let self_lifetimes: Vec = generics.lifetimes + .iter() + .map(|ld| ld.lifetime) + .collect(); // Create the type of `self`. - let self_type = cx.ty_path( - cx.path_all(self.span, false, vec!( type_ident ), self_lifetimes, - self_ty_params, Vec::new())); - - let attr = cx.attribute( - self.span, - cx.meta_word(self.span, - InternedString::new("automatically_derived"))); + let self_type = cx.ty_path(cx.path_all(self.span, + false, + vec![type_ident], + self_lifetimes, + self_ty_params, + Vec::new())); + + let attr = cx.attribute(self.span, + cx.meta_word(self.span, + InternedString::new("automatically_derived"))); // Just mark it now since we know that it'll end up used downstream attr::mark_used(&attr); let opt_trait_ref = Some(trait_ref); - let unused_qual = cx.attribute( - self.span, - cx.meta_list(self.span, - InternedString::new("allow"), - vec![cx.meta_word(self.span, + let unused_qual = cx.attribute(self.span, + cx.meta_list(self.span, + InternedString::new("allow"), + vec![cx.meta_word(self.span, InternedString::new("unused_qualifications"))])); let mut a = vec![attr, unused_qual]; a.extend(self.attributes.iter().cloned()); @@ -635,58 +637,63 @@ impl<'a> TraitDef<'a> { ast::Unsafety::Normal }; - cx.item( - self.span, - keywords::Invalid.ident(), - a, - ast::ItemKind::Impl(unsafety, - ast::ImplPolarity::Positive, - trait_generics, - opt_trait_ref, - self_type, - methods.into_iter().chain(associated_types).collect())) + cx.item(self.span, + keywords::Invalid.ident(), + a, + ast::ItemKind::Impl(unsafety, + ast::ImplPolarity::Positive, + trait_generics, + opt_trait_ref, + self_type, + methods.into_iter().chain(associated_types).collect())) } fn expand_struct_def(&self, cx: &mut ExtCtxt, struct_def: &'a VariantData, type_ident: Ident, - generics: &Generics) -> P { - let field_tys: Vec> = struct_def.fields().iter() - .map(|field| field.ty.clone()) - .collect(); - - let methods = self.methods.iter().map(|method_def| { - let (explicit_self, self_args, nonself_args, tys) = - method_def.split_self_nonself_args( - cx, self, type_ident, generics); - - let body = if method_def.is_static() { - method_def.expand_static_struct_method_body( - cx, - self, - struct_def, - type_ident, - &self_args[..], - &nonself_args[..]) - } else { - method_def.expand_struct_method_body(cx, - self, - struct_def, - type_ident, - &self_args[..], - &nonself_args[..]) - }; - - method_def.create_method(cx, - self, - type_ident, - generics, - Abi::Rust, - explicit_self, - tys, - body) - }).collect(); + generics: &Generics) + -> P { + let field_tys: Vec> = struct_def.fields() + .iter() + .map(|field| field.ty.clone()) + .collect(); + + let methods = self.methods + .iter() + .map(|method_def| { + let (explicit_self, self_args, nonself_args, tys) = + method_def.split_self_nonself_args(cx, + self, + type_ident, + generics); + + let body = if method_def.is_static() { + method_def.expand_static_struct_method_body(cx, + self, + struct_def, + type_ident, + &self_args[..], + &nonself_args[..]) + } else { + method_def.expand_struct_method_body(cx, + self, + struct_def, + type_ident, + &self_args[..], + &nonself_args[..]) + }; + + method_def.create_method(cx, + self, + type_ident, + generics, + Abi::Rust, + explicit_self, + tys, + body) + }) + .collect(); self.create_derived_impl(cx, type_ident, generics, field_tys, methods) } @@ -696,53 +703,60 @@ impl<'a> TraitDef<'a> { enum_def: &'a EnumDef, type_attrs: &[ast::Attribute], type_ident: Ident, - generics: &Generics) -> P { + generics: &Generics) + -> P { let mut field_tys = Vec::new(); for variant in &enum_def.variants { - field_tys.extend(variant.node.data.fields().iter() - .map(|field| field.ty.clone())); + field_tys.extend(variant.node + .data + .fields() + .iter() + .map(|field| field.ty.clone())); } - let methods = self.methods.iter().map(|method_def| { - let (explicit_self, self_args, nonself_args, tys) = - method_def.split_self_nonself_args(cx, self, - type_ident, generics); - - let body = if method_def.is_static() { - method_def.expand_static_enum_method_body( - cx, - self, - enum_def, - type_ident, - &self_args[..], - &nonself_args[..]) - } else { - method_def.expand_enum_method_body(cx, - self, - enum_def, - type_attrs, - type_ident, - self_args, - &nonself_args[..]) - }; - - method_def.create_method(cx, - self, - type_ident, - generics, - Abi::Rust, - explicit_self, - tys, - body) - }).collect(); + let methods = self.methods + .iter() + .map(|method_def| { + let (explicit_self, self_args, nonself_args, tys) = + method_def.split_self_nonself_args(cx, + self, + type_ident, + generics); + + let body = if method_def.is_static() { + method_def.expand_static_enum_method_body(cx, + self, + enum_def, + type_ident, + &self_args[..], + &nonself_args[..]) + } else { + method_def.expand_enum_method_body(cx, + self, + enum_def, + type_attrs, + type_ident, + self_args, + &nonself_args[..]) + }; + + method_def.create_method(cx, + self, + type_ident, + generics, + Abi::Rust, + explicit_self, + tys, + body) + }) + .collect(); self.create_derived_impl(cx, type_ident, generics, field_tys, methods) } } -fn find_repr_type_name(diagnostic: &Handler, - type_attrs: &[ast::Attribute]) -> &'static str { +fn find_repr_type_name(diagnostic: &Handler, type_attrs: &[ast::Attribute]) -> &'static str { let mut repr_type_name = "isize"; for a in type_attrs { for r in &attr::find_repr_attrs(diagnostic, a) { @@ -775,13 +789,13 @@ impl<'a> MethodDef<'a> { self_args: &[P], nonself_args: &[P], fields: &SubstructureFields) - -> P { + -> P { let substructure = Substructure { type_ident: type_ident, method_ident: cx.ident_of(self.name), self_args: self_args, nonself_args: nonself_args, - fields: fields + fields: fields, }; let mut f = self.combine_substructure.borrow_mut(); let f: &mut CombineSubstructureFunc = &mut *f; @@ -801,12 +815,13 @@ impl<'a> MethodDef<'a> { self.explicit_self.is_none() } - fn split_self_nonself_args(&self, - cx: &mut ExtCtxt, - trait_: &TraitDef, - type_ident: Ident, - generics: &Generics) - -> (Option, Vec>, Vec>, Vec<(Ident, P)>) { + fn split_self_nonself_args + (&self, + cx: &mut ExtCtxt, + trait_: &TraitDef, + type_ident: Ident, + generics: &Generics) + -> (Option, Vec>, Vec>, Vec<(Ident, P)>) { let mut self_args = Vec::new(); let mut nonself_args = Vec::new(); @@ -832,7 +847,7 @@ impl<'a> MethodDef<'a> { match *ty { // for static methods, just treat any Self // arguments as a normal arg - Self_ if nonstatic => { + Self_ if nonstatic => { self_args.push(arg_expr); } Ptr(ref ty, _) if **ty == Self_ && nonstatic => { @@ -854,15 +869,17 @@ impl<'a> MethodDef<'a> { generics: &Generics, abi: Abi, explicit_self: Option, - arg_types: Vec<(Ident, P)> , - body: P) -> ast::ImplItem { + arg_types: Vec<(Ident, P)>, + body: P) + -> ast::ImplItem { // create the generics that aren't for Self let fn_generics = self.generics.to_generics(cx, trait_.span, type_ident, generics); let args = { let self_args = explicit_self.map(|explicit_self| { - ast::Arg::from_self(explicit_self, respan(trait_.span, keywords::SelfValue.ident())) + ast::Arg::from_self(explicit_self, + respan(trait_.span, keywords::SelfValue.ident())) }); let nonself_args = arg_types.into_iter() .map(|(name, ty)| cx.arg(trait_.span, name, ty)); @@ -890,12 +907,13 @@ impl<'a> MethodDef<'a> { defaultness: ast::Defaultness::Final, ident: method_ident, node: ast::ImplItemKind::Method(ast::MethodSig { - generics: fn_generics, - abi: abi, - unsafety: unsafety, - constness: ast::Constness::NotConst, - decl: fn_decl - }, body_block) + generics: fn_generics, + abi: abi, + unsafety: unsafety, + constness: ast::Constness::NotConst, + decl: fn_decl, + }, + body_block), } } @@ -919,26 +937,24 @@ impl<'a> MethodDef<'a> { /// } /// ``` fn expand_struct_method_body<'b>(&self, - cx: &mut ExtCtxt, - trait_: &TraitDef<'b>, - struct_def: &'b VariantData, - type_ident: Ident, - self_args: &[P], - nonself_args: &[P]) - -> P { + cx: &mut ExtCtxt, + trait_: &TraitDef<'b>, + struct_def: &'b VariantData, + type_ident: Ident, + self_args: &[P], + nonself_args: &[P]) + -> P { let mut raw_fields = Vec::new(); // Vec<[fields of self], // [fields of next Self arg], [etc]> let mut patterns = Vec::new(); for i in 0..self_args.len() { - let struct_path= cx.path(DUMMY_SP, vec!( type_ident )); - let (pat, ident_expr) = - trait_.create_struct_pattern(cx, - struct_path, - struct_def, - &format!("__self_{}", - i), - ast::Mutability::Immutable); + let struct_path = cx.path(DUMMY_SP, vec![type_ident]); + let (pat, ident_expr) = trait_.create_struct_pattern(cx, + struct_path, + struct_def, + &format!("__self_{}", i), + ast::Mutability::Immutable); patterns.push(pat); raw_fields.push(ident_expr); } @@ -947,21 +963,23 @@ impl<'a> MethodDef<'a> { let fields = if !raw_fields.is_empty() { let mut raw_fields = raw_fields.into_iter().map(|v| v.into_iter()); let first_field = raw_fields.next().unwrap(); - let mut other_fields: Vec> - = raw_fields.collect(); + let mut other_fields: Vec> = raw_fields.collect(); first_field.map(|(span, opt_id, field, attrs)| { - FieldInfo { - span: span, - name: opt_id, - self_: field, - other: other_fields.iter_mut().map(|l| { - match l.next().unwrap() { - (_, _, ex, _) => ex - } - }).collect(), - attrs: attrs, - } - }).collect() + FieldInfo { + span: span, + name: opt_id, + self_: field, + other: other_fields.iter_mut() + .map(|l| { + match l.next().unwrap() { + (_, _, ex, _) => ex, + } + }) + .collect(), + attrs: attrs, + } + }) + .collect() } else { cx.span_bug(trait_.span, "no self arguments to non-static method in generic \ @@ -969,20 +987,20 @@ impl<'a> MethodDef<'a> { }; // body of the inner most destructuring match - let mut body = self.call_substructure_method( - cx, - trait_, - type_ident, - self_args, - nonself_args, - &Struct(struct_def, fields)); + let mut body = self.call_substructure_method(cx, + trait_, + type_ident, + self_args, + nonself_args, + &Struct(struct_def, fields)); // make a series of nested matches, to destructure the // structs. This is actually right-to-left, but it shouldn't // matter. for (arg_expr, pat) in self_args.iter().zip(patterns) { - body = cx.expr_match(trait_.span, arg_expr.clone(), - vec!( cx.arm(trait_.span, vec!(pat.clone()), body) )) + body = cx.expr_match(trait_.span, + arg_expr.clone(), + vec![cx.arm(trait_.span, vec![pat.clone()], body)]) } body @@ -995,13 +1013,14 @@ impl<'a> MethodDef<'a> { type_ident: Ident, self_args: &[P], nonself_args: &[P]) - -> P { + -> P { let summary = trait_.summarise_struct(cx, struct_def); self.call_substructure_method(cx, trait_, type_ident, - self_args, nonself_args, + self_args, + nonself_args, &StaticStruct(struct_def, summary)) } @@ -1035,16 +1054,21 @@ impl<'a> MethodDef<'a> { /// as their results are unused. The point of `__self_vi` and /// `__arg_1_vi` is for `PartialOrd`; see #15503.) fn expand_enum_method_body<'b>(&self, - cx: &mut ExtCtxt, - trait_: &TraitDef<'b>, - enum_def: &'b EnumDef, - type_attrs: &[ast::Attribute], - type_ident: Ident, - self_args: Vec>, - nonself_args: &[P]) - -> P { - self.build_enum_match_tuple( - cx, trait_, enum_def, type_attrs, type_ident, self_args, nonself_args) + cx: &mut ExtCtxt, + trait_: &TraitDef<'b>, + enum_def: &'b EnumDef, + type_attrs: &[ast::Attribute], + type_ident: Ident, + self_args: Vec>, + nonself_args: &[P]) + -> P { + self.build_enum_match_tuple(cx, + trait_, + enum_def, + type_attrs, + type_ident, + self_args, + nonself_args) } @@ -1083,46 +1107,51 @@ impl<'a> MethodDef<'a> { /// ... // catch-all remainder can inspect above variant index values. /// } /// ``` - fn build_enum_match_tuple<'b>( - &self, - cx: &mut ExtCtxt, - trait_: &TraitDef<'b>, - enum_def: &'b EnumDef, - type_attrs: &[ast::Attribute], - type_ident: Ident, - self_args: Vec>, - nonself_args: &[P]) -> P { + fn build_enum_match_tuple<'b>(&self, + cx: &mut ExtCtxt, + trait_: &TraitDef<'b>, + enum_def: &'b EnumDef, + type_attrs: &[ast::Attribute], + type_ident: Ident, + self_args: Vec>, + nonself_args: &[P]) + -> P { let sp = trait_.span; let variants = &enum_def.variants; - let self_arg_names = self_args.iter().enumerate() - .map(|(arg_count, _self_arg)| { - if arg_count == 0 { - "__self".to_string() - } else { - format!("__arg_{}", arg_count) - } - }) - .collect::>(); + let self_arg_names = self_args.iter() + .enumerate() + .map(|(arg_count, _self_arg)| { + if arg_count == 0 { + "__self".to_string() + } else { + format!("__arg_{}", arg_count) + } + }) + .collect::>(); let self_arg_idents = self_arg_names.iter() - .map(|name|cx.ident_of(&name[..])) - .collect::>(); + .map(|name| cx.ident_of(&name[..])) + .collect::>(); // The `vi_idents` will be bound, solely in the catch-all, to // a series of let statements mapping each self_arg to an int // value corresponding to its discriminant. let vi_idents: Vec = self_arg_names.iter() - .map(|name| { let vi_suffix = format!("{}_vi", &name[..]); - cx.ident_of(&vi_suffix[..]) }) - .collect::>(); + .map(|name| { + let vi_suffix = format!("{}_vi", + &name[..]); + cx.ident_of(&vi_suffix[..]) + }) + .collect::>(); // Builds, via callback to call_substructure_method, the // delegated expression that handles the catch-all case, // using `__variants_tuple` to drive logic if necessary. - let catch_all_substructure = EnumNonMatchingCollapsed( - self_arg_idents, &variants[..], &vi_idents[..]); + let catch_all_substructure = EnumNonMatchingCollapsed(self_arg_idents, + &variants[..], + &vi_idents[..]); let first_fieldless = variants.iter().find(|v| v.node.data.fields().is_empty()); @@ -1131,48 +1160,53 @@ impl<'a> MethodDef<'a> { // (Variant2, Variant2, ...) => Body2 // ... // where each tuple has length = self_args.len() - let mut match_arms: Vec = variants.iter().enumerate() - .filter(|&(_, v)| !(self.unify_fieldless_variants && v.node.data.fields().is_empty())) - .map(|(index, variant)| { - let mk_self_pat = |cx: &mut ExtCtxt, self_arg_name: &str| { - let (p, idents) = trait_.create_enum_variant_pattern( - cx, type_ident, - variant, - self_arg_name, - ast::Mutability::Immutable); - (cx.pat(sp, PatKind::Ref(p, ast::Mutability::Immutable)), idents) - }; + let mut match_arms: Vec = + variants.iter() + .enumerate() + .filter(|&(_, v)| { + !(self.unify_fieldless_variants && v.node.data.fields().is_empty()) + }) + .map(|(index, variant)| { + let mk_self_pat = |cx: &mut ExtCtxt, self_arg_name: &str| { + let (p, idents) = + trait_.create_enum_variant_pattern(cx, + type_ident, + variant, + self_arg_name, + ast::Mutability::Immutable); + (cx.pat(sp, PatKind::Ref(p, ast::Mutability::Immutable)), idents) + }; - // A single arm has form (&VariantK, &VariantK, ...) => BodyK - // (see "Final wrinkle" note below for why.) - let mut subpats = Vec::with_capacity(self_arg_names.len()); - let mut self_pats_idents = Vec::with_capacity(self_arg_names.len() - 1); - let first_self_pat_idents = { - let (p, idents) = mk_self_pat(cx, &self_arg_names[0]); - subpats.push(p); - idents - }; - for self_arg_name in &self_arg_names[1..] { - let (p, idents) = mk_self_pat(cx, &self_arg_name[..]); - subpats.push(p); - self_pats_idents.push(idents); - } + // A single arm has form (&VariantK, &VariantK, ...) => BodyK + // (see "Final wrinkle" note below for why.) + let mut subpats = Vec::with_capacity(self_arg_names.len()); + let mut self_pats_idents = Vec::with_capacity(self_arg_names.len() - 1); + let first_self_pat_idents = { + let (p, idents) = mk_self_pat(cx, &self_arg_names[0]); + subpats.push(p); + idents + }; + for self_arg_name in &self_arg_names[1..] { + let (p, idents) = mk_self_pat(cx, &self_arg_name[..]); + subpats.push(p); + self_pats_idents.push(idents); + } - // Here is the pat = `(&VariantK, &VariantK, ...)` - let single_pat = cx.pat_tuple(sp, subpats); + // Here is the pat = `(&VariantK, &VariantK, ...)` + let single_pat = cx.pat_tuple(sp, subpats); - // For the BodyK, we need to delegate to our caller, - // passing it an EnumMatching to indicate which case - // we are in. + // For the BodyK, we need to delegate to our caller, + // passing it an EnumMatching to indicate which case + // we are in. - // All of the Self args have the same variant in these - // cases. So we transpose the info in self_pats_idents - // to gather the getter expressions together, in the - // form that EnumMatching expects. + // All of the Self args have the same variant in these + // cases. So we transpose the info in self_pats_idents + // to gather the getter expressions together, in the + // form that EnumMatching expects. - // The transposition is driven by walking across the - // arg fields of the variant for the first self pat. - let field_tuples = first_self_pat_idents.into_iter().enumerate() + // The transposition is driven by walking across the + // arg fields of the variant for the first self pat. + let field_tuples = first_self_pat_idents.into_iter().enumerate() // For each arg field of self, pull out its getter expr ... .map(|(field_index, (sp, opt_ident, self_getter_expr, attrs))| { // ... but FieldInfo also wants getter expr @@ -1202,28 +1236,33 @@ impl<'a> MethodDef<'a> { } }).collect::>(); - // Now, for some given VariantK, we have built up - // expressions for referencing every field of every - // Self arg, assuming all are instances of VariantK. - // Build up code associated with such a case. - let substructure = EnumMatching(index, - variant, - field_tuples); - let arm_expr = self.call_substructure_method( - cx, trait_, type_ident, &self_args[..], nonself_args, - &substructure); - - cx.arm(sp, vec![single_pat], arm_expr) - }).collect(); + // Now, for some given VariantK, we have built up + // expressions for referencing every field of every + // Self arg, assuming all are instances of VariantK. + // Build up code associated with such a case. + let substructure = EnumMatching(index, variant, field_tuples); + let arm_expr = self.call_substructure_method(cx, + trait_, + type_ident, + &self_args[..], + nonself_args, + &substructure); + + cx.arm(sp, vec![single_pat], arm_expr) + }) + .collect(); let default = match first_fieldless { Some(v) if self.unify_fieldless_variants => { // We need a default case that handles the fieldless variants. // The index and actual variant aren't meaningful in this case, // so just use whatever - Some(self.call_substructure_method( - cx, trait_, type_ident, &self_args[..], nonself_args, - &EnumMatching(0, v, Vec::new()))) + Some(self.call_substructure_method(cx, + trait_, + type_ident, + &self_args[..], + nonself_args, + &EnumMatching(0, v, Vec::new()))) } _ if variants.len() > 1 && self_args.len() > 1 => { // Since we know that all the arguments will match if we reach @@ -1231,7 +1270,7 @@ impl<'a> MethodDef<'a> { // result of the catch all which should help llvm in optimizing it Some(deriving::call_intrinsic(cx, sp, "unreachable", vec![])) } - _ => None + _ => None, }; if let Some(arm) = default { match_arms.push(cx.arm(sp, vec![cx.pat_wild(sp)], arm)); @@ -1272,12 +1311,11 @@ impl<'a> MethodDef<'a> { // ``` let mut index_let_stmts: Vec = Vec::new(); - //We also build an expression which checks whether all discriminants are equal + // We also build an expression which checks whether all discriminants are equal // discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ... let mut discriminant_test = cx.expr_bool(sp, true); - let target_type_name = - find_repr_type_name(&cx.parse_sess.span_diagnostic, type_attrs); + let target_type_name = find_repr_type_name(&cx.parse_sess.span_diagnostic, type_attrs); let mut first_ident = None; for (&ident, self_arg) in vi_idents.iter().zip(&self_args) { @@ -1297,8 +1335,10 @@ impl<'a> MethodDef<'a> { let first_expr = cx.expr_ident(sp, first); let id = cx.expr_ident(sp, ident); let test = cx.expr_binary(sp, BinOpKind::Eq, first_expr, id); - discriminant_test = cx.expr_binary(sp, BinOpKind::And, - discriminant_test, test) + discriminant_test = cx.expr_binary(sp, + BinOpKind::And, + discriminant_test, + test) } None => { first_ident = Some(ident); @@ -1306,9 +1346,12 @@ impl<'a> MethodDef<'a> { } } - let arm_expr = self.call_substructure_method( - cx, trait_, type_ident, &self_args[..], nonself_args, - &catch_all_substructure); + let arm_expr = self.call_substructure_method(cx, + trait_, + type_ident, + &self_args[..], + nonself_args, + &catch_all_substructure); // Final wrinkle: the self_args are expressions that deref // down to desired l-values, but we cannot actually deref @@ -1318,7 +1361,7 @@ impl<'a> MethodDef<'a> { let borrowed_self_args = self_args.move_map(|self_arg| cx.expr_addr_of(sp, self_arg)); let match_arg = cx.expr(sp, ast::ExprKind::Tup(borrowed_self_args)); - //Lastly we create an expression which branches on all discriminants being equal + // Lastly we create an expression which branches on all discriminants being equal // if discriminant_test { // match (...) { // (Variant1, Variant1, ...) => Body1 @@ -1332,8 +1375,7 @@ impl<'a> MethodDef<'a> { // } let all_match = cx.expr_match(sp, match_arg, match_arms); let arm_expr = cx.expr_if(sp, discriminant_test, all_match, Some(arm_expr)); - cx.expr_block( - cx.block_all(sp, index_let_stmts, Some(arm_expr))) + cx.expr_block(cx.block_all(sp, index_let_stmts, Some(arm_expr))) } else if variants.is_empty() { // As an additional wrinkle, For a zero-variant enum A, // currently the compiler @@ -1385,8 +1427,7 @@ impl<'a> MethodDef<'a> { // that needs the feature gate enabled.) deriving::call_intrinsic(cx, sp, "unreachable", vec![]) - } - else { + } else { // Final wrinkle: the self_args are expressions that deref // down to desired l-values, but we cannot actually deref @@ -1406,26 +1447,30 @@ impl<'a> MethodDef<'a> { type_ident: Ident, self_args: &[P], nonself_args: &[P]) - -> P { - let summary = enum_def.variants.iter().map(|v| { - let ident = v.node.name; - let summary = trait_.summarise_struct(cx, &v.node.data); - (ident, v.span, summary) - }).collect(); - self.call_substructure_method(cx, trait_, type_ident, - self_args, nonself_args, + -> P { + let summary = enum_def.variants + .iter() + .map(|v| { + let ident = v.node.name; + let summary = trait_.summarise_struct(cx, &v.node.data); + (ident, v.span, summary) + }) + .collect(); + self.call_substructure_method(cx, + trait_, + type_ident, + self_args, + nonself_args, &StaticEnum(enum_def, summary)) } } // general helper methods. impl<'a> TraitDef<'a> { - fn summarise_struct(&self, - cx: &mut ExtCtxt, - struct_def: &VariantData) -> StaticFields { + fn summarise_struct(&self, cx: &mut ExtCtxt, struct_def: &VariantData) -> StaticFields { let mut named_idents = Vec::new(); let mut just_spans = Vec::new(); - for field in struct_def.fields(){ + for field in struct_def.fields() { let sp = Span { expn_id: self.span.expn_id, ..field.span }; match field.ident { Some(ident) => named_idents.push((ident, sp)), @@ -1434,9 +1479,11 @@ impl<'a> TraitDef<'a> { } match (just_spans.is_empty(), named_idents.is_empty()) { - (false, false) => cx.span_bug(self.span, - "a struct with named and unnamed \ - fields in generic `derive`"), + (false, false) => { + cx.span_bug(self.span, + "a struct with named and unnamed \ + fields in generic `derive`") + } // named fields (_, false) => Named(named_idents), // empty structs @@ -1447,46 +1494,58 @@ impl<'a> TraitDef<'a> { fn create_subpatterns(&self, cx: &mut ExtCtxt, - field_paths: Vec , + field_paths: Vec, mutbl: ast::Mutability) -> Vec> { - field_paths.iter().map(|path| { - cx.pat(path.span, - PatKind::Ident(ast::BindingMode::ByRef(mutbl), (*path).clone(), None)) - }).collect() + field_paths.iter() + .map(|path| { + cx.pat(path.span, + PatKind::Ident(ast::BindingMode::ByRef(mutbl), (*path).clone(), None)) + }) + .collect() } - fn create_struct_pattern(&self, - cx: &mut ExtCtxt, - struct_path: ast::Path, - struct_def: &'a VariantData, - prefix: &str, - mutbl: ast::Mutability) - -> (P, Vec<(Span, Option, - P, - &'a [ast::Attribute])>) { + fn create_struct_pattern + (&self, + cx: &mut ExtCtxt, + struct_path: ast::Path, + struct_def: &'a VariantData, + prefix: &str, + mutbl: ast::Mutability) + -> (P, Vec<(Span, Option, P, &'a [ast::Attribute])>) { let mut paths = Vec::new(); let mut ident_exprs = Vec::new(); for (i, struct_field) in struct_def.fields().iter().enumerate() { let sp = Span { expn_id: self.span.expn_id, ..struct_field.span }; let ident = cx.ident_of(&format!("{}_{}", prefix, i)); - paths.push(codemap::Spanned{span: sp, node: ident}); - let val = cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident))); + paths.push(codemap::Spanned { + span: sp, + node: ident, + }); + let val = cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp, ident))); let val = cx.expr(sp, ast::ExprKind::Paren(val)); ident_exprs.push((sp, struct_field.ident, val, &struct_field.attrs[..])); } let subpats = self.create_subpatterns(cx, paths, mutbl); let pattern = if struct_def.is_struct() { - let field_pats = subpats.into_iter().zip(&ident_exprs).map(|(pat, &(sp, ident, _, _))| { - if ident.is_none() { - cx.span_bug(sp, "a braced struct with unnamed fields in `derive`"); - } - codemap::Spanned { - span: pat.span, - node: ast::FieldPat { ident: ident.unwrap(), pat: pat, is_shorthand: false }, - } - }).collect(); + let field_pats = + subpats.into_iter() + .zip(&ident_exprs) + .map(|(pat, &(sp, ident, _, _))| { + if ident.is_none() { + cx.span_bug(sp, "a braced struct with unnamed fields in `derive`"); + } + codemap::Spanned { + span: pat.span, + node: ast::FieldPat { + ident: ident.unwrap(), + pat: pat, + is_shorthand: false, + }, + } + }) + .collect(); cx.pat_struct(self.span, struct_path, field_pats) } else { cx.pat_enum(self.span, struct_path, subpats) @@ -1495,20 +1554,21 @@ impl<'a> TraitDef<'a> { (pattern, ident_exprs) } - fn create_enum_variant_pattern(&self, - cx: &mut ExtCtxt, - enum_ident: ast::Ident, - variant: &'a ast::Variant, - prefix: &str, - mutbl: ast::Mutability) - -> (P, Vec<(Span, Option, P, &'a [ast::Attribute])>) { + fn create_enum_variant_pattern + (&self, + cx: &mut ExtCtxt, + enum_ident: ast::Ident, + variant: &'a ast::Variant, + prefix: &str, + mutbl: ast::Mutability) + -> (P, Vec<(Span, Option, P, &'a [ast::Attribute])>) { let variant_ident = variant.node.name; let variant_path = cx.path(variant.span, vec![enum_ident, variant_ident]); self.create_struct_pattern(cx, variant_path, &variant.node.data, prefix, mutbl) } } -/* helpful premade recipes */ +// helpful premade recipes /// Fold the fields. `use_foldl` controls whether this is done /// left-to-right (`true`) or right-to-left (`false`). @@ -1519,35 +1579,28 @@ pub fn cs_fold(use_foldl: bool, cx: &mut ExtCtxt, trait_span: Span, substructure: &Substructure) - -> P where - F: FnMut(&mut ExtCtxt, Span, P, P, &[P]) -> P, + -> P + where F: FnMut(&mut ExtCtxt, Span, P, P, &[P]) -> P { match *substructure.fields { EnumMatching(_, _, ref all_fields) | Struct(_, ref all_fields) => { if use_foldl { all_fields.iter().fold(base, |old, field| { - f(cx, - field.span, - old, - field.self_.clone(), - &field.other) + f(cx, field.span, old, field.self_.clone(), &field.other) }) } else { all_fields.iter().rev().fold(base, |old, field| { - f(cx, - field.span, - old, - field.self_.clone(), - &field.other) + f(cx, field.span, old, field.self_.clone(), &field.other) }) } - }, - EnumNonMatchingCollapsed(ref all_args, _, tuple) => - enum_nonmatch_f(cx, trait_span, (&all_args[..], tuple), - substructure.nonself_args), - StaticEnum(..) | StaticStruct(..) => { - cx.span_bug(trait_span, "static function in `derive`") } + EnumNonMatchingCollapsed(ref all_args, _, tuple) => { + enum_nonmatch_f(cx, + trait_span, + (&all_args[..], tuple), + substructure.nonself_args) + } + StaticEnum(..) | StaticStruct(..) => cx.span_bug(trait_span, "static function in `derive`"), } } @@ -1565,29 +1618,36 @@ pub fn cs_same_method(f: F, cx: &mut ExtCtxt, trait_span: Span, substructure: &Substructure) - -> P where - F: FnOnce(&mut ExtCtxt, Span, Vec>) -> P, + -> P + where F: FnOnce(&mut ExtCtxt, Span, Vec>) -> P { match *substructure.fields { EnumMatching(_, _, ref all_fields) | Struct(_, ref all_fields) => { // call self_n.method(other_1_n, other_2_n, ...) - let called = all_fields.iter().map(|field| { - cx.expr_method_call(field.span, - field.self_.clone(), - substructure.method_ident, - field.other.iter() - .map(|e| cx.expr_addr_of(field.span, e.clone())) - .collect()) - }).collect(); + let called = all_fields.iter() + .map(|field| { + cx.expr_method_call(field.span, + field.self_.clone(), + substructure.method_ident, + field.other + .iter() + .map(|e| { + cx.expr_addr_of(field.span, + e.clone()) + }) + .collect()) + }) + .collect(); f(cx, trait_span, called) - }, - EnumNonMatchingCollapsed(ref all_self_args, _, tuple) => - enum_nonmatch_f(cx, trait_span, (&all_self_args[..], tuple), - substructure.nonself_args), - StaticEnum(..) | StaticStruct(..) => { - cx.span_bug(trait_span, "static function in `derive`") } + EnumNonMatchingCollapsed(ref all_self_args, _, tuple) => { + enum_nonmatch_f(cx, + trait_span, + (&all_self_args[..], tuple), + substructure.nonself_args) + } + StaticEnum(..) | StaticStruct(..) => cx.span_bug(trait_span, "static function in `derive`"), } } @@ -1599,10 +1659,8 @@ pub fn is_type_without_fields(item: &Annotatable) -> bool { ast::ItemKind::Enum(ref enum_def, _) => { enum_def.variants.iter().all(|v| v.node.data.fields().is_empty()) } - ast::ItemKind::Struct(ref variant_data, _) => { - variant_data.fields().is_empty() - } - _ => false + ast::ItemKind::Struct(ref variant_data, _) => variant_data.fields().is_empty(), + _ => false, } } else { false diff --git a/src/libsyntax_ext/deriving/generic/ty.rs b/src/libsyntax_ext/deriving/generic/ty.rs index b581f5267eaac..455741c326024 100644 --- a/src/libsyntax_ext/deriving/generic/ty.rs +++ b/src/libsyntax_ext/deriving/generic/ty.rs @@ -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>>, 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>>, 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> ) + Tuple(Vec>), } 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 { 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 { 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, ty_params: Vec) - -> Generics { +fn mk_generics(lifetimes: Vec, ty_params: Vec) -> 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) - -> (P, ast::ExplicitSelf) { +pub fn get_explicit_self(cx: &ExtCtxt, + span: Span, + self_ptr: &Option) + -> (P, 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) }