Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip pub structs with repr(c) and repr(transparent) in dead code analysis #127104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,11 @@ fn create_and_seed_worklist(
match tcx.def_kind(id) {
DefKind::Impl { .. } => false,
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
DefKind::Struct => struct_all_fields_are_public(tcx, id) || has_allow_dead_code_or_lang_attr(tcx, id).is_some(),
DefKind::Struct => struct_all_fields_are_public(tcx, id) || has_allow_dead_code_or_lang_attr(tcx, id).is_some() || {
let def = tcx.adt_def(id);
// often declared in Rust but constructed by FFI, so ignore
def.repr().c() || def.repr().transparent()
mu001999 marked this conversation as resolved.
Show resolved Hide resolved
},
_ => true
})
.map(|id| (id, ComesFromAllowExpect::No))
Expand Down
39 changes: 39 additions & 0 deletions tests/ui/lint/dead-code/not-lint-structs-constructed-externally.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//@ check-pass
#![deny(dead_code)]

#[repr(C)]
pub struct Foo {
pub i: i16,
align: i16
}

mod ffi {
use super::*;

extern "C" {
pub fn DomPromise_AddRef(promise: *const Promise);
pub fn DomPromise_Release(promise: *const Promise);
}
}

#[repr(C)]
pub struct Promise {
private: [u8; 0],
__nosync: ::std::marker::PhantomData<::std::rc::Rc<u8>>,
}

pub unsafe trait RefCounted {
unsafe fn addref(&self);
unsafe fn release(&self);
}

unsafe impl RefCounted for Promise {
unsafe fn addref(&self) {
ffi::DomPromise_AddRef(self)
}
unsafe fn release(&self) {
ffi::DomPromise_Release(self)
}
}

fn main() {}
Loading