@@ -11,7 +11,6 @@ use ir::ty::TypeKind;
11
11
use ir:: comp:: Field ;
12
12
use ir:: traversal:: Trace ;
13
13
use ir:: comp:: FieldMethods ;
14
- use ir:: layout:: Layout ;
15
14
use ir:: derive:: CanTriviallyDeriveDebug ;
16
15
use ir:: comp:: CompKind ;
17
16
@@ -79,13 +78,16 @@ impl<'ctx, 'gen> CannotDeriveDebug<'ctx, 'gen> {
79
78
}
80
79
81
80
fn insert ( & mut self , id : ItemId ) -> ConstrainResult {
81
+ trace ! ( "inserting {:?} into the cannot_derive_debug set" , id) ;
82
+
82
83
let was_not_already_in_set = self . cannot_derive_debug . insert ( id) ;
83
84
assert ! (
84
85
was_not_already_in_set,
85
86
"We shouldn't try and insert {:?} twice because if it was \
86
87
already in the set, `constrain` should have exited early.",
87
88
id
88
89
) ;
90
+
89
91
ConstrainResult :: Changed
90
92
}
91
93
}
@@ -128,16 +130,35 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
128
130
}
129
131
130
132
fn constrain ( & mut self , id : ItemId ) -> ConstrainResult {
133
+ trace ! ( "constrain: {:?}" , id) ;
134
+
131
135
if self . cannot_derive_debug . contains ( & id) {
136
+ trace ! ( " already know it cannot derive Debug" ) ;
132
137
return ConstrainResult :: Same ;
133
138
}
134
139
135
140
let item = self . ctx . resolve_item ( id) ;
136
141
let ty = match item. as_type ( ) {
137
- None => return ConstrainResult :: Same ,
138
- Some ( ty) => ty
142
+ Some ( ty) => ty,
143
+ None => {
144
+ trace ! ( " not a type; ignoring" ) ;
145
+ return ConstrainResult :: Same ;
146
+ }
139
147
} ;
140
148
149
+ if ty. is_opaque ( self . ctx , item) {
150
+ let layout_can_derive = ty. layout ( self . ctx ) . map_or ( true , |l| {
151
+ l. opaque ( ) . can_trivially_derive_debug ( self . ctx , ( ) )
152
+ } ) ;
153
+ return if layout_can_derive {
154
+ trace ! ( " we can trivially derive Debug for the layout" ) ;
155
+ ConstrainResult :: Same
156
+ } else {
157
+ trace ! ( " we cannot derive Debug for the layout" ) ;
158
+ self . insert ( id)
159
+ } ;
160
+ }
161
+
141
162
match * ty. kind ( ) {
142
163
// Handle the simple cases. These can derive debug without further
143
164
// information.
@@ -155,61 +176,59 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
155
176
TypeKind :: ObjCInterface ( ..) |
156
177
TypeKind :: ObjCId |
157
178
TypeKind :: ObjCSel => {
179
+ trace ! ( " simple type that can always derive Debug" ) ;
158
180
ConstrainResult :: Same
159
- } ,
160
-
161
- TypeKind :: Opaque => {
162
- if ty. layout ( self . ctx )
163
- . map_or ( true , |l| l. opaque ( ) . can_trivially_derive_debug ( self . ctx , ( ) ) ) {
164
- ConstrainResult :: Same
165
- } else {
166
- self . insert ( id)
167
- }
168
- } ,
181
+ }
169
182
170
183
TypeKind :: Array ( t, len) => {
171
184
if self . cannot_derive_debug . contains ( & t) {
185
+ trace ! ( " arrays of T for which we cannot derive Debug \
186
+ also cannot derive Debug") ;
172
187
return self . insert ( id) ;
173
188
}
174
189
175
190
if len <= RUST_DERIVE_IN_ARRAY_LIMIT {
191
+ trace ! ( " array is small enough to derive Debug" ) ;
176
192
ConstrainResult :: Same
177
193
} else {
194
+ trace ! ( " array is too large to derive Debug" ) ;
178
195
self . insert ( id)
179
196
}
180
- } ,
197
+ }
181
198
182
199
TypeKind :: ResolvedTypeRef ( t) |
183
200
TypeKind :: TemplateAlias ( t, _) |
184
201
TypeKind :: Alias ( t) => {
185
202
if self . cannot_derive_debug . contains ( & t) {
203
+ trace ! ( " aliases and type refs to T which cannot derive \
204
+ Debug also cannot derive Debug") ;
186
205
self . insert ( id)
187
206
} else {
207
+ trace ! ( " aliases and type refs to T which can derive \
208
+ Debug can also derive Debug") ;
188
209
ConstrainResult :: Same
189
210
}
190
- } ,
211
+ }
191
212
192
213
TypeKind :: Comp ( ref info) => {
193
- if info. has_non_type_template_params ( ) {
194
- if ty. layout ( self . ctx )
195
- . map_or ( true ,
196
- |l| l. opaque ( ) . can_trivially_derive_debug ( self . ctx , ( ) ) ) {
197
- return ConstrainResult :: Same ;
198
- } else {
199
- return self . insert ( id) ;
200
- }
201
- }
214
+ assert ! (
215
+ !info. has_non_type_template_params( ) ,
216
+ "The early ty.is_opaque check should have handled this case"
217
+ ) ;
202
218
203
219
if info. kind ( ) == CompKind :: Union {
204
220
if self . ctx . options ( ) . unstable_rust {
221
+ trace ! ( " cannot derive Debug for Rust unions" ) ;
205
222
return self . insert ( id) ;
206
223
}
207
224
208
225
if ty. layout ( self . ctx )
209
226
. map_or ( true ,
210
227
|l| l. opaque ( ) . can_trivially_derive_debug ( self . ctx , ( ) ) ) {
228
+ trace ! ( " union layout can trivially derive Debug" ) ;
211
229
return ConstrainResult :: Same ;
212
230
} else {
231
+ trace ! ( " union layout cannot derive Debug" ) ;
213
232
return self . insert ( id) ;
214
233
}
215
234
}
@@ -218,6 +237,8 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
218
237
. iter ( )
219
238
. any ( |base| self . cannot_derive_debug . contains ( & base. ty ) ) ;
220
239
if bases_cannot_derive {
240
+ trace ! ( " base members cannot derive Debug, so we can't \
241
+ either") ;
221
242
return self . insert ( id) ;
222
243
}
223
244
@@ -237,69 +258,57 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
237
258
}
238
259
} ) ;
239
260
if fields_cannot_derive {
261
+ trace ! ( " fields cannot derive Debug, so we can't either" ) ;
240
262
return self . insert ( id) ;
241
263
}
242
264
265
+ trace ! ( " comp can derive Debug" ) ;
243
266
ConstrainResult :: Same
244
- } ,
267
+ }
245
268
246
269
TypeKind :: Pointer ( inner) => {
247
270
let inner_type = self . ctx . resolve_type ( inner) . canonical_type ( self . ctx ) ;
248
271
if let TypeKind :: Function ( ref sig) = * inner_type. kind ( ) {
249
272
if !sig. can_trivially_derive_debug ( & self . ctx , ( ) ) {
273
+ trace ! ( " function pointer that can't trivially derive Debug" ) ;
250
274
return self . insert ( id) ;
251
275
}
252
276
}
277
+ trace ! ( " pointers can derive Debug" ) ;
253
278
ConstrainResult :: Same
254
- } ,
279
+ }
255
280
256
281
TypeKind :: TemplateInstantiation ( ref template) => {
257
282
let args_cannot_derive = template. template_arguments ( )
258
283
. iter ( )
259
284
. any ( |arg| self . cannot_derive_debug . contains ( & arg) ) ;
260
285
if args_cannot_derive {
286
+ trace ! ( " template args cannot derive Debug, so \
287
+ insantiation can't either") ;
261
288
return self . insert ( id) ;
262
289
}
263
290
264
- let template_definition = template. template_definition ( )
265
- . into_resolver ( )
266
- . through_type_refs ( )
267
- . through_type_aliases ( )
268
- . resolve ( self . ctx ) ;
269
-
270
- let ty_cannot_derive = template_definition
271
- . as_type ( )
272
- . expect ( "Instantiations of a non-type?" )
273
- . as_comp ( )
274
- . and_then ( |c| {
275
- // For non-type template parameters, or opaque template
276
- // definitions, we generate an opaque blob, and in this
277
- // case the instantiation has a better idea of the
278
- // layout than the definition does.
279
- if template_definition. is_opaque ( self . ctx , & ( ) ) ||
280
- c. has_non_type_template_params ( ) {
281
- let opaque = ty. layout ( self . ctx )
282
- . or_else ( || {
283
- self . ctx
284
- . resolve_type ( template. template_definition ( ) )
285
- . layout ( self . ctx )
286
- } )
287
- . unwrap_or ( Layout :: zero ( ) )
288
- . opaque ( ) ;
289
- Some ( !opaque. can_trivially_derive_debug ( & self . ctx , ( ) ) )
290
- } else {
291
- None
292
- }
293
- } )
294
- . unwrap_or_else ( || {
295
- self . cannot_derive_debug . contains ( & template. template_definition ( ) )
296
- } ) ;
297
- if ty_cannot_derive {
291
+ assert ! (
292
+ !template. template_definition( ) . is_opaque( self . ctx, & ( ) ) ,
293
+ "The early ty.is_opaque check should have handled this case"
294
+ ) ;
295
+ let def_cannot_derive = self . cannot_derive_debug
296
+ . contains ( & template. template_definition ( ) ) ;
297
+ if def_cannot_derive {
298
+ trace ! ( " template definition cannot derive Debug, so \
299
+ insantiation can't either") ;
298
300
return self . insert ( id) ;
299
301
}
300
302
303
+ trace ! ( " template instantiation can derive Debug" ) ;
301
304
ConstrainResult :: Same
302
- } ,
305
+ }
306
+
307
+ TypeKind :: Opaque => {
308
+ unreachable ! (
309
+ "The early ty.is_opaque check should have handled this case"
310
+ )
311
+ }
303
312
}
304
313
}
305
314
0 commit comments