37
37
//! following snippet
38
38
//!
39
39
//! ```rust
40
- //! # #![allow(dead_code)]
41
- //! struct A { x : i32 }
40
+ //! struct A {
41
+ //! x: i32,
42
+ //! }
42
43
//!
43
44
//! struct B(i32);
44
45
//!
74
75
//! trait PartialEq {
75
76
//! fn eq(&self, other: &Self) -> bool;
76
77
//! }
78
+ //!
77
79
//! impl PartialEq for i32 {
78
80
//! fn eq(&self, other: &i32) -> bool {
79
81
//! *self == *other
90
92
//!
91
93
//! ```text
92
94
//! Struct(vec![FieldInfo {
93
- //! span: <span of x>
94
- //! name: Some(<ident of x>),
95
- //! self_: <expr for &self.x>,
96
- //! other: vec![<expr for &other.x]
97
- //! }])
95
+ //! span: <span of x>,
96
+ //! name: Some(<ident of x>),
97
+ //! self_: <expr for &self.x>,
98
+ //! other: vec![<expr for &other.x>],
99
+ //! }])
98
100
//! ```
99
101
//!
100
102
//! For the `B` impl, called with `B(a)` and `B(b)`,
101
103
//!
102
104
//! ```text
103
105
//! Struct(vec![FieldInfo {
104
- //! span: <span of ` i32` >,
105
- //! name: None,
106
- //! self_: <expr for &a>
107
- //! other: vec![<expr for &b>]
108
- //! }])
106
+ //! span: <span of i32>,
107
+ //! name: None,
108
+ //! self_: <expr for &a>,
109
+ //! other: vec![<expr for &b>],
110
+ //! }])
109
111
//! ```
110
112
//!
111
113
//! ## Enums
114
116
//! == C0(b)`, the SubstructureFields is
115
117
//!
116
118
//! ```text
117
- //! EnumMatching(0, <ast::Variant for C0>,
118
- //! vec![FieldInfo {
119
- //! span: <span of i32>
120
- //! name: None,
121
- //! self_: <expr for &a>,
122
- //! other: vec![<expr for &b>]
123
- //! }])
119
+ //! EnumMatching(
120
+ //! 0,
121
+ //! <ast::Variant for C0>,
122
+ //! vec![FieldInfo {
123
+ //! span: <span of i32>,
124
+ //! name: None,
125
+ //! self_: <expr for &a>,
126
+ //! other: vec![<expr for &b>],
127
+ //! }],
128
+ //! )
124
129
//! ```
125
130
//!
126
131
//! For `C1 {x}` and `C1 {x}`,
127
132
//!
128
133
//! ```text
129
- //! EnumMatching(1, <ast::Variant for C1>,
130
- //! vec![FieldInfo {
131
- //! span: <span of x>
132
- //! name: Some(<ident of x>),
133
- //! self_: <expr for &self.x>,
134
- //! other: vec![<expr for &other.x>]
135
- //! }])
134
+ //! EnumMatching(
135
+ //! 1,
136
+ //! <ast::Variant for C1>,
137
+ //! vec![FieldInfo {
138
+ //! span: <span of x>,
139
+ //! name: Some(<ident of x>),
140
+ //! self_: <expr for &self.x>,
141
+ //! other: vec![<expr for &other.x>],
142
+ //! }],
143
+ //! )
136
144
//! ```
137
145
//!
138
146
//! For the tags,
139
147
//!
140
148
//! ```text
141
149
//! EnumTag(
142
- //! &[<ident of self tag>, <ident of other tag>], <expr to combine with>)
150
+ //! &[<ident of self tag>, <ident of other tag>],
151
+ //! <expr to combine with>,
152
+ //! )
143
153
//! ```
154
+ //!
144
155
//! Note that this setup doesn't allow for the brute-force "match every variant
145
156
//! against every other variant" approach, which is bad because it produces a
146
157
//! quadratic amount of code (see #15375).
154
165
//!
155
166
//! StaticStruct(<ast::VariantData of B>, Unnamed(vec![<span of x>]))
156
167
//!
157
- //! StaticEnum(<ast::EnumDef of C>,
158
- //! vec![(<ident of C0>, <span of C0>, Unnamed(vec![<span of i32>])),
159
- //! (<ident of C1>, <span of C1>, Named(vec![(<ident of x>, <span of x>)]))])
168
+ //! StaticEnum(
169
+ //! <ast::EnumDef of C>,
170
+ //! vec![
171
+ //! (<ident of C0>, <span of C0>, Unnamed(vec![<span of i32>])),
172
+ //! (<ident of C1>, <span of C1>, Named(vec![(<ident of x>, <span of x>)])),
173
+ //! ],
174
+ //! )
160
175
//! ```
161
176
162
177
pub use StaticFields :: * ;
@@ -522,7 +537,10 @@ impl<'a> TraitDef<'a> {
522
537
/// Given that we are deriving a trait `DerivedTrait` for a type like:
523
538
///
524
539
/// ```ignore (only-for-syntax-highlight)
525
- /// struct Struct<'a, ..., 'z, A, B: DeclaredTrait, C, ..., Z> where C: WhereTrait {
540
+ /// struct Struct<'a, ..., 'z, A, B: DeclaredTrait, C, ..., Z>
541
+ /// where
542
+ /// C: WhereTrait,
543
+ /// {
526
544
/// a: A,
527
545
/// b: B::Item,
528
546
/// b1: <B as DeclaredTrait>::Item,
@@ -535,12 +553,13 @@ impl<'a> TraitDef<'a> {
535
553
/// create an impl like:
536
554
///
537
555
/// ```ignore (only-for-syntax-highlight)
538
- /// impl<'a, ..., 'z, A, B: DeclaredTrait, C, ... Z> where
539
- /// C: WhereTrait,
556
+ /// impl<'a, ..., 'z, A, B: DeclaredTrait, C, ..., Z>
557
+ /// where
558
+ /// C: WhereTrait,
540
559
/// A: DerivedTrait + B1 + ... + BN,
541
560
/// B: DerivedTrait + B1 + ... + BN,
542
561
/// C: DerivedTrait + B1 + ... + BN,
543
- /// B::Item: DerivedTrait + B1 + ... + BN,
562
+ /// B::Item: DerivedTrait + B1 + ... + BN,
544
563
/// <C as WhereTrait>::Item: DerivedTrait + B1 + ... + BN,
545
564
/// ...
546
565
/// {
@@ -1026,6 +1045,7 @@ impl<'a> MethodDef<'a> {
1026
1045
}
1027
1046
1028
1047
/// The normal case uses field access.
1048
+ ///
1029
1049
/// ```
1030
1050
/// #[derive(PartialEq)]
1031
1051
/// # struct Dummy;
@@ -1038,10 +1058,12 @@ impl<'a> MethodDef<'a> {
1038
1058
/// }
1039
1059
/// }
1040
1060
/// ```
1061
+ ///
1041
1062
/// But if the struct is `repr(packed)`, we can't use something like
1042
1063
/// `&self.x` because that might cause an unaligned ref. So for any trait
1043
1064
/// method that takes a reference, we use a local block to force a copy.
1044
1065
/// This requires that the field impl `Copy`.
1066
+ ///
1045
1067
/// ```rust,ignore (example)
1046
1068
/// # struct A { x: u8, y: u8 }
1047
1069
/// impl PartialEq for A {
@@ -1053,7 +1075,7 @@ impl<'a> MethodDef<'a> {
1053
1075
/// impl Hash for A {
1054
1076
/// fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
1055
1077
/// ::core::hash::Hash::hash(&{ self.x }, state);
1056
- /// ::core::hash::Hash::hash(&{ self.y }, state)
1078
+ /// ::core::hash::Hash::hash(&{ self.y }, state);
1057
1079
/// }
1058
1080
/// }
1059
1081
/// ```
@@ -1107,7 +1129,9 @@ impl<'a> MethodDef<'a> {
1107
1129
/// A2(i32)
1108
1130
/// }
1109
1131
/// ```
1132
+ ///
1110
1133
/// is equivalent to:
1134
+ ///
1111
1135
/// ```
1112
1136
/// #![feature(core_intrinsics)]
1113
1137
/// enum A {
@@ -1119,15 +1143,15 @@ impl<'a> MethodDef<'a> {
1119
1143
/// fn eq(&self, other: &A) -> bool {
1120
1144
/// let __self_tag = ::core::intrinsics::discriminant_value(self);
1121
1145
/// let __arg1_tag = ::core::intrinsics::discriminant_value(other);
1122
- /// __self_tag == __arg1_tag &&
1123
- /// match (self, other) {
1124
- /// (A::A2(__self_0), A::A2(__arg1_0)) =>
1125
- /// *__self_0 == *__arg1_0,
1146
+ /// __self_tag == __arg1_tag
1147
+ /// && match (self, other) {
1148
+ /// (A::A2(__self_0), A::A2(__arg1_0)) => *__self_0 == *__arg1_0,
1126
1149
/// _ => true,
1127
1150
/// }
1128
1151
/// }
1129
1152
/// }
1130
1153
/// ```
1154
+ ///
1131
1155
/// Creates a tag check combined with a match for a tuple of all
1132
1156
/// `selflike_args`, with an arm for each variant with fields, possibly an
1133
1157
/// arm for each fieldless variant (if `unify_fieldless_variants` is not
@@ -1349,7 +1373,7 @@ impl<'a> MethodDef<'a> {
1349
1373
// (Variant1, Variant1, ...) => Body1
1350
1374
// (Variant2, Variant2, ...) => Body2,
1351
1375
// ...
1352
- // _ => ::core::intrinsics::unreachable()
1376
+ // _ => ::core::intrinsics::unreachable(),
1353
1377
// }
1354
1378
let get_match_expr = |mut selflike_args : ThinVec < P < Expr > > | {
1355
1379
let match_arg = if selflike_args. len ( ) == 1 {
0 commit comments