Skip to content

Commit 90685c6

Browse files
committed
check def_kind before fetching item
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
1 parent 0a029e2 commit 90685c6

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

compiler/rustc_passes/src/reachable.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,22 @@ impl<'tcx> ReachableContext<'tcx> {
315315

316316
fn check_item<'tcx>(
317317
tcx: TyCtxt<'tcx>,
318-
item: &hir::Item<'_>,
318+
id: hir::ItemId,
319319
worklist: &mut Vec<LocalDefId>,
320-
access_levels: &privacy::AccessLevels
320+
access_levels: &privacy::AccessLevels,
321321
) {
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+
}
323329

324330
// We need only trait impls here, not inherent impls, and only non-exported ones
331+
let item = tcx.hir().item(id);
325332
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(ref trait_ref), ref items, .. }) =
326-
item.kind
333+
item.kind
327334
{
328335
if !access_levels.is_reachable(item.def_id) {
329336
// FIXME(#53488) remove `let`
@@ -339,30 +346,27 @@ fn check_item<'tcx>(
339346
}
340347

341348
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()),
344350
);
345351
}
346352
}
347353
}
348354

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 {
350356
// Anything which has custom linkage gets thrown on the worklist no
351357
// matter where it is in the crate, along with "special std symbols"
352358
// 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;
365361
}
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)
366370
}
367371

368372
fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashSet<LocalDefId> {
@@ -405,11 +409,13 @@ fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashSet<LocalDefId> {
405409
let crate_items = tcx.hir_crate_items(());
406410

407411
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);
409413
}
410414

411415
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+
}
413419
}
414420
}
415421

0 commit comments

Comments
 (0)