@@ -315,15 +315,22 @@ impl<'tcx> ReachableContext<'tcx> {
315
315
316
316
fn check_item < ' tcx > (
317
317
tcx : TyCtxt < ' tcx > ,
318
- item : & hir:: Item < ' _ > ,
318
+ id : hir:: ItemId ,
319
319
worklist : & mut Vec < LocalDefId > ,
320
- access_levels : & privacy:: AccessLevels
320
+ access_levels : & privacy:: AccessLevels ,
321
321
) {
322
- push_to_worklist_if_has_custom_linkage ( tcx, worklist, item. def_id ) ;
322
+ if has_custom_linkage ( tcx, id. def_id ) {
323
+ worklist. push ( id. def_id ) ;
324
+ }
325
+
326
+ if !matches ! ( tcx. def_kind( id. def_id) , DefKind :: Impl ) {
327
+ return ;
328
+ }
323
329
324
330
// We need only trait impls here, not inherent impls, and only non-exported ones
331
+ let item = tcx. hir ( ) . item ( id) ;
325
332
if let hir:: ItemKind :: Impl ( hir:: Impl { of_trait : Some ( ref trait_ref) , ref items, .. } ) =
326
- item. kind
333
+ item. kind
327
334
{
328
335
if !access_levels. is_reachable ( item. def_id ) {
329
336
// FIXME(#53488) remove `let`
@@ -339,30 +346,27 @@ fn check_item<'tcx>(
339
346
}
340
347
341
348
worklist. extend (
342
- tcx. provided_trait_methods ( trait_def_id)
343
- . map ( |assoc| assoc. def_id . expect_local ( ) ) ,
349
+ tcx. provided_trait_methods ( trait_def_id) . map ( |assoc| assoc. def_id . expect_local ( ) ) ,
344
350
) ;
345
351
}
346
352
}
347
353
}
348
354
349
- fn push_to_worklist_if_has_custom_linkage < ' tcx > ( tcx : TyCtxt < ' tcx > , worklist : & mut Vec < LocalDefId > , def_id : LocalDefId ) {
355
+ fn has_custom_linkage < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : LocalDefId ) -> bool {
350
356
// Anything which has custom linkage gets thrown on the worklist no
351
357
// matter where it is in the crate, along with "special std symbols"
352
358
// which are currently akin to allocator symbols.
353
- if tcx. def_kind ( def_id) . has_codegen_attrs ( ) {
354
- let codegen_attrs = tcx. codegen_fn_attrs ( def_id) ;
355
- if codegen_attrs. contains_extern_indicator ( )
356
- || codegen_attrs. flags . contains ( CodegenFnAttrFlags :: RUSTC_STD_INTERNAL_SYMBOL )
357
- // FIXME(nbdd0121): `#[used]` are marked as reachable here so it's picked up by
358
- // `linked_symbols` in cg_ssa. They won't be exported in binary or cdylib due to their
359
- // `SymbolExportLevel::Rust` export level but may end up being exported in dylibs.
360
- || codegen_attrs. flags . contains ( CodegenFnAttrFlags :: USED )
361
- || codegen_attrs. flags . contains ( CodegenFnAttrFlags :: USED_LINKER )
362
- {
363
- worklist. push ( def_id) ;
364
- }
359
+ if !tcx. def_kind ( def_id) . has_codegen_attrs ( ) {
360
+ return false ;
365
361
}
362
+ let codegen_attrs = tcx. codegen_fn_attrs ( def_id) ;
363
+ codegen_attrs. contains_extern_indicator ( )
364
+ || codegen_attrs. flags . contains ( CodegenFnAttrFlags :: RUSTC_STD_INTERNAL_SYMBOL )
365
+ // FIXME(nbdd0121): `#[used]` are marked as reachable here so it's picked up by
366
+ // `linked_symbols` in cg_ssa. They won't be exported in binary or cdylib due to their
367
+ // `SymbolExportLevel::Rust` export level but may end up being exported in dylibs.
368
+ || codegen_attrs. flags . contains ( CodegenFnAttrFlags :: USED )
369
+ || codegen_attrs. flags . contains ( CodegenFnAttrFlags :: USED_LINKER )
366
370
}
367
371
368
372
fn reachable_set < ' tcx > ( tcx : TyCtxt < ' tcx > , ( ) : ( ) ) -> FxHashSet < LocalDefId > {
@@ -405,11 +409,13 @@ fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashSet<LocalDefId> {
405
409
let crate_items = tcx. hir_crate_items ( ( ) ) ;
406
410
407
411
for id in crate_items. items ( ) {
408
- check_item ( tcx, tcx . hir ( ) . item ( id ) , & mut reachable_context. worklist , access_levels) ;
412
+ check_item ( tcx, id , & mut reachable_context. worklist , access_levels) ;
409
413
}
410
414
411
415
for id in crate_items. impl_items ( ) {
412
- push_to_worklist_if_has_custom_linkage ( tcx, & mut reachable_context. worklist , id. def_id )
416
+ if has_custom_linkage ( tcx, id. def_id ) {
417
+ reachable_context. worklist . push ( id. def_id ) ;
418
+ }
413
419
}
414
420
}
415
421
0 commit comments