From 9985f4611b0d52e7faf6fc68adcdbced786871a9 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 14 Nov 2022 13:49:08 +1100 Subject: [PATCH 1/3] Clarify `expand_struct_method_body`. Spotted by @RalfJung. This causes no behavioural changes. --- compiler/rustc_builtin_macros/src/deriving/generic/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 16ee3aa89bb12..7b3c73de6de7d 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -1060,18 +1060,17 @@ impl<'a> MethodDef<'a> { trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, true); mk_body(cx, selflike_fields) } else { - // Neither packed nor copy. Need to use ref patterns. + // Packed and not copy. Need to use ref patterns. let prefixes: Vec<_> = (0..selflike_args.len()).map(|i| format!("__self_{}", i)).collect(); - let addr_of = always_copy; + let addr_of = false; let selflike_fields = trait_.create_struct_pattern_fields(cx, struct_def, &prefixes, addr_of); let mut body = mk_body(cx, selflike_fields); let struct_path = cx.path(span, vec![Ident::new(kw::SelfUpper, type_ident.span)]); - let by_ref = ByRef::from(is_packed && !always_copy); let patterns = - trait_.create_struct_patterns(cx, struct_path, struct_def, &prefixes, by_ref); + trait_.create_struct_patterns(cx, struct_path, struct_def, &prefixes, ByRef::Yes); // Do the let-destructuring. let mut stmts: Vec<_> = iter::zip(selflike_args, patterns) From 96280b6a1d39af0d6305cfeb53a2468964e04155 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 14 Nov 2022 13:59:54 +1100 Subject: [PATCH 2/3] Remove `addr_of` argument from `create_struct_pattern_fields`. Because it's always false. --- .../rustc_builtin_macros/src/deriving/generic/mod.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 7b3c73de6de7d..73cf1989341e8 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -1063,9 +1063,7 @@ impl<'a> MethodDef<'a> { // Packed and not copy. Need to use ref patterns. let prefixes: Vec<_> = (0..selflike_args.len()).map(|i| format!("__self_{}", i)).collect(); - let addr_of = false; - let selflike_fields = - trait_.create_struct_pattern_fields(cx, struct_def, &prefixes, addr_of); + let selflike_fields = trait_.create_struct_pattern_fields(cx, struct_def, &prefixes); let mut body = mk_body(cx, selflike_fields); let struct_path = cx.path(span, vec![Ident::new(kw::SelfUpper, type_ident.span)]); @@ -1251,9 +1249,7 @@ impl<'a> MethodDef<'a> { // A single arm has form (&VariantK, &VariantK, ...) => BodyK // (see "Final wrinkle" note below for why.) - let addr_of = false; // because enums can't be repr(packed) - let fields = - trait_.create_struct_pattern_fields(cx, &variant.data, &prefixes, addr_of); + let fields = trait_.create_struct_pattern_fields(cx, &variant.data, &prefixes); let sp = variant.span.with_ctxt(trait_.span.ctxt()); let variant_path = cx.path(sp, vec![type_ident, variant.ident]); @@ -1516,15 +1512,13 @@ impl<'a> TraitDef<'a> { cx: &mut ExtCtxt<'_>, struct_def: &'a VariantData, prefixes: &[String], - addr_of: bool, ) -> Vec { self.create_fields(struct_def, |i, _struct_field, sp| { prefixes .iter() .map(|prefix| { let ident = self.mk_pattern_ident(prefix, i); - let expr = cx.expr_path(cx.path_ident(sp, ident)); - if addr_of { cx.expr_addr_of(sp, expr) } else { expr } + cx.expr_path(cx.path_ident(sp, ident)) }) .collect() }) From 111db7d3a87ff0bfc8753e4d03563de2539dd9da Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 14 Nov 2022 15:43:10 +1100 Subject: [PATCH 3/3] Remove TraitDef::generics. Because it's always empty. --- compiler/rustc_ast/src/ast.rs | 16 +++--- .../src/deriving/bounds.rs | 2 - .../src/deriving/clone.rs | 1 - .../src/deriving/cmp/eq.rs | 1 - .../src/deriving/cmp/ord.rs | 1 - .../src/deriving/cmp/partial_eq.rs | 1 - .../src/deriving/cmp/partial_ord.rs | 1 - .../src/deriving/debug.rs | 1 - .../src/deriving/decodable.rs | 1 - .../src/deriving/default.rs | 1 - .../src/deriving/encodable.rs | 1 - .../src/deriving/generic/mod.rs | 52 +++++++++---------- .../rustc_builtin_macros/src/deriving/hash.rs | 1 - 13 files changed, 33 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 4ef43735a62c8..9cd954e3199de 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -392,15 +392,7 @@ pub struct Generics { impl Default for Generics { /// Creates an instance of `Generics`. fn default() -> Generics { - Generics { - params: Vec::new(), - where_clause: WhereClause { - has_where_token: false, - predicates: Vec::new(), - span: DUMMY_SP, - }, - span: DUMMY_SP, - } + Generics { params: Vec::new(), where_clause: Default::default(), span: DUMMY_SP } } } @@ -415,6 +407,12 @@ pub struct WhereClause { pub span: Span, } +impl Default for WhereClause { + fn default() -> WhereClause { + WhereClause { has_where_token: false, predicates: Vec::new(), span: DUMMY_SP } + } +} + /// A single predicate in a where-clause. #[derive(Clone, Encodable, Decodable, Debug)] pub enum WherePredicate { diff --git a/compiler/rustc_builtin_macros/src/deriving/bounds.rs b/compiler/rustc_builtin_macros/src/deriving/bounds.rs index 7bd344467d032..a5aa46aae77a7 100644 --- a/compiler/rustc_builtin_macros/src/deriving/bounds.rs +++ b/compiler/rustc_builtin_macros/src/deriving/bounds.rs @@ -1,4 +1,3 @@ -use crate::deriving::generic::ty::*; use crate::deriving::generic::*; use crate::deriving::path_std; @@ -18,7 +17,6 @@ pub fn expand_deriving_copy( path: path_std!(marker::Copy), skip_path_as_bound: false, additional_bounds: Vec::new(), - generics: Bounds::empty(), supports_unions: true, methods: Vec::new(), associated_types: Vec::new(), diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs index fa8685f5f4e56..70e28d29dc1a8 100644 --- a/compiler/rustc_builtin_macros/src/deriving/clone.rs +++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs @@ -74,7 +74,6 @@ pub fn expand_deriving_clone( path: path_std!(clone::Clone), skip_path_as_bound: false, additional_bounds: bounds, - generics: Bounds::empty(), supports_unions: true, methods: vec![MethodDef { name: sym::clone, diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs index eab67b0d354cf..4ed32c4274b6a 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs @@ -27,7 +27,6 @@ pub fn expand_deriving_eq( path: path_std!(cmp::Eq), skip_path_as_bound: false, additional_bounds: Vec::new(), - generics: Bounds::empty(), supports_unions: true, methods: vec![MethodDef { name: sym::assert_receiver_is_total_eq, diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs index 7f117981a9a2f..7e36389fd168d 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs @@ -21,7 +21,6 @@ pub fn expand_deriving_ord( path: path_std!(cmp::Ord), skip_path_as_bound: false, additional_bounds: Vec::new(), - generics: Bounds::empty(), supports_unions: false, methods: vec![MethodDef { name: sym::cmp, diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs index 236cbccaf9fee..53d7b05e59373 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs @@ -85,7 +85,6 @@ pub fn expand_deriving_partial_eq( path: path_std!(cmp::PartialEq), skip_path_as_bound: false, additional_bounds: Vec::new(), - generics: Bounds::empty(), supports_unions: false, methods, associated_types: Vec::new(), diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs index 4173403a1b84a..b619719570c16 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs @@ -39,7 +39,6 @@ pub fn expand_deriving_partial_ord( path: path_std!(cmp::PartialOrd), skip_path_as_bound: false, additional_bounds: vec![], - generics: Bounds::empty(), supports_unions: false, methods: vec![partial_cmp_def], associated_types: Vec::new(), diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs index 2cf614ed9476c..faffb9f8abc5b 100644 --- a/compiler/rustc_builtin_macros/src/deriving/debug.rs +++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs @@ -22,7 +22,6 @@ pub fn expand_deriving_debug( path: path_std!(fmt::Debug), skip_path_as_bound: false, additional_bounds: Vec::new(), - generics: Bounds::empty(), supports_unions: false, methods: vec![MethodDef { name: sym::fmt, diff --git a/compiler/rustc_builtin_macros/src/deriving/decodable.rs b/compiler/rustc_builtin_macros/src/deriving/decodable.rs index d669f616802fe..71b523ac6aca9 100644 --- a/compiler/rustc_builtin_macros/src/deriving/decodable.rs +++ b/compiler/rustc_builtin_macros/src/deriving/decodable.rs @@ -25,7 +25,6 @@ pub fn expand_deriving_rustc_decodable( path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global), skip_path_as_bound: false, additional_bounds: Vec::new(), - generics: Bounds::empty(), supports_unions: false, methods: vec![MethodDef { name: sym::decode, diff --git a/compiler/rustc_builtin_macros/src/deriving/default.rs b/compiler/rustc_builtin_macros/src/deriving/default.rs index 17df9fb279ad6..66007e040479e 100644 --- a/compiler/rustc_builtin_macros/src/deriving/default.rs +++ b/compiler/rustc_builtin_macros/src/deriving/default.rs @@ -26,7 +26,6 @@ pub fn expand_deriving_default( path: Path::new(vec![kw::Default, sym::Default]), skip_path_as_bound: has_a_default_variant(item), additional_bounds: Vec::new(), - generics: Bounds::empty(), supports_unions: false, methods: vec![MethodDef { name: kw::Default, diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs index f83f58b97d38f..b5ed4bfb1c7a7 100644 --- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs +++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs @@ -109,7 +109,6 @@ pub fn expand_deriving_rustc_encodable( path: Path::new_(vec![krate, sym::Encodable], vec![], PathKind::Global), skip_path_as_bound: false, additional_bounds: Vec::new(), - generics: Bounds::empty(), supports_unions: false, methods: vec![MethodDef { name: sym::encode, diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 73cf1989341e8..b873018660e75 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -195,9 +195,6 @@ pub struct TraitDef<'a> { /// other than the current trait pub additional_bounds: Vec, - /// Any extra lifetimes and/or bounds, e.g., `D: serialize::Decoder` - pub generics: Bounds, - /// Can this trait be derived for unions? pub supports_unions: bool, @@ -581,19 +578,21 @@ impl<'a> TraitDef<'a> { }) }); - let Generics { mut params, mut where_clause, .. } = - self.generics.to_generics(cx, self.span, type_ident, generics); + let mut where_clause = ast::WhereClause::default(); where_clause.span = generics.where_clause.span; let ctxt = self.span.ctxt(); let span = generics.span.with_ctxt(ctxt); // Create the generic parameters - params.extend(generics.params.iter().map(|param| match ¶m.kind { - GenericParamKind::Lifetime { .. } => param.clone(), - GenericParamKind::Type { .. } => { - // I don't think this can be moved out of the loop, since - // a GenericBound requires an ast id - let bounds: Vec<_> = + let params: Vec<_> = generics + .params + .iter() + .map(|param| match ¶m.kind { + GenericParamKind::Lifetime { .. } => param.clone(), + GenericParamKind::Type { .. } => { + // I don't think this can be moved out of the loop, since + // a GenericBound requires an ast id + let bounds: Vec<_> = // extra restrictions on the generics parameters to the // type being derived upon self.additional_bounds.iter().map(|p| { @@ -606,21 +605,22 @@ impl<'a> TraitDef<'a> { param.bounds.iter().cloned() ).collect(); - cx.typaram(param.ident.span.with_ctxt(ctxt), param.ident, bounds, None) - } - GenericParamKind::Const { ty, kw_span, .. } => { - let const_nodefault_kind = GenericParamKind::Const { - ty: ty.clone(), - kw_span: kw_span.with_ctxt(ctxt), - - // We can't have default values inside impl block - default: None, - }; - let mut param_clone = param.clone(); - param_clone.kind = const_nodefault_kind; - param_clone - } - })); + cx.typaram(param.ident.span.with_ctxt(ctxt), param.ident, bounds, None) + } + GenericParamKind::Const { ty, kw_span, .. } => { + let const_nodefault_kind = GenericParamKind::Const { + ty: ty.clone(), + kw_span: kw_span.with_ctxt(ctxt), + + // We can't have default values inside impl block + default: None, + }; + let mut param_clone = param.clone(); + param_clone.kind = const_nodefault_kind; + param_clone + } + }) + .collect(); // and similarly for where clauses where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| { diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs index 6e9d5f08b9443..53b6620efd6c0 100644 --- a/compiler/rustc_builtin_macros/src/deriving/hash.rs +++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs @@ -24,7 +24,6 @@ pub fn expand_deriving_hash( path, skip_path_as_bound: false, additional_bounds: Vec::new(), - generics: Bounds::empty(), supports_unions: false, methods: vec![MethodDef { name: sym::hash,