Skip to content

Commit 2c0bfbe

Browse files
authored
Rollup merge of #103089 - cjgillot:automatic-structural-eq, r=oli-obk
Mark derived StructuralEq as automatically derived. Fixes #69952 Drive-by: use correct spans for generic params.
2 parents e31ae4f + 16e22e1 commit 2c0bfbe

File tree

3 files changed

+70
-29
lines changed

3 files changed

+70
-29
lines changed

compiler/rustc_builtin_macros/src/deriving/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,27 @@ fn inject_impl_of_structural_trait(
131131
// Create generics param list for where clauses and impl headers
132132
let mut generics = generics.clone();
133133

134+
let ctxt = span.ctxt();
135+
134136
// Create the type of `self`.
135137
//
136138
// in addition, remove defaults from generic params (impls cannot have them).
137139
let self_params: Vec<_> = generics
138140
.params
139141
.iter_mut()
140142
.map(|param| match &mut param.kind {
141-
ast::GenericParamKind::Lifetime => {
142-
ast::GenericArg::Lifetime(cx.lifetime(span, param.ident))
143-
}
143+
ast::GenericParamKind::Lifetime => ast::GenericArg::Lifetime(
144+
cx.lifetime(param.ident.span.with_ctxt(ctxt), param.ident),
145+
),
144146
ast::GenericParamKind::Type { default } => {
145147
*default = None;
146-
ast::GenericArg::Type(cx.ty_ident(span, param.ident))
148+
ast::GenericArg::Type(cx.ty_ident(param.ident.span.with_ctxt(ctxt), param.ident))
147149
}
148150
ast::GenericParamKind::Const { ty: _, kw_span: _, default } => {
149151
*default = None;
150-
ast::GenericArg::Const(cx.const_ident(span, param.ident))
152+
ast::GenericArg::Const(
153+
cx.const_ident(param.ident.span.with_ctxt(ctxt), param.ident),
154+
)
151155
}
152156
})
153157
.collect();
@@ -174,6 +178,8 @@ fn inject_impl_of_structural_trait(
174178
})
175179
.cloned(),
176180
);
181+
// Mark as `automatically_derived` to avoid some silly lints.
182+
attrs.push(cx.attribute(cx.meta_word(span, sym::automatically_derived)));
177183

