From c265a7562a5ee815057efcc8b1aca31cd3d5318d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 21 Mar 2020 15:06:26 +0100 Subject: [PATCH] parser: Don't panic when finding associated constants to a primitive. We don't handle it, but no reason to panic. Fixes #493 --- src/bindgen/parser.rs | 19 +++++++++++++++---- .../expectations/associated_constant_panic.c | 4 ++++ .../associated_constant_panic.compat.c | 4 ++++ .../associated_constant_panic.cpp | 4 ++++ .../both/associated_constant_panic.c | 4 ++++ .../both/associated_constant_panic.compat.c | 4 ++++ .../tag/associated_constant_panic.c | 4 ++++ .../tag/associated_constant_panic.compat.c | 4 ++++ tests/rust/associated_constant_panic.rs | 7 +++++++ 9 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 tests/expectations/associated_constant_panic.c create mode 100644 tests/expectations/associated_constant_panic.compat.c create mode 100644 tests/expectations/associated_constant_panic.cpp create mode 100644 tests/expectations/both/associated_constant_panic.c create mode 100644 tests/expectations/both/associated_constant_panic.compat.c create mode 100644 tests/expectations/tag/associated_constant_panic.c create mode 100644 tests/expectations/tag/associated_constant_panic.compat.c create mode 100644 tests/rust/associated_constant_panic.rs diff --git a/src/bindgen/parser.rs b/src/bindgen/parser.rs index 6c95fa6..ebed817 100644 --- a/src/bindgen/parser.rs +++ b/src/bindgen/parser.rs @@ -707,11 +707,22 @@ impl Parse { return; } }; - if ty.is_none() { - return; - } - let impl_path = ty.unwrap().get_root_path().unwrap(); + let ty = match ty { + Some(ty) => ty, + None => return, + }; + + let impl_path = match ty.get_root_path() { + Some(p) => p, + None => { + warn!( + "Couldn't find path for {:?}, skipping associated constants", + ty + ); + return; + } + }; for item in items.into_iter() { if let syn::Visibility::Public(_) = item.vis { diff --git a/tests/expectations/associated_constant_panic.c b/tests/expectations/associated_constant_panic.c new file mode 100644 index 0000000..20c8381 --- /dev/null +++ b/tests/expectations/associated_constant_panic.c @@ -0,0 +1,4 @@ +#include +#include +#include +#include diff --git a/tests/expectations/associated_constant_panic.compat.c b/tests/expectations/associated_constant_panic.compat.c new file mode 100644 index 0000000..20c8381 --- /dev/null +++ b/tests/expectations/associated_constant_panic.compat.c @@ -0,0 +1,4 @@ +#include +#include +#include +#include diff --git a/tests/expectations/associated_constant_panic.cpp b/tests/expectations/associated_constant_panic.cpp new file mode 100644 index 0000000..9849fa3 --- /dev/null +++ b/tests/expectations/associated_constant_panic.cpp @@ -0,0 +1,4 @@ +#include +#include +#include +#include diff --git a/tests/expectations/both/associated_constant_panic.c b/tests/expectations/both/associated_constant_panic.c new file mode 100644 index 0000000..20c8381 --- /dev/null +++ b/tests/expectations/both/associated_constant_panic.c @@ -0,0 +1,4 @@ +#include +#include +#include +#include diff --git a/tests/expectations/both/associated_constant_panic.compat.c b/tests/expectations/both/associated_constant_panic.compat.c new file mode 100644 index 0000000..20c8381 --- /dev/null +++ b/tests/expectations/both/associated_constant_panic.compat.c @@ -0,0 +1,4 @@ +#include +#include +#include +#include diff --git a/tests/expectations/tag/associated_constant_panic.c b/tests/expectations/tag/associated_constant_panic.c new file mode 100644 index 0000000..20c8381 --- /dev/null +++ b/tests/expectations/tag/associated_constant_panic.c @@ -0,0 +1,4 @@ +#include +#include +#include +#include diff --git a/tests/expectations/tag/associated_constant_panic.compat.c b/tests/expectations/tag/associated_constant_panic.compat.c new file mode 100644 index 0000000..20c8381 --- /dev/null +++ b/tests/expectations/tag/associated_constant_panic.compat.c @@ -0,0 +1,4 @@ +#include +#include +#include +#include diff --git a/tests/rust/associated_constant_panic.rs b/tests/rust/associated_constant_panic.rs new file mode 100644 index 0000000..ff0c8ec --- /dev/null +++ b/tests/rust/associated_constant_panic.rs @@ -0,0 +1,7 @@ +pub trait F { + const B: u8; +} + +impl F for u16 { + const B: u8 = 3; +}