Skip to content

Commit 2c911dc

Browse files
committed
Avoid unnecessary 1-tuples in derived code.
1 parent a7b1d31 commit 2c911dc

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,11 @@ impl<'a> MethodDef<'a> {
12391239
}
12401240

12411241
// Here is the pat = `(&VariantK, &VariantK, ...)`
1242-
let single_pat = cx.pat_tuple(span, subpats);
1242+
let single_pat = if subpats.len() == 1 {
1243+
subpats.pop().unwrap()
1244+
} else {
1245+
cx.pat_tuple(span, subpats)
1246+
};
12431247

12441248
// For the BodyK, we need to delegate to our caller,
12451249
// passing it an EnumMatching to indicate which case
@@ -1471,7 +1475,11 @@ impl<'a> MethodDef<'a> {
14711475
// expression; here add a layer of borrowing, turning
14721476
// `(*self, *__arg_0, ...)` into `(&*self, &*__arg_0, ...)`.
14731477
self_args.map_in_place(|self_arg| cx.expr_addr_of(span, self_arg));
1474-
let match_arg = cx.expr(span, ast::ExprKind::Tup(self_args));
1478+
let match_arg = if self_args.len() == 1 {
1479+
self_args.pop().unwrap()
1480+
} else {
1481+
cx.expr(span, ast::ExprKind::Tup(self_args))
1482+
};
14751483
BlockOrExpr(vec![], Some(cx.expr_match(span, match_arg, match_arms)))
14761484
}
14771485
}

src/test/codegen/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// CHECK: @STATIC = {{.*}}, align 4
1111

1212
// This checks the constants from inline_enum_const
13-
// CHECK: @alloc14 = {{.*}}, align 2
13+
// CHECK: @alloc12 = {{.*}}, align 2
1414

1515
// This checks the constants from {low,high}_align_const, they share the same
1616
// constant, but the alignment differs, so the higher one should be used

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

