1
1
//! Determining which types for which we can emit `#[derive(Debug)]`.
2
- use super :: MonotoneFramework ;
2
+
3
+ use super :: { ConstrainResult , MonotoneFramework } ;
3
4
use ir:: context:: { BindgenContext , ItemId } ;
4
5
use ir:: item:: ItemSet ;
5
6
use std:: collections:: HashSet ;
@@ -77,15 +78,15 @@ impl<'ctx, 'gen> CannotDeriveDebug<'ctx, 'gen> {
77
78
}
78
79
}
79
80
80
- fn insert ( & mut self , id : ItemId ) -> bool {
81
+ fn insert ( & mut self , id : ItemId ) -> ConstrainResult {
81
82
let was_not_already_in_set = self . cannot_derive_debug . insert ( id) ;
82
83
assert ! (
83
84
was_not_already_in_set,
84
85
"We shouldn't try and insert {:?} twice because if it was \
85
86
already in the set, `constrain` should have exited early.",
86
87
id
87
88
) ;
88
- true
89
+ ConstrainResult :: Changed
89
90
}
90
91
}
91
92
@@ -137,20 +138,20 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
137
138
self . ctx . whitelisted_items ( ) . iter ( ) . cloned ( ) . collect ( )
138
139
}
139
140
140
- fn constrain ( & mut self , id : ItemId ) -> bool {
141
+ fn constrain ( & mut self , id : ItemId ) -> ConstrainResult {
141
142
if self . cannot_derive_debug . contains ( & id) {
142
- return false ;
143
+ return ConstrainResult :: Same ;
143
144
}
144
145
145
146
let item = self . ctx . resolve_item ( id) ;
146
147
let ty = match item. as_type ( ) {
147
- None => return false ,
148
+ None => return ConstrainResult :: Same ,
148
149
Some ( ty) => ty
149
150
} ;
150
151
151
152
match * ty. kind ( ) {
152
153
// Handle the simple cases. These can derive debug without further
153
- // information
154
+ // information.
154
155
TypeKind :: Void |
155
156
TypeKind :: NullPtr |
156
157
TypeKind :: Int ( ..) |
@@ -165,96 +166,112 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
165
166
TypeKind :: ObjCInterface ( ..) |
166
167
TypeKind :: ObjCId |
167
168
TypeKind :: ObjCSel => {
168
- return false ;
169
+ ConstrainResult :: Same
169
170
} ,
171
+
170
172
TypeKind :: Opaque => {
171
173
if ty. layout ( self . ctx )
172
174
. map_or ( true , |l| l. opaque ( ) . can_trivially_derive_debug ( self . ctx , ( ) ) ) {
173
- return false ;
175
+ ConstrainResult :: Same
174
176
} else {
175
- return self . insert ( id) ;
177
+ self . insert ( id)
176
178
}
177
179
} ,
180
+
178
181
TypeKind :: Array ( t, len) => {
182
+ if self . cannot_derive_debug . contains ( & t) {
183
+ return self . insert ( id) ;
184
+ }
185
+
179
186
if len <= RUST_DERIVE_IN_ARRAY_LIMIT {
180
- if self . cannot_derive_debug . contains ( & t) {
181
- return self . insert ( id) ;
182
- }
183
- return false ;
187
+ ConstrainResult :: Same
184
188
} else {
185
- return self . insert ( id) ;
189
+ self . insert ( id)
186
190
}
187
191
} ,
192
+
188
193
TypeKind :: ResolvedTypeRef ( t) |
189
194
TypeKind :: TemplateAlias ( t, _) |
190
195
TypeKind :: Alias ( t) => {
191
196
if self . cannot_derive_debug . contains ( & t) {
192
- return self . insert ( id) ;
197
+ self . insert ( id)
198
+ } else {
199
+ ConstrainResult :: Same
193
200
}
194
- return false ;
195
201
} ,
202
+
196
203
TypeKind :: Comp ( ref info) => {
197
204
if info. has_non_type_template_params ( ) {
198
- if ty. layout ( self . ctx ) . map_or ( true ,
199
- |l| l. opaque ( ) . can_trivially_derive_debug ( self . ctx , ( ) ) ) {
200
- return false ;
205
+ if ty. layout ( self . ctx )
206
+ . map_or ( true ,
207
+ |l| l. opaque ( ) . can_trivially_derive_debug ( self . ctx , ( ) ) ) {
208
+ return ConstrainResult :: Same ;
201
209
} else {
202
210
return self . insert ( id) ;
203
211
}
204
212
}
213
+
205
214
if info. kind ( ) == CompKind :: Union {
206
215
if self . ctx . options ( ) . unstable_rust {
207
216
return self . insert ( id) ;
208
217
}
209
218
210
- if ty. layout ( self . ctx ) . map_or ( true ,
211
- |l| l. opaque ( ) . can_trivially_derive_debug ( self . ctx , ( ) ) ) {
212
- return false ;
219
+ if ty. layout ( self . ctx )
220
+ . map_or ( true ,
221
+ |l| l. opaque ( ) . can_trivially_derive_debug ( self . ctx , ( ) ) ) {
222
+ return ConstrainResult :: Same ;
213
223
} else {
214
224
return self . insert ( id) ;
215
225
}
216
226
}
227
+
217
228
let bases_cannot_derive = info. base_members ( )
218
229
. iter ( )
219
230
. any ( |base| self . cannot_derive_debug . contains ( & base. ty ) ) ;
220
231
if bases_cannot_derive {
221
232
return self . insert ( id) ;
222
233
}
234
+
223
235
let fields_cannot_derive = info. fields ( )
224
236
. iter ( )
225
237
. any ( |f| {
226
- match f {
227
- & Field :: DataMember ( ref data) => self . cannot_derive_debug . contains ( & data. ty ( ) ) ,
228
- & Field :: Bitfields ( ref bfu) => bfu. bitfields ( )
229
- . iter ( ) . any ( |b| {
230
- self . cannot_derive_debug . contains ( & b. ty ( ) )
231
- } )
238
+ match * f {
239
+ Field :: DataMember ( ref data) => {
240
+ self . cannot_derive_debug . contains ( & data. ty ( ) )
241
+ }
242
+ Field :: Bitfields ( ref bfu) => {
243
+ bfu. bitfields ( )
244
+ . iter ( ) . any ( |b| {
245
+ self . cannot_derive_debug . contains ( & b. ty ( ) )
246
+ } )
247
+ }
232
248
}
233
249
} ) ;
234
250
if fields_cannot_derive {
235
251
return self . insert ( id) ;
236
252
}
237
- false
253
+
254
+ ConstrainResult :: Same
238
255
} ,
256
+
239
257
TypeKind :: Pointer ( inner) => {
240
- let inner_type = self . ctx . resolve_type ( inner) ;
241
- if let TypeKind :: Function ( ref sig) =
242
- * inner_type. canonical_type ( self . ctx ) . kind ( ) {
243
- if sig. can_trivially_derive_debug ( & self . ctx , ( ) ) {
244
- return false ;
245
- } else {
246
- return self . insert ( id) ;
247
- }
258
+ let inner_type = self . ctx . resolve_type ( inner) . canonical_type ( self . ctx ) ;
259
+ if let TypeKind :: Function ( ref sig) = * inner_type. kind ( ) {
260
+ if !sig. can_trivially_derive_debug ( & self . ctx , ( ) ) {
261
+ return self . insert ( id) ;
248
262
}
249
- false
263
+ }
264
+ ConstrainResult :: Same
250
265
} ,
266
+
251
267
TypeKind :: TemplateInstantiation ( ref template) => {
252
268
let args_cannot_derive = template. template_arguments ( )
253
269
. iter ( )
254
270
. any ( |arg| self . cannot_derive_debug . contains ( & arg) ) ;
255
271
if args_cannot_derive {
256
272
return self . insert ( id) ;
257
273
}
274
+
258
275
let ty_cannot_derive = template. template_definition ( )
259
276
. into_resolver ( )
260
277
. through_type_refs ( )
@@ -269,19 +286,26 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
269
286
// idea of the layout than the definition does.
270
287
if c. has_non_type_template_params ( ) {
271
288
let opaque = ty. layout ( self . ctx )
272
- . or_else ( || self . ctx . resolve_type ( template. template_definition ( ) ) . layout ( self . ctx ) )
289
+ . or_else ( || {
290
+ self . ctx
291
+ . resolve_type ( template. template_definition ( ) )
292
+ . layout ( self . ctx )
293
+ } )
273
294
. unwrap_or ( Layout :: zero ( ) )
274
295
. opaque ( ) ;
275
296
Some ( !opaque. can_trivially_derive_debug ( & self . ctx , ( ) ) )
276
297
} else {
277
298
None
278
299
}
279
300
} )
280
- . unwrap_or_else ( || self . cannot_derive_debug . contains ( & template. template_definition ( ) ) ) ;
301
+ . unwrap_or_else ( || {
302
+ self . cannot_derive_debug . contains ( & template. template_definition ( ) )
303
+ } ) ;
281
304
if ty_cannot_derive {
282
305
return self . insert ( id) ;
283
306
}
284
- false
307
+
308
+ ConstrainResult :: Same
285
309
} ,
286
310
}
287
311
}
0 commit comments