Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn default_struct_substructure(
cx: &ExtCtxt<'_>,
trait_span: Span,
substr: &Substructure<'_>,
summary: &StaticFields,
summary: &StaticFields<'_>,
) -> BlockOrExpr {
let expr = match summary {
Unnamed(_, IsTuple::No) => cx.expr_ident(trait_span, substr.type_ident),
Expand All @@ -78,16 +78,16 @@ fn default_struct_substructure(
Named(fields) => {
let default_fields = fields
.iter()
.map(|(ident, span, default_val)| {
.map(|&(ident, span, default_val)| {
let value = match default_val {
// We use `Default::default()`.
None => default_call(cx, *span),
None => default_call(cx, span),
// We use the field default const expression.
Some(val) => {
cx.expr(val.value.span, ast::ExprKind::ConstBlock(val.clone()))
}
};
cx.field_imm(*span, *ident, value)
cx.field_imm(span, ident, value)
})
.collect();
cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
Expand Down
31 changes: 16 additions & 15 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ pub(crate) enum IsTuple {
}

/// Fields for a static method
pub(crate) enum StaticFields {
pub(crate) enum StaticFields<'a> {
/// Tuple and unit structs/enum variants like this.
Unnamed(Vec<Span>, IsTuple),
/// Normal structs/struct variants.
Named(Vec<(Ident, Span, Option<AnonConst>)>),
Named(Vec<(Ident, Span, Option<&'a AnonConst>)>),
}

/// A summary of the possible sets of fields.
Expand All @@ -331,7 +331,7 @@ pub(crate) enum SubstructureFields<'a> {
EnumDiscr(FieldInfo, Option<Box<Expr>>),

/// A static method where `Self` is a struct.
StaticStruct(&'a ast::VariantData, StaticFields),
StaticStruct(&'a ast::VariantData, StaticFields<'a>),

/// A static method where `Self` is an enum.
StaticEnum(&'a ast::EnumDef),
Expand Down Expand Up @@ -596,7 +596,7 @@ impl<'a> TraitDef<'a> {
cx: &ExtCtxt<'_>,
type_ident: Ident,
generics: &Generics,
field_tys: Vec<Box<ast::Ty>>,
field_tys: Vec<&ast::Ty>,
methods: Vec<Box<ast::AssocItem>>,
is_packed: bool,
) -> Box<ast::Item> {
Expand Down Expand Up @@ -870,8 +870,7 @@ impl<'a> TraitDef<'a> {
from_scratch: bool,
is_packed: bool,
) -> Box<ast::Item> {
let field_tys: Vec<Box<ast::Ty>> =
struct_def.fields().iter().map(|field| field.ty.clone()).collect();
let field_tys = Vec::from_iter(struct_def.fields().iter().map(|field| &*field.ty));

let methods = self
.methods
Expand Down Expand Up @@ -923,11 +922,13 @@ impl<'a> TraitDef<'a> {
generics: &Generics,
from_scratch: bool,
) -> Box<ast::Item> {
let mut field_tys = Vec::new();

for variant in &enum_def.variants {
field_tys.extend(variant.data.fields().iter().map(|field| field.ty.clone()));
}
let field_tys = Vec::from_iter(
enum_def
.variants
.iter()
.flat_map(|variant| variant.data.fields())
.map(|field| &*field.ty),
);

let methods = self
.methods
Expand Down Expand Up @@ -1160,8 +1161,8 @@ impl<'a> MethodDef<'a> {
fn expand_static_struct_method_body(
&self,
cx: &ExtCtxt<'_>,
trait_: &TraitDef<'_>,
struct_def: &VariantData,
trait_: &TraitDef<'a>,
struct_def: &'a VariantData,
type_ident: Ident,
nonselflike_args: &[Box<Expr>],
) -> BlockOrExpr {
Expand Down Expand Up @@ -1480,13 +1481,13 @@ impl<'a> MethodDef<'a> {

// general helper methods.
impl<'a> TraitDef<'a> {
fn summarise_struct(&self, cx: &ExtCtxt<'_>, struct_def: &VariantData) -> StaticFields {
fn summarise_struct(&self, cx: &ExtCtxt<'_>, struct_def: &'a VariantData) -> StaticFields<'a> {
let mut named_idents = Vec::new();
let mut just_spans = Vec::new();
for field in struct_def.fields() {
let sp = field.span.with_ctxt(self.span.ctxt());
match field.ident {
Some(ident) => named_idents.push((ident, sp, field.default.clone())),
Some(ident) => named_idents.push((ident, sp, field.default.as_ref())),
_ => just_spans.push(sp),
}
}
Expand Down
Loading