From d7f286219198f78dc75b211366c0d4fef2df6b69 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Tue, 10 Dec 2024 19:52:23 -0600 Subject: [PATCH] error handling --- crates/libs/bindgen/src/libraries.rs | 2 +- crates/libs/bindgen/src/tables/constant.rs | 2 +- crates/libs/bindgen/src/tables/method_def.rs | 8 ++--- crates/libs/bindgen/src/tables/type_def.rs | 2 +- crates/libs/bindgen/src/types/class.rs | 14 +++----- crates/libs/bindgen/src/types/cpp_enum.rs | 10 ++---- crates/libs/bindgen/src/types/cpp_fn.rs | 35 ++++++++------------ crates/libs/bindgen/src/types/cpp_struct.rs | 10 ++---- crates/libs/bindgen/src/winmd/blob.rs | 4 +-- crates/libs/bindgen/src/winmd/reader.rs | 1 + crates/libs/bindgen/src/writer/cpp_handle.rs | 24 ++++++-------- crates/tools/bindgen/src/main.rs | 2 +- 12 files changed, 44 insertions(+), 70 deletions(-) diff --git a/crates/libs/bindgen/src/libraries.rs b/crates/libs/bindgen/src/libraries.rs index b019b3c8db..eaece2a530 100644 --- a/crates/libs/bindgen/src/libraries.rs +++ b/crates/libs/bindgen/src/libraries.rs @@ -28,7 +28,7 @@ fn combine_libraries( // Windows libs are always produced with lower case module names. let library = ty.method.module_name().to_lowercase(); - let impl_map = ty.method.impl_map().expect("ImplMap not found"); + let impl_map = ty.method.impl_map().unwrap(); let flags = impl_map.flags(); let name = impl_map.import_name().to_string(); diff --git a/crates/libs/bindgen/src/tables/constant.rs b/crates/libs/bindgen/src/tables/constant.rs index dcea922db8..c7063554f8 100644 --- a/crates/libs/bindgen/src/tables/constant.rs +++ b/crates/libs/bindgen/src/tables/constant.rs @@ -8,7 +8,7 @@ impl std::fmt::Debug for Constant { impl Constant { pub fn ty(&self) -> Type { - Type::from_element_type(self.usize(0)).unwrap_or_else(|| panic!("`Constant` type invalid")) + Type::from_element_type(self.usize(0)).unwrap() } pub fn value(&self) -> Value { diff --git a/crates/libs/bindgen/src/tables/method_def.rs b/crates/libs/bindgen/src/tables/method_def.rs index 671471c6d9..1172f40c31 100644 --- a/crates/libs/bindgen/src/tables/method_def.rs +++ b/crates/libs/bindgen/src/tables/method_def.rs @@ -61,11 +61,9 @@ impl MethodDef { if !param_is_output { if let Some(attribute) = param.find_attribute("AssociatedEnumAttribute") { if let Some((_, Value::Str(name))) = attribute.args().first() { - if let Some(overload) = - param.reader().with_full_name(namespace, name).next() - { - ty = Type::PrimitiveOrEnum(Box::new(ty), Box::new(overload)); - } + let overload = param.reader().unwrap_full_name(namespace, name); + + ty = Type::PrimitiveOrEnum(Box::new(ty), Box::new(overload)); } } } diff --git a/crates/libs/bindgen/src/tables/type_def.rs b/crates/libs/bindgen/src/tables/type_def.rs index db4bb8ac52..f5bb1b59f3 100644 --- a/crates/libs/bindgen/src/tables/type_def.rs +++ b/crates/libs/bindgen/src/tables/type_def.rs @@ -57,7 +57,7 @@ impl TypeDef { } pub fn underlying_type(&self) -> Type { - let field = self.fields().next().expect("field not found"); + let field = self.fields().next().unwrap(); if let Some(constant) = field.constant() { constant.ty() } else { diff --git a/crates/libs/bindgen/src/types/class.rs b/crates/libs/bindgen/src/types/class.rs index 93036e1007..bca5680158 100644 --- a/crates/libs/bindgen/src/types/class.rs +++ b/crates/libs/bindgen/src/types/class.rs @@ -261,11 +261,9 @@ impl Class { break; } - let Some(Type::Class(base)) = reader - .with_full_name(extends.namespace(), extends.name()) - .next() + let Type::Class(base) = reader.unwrap_full_name(extends.namespace(), extends.name()) else { - panic!("Type not found"); + panic!("type not found: {extends}"); }; def = base.def; @@ -319,17 +317,15 @@ impl Class { for (_, arg) in attribute.args() { if let Value::TypeName(tn) = arg { - let Some(Type::Interface(mut interface)) = self + let Type::Interface(mut interface) = self .def .reader() - .with_full_name(tn.namespace(), tn.name()) - .next() + .unwrap_full_name(tn.namespace(), tn.name()) else { - panic!("Type not found"); + panic!("type not found: {tn}"); }; interface.kind = kind; - set.push(interface); break; } diff --git a/crates/libs/bindgen/src/types/cpp_enum.rs b/crates/libs/bindgen/src/types/cpp_enum.rs index 080056aa06..91e95e88b6 100644 --- a/crates/libs/bindgen/src/types/cpp_enum.rs +++ b/crates/libs/bindgen/src/types/cpp_enum.rs @@ -127,14 +127,10 @@ impl CppEnum { pub fn dependencies(&self, dependencies: &mut TypeMap) { if let Some(attribute) = self.def.find_attribute("AlsoUsableForAttribute") { if let Some((_, Value::Str(type_name))) = attribute.args().first() { - if let Some(ty) = self - .def + self.def .reader() - .with_full_name(self.def.namespace(), type_name) - .next() - { - ty.dependencies(dependencies); - } + .unwrap_full_name(self.def.namespace(), type_name) + .dependencies(dependencies); } } } diff --git a/crates/libs/bindgen/src/types/cpp_fn.rs b/crates/libs/bindgen/src/types/cpp_fn.rs index 6950e65e94..e8bdabe11d 100644 --- a/crates/libs/bindgen/src/types/cpp_fn.rs +++ b/crates/libs/bindgen/src/types/cpp_fn.rs @@ -30,7 +30,7 @@ impl CppFn { pub fn write_link(&self, writer: &Writer, underlying_types: bool) -> TokenStream { let name = self.method.name(); let library = self.method.module_name().to_lowercase(); - let impl_map = self.method.impl_map().expect("ImplMap not found"); + let impl_map = self.method.impl_map().unwrap(); let mut symbol = Some(impl_map.import_name()); if symbol == Some(name) { @@ -263,28 +263,19 @@ impl CppFn { .signature(self.namespace, &[]) .dependencies(dependencies); - match self.method.name() { - "GetWindowLongPtrA" => self - .method - .reader() - .unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "GetWindowLongA") - .dependencies(dependencies), - "GetWindowLongPtrW" => self - .method - .reader() - .unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "GetWindowLongW") - .dependencies(dependencies), - "SetWindowLongPtrA" => self - .method - .reader() - .unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "SetWindowLongA") - .dependencies(dependencies), - "SetWindowLongPtrW" => self - .method + let dependency = match self.method.name() { + "GetWindowLongPtrA" => Some("GetWindowLongA"), + "GetWindowLongPtrW" => Some("GetWindowLongW"), + "SetWindowLongPtrA" => Some("SetWindowLongA"), + "SetWindowLongPtrW" => Some("SetWindowLongW"), + _ => None, + }; + + if let Some(dependency) = dependency { + self.method .reader() - .unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "SetWindowLongW") - .dependencies(dependencies), - _ => {} + .unwrap_full_name(self.namespace, dependency) + .dependencies(dependencies); } } } diff --git a/crates/libs/bindgen/src/types/cpp_struct.rs b/crates/libs/bindgen/src/types/cpp_struct.rs index 291069b085..12bb5e1cf5 100644 --- a/crates/libs/bindgen/src/types/cpp_struct.rs +++ b/crates/libs/bindgen/src/types/cpp_struct.rs @@ -228,14 +228,10 @@ impl CppStruct { if let Some(attribute) = self.def.find_attribute("AlsoUsableForAttribute") { if let Some((_, Value::Str(type_name))) = attribute.args().first() { - if let Some(ty) = self - .def + self.def .reader() - .with_full_name(self.def.namespace(), type_name) - .next() - { - ty.dependencies(dependencies); - } + .unwrap_full_name(self.def.namespace(), type_name) + .dependencies(dependencies); } } } diff --git a/crates/libs/bindgen/src/winmd/blob.rs b/crates/libs/bindgen/src/winmd/blob.rs index a7aaa76f14..4ec52140bc 100644 --- a/crates/libs/bindgen/src/winmd/blob.rs +++ b/crates/libs/bindgen/src/winmd/blob.rs @@ -101,7 +101,7 @@ impl Blob { match self.read_u8() { 0 => false, 1 => true, - _ => panic!("illegal bool value"), + _ => panic!(), } } @@ -175,7 +175,7 @@ impl Blob { Type::U32 => Value::U32(self.read_u32()), Type::I64 => Value::I64(self.read_i64()), Type::U64 => Value::U64(self.read_u64()), - _ => panic!("type is not an integer"), + _ => panic!(), } } diff --git a/crates/libs/bindgen/src/winmd/reader.rs b/crates/libs/bindgen/src/winmd/reader.rs index 3e66532126..7a0facb9d1 100644 --- a/crates/libs/bindgen/src/winmd/reader.rs +++ b/crates/libs/bindgen/src/winmd/reader.rs @@ -170,6 +170,7 @@ impl Reader { reader } + #[track_caller] pub fn unwrap_full_name(&self, namespace: &str, name: &str) -> Type { if let Some(ty) = self.with_full_name(namespace, name).next() { ty diff --git a/crates/libs/bindgen/src/writer/cpp_handle.rs b/crates/libs/bindgen/src/writer/cpp_handle.rs index f7703f2b27..350d895265 100644 --- a/crates/libs/bindgen/src/writer/cpp_handle.rs +++ b/crates/libs/bindgen/src/writer/cpp_handle.rs @@ -111,22 +111,18 @@ impl Writer { if let Some(attribute) = def.find_attribute("AlsoUsableForAttribute") { if let Some((_, Value::Str(type_name))) = attribute.args().first() { - if let Some(ty) = def - .reader() - .with_full_name(def.namespace(), type_name) - .next() - { - let ty = ty.write_name(self); + let ty = def.reader().unwrap_full_name(def.namespace(), type_name); - result.combine(quote! { - impl windows_core::imp::CanInto<#ty> for #name {} - impl From<#name> for #ty { - fn from(value: #name) -> Self { - Self(value.0) - } + let ty = ty.write_name(self); + + result.combine(quote! { + impl windows_core::imp::CanInto<#ty> for #name {} + impl From<#name> for #ty { + fn from(value: #name) -> Self { + Self(value.0) } - }); - } + } + }); } } diff --git a/crates/tools/bindgen/src/main.rs b/crates/tools/bindgen/src/main.rs index 4e77797429..0fc7031f2e 100644 --- a/crates/tools/bindgen/src/main.rs +++ b/crates/tools/bindgen/src/main.rs @@ -206,6 +206,6 @@ fn write_lib() { cmd.arg("lib.rs"); if !cmd.status().unwrap().success() { - panic!("Failed to run rustfmt"); + panic!("failed to run rustfmt"); } }