+31-31
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ enum Enum1 {
554554
impl ::core::clone::Clone for Enum1 {
555555
#[inline]
556556
fn clone(&self) -> Enum1 {
557-
match (&*self,) {
558-
(&Enum1::Single { x: ref __self_0 },) =>
557+
match &*self {
558+
&Enum1::Single { x: ref __self_0 } =>
559559
Enum1::Single { x: ::core::clone::Clone::clone(&*__self_0) },
560560
}
561561
}
@@ -564,8 +564,8 @@ impl ::core::clone::Clone for Enum1 {
564564
#[allow(unused_qualifications)]
565565
impl ::core::fmt::Debug for Enum1 {
566566
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
567-
match (&*self,) {
568-
(&Enum1::Single { x: ref __self_0 },) =>
567+
match &*self {
568+
&Enum1::Single { x: ref __self_0 } =>
569569
::core::fmt::Formatter::debug_struct_field1_finish(f,
570570
"Single", "x", &&*__self_0),
571571
}
@@ -575,8 +575,8 @@ impl ::core::fmt::Debug for Enum1 {
575575
#[allow(unused_qualifications)]
576576
impl ::core::hash::Hash for Enum1 {
577577
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
578-
match (&*self,) {
579-
(&Enum1::Single { x: ref __self_0 },) => {
578+
match &*self {
579+
&Enum1::Single { x: ref __self_0 } => {
580580
::core::hash::Hash::hash(&*__self_0, state)
581581
}
582582
}
@@ -669,10 +669,10 @@ impl ::core::marker::Copy for Fieldless { }
669669
#[allow(unused_qualifications)]
670670
impl ::core::fmt::Debug for Fieldless {
671671
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
672-
match (&*self,) {
673-
(&Fieldless::A,) => ::core::fmt::Formatter::write_str(f, "A"),
674-
(&Fieldless::B,) => ::core::fmt::Formatter::write_str(f, "B"),
675-
(&Fieldless::C,) => ::core::fmt::Formatter::write_str(f, "C"),
672+
match &*self {
673+
&Fieldless::A => ::core::fmt::Formatter::write_str(f, "A"),
674+
&Fieldless::B => ::core::fmt::Formatter::write_str(f, "B"),
675+
&Fieldless::C => ::core::fmt::Formatter::write_str(f, "C"),
676676
}
677677
}
678678
}
@@ -686,7 +686,7 @@ impl ::core::default::Default for Fieldless {
686686
#[allow(unused_qualifications)]
687687
impl ::core::hash::Hash for Fieldless {
688688
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
689-
match (&*self,) {
689+
match &*self {
690690
_ => {
691691
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
692692
state)
@@ -775,13 +775,13 @@ impl ::core::marker::Copy for Mixed { }
775775
#[allow(unused_qualifications)]
776776
impl ::core::fmt::Debug for Mixed {
777777
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
778-
match (&*self,) {
779-
(&Mixed::P,) => ::core::fmt::Formatter::write_str(f, "P"),
780-
(&Mixed::Q,) => ::core::fmt::Formatter::write_str(f, "Q"),
781-
(&Mixed::R(ref __self_0),) =>
778+
match &*self {
779+
&Mixed::P => ::core::fmt::Formatter::write_str(f, "P"),
780+
&Mixed::Q => ::core::fmt::Formatter::write_str(f, "Q"),
781+
&Mixed::R(ref __self_0) =>
782782
::core::fmt::Formatter::debug_tuple_field1_finish(f, "R",
783783
&&*__self_0),
784-
(&Mixed::S { d1: ref __self_0, d2: ref __self_1 },) =>
784+
&Mixed::S { d1: ref __self_0, d2: ref __self_1 } =>
785785
::core::fmt::Formatter::debug_struct_field2_finish(f, "S",
786786
"d1", &&*__self_0, "d2", &&*__self_1),
787787
}
@@ -797,13 +797,13 @@ impl ::core::default::Default for Mixed {
797797
#[allow(unused_qualifications)]
798798
impl ::core::hash::Hash for Mixed {
799799
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
800-
match (&*self,) {
801-
(&Mixed::R(ref __self_0),) => {
800+
match &*self {
801+
&Mixed::R(ref __self_0) => {
802802
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
803803
state);
804804
::core::hash::Hash::hash(&*__self_0, state)
805805
}
806-
(&Mixed::S { d1: ref __self_0, d2: ref __self_1 },) => {
806+
&Mixed::S { d1: ref __self_0, d2: ref __self_1 } => {
807807
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
808808
state);
809809
::core::hash::Hash::hash(&*__self_0, state);
@@ -943,12 +943,12 @@ enum Fielded { X(u32), Y(bool), Z(Option<i32>), }
943943
impl ::core::clone::Clone for Fielded {
944944
#[inline]
945945
fn clone(&self) -> Fielded {
946-
match (&*self,) {
947-
(&Fielded::X(ref __self_0),) =>
946+
match &*self {
947+
&Fielded::X(ref __self_0) =>
948948
Fielded::X(::core::clone::Clone::clone(&*__self_0)),
949-
(&Fielded::Y(ref __self_0),) =>
949+
&Fielded::Y(ref __self_0) =>
950950
Fielded::Y(::core::clone::Clone::clone(&*__self_0)),
951-
(&Fielded::Z(ref __self_0),) =>
951+
&Fielded::Z(ref __self_0) =>
952952
Fielded::Z(::core::clone::Clone::clone(&*__self_0)),
953953
}
954954
}
@@ -957,14 +957,14 @@ impl ::core::clone::Clone for Fielded {
957957
#[allow(unused_qualifications)]
958958
impl ::core::fmt::Debug for Fielded {
959959
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
960-
match (&*self,) {
961-
(&Fielded::X(ref __self_0),) =>
960+
match &*self {
961+
&Fielded::X(ref __self_0) =>
962962
::core::fmt::Formatter::debug_tuple_field1_finish(f, "X",
963963
&&*__self_0),
964-
(&Fielded::Y(ref __self_0),) =>
964+
&Fielded::Y(ref __self_0) =>
965965
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Y",
966966
&&*__self_0),
967-
(&Fielded::Z(ref __self_0),) =>
967+
&Fielded::Z(ref __self_0) =>
968968
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Z",
969969
&&*__self_0),
970970
}
@@ -974,18 +974,18 @@ impl ::core::fmt::Debug for Fielded {
974974
#[allow(unused_qualifications)]
975975
impl ::core::hash::Hash for Fielded {
976976
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
977-
match (&*self,) {
978-
(&Fielded::X(ref __self_0),) => {
977+
match &*self {
978+
&Fielded::X(ref __self_0) => {
979979
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
980980
state);
981981
::core::hash::Hash::hash(&*__self_0, state)
982982
}
983-
(&Fielded::Y(ref __self_0),) => {
983+
&Fielded::Y(ref __self_0) => {
984984
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
985985
state);
986986
::core::hash::Hash::hash(&*__self_0, state)
987987
}
988-
(&Fielded::Z(ref __self_0),) => {
988+
&Fielded::Z(ref __self_0) => {
989989
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
990990
state);
991991
::core::hash::Hash::hash(&*__self_0, state)

0 commit comments

Comments
 (0)