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

Avoid generating invalid Debug traits #1473

Merged
merged 1 commit into from Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/structs.rs
Expand Up @@ -194,7 +194,7 @@ fn gen_compare_traits(def: &TypeDef, name: &TokenStream, cfg: &Cfg, gen: &Gen) -
}

fn gen_debug(def: &TypeDef, ident: &TokenStream, cfg: &Cfg, gen: &Gen) -> TokenStream {
if gen.sys || def.is_union() || def.has_explicit() || def.is_packed() {
if gen.sys || def.has_union() || def.has_pack() {
quote! {}
} else {
let name = ident.as_str();
Expand Down
14 changes: 11 additions & 3 deletions crates/libs/metadata/src/element_type.rs
Expand Up @@ -199,10 +199,18 @@ impl ElementType {
}
}

pub fn has_explicit(&self) -> bool {
pub fn has_union(&self) -> bool {
match self {
Self::TypeDef(t) => t.has_explicit(),
Self::Array((kind, _)) => kind.has_explicit(),
Self::TypeDef(t) => t.has_union(),
Self::Array((kind, _)) => kind.has_union(),
_ => false,
}
}

pub fn has_pack(&self) -> bool {
match self {
Self::TypeDef(t) => t.has_pack(),
Self::Array((kind, _)) => kind.has_pack(),
_ => false,
}
}
Expand Down
16 changes: 4 additions & 12 deletions crates/libs/metadata/src/signature.rs
Expand Up @@ -18,20 +18,12 @@ impl Signature {
self.pointers == 0 && self.kind.is_udt()
}

pub fn has_explicit(&self) -> bool {
self.pointers == 0 && self.kind.has_explicit()
pub fn has_union(&self) -> bool {
self.pointers == 0 && self.kind.has_union()
}

pub fn is_packed(&self) -> bool {
if self.pointers > 0 {
return false;
}

match &self.kind {
ElementType::TypeDef(def) => def.is_packed(),
ElementType::Array((signature, _)) => signature.is_packed(),
_ => false,
}
pub fn has_pack(&self) -> bool {
self.pointers == 0 && self.kind.has_pack()
}

pub fn is_callback(&self) -> bool {
Expand Down
74 changes: 55 additions & 19 deletions crates/libs/metadata/src/tables/type_def.rs
Expand Up @@ -141,18 +141,6 @@ impl TypeDef {
result
}

pub fn is_packed(&self) -> bool {
if self.kind() != TypeKind::Struct {
return false;
}

if self.class_layout().is_some() {
return true;
}

self.fields().any(|field| field.signature(Some(self)).is_packed())
}

pub fn size(&self) -> usize {
if self.kind() == TypeKind::Struct {
self.fields().fold(0, |sum, field| sum + field.signature(Some(self)).size())
Expand Down Expand Up @@ -237,16 +225,64 @@ impl TypeDef {
self.row.u32(0) & 0b1_0000 != 0
}

pub fn has_explicit(&self) -> bool {
if self.kind() != TypeKind::Struct {
return false;
pub fn has_union(&self) -> bool {
fn has_union(def: &TypeDef) -> bool {
if def.kind() != TypeKind::Struct {
return false;
}

if def.is_union() {
true
} else {
def.fields().any(|f| f.signature(Some(def)).has_union())
}
}

if self.is_union() {
true
} else {
self.fields().any(|f| f.signature(Some(self)).has_explicit())
if has_union(self) {
return true;
}

if let Some(entry) = TypeReader::get().get_type_entry(self.type_name()) {
for def in entry {
if let ElementType::TypeDef(def) = def {
if has_union(def) {
return true;
}
}
}
}

false
}

pub fn has_pack(&self) -> bool {
fn has_pack(def: &TypeDef) -> bool {
if def.kind() != TypeKind::Struct {
return false;
}

if def.class_layout().is_some() {
true
} else {
def.fields().any(|f| f.signature(Some(def)).has_pack())
}
}

if has_pack(self) {
return true;
}

if let Some(entry) = TypeReader::get().get_type_entry(self.type_name()) {
for def in entry {
if let ElementType::TypeDef(def) = def {
if has_pack(def) {
return true;
}
}
}
}

false
}

pub fn type_signature(&self) -> String {
Expand Down