Skip to content

Commit b925ac9

Browse files
committed
Skip pub structs with repr(c) and repr(transparent) in dead code analysis
1 parent e9e6e2e commit b925ac9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Diff for: compiler/rustc_passes/src/dead.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,10 @@ fn create_and_seed_worklist(
931931
match tcx.def_kind(id) {
932932
DefKind::Impl { .. } => false,
933933
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
934-
DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()) || has_allow_dead_code_or_lang_attr(tcx, id).is_some(),
934+
DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()) || has_allow_dead_code_or_lang_attr(tcx, id).is_some() || {
935+
let def = tcx.adt_def(id);
936+
def.repr().c() || def.repr().transparent()
937+
},
935938
_ => true
936939
})
937940
.map(|id| (id, ComesFromAllowExpect::No))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ check-pass
2+
#![deny(dead_code)]
3+
4+
#[repr(C)]
5+
pub struct Foo {
6+
pub i: i16,
7+
align: i16
8+
}
9+
10+
mod ffi {
11+
use super::*;
12+
13+
extern "C" {
14+
pub fn DomPromise_AddRef(promise: *const Promise);
15+
pub fn DomPromise_Release(promise: *const Promise);
16+
}
17+
}
18+
19+
#[repr(C)]
20+
pub struct Promise {
21+
private: [u8; 0],
22+
__nosync: ::std::marker::PhantomData<::std::rc::Rc<u8>>,
23+
}
24+
25+
pub unsafe trait RefCounted {
26+
unsafe fn addref(&self);
27+
unsafe fn release(&self);
28+
}
29+
30+
unsafe impl RefCounted for Promise {
31+
unsafe fn addref(&self) {
32+
ffi::DomPromise_AddRef(self)
33+
}
34+
unsafe fn release(&self) {
35+
ffi::DomPromise_Release(self)
36+
}
37+
}
38+
39+
fn main() {}

0 commit comments

Comments
 (0)