From 4ae9488ce8fc89d1ddaa70ac148b8d66c311e517 Mon Sep 17 00:00:00 2001 From: James Sanderson Date: Sun, 15 Apr 2018 20:51:10 +0100 Subject: Provide better names for builtin deriving-generated attributes --- src/libsyntax_ext/deriving/cmp/ord.rs | 2 +- src/libsyntax_ext/deriving/cmp/partial_eq.rs | 2 +- src/libsyntax_ext/deriving/cmp/partial_ord.rs | 4 ++-- src/libsyntax_ext/deriving/debug.rs | 2 +- src/libsyntax_ext/deriving/decodable.rs | 4 ++-- src/libsyntax_ext/deriving/encodable.rs | 4 ++-- src/libsyntax_ext/deriving/generic/mod.rs | 6 +++--- src/libsyntax_ext/deriving/hash.rs | 4 ++-- 8 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src/libsyntax_ext') diff --git a/src/libsyntax_ext/deriving/cmp/ord.rs b/src/libsyntax_ext/deriving/cmp/ord.rs index 1a392ac3765..746cec91a77 100644 --- a/src/libsyntax_ext/deriving/cmp/ord.rs +++ b/src/libsyntax_ext/deriving/cmp/ord.rs @@ -38,7 +38,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt, name: "cmp", generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), - args: vec![borrowed_self()], + args: vec![(borrowed_self(), "other")], ret_ty: Literal(path_std!(cx, cmp::Ordering)), attributes: attrs, is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/cmp/partial_eq.rs b/src/libsyntax_ext/deriving/cmp/partial_eq.rs index 75db7cc1e4c..353adc9796c 100644 --- a/src/libsyntax_ext/deriving/cmp/partial_eq.rs +++ b/src/libsyntax_ext/deriving/cmp/partial_eq.rs @@ -71,7 +71,7 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt, name: $name, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), - args: vec![borrowed_self()], + args: vec![(borrowed_self(), "_other")], ret_ty: Literal(path_local!(bool)), attributes: attrs, is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/cmp/partial_ord.rs b/src/libsyntax_ext/deriving/cmp/partial_ord.rs index 92183c58eb2..5ac93a4d720 100644 --- a/src/libsyntax_ext/deriving/cmp/partial_ord.rs +++ b/src/libsyntax_ext/deriving/cmp/partial_ord.rs @@ -34,7 +34,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt, name: $name, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), - args: vec![borrowed_self()], + args: vec![(borrowed_self(), "other")], ret_ty: Literal(path_local!(bool)), attributes: attrs, is_unsafe: false, @@ -59,7 +59,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt, name: "partial_cmp", generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), - args: vec![borrowed_self()], + args: vec![(borrowed_self(), "other")], ret_ty, attributes: attrs, is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/debug.rs b/src/libsyntax_ext/deriving/debug.rs index c007285cd85..b06f30effbb 100644 --- a/src/libsyntax_ext/deriving/debug.rs +++ b/src/libsyntax_ext/deriving/debug.rs @@ -40,7 +40,7 @@ pub fn expand_deriving_debug(cx: &mut ExtCtxt, name: "fmt", generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), - args: vec![fmtr], + args: vec![(fmtr, "_f")], ret_ty: Literal(path_std!(cx, fmt::Result)), attributes: Vec::new(), is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/decodable.rs b/src/libsyntax_ext/deriving/decodable.rs index 46dada256b8..7618fe63ab3 100644 --- a/src/libsyntax_ext/deriving/decodable.rs +++ b/src/libsyntax_ext/deriving/decodable.rs @@ -67,8 +67,8 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt, PathKind::Global)])], }, explicit_self: None, - args: vec![Ptr(Box::new(Literal(Path::new_local(typaram))), - Borrowed(None, Mutability::Mutable))], + args: vec![(Ptr(Box::new(Literal(Path::new_local(typaram))), + Borrowed(None, Mutability::Mutable)), "d")], ret_ty: Literal(Path::new_(pathvec_std!(cx, result::Result), None, diff --git a/src/libsyntax_ext/deriving/encodable.rs b/src/libsyntax_ext/deriving/encodable.rs index d579b3abd45..2c6c18cc51c 100644 --- a/src/libsyntax_ext/deriving/encodable.rs +++ b/src/libsyntax_ext/deriving/encodable.rs @@ -148,8 +148,8 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt, ], }, explicit_self: borrowed_explicit_self(), - args: vec![Ptr(Box::new(Literal(Path::new_local(typaram))), - Borrowed(None, Mutability::Mutable))], + args: vec![(Ptr(Box::new(Literal(Path::new_local(typaram))), + Borrowed(None, Mutability::Mutable)), "s")], ret_ty: Literal(Path::new_( pathvec_std!(cx, result::Result), None, diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index 66053e037e1..84fcabd0f5a 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -252,7 +252,7 @@ pub struct MethodDef<'a> { pub explicit_self: Option>>, /// Arguments other than the self argument - pub args: Vec>, + pub args: Vec<(Ty<'a>, &'a str)>, /// Return type pub ret_ty: Ty<'a>, @@ -915,9 +915,9 @@ impl<'a> MethodDef<'a> { explicit_self }); - for (i, ty) in self.args.iter().enumerate() { + for (ty, name) in self.args.iter() { let ast_ty = ty.to_ty(cx, trait_.span, type_ident, generics); - let ident = cx.ident_of(&format!("__arg_{}", i)); + let ident = cx.ident_of(name).gensym(); arg_tys.push((ident, ast_ty)); let arg_expr = cx.expr_ident(trait_.span, ident); diff --git a/src/libsyntax_ext/deriving/hash.rs b/src/libsyntax_ext/deriving/hash.rs index b192ab2527e..1980fa74de0 100644 --- a/src/libsyntax_ext/deriving/hash.rs +++ b/src/libsyntax_ext/deriving/hash.rs @@ -44,8 +44,8 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt, bounds: vec![(typaram, vec![path_std!(cx, hash::Hasher)])], }, explicit_self: borrowed_explicit_self(), - args: vec![Ptr(Box::new(Literal(arg)), - Borrowed(None, Mutability::Mutable))], + args: vec![(Ptr(Box::new(Literal(arg)), + Borrowed(None, Mutability::Mutable)), "_state")], ret_ty: nil_ty(), attributes: vec![], is_unsafe: false, -- cgit 1.4.1-3-g733a5 From a3241d1350ab95e0a7ef4c84cbd828c207f211b5 Mon Sep 17 00:00:00 2001 From: James Sanderson Date: Thu, 19 Apr 2018 13:57:43 +0100 Subject: Don't prepend deriving-generated attributes with _ --- src/libsyntax_ext/deriving/cmp/partial_eq.rs | 2 +- src/libsyntax_ext/deriving/debug.rs | 2 +- src/libsyntax_ext/deriving/hash.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/libsyntax_ext') diff --git a/src/libsyntax_ext/deriving/cmp/partial_eq.rs b/src/libsyntax_ext/deriving/cmp/partial_eq.rs index 353adc9796c..680ca56c82e 100644 --- a/src/libsyntax_ext/deriving/cmp/partial_eq.rs +++ b/src/libsyntax_ext/deriving/cmp/partial_eq.rs @@ -71,7 +71,7 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt, name: $name, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), - args: vec![(borrowed_self(), "_other")], + args: vec![(borrowed_self(), "other")], ret_ty: Literal(path_local!(bool)), attributes: attrs, is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/debug.rs b/src/libsyntax_ext/deriving/debug.rs index b06f30effbb..538e1469f46 100644 --- a/src/libsyntax_ext/deriving/debug.rs +++ b/src/libsyntax_ext/deriving/debug.rs @@ -40,7 +40,7 @@ pub fn expand_deriving_debug(cx: &mut ExtCtxt, name: "fmt", generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), - args: vec![(fmtr, "_f")], + args: vec![(fmtr, "f")], ret_ty: Literal(path_std!(cx, fmt::Result)), attributes: Vec::new(), is_unsafe: false, diff --git a/src/libsyntax_ext/deriving/hash.rs b/src/libsyntax_ext/deriving/hash.rs index 1980fa74de0..67096cdb49a 100644 --- a/src/libsyntax_ext/deriving/hash.rs +++ b/src/libsyntax_ext/deriving/hash.rs @@ -45,7 +45,7 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt, }, explicit_self: borrowed_explicit_self(), args: vec![(Ptr(Box::new(Literal(arg)), - Borrowed(None, Mutability::Mutable)), "_state")], + Borrowed(None, Mutability::Mutable)), "state")], ret_ty: nil_ty(), attributes: vec![], is_unsafe: false, -- cgit 1.4.1-3-g733a5 From b9b650c412d901dc92f795c4d32eb382f530f7e1 Mon Sep 17 00:00:00 2001 From: James Sanderson Date: Thu, 19 Apr 2018 19:04:06 +0100 Subject: Gensym remaining identifiers --- src/libsyntax_ext/deriving/cmp/ord.rs | 2 +- src/libsyntax_ext/deriving/cmp/partial_ord.rs | 2 +- src/libsyntax_ext/deriving/debug.rs | 2 +- src/libsyntax_ext/deriving/generic/mod.rs | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/libsyntax_ext') diff --git a/src/libsyntax_ext/deriving/cmp/ord.rs b/src/libsyntax_ext/deriving/cmp/ord.rs index 746cec91a77..faeaed76c37 100644 --- a/src/libsyntax_ext/deriving/cmp/ord.rs +++ b/src/libsyntax_ext/deriving/cmp/ord.rs @@ -64,7 +64,7 @@ pub fn ordering_collapsed(cx: &mut ExtCtxt, } pub fn cs_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P { - let test_id = cx.ident_of("__cmp"); + let test_id = cx.ident_of("cmp").gensym(); let equals_path = cx.path_global(span, cx.std_path(&["cmp", "Ordering", "Equal"])); let cmp_path = cx.std_path(&["cmp", "Ord", "cmp"]); diff --git a/src/libsyntax_ext/deriving/cmp/partial_ord.rs b/src/libsyntax_ext/deriving/cmp/partial_ord.rs index 5ac93a4d720..a8c4e0a43ff 100644 --- a/src/libsyntax_ext/deriving/cmp/partial_ord.rs +++ b/src/libsyntax_ext/deriving/cmp/partial_ord.rs @@ -123,7 +123,7 @@ pub fn some_ordering_collapsed(cx: &mut ExtCtxt, } pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P { - let test_id = cx.ident_of("__cmp"); + let test_id = cx.ident_of("cmp").gensym(); let ordering = cx.path_global(span, cx.std_path(&["cmp", "Ordering", "Equal"])); let ordering_expr = cx.expr_path(ordering.clone()); let equals_expr = cx.expr_some(span, ordering_expr); diff --git a/src/libsyntax_ext/deriving/debug.rs b/src/libsyntax_ext/deriving/debug.rs index 538e1469f46..b546f5df157 100644 --- a/src/libsyntax_ext/deriving/debug.rs +++ b/src/libsyntax_ext/deriving/debug.rs @@ -70,7 +70,7 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P MethodDef<'a> { let vi_idents: Vec = self_arg_names.iter() .map(|name| { let vi_suffix = format!("{}_vi", &name[..]); - cx.ident_of(&vi_suffix[..]) + cx.ident_of(&vi_suffix[..]).gensym() }) .collect::>(); @@ -1616,7 +1616,7 @@ impl<'a> TraitDef<'a> { let mut ident_exprs = Vec::new(); for (i, struct_field) in struct_def.fields().iter().enumerate() { let sp = struct_field.span.with_ctxt(self.span.ctxt()); - let ident = cx.ident_of(&format!("{}_{}", prefix, i)); + let ident = cx.ident_of(&format!("{}_{}", prefix, i)).gensym(); paths.push(ident.with_span_pos(sp)); let val = cx.expr_path(cx.path_ident(sp, ident)); let val = if use_temporaries { -- cgit 1.4.1-3-g733a5 From 1a72d6dcd17ae02d56942fd3eb1a891eb51c45c6 Mon Sep 17 00:00:00 2001 From: James Sanderson Date: Thu, 19 Apr 2018 20:17:06 +0100 Subject: Fix some of the __names in the comments --- src/libsyntax_ext/deriving/cmp/ord.rs | 6 +++--- src/libsyntax_ext/deriving/cmp/partial_ord.rs | 6 +++--- src/libsyntax_ext/deriving/generic/mod.rs | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) (limited to 'src/libsyntax_ext') diff --git a/src/libsyntax_ext/deriving/cmp/ord.rs b/src/libsyntax_ext/deriving/cmp/ord.rs index faeaed76c37..99b6f752e94 100644 --- a/src/libsyntax_ext/deriving/cmp/ord.rs +++ b/src/libsyntax_ext/deriving/cmp/ord.rs @@ -77,9 +77,9 @@ pub fn cs_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P { // ::std::cmp::Ordering::Equal => { // ... // } - // __cmp => __cmp + // cmp => cmp // }, - // __cmp => __cmp + // cmp => cmp // } // cs_fold(// foldr nests the if-elses correctly, leaving the first field @@ -88,7 +88,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P { |cx, span, old, self_f, other_fs| { // match new { // ::std::cmp::Ordering::Equal => old, - // __cmp => __cmp + // cmp => cmp // } let new = { diff --git a/src/libsyntax_ext/deriving/cmp/partial_ord.rs b/src/libsyntax_ext/deriving/cmp/partial_ord.rs index a8c4e0a43ff..804b6f9a318 100644 --- a/src/libsyntax_ext/deriving/cmp/partial_ord.rs +++ b/src/libsyntax_ext/deriving/cmp/partial_ord.rs @@ -138,9 +138,9 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P< // ::std::option::Option::Some(::std::cmp::Ordering::Equal) => { // ... // } - // __cmp => __cmp + // cmp => cmp // }, - // __cmp => __cmp + // cmp => cmp // } // cs_fold(// foldr nests the if-elses correctly, leaving the first field @@ -149,7 +149,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P< |cx, span, old, self_f, other_fs| { // match new { // Some(::std::cmp::Ordering::Equal) => old, - // __cmp => __cmp + // cmp => cmp // } let new = { diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index 8799268b7e0..457cae51292 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -1004,10 +1004,10 @@ impl<'a> MethodDef<'a> { /// /// // equivalent to: /// impl PartialEq for A { - /// fn eq(&self, __arg_1: &A) -> bool { + /// fn eq(&self, other: &A) -> bool { /// match *self { /// A {x: ref __self_0_0, y: ref __self_0_1} => { - /// match *__arg_1 { + /// match *other { /// A {x: ref __self_1_0, y: ref __self_1_1} => { /// __self_0_0.eq(__self_1_0) && __self_0_1.eq(__self_1_1) /// } @@ -1020,10 +1020,10 @@ impl<'a> MethodDef<'a> { /// // or if A is repr(packed) - note fields are matched by-value /// // instead of by-reference. /// impl PartialEq for A { - /// fn eq(&self, __arg_1: &A) -> bool { + /// fn eq(&self, other: &A) -> bool { /// match *self { /// A {x: __self_0_0, y: __self_0_1} => { - /// match __arg_1 { + /// match other { /// A {x: __self_1_0, y: __self_1_1} => { /// __self_0_0.eq(&__self_1_0) && __self_0_1.eq(&__self_1_1) /// } @@ -1134,14 +1134,14 @@ impl<'a> MethodDef<'a> { /// // is equivalent to /// /// impl PartialEq for A { - /// fn eq(&self, __arg_1: &A) -> ::bool { - /// match (&*self, &*__arg_1) { + /// fn eq(&self, other: &A) -> ::bool { + /// match (&*self, &*other) { /// (&A1, &A1) => true, /// (&A2(ref self_0), /// &A2(ref __arg_1_0)) => (*self_0).eq(&(*__arg_1_0)), /// _ => { /// let __self_vi = match *self { A1(..) => 0, A2(..) => 1 }; - /// let __arg_1_vi = match *__arg_1 { A1(..) => 0, A2(..) => 1 }; + /// let __arg_1_vi = match *other { A1(..) => 0, A2(..) => 1 }; /// false /// } /// } -- cgit 1.4.1-3-g733a5 From 27b0f1e1933280e7c5b242b1e2a1f02777e23c25 Mon Sep 17 00:00:00 2001 From: James Sanderson Date: Tue, 24 Apr 2018 21:30:13 +0100 Subject: Gensym arguments for format macro --- src/libsyntax_ext/format.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/libsyntax_ext') diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index c78decb1eb9..6b155b6596d 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -543,6 +543,10 @@ impl<'a, 'b> Context<'a, 'b> { let mut pats = Vec::new(); let mut heads = Vec::new(); + let names_pos: Vec<_> = (0..self.args.len()).map(|i| { + self.ecx.ident_of(&format!("arg{}", i)).gensym() + }).collect(); + // First, build up the static array which will become our precompiled // format "string" let pieces = self.ecx.expr_vec_slice(self.fmtsp, self.str_pieces); @@ -560,7 +564,7 @@ impl<'a, 'b> Context<'a, 'b> { // of each variable because we don't want to move out of the arguments // passed to this function. for (i, e) in self.args.into_iter().enumerate() { - let name = self.ecx.ident_of(&format!("__arg{}", i)); + let name = names_pos[i]; let span = DUMMY_SP.with_ctxt(e.span.ctxt().apply_mark(self.ecx.current_expansion.mark)); pats.push(self.ecx.pat_ident(span, name)); @@ -570,14 +574,12 @@ impl<'a, 'b> Context<'a, 'b> { heads.push(self.ecx.expr_addr_of(e.span, e)); } for pos in self.count_args { - let name = self.ecx.ident_of(&match pos { - Exact(i) => format!("__arg{}", i), - _ => panic!("should never happen"), - }); - let span = match pos { - Exact(i) => spans_pos[i], + let index = match pos { + Exact(i) => i, _ => panic!("should never happen"), }; + let name = names_pos[index]; + let span = spans_pos[index]; counts.push(Context::format_arg(self.ecx, self.macsp, span, &Count, name)); } -- cgit 1.4.1-3-g733a5