Skip to content

Commit 1cb1d63

Browse files
committed
Use &{self.x} for packed Copy structs.
Because it's more concise than the `let` form.
1 parent 10144e2 commit 1cb1d63

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+33-19
Original file line numberDiff line numberDiff line change
@@ -1013,20 +1013,25 @@ impl<'a> MethodDef<'a> {
10131013
/// }
10141014
/// ```
10151015
/// But if the struct is `repr(packed)`, we can't use something like
1016-
/// `&self.x` on a packed type (as required for e.g. `Debug` and `Hash`)
1017-
/// because that might cause an unaligned ref. So we use let-destructuring
1018-
/// instead. If the struct impls `Copy`:
1016+
/// `&self.x` because that might cause an unaligned ref. So for any trait
1017+
/// method that takes a reference, if the struct impls `Copy` then we use a
1018+
/// local block to force a copy:
10191019
/// ```
10201020
/// # struct A { x: u8, y: u8 }
10211021
/// impl PartialEq for A {
10221022
/// fn eq(&self, other: &A) -> bool {
1023-
/// let Self { x: __self_0_0, y: __self_0_1 } = *self;
1024-
/// let Self { x: __self_1_0, y: __self_1_1 } = *other;
1025-
/// __self_0_0 == __self_1_0 && __self_0_1 == __self_1_1
1023+
/// // Desugars to `{ self.x }.eq(&{ other.y }) && ...`
1024+
/// { self.x } == { other.y } && { self.y } == { other.y }
1025+
/// }
1026+
/// }
1027+
/// impl Hash for A {
1028+
/// fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
1029+
/// ::core::hash::Hash::hash(&{ self.x }, state);
1030+
/// ::core::hash::Hash::hash(&{ self.y }, state)
10261031
/// }
10271032
/// }
10281033
/// ```
1029-
/// If it doesn't impl `Copy`:
1034+
/// If the struct doesn't impl `Copy`, we use let-destructuring with `ref`:
10301035
/// ```
10311036
/// # struct A { x: u8, y: u8 }
10321037
/// impl PartialEq for A {
@@ -1038,7 +1043,7 @@ impl<'a> MethodDef<'a> {
10381043
/// }
10391044
/// ```
10401045
/// This latter case only works if the fields match the alignment required
1041-
/// by the `packed(N)` attribute.
1046+
/// by the `packed(N)` attribute. (We'll get errors later on if not.)
10421047
fn expand_struct_method_body<'b>(
10431048
&self,
10441049
cx: &mut ExtCtxt<'_>,
@@ -1065,9 +1070,14 @@ impl<'a> MethodDef<'a> {
10651070

10661071
if !is_packed {
10671072
let selflike_fields =
1068-
trait_.create_struct_field_access_fields(cx, selflike_args, struct_def);
1073+
trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, false);
1074+
mk_body(cx, selflike_fields)
1075+
} else if always_copy {
1076+
let selflike_fields =
1077+
trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, true);
10691078
mk_body(cx, selflike_fields)
10701079
} else {
1080+
// Neither packed nor copy. Need to use ref patterns.
10711081
let prefixes: Vec<_> =
10721082
(0..selflike_args.len()).map(|i| format!("__self_{}", i)).collect();
10731083
let addr_of = always_copy;
@@ -1536,6 +1546,7 @@ impl<'a> TraitDef<'a> {
15361546
cx: &mut ExtCtxt<'_>,
15371547
selflike_args: &[P<Expr>],
15381548
struct_def: &'a VariantData,
1549+
copy: bool,
15391550
) -> Vec<FieldInfo> {
15401551
self.create_fields(struct_def, |i, struct_field, sp| {
15411552
selflike_args
@@ -1545,18 +1556,21 @@ impl<'a> TraitDef<'a> {
15451556
// `unwrap_or_else` case otherwise the hygiene is wrong and we get
15461557
// "field `0` of struct `Point` is private" errors on tuple
15471558
// structs.
1548-
cx.expr_addr_of(
1559+
let mut field_expr = cx.expr(
15491560
sp,
1550-
cx.expr(
1551-
sp,
1552-
ast::ExprKind::Field(
1553-
selflike_arg.clone(),
1554-
struct_field.ident.unwrap_or_else(|| {
1555-
Ident::from_str_and_span(&i.to_string(), struct_field.span)
1556-
}),
1557-
),
1561+
ast::ExprKind::Field(
1562+
selflike_arg.clone(),
1563+
struct_field.ident.unwrap_or_else(|| {
1564+
Ident::from_str_and_span(&i.to_string(), struct_field.span)
1565+
}),
15581566
),
1559-
)
1567+
);
1568+
if copy {
1569+
field_expr = cx.expr_block(
1570+
cx.block(struct_field.span, vec![cx.stmt_expr(field_expr)]),
1571+
);
1572+
}
1573+
cx.expr_addr_of(sp, field_expr)
15601574
})
15611575
.collect()
15621576
})

src/test/ui/deriving/deriving-all-codegen.stdout

+6-20
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,8 @@ impl ::core::marker::Copy for PackedCopy { }
441441
#[allow(unused_qualifications)]
442442
impl ::core::fmt::Debug for PackedCopy {
443443
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
444-
let Self(__self_0_0) = *self;
445444
::core::fmt::Formatter::debug_tuple_field1_finish(f, "PackedCopy",
446-
&&__self_0_0)
445+
&&{ self.0 })
447446
}
448447
}
449448
#[automatically_derived]
@@ -458,26 +457,17 @@ impl ::core::default::Default for PackedCopy {
458457
#[allow(unused_qualifications)]
459458
impl ::core::hash::Hash for PackedCopy {
460459
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
461-
let Self(__self_0_0) = *self;
462-
::core::hash::Hash::hash(&__self_0_0, state)
460+
::core::hash::Hash::hash(&{ self.0 }, state)
463461
}
464462
}
465463
impl ::core::marker::StructuralPartialEq for PackedCopy {}
466464
#[automatically_derived]
467465
#[allow(unused_qualifications)]
468466
impl ::core::cmp::PartialEq for PackedCopy {
469467
#[inline]
470-
fn eq(&self, other: &PackedCopy) -> bool {
471-
let Self(__self_0_0) = *self;
472-
let Self(__self_1_0) = *other;
473-
__self_0_0 == __self_1_0
474-
}
468+
fn eq(&self, other: &PackedCopy) -> bool { { self.0 } == { other.0 } }
475469
#[inline]
476-
fn ne(&self, other: &PackedCopy) -> bool {
477-
let Self(__self_0_0) = *self;
478-
let Self(__self_1_0) = *other;
479-
__self_0_0 != __self_1_0
480-
}
470+
fn ne(&self, other: &PackedCopy) -> bool { { self.0 } != { other.0 } }
481471
}
482472
impl ::core::marker::StructuralEq for PackedCopy {}
483473
#[automatically_derived]
@@ -496,19 +486,15 @@ impl ::core::cmp::PartialOrd for PackedCopy {
496486
#[inline]
497487
fn partial_cmp(&self, other: &PackedCopy)
498488
-> ::core::option::Option<::core::cmp::Ordering> {
499-
let Self(__self_0_0) = *self;
500-
let Self(__self_1_0) = *other;
501-
::core::cmp::PartialOrd::partial_cmp(&__self_0_0, &__self_1_0)
489+
::core::cmp::PartialOrd::partial_cmp(&{ self.0 }, &{ other.0 })
502490
}
503491
}
504492
#[automatically_derived]
505493
#[allow(unused_qualifications)]
506494
impl ::core::cmp::Ord for PackedCopy {
507495
#[inline]
508496
fn cmp(&self, other: &PackedCopy) -> ::core::cmp::Ordering {
509-
let Self(__self_0_0) = *self;
510-
let Self(__self_1_0) = *other;
511-
::core::cmp::Ord::cmp(&__self_0_0, &__self_1_0)
497+
::core::cmp::Ord::cmp(&{ self.0 }, &{ other.0 })
512498
}
513499
}
514500

0 commit comments

Comments
 (0)