@@ -87,6 +87,11 @@ impl<'ctx, 'gen> CannotDeriveDefault<'ctx, 'gen> {
87
87
88
88
ConstrainResult :: Changed
89
89
}
90
+
91
+ fn is_not_default ( & self , id : ItemId ) -> bool {
92
+ self . cannot_derive_default . contains ( & id) ||
93
+ !self . ctx . whitelisted_items ( ) . contains ( & id)
94
+ }
90
95
}
91
96
92
97
impl < ' ctx , ' gen > MonotoneFramework for CannotDeriveDefault < ' ctx , ' gen > {
@@ -153,6 +158,11 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
153
158
return ConstrainResult :: Same ;
154
159
}
155
160
161
+ if !self . ctx . whitelisted_items ( ) . contains ( & id) {
162
+ trace ! ( " blacklisted items pessimistically cannot derive Default" ) ;
163
+ return ConstrainResult :: Same ;
164
+ }
165
+
156
166
let item = self . ctx . resolve_item ( id) ;
157
167
let ty = match item. as_type ( ) {
158
168
Some ( ty) => ty,
@@ -166,7 +176,9 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
166
176
let layout_can_derive = ty. layout ( self . ctx ) . map_or ( true , |l| {
167
177
l. opaque ( ) . can_trivially_derive_default ( )
168
178
} ) ;
169
- return if layout_can_derive {
179
+ return if layout_can_derive &&
180
+ !( ty. is_union ( ) &&
181
+ self . ctx . options ( ) . rust_features ( ) . untagged_union ( ) ) {
170
182
trace ! ( " we can trivially derive Default for the layout" ) ;
171
183
ConstrainResult :: Same
172
184
} else {
@@ -213,7 +225,7 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
213
225
}
214
226
215
227
TypeKind :: Array ( t, len) => {
216
- if self . cannot_derive_default . contains ( & t) {
228
+ if self . is_not_default ( t) {
217
229
trace ! (
218
230
" arrays of T for which we cannot derive Default \
219
231
also cannot derive Default"
@@ -233,7 +245,7 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
233
245
TypeKind :: ResolvedTypeRef ( t) |
234
246
TypeKind :: TemplateAlias ( t, _) |
235
247
TypeKind :: Alias ( t) => {
236
- if self . cannot_derive_default . contains ( & t) {
248
+ if self . is_not_default ( t) {
237
249
trace ! (
238
250
" aliases and type refs to T which cannot derive \
239
251
Default also cannot derive Default"
@@ -280,7 +292,7 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
280
292
let bases_cannot_derive =
281
293
info. base_members ( ) . iter ( ) . any ( |base| {
282
294
!self . ctx . whitelisted_items ( ) . contains ( & base. ty ) ||
283
- self . cannot_derive_default . contains ( & base. ty )
295
+ self . is_not_default ( base. ty )
284
296
} ) ;
285
297
if bases_cannot_derive {
286
298
trace ! (
@@ -296,14 +308,14 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
296
308
!self . ctx . whitelisted_items ( ) . contains (
297
309
& data. ty ( ) ,
298
310
) ||
299
- self . cannot_derive_default . contains ( & data. ty ( ) )
311
+ self . is_not_default ( data. ty ( ) )
300
312
}
301
313
Field :: Bitfields ( ref bfu) => {
302
314
bfu. bitfields ( ) . iter ( ) . any ( |b| {
303
315
!self . ctx . whitelisted_items ( ) . contains (
304
316
& b. ty ( ) ,
305
317
) ||
306
- self . cannot_derive_default . contains ( & b. ty ( ) )
318
+ self . is_not_default ( b. ty ( ) )
307
319
} )
308
320
}
309
321
} ) ;
@@ -319,45 +331,34 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
319
331
}
320
332
321
333
TypeKind :: TemplateInstantiation ( ref template) => {
322
- if self . ctx . whitelisted_items ( ) . contains (
323
- & template. template_definition ( ) ,
324
- )
325
- {
326
- let args_cannot_derive =
327
- template. template_arguments ( ) . iter ( ) . any ( |arg| {
328
- self . cannot_derive_default . contains ( & arg)
329
- } ) ;
330
- if args_cannot_derive {
331
- trace ! (
332
- " template args cannot derive Default, so \
333
- insantiation can't either"
334
- ) ;
335
- return self . insert ( id) ;
336
- }
337
-
338
- assert ! (
339
- !template. template_definition( ) . is_opaque( self . ctx, & ( ) ) ,
340
- "The early ty.is_opaque check should have handled this case"
334
+ let args_cannot_derive =
335
+ template. template_arguments ( ) . iter ( ) . any ( |arg| {
336
+ self . is_not_default ( * arg)
337
+ } ) ;
338
+ if args_cannot_derive {
339
+ trace ! (
340
+ " template args cannot derive Default, so \
341
+ insantiation can't either"
341
342
) ;
342
- let def_cannot_derive =
343
- self . cannot_derive_default . contains ( & template
344
- . template_definition ( ) ) ;
345
- if def_cannot_derive {
346
- trace ! (
347
- " template definition cannot derive Default, so \
348
- insantiation can't either"
349
- ) ;
350
- return self . insert ( id) ;
351
- }
343
+ return self . insert ( id) ;
344
+ }
352
345
353
- trace ! ( " template instantiation can derive Default" ) ;
354
- ConstrainResult :: Same
355
- } else {
346
+ assert ! (
347
+ !template. template_definition( ) . is_opaque( self . ctx, & ( ) ) ,
348
+ "The early ty.is_opaque check should have handled this case"
349
+ ) ;
350
+ let def_cannot_derive =
351
+ self . is_not_default ( template. template_definition ( ) ) ;
352
+ if def_cannot_derive {
356
353
trace ! (
357
- " blacklisted template instantiation cannot derive default"
354
+ " template definition cannot derive Default, so \
355
+ insantiation can't either"
358
356
) ;
359
357
return self . insert ( id) ;
360
358
}
359
+
360
+ trace ! ( " template instantiation can derive Default" ) ;
361
+ ConstrainResult :: Same
361
362
}
362
363
363
364
TypeKind :: Opaque => {
0 commit comments