Skip to content

Commit

Permalink
Add newtype for IsTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Feb 20, 2024
1 parent 06d6c62 commit f5d0d08
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ where
match fields {
Unnamed(fields, is_tuple) => {
let path_expr = cx.expr_path(outer_pat_path);
if !*is_tuple {
if matches!(is_tuple, IsTuple::No) {
path_expr
} else {
let fields = fields
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ fn default_struct_substructure(
let default_call = |span| cx.expr_call_global(span, default_ident.clone(), ThinVec::new());

let expr = match summary {
Unnamed(_, false) => cx.expr_ident(trait_span, substr.type_ident),
Unnamed(fields, true) => {
Unnamed(_, IsTuple::No) => cx.expr_ident(trait_span, substr.type_ident),
Unnamed(fields, IsTuple::Yes) => {
let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
cx.expr_call_ident(trait_span, substr.type_ident, exprs)
}
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,16 @@ pub struct FieldInfo {
pub other_selflike_exprs: Vec<P<Expr>>,
}

#[derive(Copy, Clone)]
pub enum IsTuple {
No,
Yes,
}

/// Fields for a static method
pub enum StaticFields {
/// Tuple and unit structs/enum variants like this.
Unnamed(Vec<Span>, bool /*is tuple*/),
Unnamed(Vec<Span>, IsTuple),
/// Normal structs/struct variants.
Named(Vec<(Ident, Span)>),
}
Expand Down Expand Up @@ -1439,7 +1445,10 @@ impl<'a> TraitDef<'a> {
}
}

let is_tuple = matches!(struct_def, ast::VariantData::Tuple(..));
let is_tuple = match struct_def {
ast::VariantData::Tuple(..) => IsTuple::Yes,
_ => IsTuple::No,
};
match (just_spans.is_empty(), named_idents.is_empty()) {
(false, false) => cx
.dcx()
Expand Down

0 comments on commit f5d0d08

Please sign in to comment.