178184
let newitem = cx.item(
179185
span,

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

+48-24
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ impl ::core::default::Default for Empty {
4646
impl ::core::hash::Hash for Empty {
4747
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {}
4848
}
49-
impl ::core::marker::StructuralPartialEq for Empty {}
49+
#[automatically_derived]
50+
impl ::core::marker::StructuralPartialEq for Empty { }
5051
#[automatically_derived]
5152
impl ::core::cmp::PartialEq for Empty {
5253
#[inline]
5354
fn eq(&self, other: &Empty) -> bool { true }
5455
}
55-
impl ::core::marker::StructuralEq for Empty {}
56+
#[automatically_derived]
57+
impl ::core::marker::StructuralEq for Empty { }
5658
#[automatically_derived]
5759
impl ::core::cmp::Eq for Empty {
5860
#[inline]
@@ -115,15 +117,17 @@ impl ::core::hash::Hash for Point {
115117
::core::hash::Hash::hash(&self.y, state)
116118
}
117119
}
118-
impl ::core::marker::StructuralPartialEq for Point {}
120+
#[automatically_derived]
121+
impl ::core::marker::StructuralPartialEq for Point { }
119122
#[automatically_derived]
120123
impl ::core::cmp::PartialEq for Point {
121124
#[inline]
122125
fn eq(&self, other: &Point) -> bool {
123126
self.x == other.x && self.y == other.y
124127
}
125128
}
126-
impl ::core::marker::StructuralEq for Point {}
129+
#[automatically_derived]
130+
impl ::core::marker::StructuralEq for Point { }
127131
#[automatically_derived]
128132
impl ::core::cmp::Eq for Point {
129133
#[inline]
@@ -225,7 +229,8 @@ impl ::core::hash::Hash for Big {
225229
::core::hash::Hash::hash(&self.b8, state)
226230
}
227231
}
228-
impl ::core::marker::StructuralPartialEq for Big {}
232+
#[automatically_derived]
233+
impl ::core::marker::StructuralPartialEq for Big { }
229234
#[automatically_derived]
230235
impl ::core::cmp::PartialEq for Big {
231236
#[inline]
@@ -236,7 +241,8 @@ impl ::core::cmp::PartialEq for Big {
236241
self.b8 == other.b8
237242
}
238243
}
239-
impl ::core::marker::StructuralEq for Big {}
244+
#[automatically_derived]
245+
impl ::core::marker::StructuralEq for Big { }
240246
#[automatically_derived]
241247
impl ::core::cmp::Eq for Big {
242248
#[inline]
@@ -345,13 +351,15 @@ impl ::core::hash::Hash for Unsized {
345351
::core::hash::Hash::hash(&self.0, state)
346352
}
347353
}
348-
impl ::core::marker::StructuralPartialEq for Unsized {}
354+
#[automatically_derived]
355+
impl ::core::marker::StructuralPartialEq for Unsized { }
349356
#[automatically_derived]
350357
impl ::core::cmp::PartialEq for Unsized {
351358
#[inline]
352359
fn eq(&self, other: &Unsized) -> bool { self.0 == other.0 }
353360
}
354-
impl ::core::marker::StructuralEq for Unsized {}
361+
#[automatically_derived]
362+
impl ::core::marker::StructuralEq for Unsized { }
355363
#[automatically_derived]
356364
impl ::core::cmp::Eq for Unsized {
357365
#[inline]
@@ -410,13 +418,15 @@ impl ::core::hash::Hash for PackedCopy {
410418
::core::hash::Hash::hash(&{ self.0 }, state)
411419
}
412420
}
413-
impl ::core::marker::StructuralPartialEq for PackedCopy {}
421+
#[automatically_derived]
422+
impl ::core::marker::StructuralPartialEq for PackedCopy { }
414423
#[automatically_derived]
415424
impl ::core::cmp::PartialEq for PackedCopy {
416425
#[inline]
417426
fn eq(&self, other: &PackedCopy) -> bool { { self.0 } == { other.0 } }
418427
}
419-
impl ::core::marker::StructuralEq for PackedCopy {}
428+
#[automatically_derived]
429+
impl ::core::marker::StructuralEq for PackedCopy { }
420430
#[automatically_derived]
421431
impl ::core::cmp::Eq for PackedCopy {
422432
#[inline]
@@ -479,7 +489,8 @@ impl ::core::hash::Hash for PackedNonCopy {
479489
::core::hash::Hash::hash(__self_0_0, state)
480490
}
481491
}
482-
impl ::core::marker::StructuralPartialEq for PackedNonCopy {}
492+
#[automatically_derived]
493+
impl ::core::marker::StructuralPartialEq for PackedNonCopy { }
483494
#[automatically_derived]
484495
impl ::core::cmp::PartialEq for PackedNonCopy {
485496
#[inline]
@@ -489,7 +500,8 @@ impl ::core::cmp::PartialEq for PackedNonCopy {
489500
*__self_0_0 == *__self_1_0
490501
}
491502
}
492-
impl ::core::marker::StructuralEq for PackedNonCopy {}
503+
#[automatically_derived]
504+
impl ::core::marker::StructuralEq for PackedNonCopy { }
493505
#[automatically_derived]
494506
impl ::core::cmp::Eq for PackedNonCopy {
495507
#[inline]
@@ -540,15 +552,17 @@ impl ::core::hash::Hash for Enum0 {
540552
unsafe { ::core::intrinsics::unreachable() }
541553
}
542554
}
543-
impl ::core::marker::StructuralPartialEq for Enum0 {}
555+
#[automatically_derived]
556+
impl ::core::marker::StructuralPartialEq for Enum0 { }
544557
#[automatically_derived]
545558
impl ::core::cmp::PartialEq for Enum0 {
546559
#[inline]
547560
fn eq(&self, other: &Enum0) -> bool {
548561
unsafe { ::core::intrinsics::unreachable() }
549562
}
550563
}
551-
impl ::core::marker::StructuralEq for Enum0 {}
564+
#[automatically_derived]
565+
impl ::core::marker::StructuralEq for Enum0 { }
552566
#[automatically_derived]
553567
impl ::core::cmp::Eq for Enum0 {
554568
#[inline]
@@ -607,7 +621,8 @@ impl ::core::hash::Hash for Enum1 {
607621
}
608622
}
609623
}
610-
impl ::core::marker::StructuralPartialEq for Enum1 {}
624+
#[automatically_derived]
625+
impl ::core::marker::StructuralPartialEq for Enum1 { }
611626
#[automatically_derived]
612627
impl ::core::cmp::PartialEq for Enum1 {
613628
#[inline]
@@ -618,7 +633,8 @@ impl ::core::cmp::PartialEq for Enum1 {
618633
}
619634
}
620635
}
621-
impl ::core::marker::StructuralEq for Enum1 {}
636+
#[automatically_derived]
637+
impl ::core::marker::StructuralEq for Enum1 { }
622638
#[automatically_derived]
623639
impl ::core::cmp::Eq for Enum1 {
624640
#[inline]
@@ -676,13 +692,15 @@ impl ::core::default::Default for Fieldless1 {
676692
impl ::core::hash::Hash for Fieldless1 {
677693
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {}
678694
}
679-
impl ::core::marker::StructuralPartialEq for Fieldless1 {}
695+
#[automatically_derived]
696+
impl ::core::marker::StructuralPartialEq for Fieldless1 { }
680697
#[automatically_derived]
681698
impl ::core::cmp::PartialEq for Fieldless1 {
682699
#[inline]
683700
fn eq(&self, other: &Fieldless1) -> bool { true }
684701
}
685-
impl ::core::marker::StructuralEq for Fieldless1 {}
702+
#[automatically_derived]
703+
impl ::core::marker::StructuralEq for Fieldless1 { }
686704
#[automatically_derived]
687705
impl ::core::cmp::Eq for Fieldless1 {
688706
#[inline]
@@ -743,7 +761,8 @@ impl ::core::hash::Hash for Fieldless {
743761
::core::hash::Hash::hash(&__self_tag, state)
744762
}
745763
}
746-
impl ::core::marker::StructuralPartialEq for Fieldless {}
764+
#[automatically_derived]
765+
impl ::core::marker::StructuralPartialEq for Fieldless { }
747766
#[automatically_derived]
748767
impl ::core::cmp::PartialEq for Fieldless {
749768
#[inline]
@@ -753,7 +772,8 @@ impl ::core::cmp::PartialEq for Fieldless {
753772
__self_tag == __arg1_tag
754773
}
755774
}
756-
impl ::core::marker::StructuralEq for Fieldless {}
775+
#[automatically_derived]
776+
impl ::core::marker::StructuralEq for Fieldless { }
757777
#[automatically_derived]
758778
impl ::core::cmp::Eq for Fieldless {
759779
#[inline]
@@ -838,7 +858,8 @@ impl ::core::hash::Hash for Mixed {
838858
}
839859
}
840860
}
841-
impl ::core::marker::StructuralPartialEq for Mixed {}
861+
#[automatically_derived]
862+
impl ::core::marker::StructuralPartialEq for Mixed { }
842863
#[automatically_derived]
843864
impl ::core::cmp::PartialEq for Mixed {
844865
#[inline]
@@ -856,7 +877,8 @@ impl ::core::cmp::PartialEq for Mixed {
856877
}
857878
}
858879
}
859-
impl ::core::marker::StructuralEq for Mixed {}
880+
#[automatically_derived]
881+
impl ::core::marker::StructuralEq for Mixed { }
860882
#[automatically_derived]
861883
impl ::core::cmp::Eq for Mixed {
862884
#[inline]
@@ -963,7 +985,8 @@ impl ::core::hash::Hash for Fielded {
963985
}
964986
}
965987
}
966-
impl ::core::marker::StructuralPartialEq for Fielded {}
988+
#[automatically_derived]
989+
impl ::core::marker::StructuralPartialEq for Fielded { }
967990
#[automatically_derived]
968991
impl ::core::cmp::PartialEq for Fielded {
969992
#[inline]
@@ -982,7 +1005,8 @@ impl ::core::cmp::PartialEq for Fielded {
9821005
}
9831006
}
9841007
}
985-
impl ::core::marker::StructuralEq for Fielded {}
1008+
#[automatically_derived]
1009+
impl ::core::marker::StructuralEq for Fielded { }
9861010
#[automatically_derived]
9871011
impl ::core::cmp::Eq for Fielded {
9881012
#[inline]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// check-pass
2+
3+
#![deny(single_use_lifetimes)]
4+
5+
#[derive(PartialEq, Eq)]
6+
struct Foo<'a, T> {
7+
/// a reference to the underlying secret data that will be derefed
8+
pub data: &'a mut T,
9+
}
10+
11+
fn main() {}

0 commit comments

Comments
 (0)