Rollup merge of #104391 - nnethercote:deriving-cleanups, r=jackh726

Deriving cleanups

Fixing some minor problems `@RalfJung` found in #99046.

r? `@RalfJung`
This commit is contained in:
Matthias Krüger 2022-11-15 01:40:44 +01:00 committed by GitHub
commit aea4c0c1b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 37 additions and 58 deletions

View File

@ -392,15 +392,7 @@ pub struct Generics {
impl Default for Generics { impl Default for Generics {
/// Creates an instance of `Generics`. /// Creates an instance of `Generics`.
fn default() -> Generics { fn default() -> Generics {
Generics { Generics { params: Vec::new(), where_clause: Default::default(), span: DUMMY_SP }
params: Vec::new(),
where_clause: WhereClause {
has_where_token: false,
predicates: Vec::new(),
span: DUMMY_SP,
},
span: DUMMY_SP,
}
} }
} }
@ -415,6 +407,12 @@ pub struct WhereClause {
pub span: Span, 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. /// A single predicate in a where-clause.
#[derive(Clone, Encodable, Decodable, Debug)] #[derive(Clone, Encodable, Decodable, Debug)]
pub enum WherePredicate { pub enum WherePredicate {

View File

@ -1,4 +1,3 @@
use crate::deriving::generic::ty::*;
use crate::deriving::generic::*; use crate::deriving::generic::*;
use crate::deriving::path_std; use crate::deriving::path_std;
@ -19,7 +18,6 @@ pub fn expand_deriving_copy(
path: path_std!(marker::Copy), path: path_std!(marker::Copy),
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: Bounds::empty(),
supports_unions: true, supports_unions: true,
methods: Vec::new(), methods: Vec::new(),
associated_types: Vec::new(), associated_types: Vec::new(),

View File

@ -75,7 +75,6 @@ pub fn expand_deriving_clone(
path: path_std!(clone::Clone), path: path_std!(clone::Clone),
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: bounds, additional_bounds: bounds,
generics: Bounds::empty(),
supports_unions: true, supports_unions: true,
methods: vec![MethodDef { methods: vec![MethodDef {
name: sym::clone, name: sym::clone,

View File

@ -28,7 +28,6 @@ pub fn expand_deriving_eq(
path: path_std!(cmp::Eq), path: path_std!(cmp::Eq),
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: Bounds::empty(),
supports_unions: true, supports_unions: true,
methods: vec![MethodDef { methods: vec![MethodDef {
name: sym::assert_receiver_is_total_eq, name: sym::assert_receiver_is_total_eq,

View File

@ -22,7 +22,6 @@ pub fn expand_deriving_ord(
path: path_std!(cmp::Ord), path: path_std!(cmp::Ord),
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: Bounds::empty(),
supports_unions: false, supports_unions: false,
methods: vec![MethodDef { methods: vec![MethodDef {
name: sym::cmp, name: sym::cmp,

View File

@ -86,7 +86,6 @@ pub fn expand_deriving_partial_eq(
path: path_std!(cmp::PartialEq), path: path_std!(cmp::PartialEq),
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: Bounds::empty(),
supports_unions: false, supports_unions: false,
methods, methods,
associated_types: Vec::new(), associated_types: Vec::new(),

View File

@ -40,7 +40,6 @@ pub fn expand_deriving_partial_ord(
path: path_std!(cmp::PartialOrd), path: path_std!(cmp::PartialOrd),
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: vec![], additional_bounds: vec![],
generics: Bounds::empty(),
supports_unions: false, supports_unions: false,
methods: vec![partial_cmp_def], methods: vec![partial_cmp_def],
associated_types: Vec::new(), associated_types: Vec::new(),

View File

@ -23,7 +23,6 @@ pub fn expand_deriving_debug(
path: path_std!(fmt::Debug), path: path_std!(fmt::Debug),
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: Bounds::empty(),
supports_unions: false, supports_unions: false,
methods: vec![MethodDef { methods: vec![MethodDef {
name: sym::fmt, name: sym::fmt,

View File

@ -26,7 +26,6 @@ pub fn expand_deriving_rustc_decodable(
path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global), path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global),
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: Bounds::empty(),
supports_unions: false, supports_unions: false,
methods: vec![MethodDef { methods: vec![MethodDef {
name: sym::decode, name: sym::decode,

View File

@ -27,7 +27,6 @@ pub fn expand_deriving_default(
path: Path::new(vec![kw::Default, sym::Default]), path: Path::new(vec![kw::Default, sym::Default]),
skip_path_as_bound: has_a_default_variant(item), skip_path_as_bound: has_a_default_variant(item),
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: Bounds::empty(),
supports_unions: false, supports_unions: false,
methods: vec![MethodDef { methods: vec![MethodDef {
name: kw::Default, name: kw::Default,

View File

@ -110,7 +110,6 @@ pub fn expand_deriving_rustc_encodable(
path: Path::new_(vec![krate, sym::Encodable], vec![], PathKind::Global), path: Path::new_(vec![krate, sym::Encodable], vec![], PathKind::Global),
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: Bounds::empty(),
supports_unions: false, supports_unions: false,
methods: vec![MethodDef { methods: vec![MethodDef {
name: sym::encode, name: sym::encode,

View File

@ -195,9 +195,6 @@ pub struct TraitDef<'a> {
/// other than the current trait /// other than the current trait
pub additional_bounds: Vec<Ty>, pub additional_bounds: Vec<Ty>,
/// Any extra lifetimes and/or bounds, e.g., `D: serialize::Decoder`
pub generics: Bounds,
/// Can this trait be derived for unions? /// Can this trait be derived for unions?
pub supports_unions: bool, pub supports_unions: bool,
@ -583,14 +580,16 @@ impl<'a> TraitDef<'a> {
}) })
}); });
let Generics { mut params, mut where_clause, .. } = let mut where_clause = ast::WhereClause::default();
self.generics.to_generics(cx, self.span, type_ident, generics);
where_clause.span = generics.where_clause.span; where_clause.span = generics.where_clause.span;
let ctxt = self.span.ctxt(); let ctxt = self.span.ctxt();
let span = generics.span.with_ctxt(ctxt); let span = generics.span.with_ctxt(ctxt);
// Create the generic parameters // Create the generic parameters
params.extend(generics.params.iter().map(|param| match &param.kind { let params: Vec<_> = generics
.params
.iter()
.map(|param| match &param.kind {
GenericParamKind::Lifetime { .. } => param.clone(), GenericParamKind::Lifetime { .. } => param.clone(),
GenericParamKind::Type { .. } => { GenericParamKind::Type { .. } => {
// I don't think this can be moved out of the loop, since // I don't think this can be moved out of the loop, since
@ -622,7 +621,8 @@ impl<'a> TraitDef<'a> {
param_clone.kind = const_nodefault_kind; param_clone.kind = const_nodefault_kind;
param_clone param_clone
} }
})); })
.collect();
// and similarly for where clauses // and similarly for where clauses
where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| { where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| {
@ -1062,18 +1062,15 @@ impl<'a> MethodDef<'a> {
trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, true); trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, true);
mk_body(cx, selflike_fields) mk_body(cx, selflike_fields)
} else { } else {
// Neither packed nor copy. Need to use ref patterns. // Packed and not copy. Need to use ref patterns.
let prefixes: Vec<_> = let prefixes: Vec<_> =
(0..selflike_args.len()).map(|i| format!("__self_{}", i)).collect(); (0..selflike_args.len()).map(|i| format!("__self_{}", i)).collect();
let addr_of = always_copy; let selflike_fields = trait_.create_struct_pattern_fields(cx, struct_def, &prefixes);
let selflike_fields =
trait_.create_struct_pattern_fields(cx, struct_def, &prefixes, addr_of);
let mut body = mk_body(cx, selflike_fields); let mut body = mk_body(cx, selflike_fields);
let struct_path = cx.path(span, vec![Ident::new(kw::SelfUpper, type_ident.span)]); 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 = 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. // Do the let-destructuring.
let mut stmts: Vec<_> = iter::zip(selflike_args, patterns) let mut stmts: Vec<_> = iter::zip(selflike_args, patterns)
@ -1254,9 +1251,7 @@ impl<'a> MethodDef<'a> {
// A single arm has form (&VariantK, &VariantK, ...) => BodyK // A single arm has form (&VariantK, &VariantK, ...) => BodyK
// (see "Final wrinkle" note below for why.) // (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);
let fields =
trait_.create_struct_pattern_fields(cx, &variant.data, &prefixes, addr_of);
let sp = variant.span.with_ctxt(trait_.span.ctxt()); let sp = variant.span.with_ctxt(trait_.span.ctxt());
let variant_path = cx.path(sp, vec![type_ident, variant.ident]); let variant_path = cx.path(sp, vec![type_ident, variant.ident]);
@ -1519,15 +1514,13 @@ impl<'a> TraitDef<'a> {
cx: &mut ExtCtxt<'_>, cx: &mut ExtCtxt<'_>,
struct_def: &'a VariantData, struct_def: &'a VariantData,
prefixes: &[String], prefixes: &[String],
addr_of: bool,
) -> Vec<FieldInfo> { ) -> Vec<FieldInfo> {
self.create_fields(struct_def, |i, _struct_field, sp| { self.create_fields(struct_def, |i, _struct_field, sp| {
prefixes prefixes
.iter() .iter()
.map(|prefix| { .map(|prefix| {
let ident = self.mk_pattern_ident(prefix, i); let ident = self.mk_pattern_ident(prefix, i);
let expr = cx.expr_path(cx.path_ident(sp, ident)); cx.expr_path(cx.path_ident(sp, ident))
if addr_of { cx.expr_addr_of(sp, expr) } else { expr }
}) })
.collect() .collect()
}) })

View File

@ -25,7 +25,6 @@ pub fn expand_deriving_hash(
path, path,
skip_path_as_bound: false, skip_path_as_bound: false,
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: Bounds::empty(),
supports_unions: false, supports_unions: false,
methods: vec![MethodDef { methods: vec![MethodDef {
name: sym::hash, name: sym::hash,