Skip to content

Commit 5019791

Browse files
committed
Auto merge of #79752 - cjgillot:dead-alien, r=lcnr
Visit ForeignItems when marking dead code Follow-up to #79318 r? `@lcnr`
2 parents 4fd4a98 + 37853f9 commit 5019791

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

compiler/rustc_passes/src/dead.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -396,24 +396,6 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
396396
}
397397
}
398398
}
399-
hir::ItemKind::Trait(.., trait_item_refs) => {
400-
for trait_item_ref in trait_item_refs {
401-
let trait_item = self.krate.trait_item(trait_item_ref.id);
402-
match trait_item.kind {
403-
hir::TraitItemKind::Const(_, Some(_))
404-
| hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
405-
if has_allow_dead_code_or_lang_attr(
406-
self.tcx,
407-
trait_item.hir_id,
408-
&trait_item.attrs,
409-
) {
410-
self.worklist.push(trait_item.hir_id);
411-
}
412-
}
413-
_ => {}
414-
}
415-
}
416-
}
417399
hir::ItemKind::Impl { ref of_trait, items, .. } => {
418400
if of_trait.is_some() {
419401
self.worklist.push(item.hir_id);
@@ -440,15 +422,37 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
440422
}
441423
}
442424

443-
fn visit_trait_item(&mut self, _item: &hir::TraitItem<'_>) {
444-
// ignore: we are handling this in `visit_item` above
425+
fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
426+
match trait_item.kind {
427+
hir::TraitItemKind::Const(_, Some(_))
428+
| hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
429+
if has_allow_dead_code_or_lang_attr(self.tcx, trait_item.hir_id, &trait_item.attrs)
430+
{
431+
self.worklist.push(trait_item.hir_id);
432+
}
433+
}
434+
_ => {}
435+
}
445436
}
446437

447438
fn visit_impl_item(&mut self, _item: &hir::ImplItem<'_>) {
448439
// ignore: we are handling this in `visit_item` above
449440
}
450441

451-
fn visit_foreign_item(&mut self, _item: &'v hir::ForeignItem<'v>) {}
442+
fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem<'_>) {
443+
match foreign_item.kind {
444+
hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Fn(..) => {
445+
if has_allow_dead_code_or_lang_attr(
446+
self.tcx,
447+
foreign_item.hir_id,
448+
&foreign_item.attrs,
449+
) {
450+
self.worklist.push(foreign_item.hir_id);
451+
}
452+
}
453+
_ => {}
454+
}
455+
}
452456
}
453457

454458
fn create_and_seed_worklist<'tcx>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Verify that we do not warn on types that are used by foreign functions.
2+
// check-pass
3+
#![deny(dead_code)]
4+
5+
#[repr(C)]
6+
struct Type(u8);
7+
8+
#[repr(C)]
9+
struct Param(u8);
10+
11+
extern "C" {
12+
#[allow(dead_code)]
13+
fn hey(t: Param);
14+
15+
#[allow(dead_code)]
16+
static much: Type;
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)