@@ -448,7 +448,8 @@ impl<'a> TraitDef<'a> {
448
448
_ => unreachable ! ( ) ,
449
449
} ;
450
450
let container_id = cx. current_expansion . id . expn_data ( ) . parent . expect_local ( ) ;
451
- let always_copy = has_no_type_params && cx. resolver . has_derive_copy ( container_id) ;
451
+ let copy_fields =
452
+ is_packed && has_no_type_params && cx. resolver . has_derive_copy ( container_id) ;
452
453
453
454
let newitem = match item. kind {
454
455
ast:: ItemKind :: Struct ( ref struct_def, ref generics) => self . expand_struct_def (
@@ -457,16 +458,14 @@ impl<'a> TraitDef<'a> {
457
458
item. ident ,
458
459
generics,
459
460
from_scratch,
460
- is_packed,
461
- always_copy,
461
+ copy_fields,
462
462
) ,
463
463
ast:: ItemKind :: Enum ( ref enum_def, ref generics) => {
464
- // We ignore `is_packed`/`always_copy` here, because
465
- // `repr(packed)` enums cause an error later on.
464
+ // We ignore `is_packed` here, because `repr(packed)`
465
+ // enums cause an error later on.
466
466
//
467
467
// This can only cause further compilation errors
468
- // downstream in blatantly illegal code, so it
469
- // is fine.
468
+ // downstream in blatantly illegal code, so it is fine.
470
469
self . expand_enum_def ( cx, enum_def, item. ident , generics, from_scratch)
471
470
}
472
471
ast:: ItemKind :: Union ( ref struct_def, ref generics) => {
@@ -477,8 +476,7 @@ impl<'a> TraitDef<'a> {
477
476
item. ident ,
478
477
generics,
479
478
from_scratch,
480
- is_packed,
481
- always_copy,
479
+ copy_fields,
482
480
)
483
481
} else {
484
482
cx. span_err ( mitem. span , "this trait cannot be derived for unions" ) ;
@@ -748,8 +746,7 @@ impl<'a> TraitDef<'a> {
748
746
type_ident : Ident ,
749
747
generics : & Generics ,
750
748
from_scratch : bool ,
751
- is_packed : bool ,
752
- always_copy : bool ,
749
+ copy_fields : bool ,
753
750
) -> P < ast:: Item > {
754
751
let field_tys: Vec < P < ast:: Ty > > =
755
752
struct_def. fields ( ) . iter ( ) . map ( |field| field. ty . clone ( ) ) . collect ( ) ;
@@ -777,8 +774,7 @@ impl<'a> TraitDef<'a> {
777
774
type_ident,
778
775
& selflike_args,
779
776
& nonselflike_args,
780
- is_packed,
781
- always_copy,
777
+ copy_fields,
782
778
)
783
779
} ;
784
780
@@ -1016,19 +1012,9 @@ impl<'a> MethodDef<'a> {
1016
1012
/// }
1017
1013
/// }
1018
1014
/// ```
1019
- /// If the struct doesn't impl `Copy`, we use let-destructuring with `ref`:
1020
- /// ```
1021
- /// # struct A { x: u8, y: u8 }
1022
- /// impl PartialEq for A {
1023
- /// fn eq(&self, other: &A) -> bool {
1024
- /// let Self { x: ref __self_0_0, y: ref __self_0_1 } = *self;
1025
- /// let Self { x: ref __self_1_0, y: ref __self_1_1 } = *other;
1026
- /// *__self_0_0 == *__self_1_0 && *__self_0_1 == *__self_1_1
1027
- /// }
1028
- /// }
1029
- /// ```
1030
- /// This latter case only works if the fields match the alignment required
1031
- /// by the `packed(N)` attribute. (We'll get errors later on if not.)
1015
+ /// If the struct doesn't impl `Copy`, we use the normal `&self.x`. This
1016
+ /// only works if the fields match the alignment required by the
1017
+ /// `packed(N)` attribute. (We'll get errors later on if not.)
1032
1018
fn expand_struct_method_body < ' b > (
1033
1019
& self ,
1034
1020
cx : & mut ExtCtxt < ' _ > ,
@@ -1037,51 +1023,19 @@ impl<'a> MethodDef<'a> {
1037
1023
type_ident : Ident ,
1038
1024
selflike_args : & [ P < Expr > ] ,
1039
1025
nonselflike_args : & [ P < Expr > ] ,
1040
- is_packed : bool ,
1041
- always_copy : bool ,
1026
+ copy_fields : bool ,
1042
1027
) -> BlockOrExpr {
1043
- let span = trait_. span ;
1044
1028
assert ! ( selflike_args. len( ) == 1 || selflike_args. len( ) == 2 ) ;
1045
1029
1046
- let mk_body = |cx, selflike_fields| {
1047
- self . call_substructure_method (
1048
- cx,
1049
- trait_,
1050
- type_ident,
1051
- nonselflike_args,
1052
- & Struct ( struct_def, selflike_fields) ,
1053
- )
1054
- } ;
1055
-
1056
- if !is_packed {
1057
- let selflike_fields =
1058
- trait_. create_struct_field_access_fields ( cx, selflike_args, struct_def, false ) ;
1059
- mk_body ( cx, selflike_fields)
1060
- } else if always_copy {
1061
- let selflike_fields =
1062
- trait_. create_struct_field_access_fields ( cx, selflike_args, struct_def, true ) ;
1063
- mk_body ( cx, selflike_fields)
1064
- } else {
1065
- // Packed and not copy. Need to use ref patterns.
1066
- let prefixes: Vec < _ > =
1067
- ( 0 ..selflike_args. len ( ) ) . map ( |i| format ! ( "__self_{}" , i) ) . collect ( ) ;
1068
- let selflike_fields = trait_. create_struct_pattern_fields ( cx, struct_def, & prefixes) ;
1069
- let mut body = mk_body ( cx, selflike_fields) ;
1070
-
1071
- let struct_path = cx. path ( span, vec ! [ Ident :: new( kw:: SelfUpper , type_ident. span) ] ) ;
1072
- let patterns =
1073
- trait_. create_struct_patterns ( cx, struct_path, struct_def, & prefixes, ByRef :: Yes ) ;
1074
-
1075
- // Do the let-destructuring.
1076
- let mut stmts: Vec < _ > = iter:: zip ( selflike_args, patterns)
1077
- . map ( |( selflike_arg_expr, pat) | {
1078
- let selflike_arg_expr = cx. expr_deref ( span, selflike_arg_expr. clone ( ) ) ;
1079
- cx. stmt_let_pat ( span, pat, selflike_arg_expr)
1080
- } )
1081
- . collect ( ) ;
1082
- stmts. extend ( std:: mem:: take ( & mut body. 0 ) ) ;
1083
- BlockOrExpr ( stmts, body. 1 )
1084
- }
1030
+ let selflike_fields =
1031
+ trait_. create_struct_field_access_fields ( cx, selflike_args, struct_def, copy_fields) ;
1032
+ self . call_substructure_method (
1033
+ cx,
1034
+ trait_,
1035
+ type_ident,
1036
+ nonselflike_args,
1037
+ & Struct ( struct_def, selflike_fields) ,
1038
+ )
1085
1039
}
1086
1040
1087
1041
fn expand_static_struct_method_body (
@@ -1531,7 +1485,7 @@ impl<'a> TraitDef<'a> {
1531
1485
cx : & mut ExtCtxt < ' _ > ,
1532
1486
selflike_args : & [ P < Expr > ] ,
1533
1487
struct_def : & ' a VariantData ,
1534
- copy : bool ,
1488
+ copy_fields : bool ,
1535
1489
) -> Vec < FieldInfo > {
1536
1490
self . create_fields ( struct_def, |i, struct_field, sp| {
1537
1491
selflike_args
@@ -1550,7 +1504,7 @@ impl<'a> TraitDef<'a> {
1550
1504
} ) ,
1551
1505
) ,
1552
1506
) ;
1553
- if copy {
1507
+ if copy_fields {
1554
1508
field_expr = cx. expr_block (
1555
1509
cx. block ( struct_field. span , vec ! [ cx. stmt_expr( field_expr) ] ) ,
1556
1510
) ;
0 commit comments