Skip to content

Commit 14e8605

Browse files
committed
Skip pub structs with repr(c) and repr(transparent) in dead code analysis
1 parent 8cf5101 commit 14e8605

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

compiler/rustc_passes/src/dead.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,10 @@ fn create_and_seed_worklist(
898898
match tcx.def_kind(id) {
899899
DefKind::Impl { .. } => false,
900900
DefKind::AssocConst | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
901-
DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()),
901+
DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()) || {
902+
let def = tcx.adt_def(id);
903+
def.repr().c() || def.repr().transparent()
904+
},
902905
_ => true
903906
})
904907
.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)