From 35d17c006de5ecce7c39dab960f3db840aceeea6 Mon Sep 17 00:00:00 2001 From: cynecx Date: Thu, 10 May 2018 22:39:46 +0200 Subject: [PATCH 1/4] Canonicalize a type (pointer type) first before checking for constness --- src/ir/ty.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ir/ty.rs b/src/ir/ty.rs index b5c78c16b5..b742dcc1cc 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -1194,7 +1194,23 @@ impl Type { }; let name = if name.is_empty() { None } else { Some(name) }; - let is_const = ty.is_const(); + + // Just using ty.is_const() is wrong here, because when we declare an + // argument like 'int* const arg0', arg0 is considered + // const but the pointer itself points to mutable data. + // + // Without canonicalizing the type to the pointer type, we'll get the + // following mapping: + // + // arg0: *const c_int + // + // So by canonicalizing the type first, we can check constness by + // calling is_const() on the pointer type. + let is_const = if let Some(pty) = ty.pointee_type() { + pty.is_const() + } else { + ty.is_const() + }; let ty = Type::new(name, layout, kind, is_const); // TODO: maybe declaration.canonical()? From bd3fda1645fba5f15a3ed633d0bcdb3d71fbbede Mon Sep 17 00:00:00 2001 From: cynecx Date: Thu, 10 May 2018 22:55:17 +0200 Subject: [PATCH 2/4] Adjust proper constness in tests --- tests/expectations/tests/const_tparam.rs | 4 +--- tests/expectations/tests/derive-hash-struct-with-pointer.rs | 5 +---- tests/expectations/tests/layout_array.rs | 2 +- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/expectations/tests/const_tparam.rs b/tests/expectations/tests/const_tparam.rs index 218abbdbce..c7863931c0 100644 --- a/tests/expectations/tests/const_tparam.rs +++ b/tests/expectations/tests/const_tparam.rs @@ -1,14 +1,12 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct C { pub foo: *const T, - pub bar: *mut T, + pub bar: *const T, pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell>, } impl Default for C { diff --git a/tests/expectations/tests/derive-hash-struct-with-pointer.rs b/tests/expectations/tests/derive-hash-struct-with-pointer.rs index ee41c4f4f4..c69bcd7c4e 100644 --- a/tests/expectations/tests/derive-hash-struct-with-pointer.rs +++ b/tests/expectations/tests/derive-hash-struct-with-pointer.rs @@ -1,15 +1,12 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - - /// Pointers can derive Hash/PartialOrd/Ord/PartialEq/Eq #[repr(C)] #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] pub struct ConstPtrMutObj { - pub bar: *const ::std::os::raw::c_int, + pub bar: *mut ::std::os::raw::c_int, } #[test] fn bindgen_test_layout_ConstPtrMutObj() { diff --git a/tests/expectations/tests/layout_array.rs b/tests/expectations/tests/layout_array.rs index 7ca267dcbd..15fbfa42e4 100644 --- a/tests/expectations/tests/layout_array.rs +++ b/tests/expectations/tests/layout_array.rs @@ -27,7 +27,7 @@ pub type rte_mempool_free_t = ::std::option::Option ::std::os::raw::c_int, >; From e4c9619ade4f884e00b7e71a2bf8d6572d4e2b15 Mon Sep 17 00:00:00 2001 From: cynecx Date: Sat, 12 May 2018 16:47:29 +0200 Subject: [PATCH 3/4] Add test from issue #511 --- tests/expectations/tests/issue-511.rs | 18 ++++++++++++++++++ tests/headers/issue-511.h | 4 ++++ 2 files changed, 22 insertions(+) create mode 100644 tests/expectations/tests/issue-511.rs create mode 100644 tests/headers/issue-511.h diff --git a/tests/expectations/tests/issue-511.rs b/tests/expectations/tests/issue-511.rs new file mode 100644 index 0000000000..4fb43979f8 --- /dev/null +++ b/tests/expectations/tests/issue-511.rs @@ -0,0 +1,18 @@ +/* automatically generated by rust-bindgen */ + +extern "C" { + #[link_name = "\u{1}a"] + pub static mut a: *mut ::std::os::raw::c_char; +} +extern "C" { + #[link_name = "\u{1}b"] + pub static mut b: *const ::std::os::raw::c_char; +} +extern "C" { + #[link_name = "\u{1}c"] + pub static mut c: *mut ::std::os::raw::c_char; +} +extern "C" { + #[link_name = "\u{1}d"] + pub static mut d: *const ::std::os::raw::c_char; +} diff --git a/tests/headers/issue-511.h b/tests/headers/issue-511.h new file mode 100644 index 0000000000..da3643121e --- /dev/null +++ b/tests/headers/issue-511.h @@ -0,0 +1,4 @@ +char * a; +const char * b; +char * const c; +const char * const d; \ No newline at end of file From 1fc8172889e981d45e819c9f394fe4b6b41dbc38 Mon Sep 17 00:00:00 2001 From: cynecx Date: Sat, 12 May 2018 17:14:58 +0200 Subject: [PATCH 4/4] Add missing line in test expectation --- tests/expectations/tests/issue-511.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/expectations/tests/issue-511.rs b/tests/expectations/tests/issue-511.rs index 4fb43979f8..ff725b3393 100644 --- a/tests/expectations/tests/issue-511.rs +++ b/tests/expectations/tests/issue-511.rs @@ -1,5 +1,7 @@ /* automatically generated by rust-bindgen */ +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] + extern "C" { #[link_name = "\u{1}a"] pub static mut a: *mut ::std::os::raw::c_char;