From 5b4448a3608619ca7d1f00adae3a4e5f7e805ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 20 Oct 2018 07:07:25 +0200 Subject: [PATCH 1/2] codegen: Teach bindgen to respect constness of statics. --- src/codegen/mod.rs | 8 +++- .../expectations/tests/extern-const-struct.rs | 46 +++++++++++++++++++ tests/expectations/tests/issue-511.rs | 11 +++-- tests/expectations/tests/libclang-3.8/auto.rs | 2 +- .../tests/libclang-3.8/const_bool.rs | 6 +-- .../tests/libclang-3.8/constant-evaluate.rs | 12 ++--- .../tests/libclang-3.9/constant-evaluate.rs | 2 +- tests/headers/extern-const-struct.h | 5 ++ 8 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 tests/expectations/tests/extern-const-struct.rs create mode 100644 tests/headers/extern-const-struct.h diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index f29a3bc060..8cc55a0e1b 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -575,10 +575,16 @@ impl CodeGenerator for Var { attrs.push(attributes::link_name(self.name())); } + let maybe_mut = if self.is_const() { + quote! { } + } else { + quote! { mut } + }; + let mut tokens = quote!( extern "C" { #(#attrs)* - pub static mut #canonical_ident: #ty; + pub static #maybe_mut #canonical_ident: #ty; } ); diff --git a/tests/expectations/tests/extern-const-struct.rs b/tests/expectations/tests/extern-const-struct.rs new file mode 100644 index 0000000000..2f9ade8b03 --- /dev/null +++ b/tests/expectations/tests/extern-const-struct.rs @@ -0,0 +1,46 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Copy, Clone)] +pub struct nsFoo { + pub details: [f32; 400usize], +} +#[test] +fn bindgen_test_layout_nsFoo() { + assert_eq!( + ::std::mem::size_of::(), + 1600usize, + concat!("Size of: ", stringify!(nsFoo)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(nsFoo)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).details as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(nsFoo), + "::", + stringify!(details) + ) + ); +} +impl Default for nsFoo { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} +extern "C" { + #[link_name = "\u{1}gDetails"] + pub static gDetails: nsFoo; +} diff --git a/tests/expectations/tests/issue-511.rs b/tests/expectations/tests/issue-511.rs index ff725b3393..453fed0917 100644 --- a/tests/expectations/tests/issue-511.rs +++ b/tests/expectations/tests/issue-511.rs @@ -1,6 +1,11 @@ /* automatically generated by rust-bindgen */ -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] extern "C" { #[link_name = "\u{1}a"] @@ -12,9 +17,9 @@ extern "C" { } extern "C" { #[link_name = "\u{1}c"] - pub static mut c: *mut ::std::os::raw::c_char; + pub static c: *mut ::std::os::raw::c_char; } extern "C" { #[link_name = "\u{1}d"] - pub static mut d: *const ::std::os::raw::c_char; + pub static d: *const ::std::os::raw::c_char; } diff --git a/tests/expectations/tests/libclang-3.8/auto.rs b/tests/expectations/tests/libclang-3.8/auto.rs index 8db72d0fa8..920023f349 100644 --- a/tests/expectations/tests/libclang-3.8/auto.rs +++ b/tests/expectations/tests/libclang-3.8/auto.rs @@ -11,7 +11,7 @@ pub struct Foo { } extern "C" { #[link_name = "\u{1}_ZN3Foo4kFooE"] - pub static mut Foo_kFoo: bool; + pub static Foo_kFoo: bool; } #[test] fn bindgen_test_layout_Foo() { diff --git a/tests/expectations/tests/libclang-3.8/const_bool.rs b/tests/expectations/tests/libclang-3.8/const_bool.rs index 356368426b..af5adcaf8e 100644 --- a/tests/expectations/tests/libclang-3.8/const_bool.rs +++ b/tests/expectations/tests/libclang-3.8/const_bool.rs @@ -6,7 +6,7 @@ extern "C" { #[link_name = "\u{1}_ZL1k"] - pub static mut k: bool; + pub static k: bool; } #[repr(C)] #[derive(Debug, Default, Copy, Clone)] @@ -15,7 +15,7 @@ pub struct A { } extern "C" { #[link_name = "\u{1}_ZN1A1kE"] - pub static mut A_k: bool; + pub static A_k: bool; } #[test] fn bindgen_test_layout_A() { @@ -33,5 +33,5 @@ fn bindgen_test_layout_A() { pub type foo = bool; extern "C" { #[link_name = "\u{1}_ZL2k2"] - pub static mut k2: foo; + pub static k2: foo; } diff --git a/tests/expectations/tests/libclang-3.8/constant-evaluate.rs b/tests/expectations/tests/libclang-3.8/constant-evaluate.rs index 7df4e91877..fa54df27a6 100644 --- a/tests/expectations/tests/libclang-3.8/constant-evaluate.rs +++ b/tests/expectations/tests/libclang-3.8/constant-evaluate.rs @@ -13,27 +13,27 @@ pub type EasyToOverflow = ::std::os::raw::c_ulonglong; pub const k: EasyToOverflow = 2147483648; extern "C" { #[link_name = "\u{1}k_expr"] - pub static mut k_expr: EasyToOverflow; + pub static k_expr: EasyToOverflow; } extern "C" { #[link_name = "\u{1}wow"] - pub static mut wow: EasyToOverflow; + pub static wow: EasyToOverflow; } extern "C" { #[link_name = "\u{1}BAZ"] - pub static mut BAZ: ::std::os::raw::c_longlong; + pub static BAZ: ::std::os::raw::c_longlong; } extern "C" { #[link_name = "\u{1}fuzz"] - pub static mut fuzz: f64; + pub static fuzz: f64; } extern "C" { #[link_name = "\u{1}BAZZ"] - pub static mut BAZZ: ::std::os::raw::c_char; + pub static BAZZ: ::std::os::raw::c_char; } extern "C" { #[link_name = "\u{1}WAT"] - pub static mut WAT: ::std::os::raw::c_char; + pub static WAT: ::std::os::raw::c_char; } extern "C" { #[link_name = "\u{1}bytestring"] diff --git a/tests/expectations/tests/libclang-3.9/constant-evaluate.rs b/tests/expectations/tests/libclang-3.9/constant-evaluate.rs index 69501c6425..6a3c8a362b 100644 --- a/tests/expectations/tests/libclang-3.9/constant-evaluate.rs +++ b/tests/expectations/tests/libclang-3.9/constant-evaluate.rs @@ -17,7 +17,7 @@ pub const k: EasyToOverflow = 2147483648; pub const k_expr: EasyToOverflow = 0; extern "C" { #[link_name = "\u{1}wow"] - pub static mut wow: EasyToOverflow; + pub static wow: EasyToOverflow; } pub const BAZ: ::std::os::raw::c_longlong = 24; pub const fuzz: f64 = 51.0; diff --git a/tests/headers/extern-const-struct.h b/tests/headers/extern-const-struct.h new file mode 100644 index 0000000000..10006e8284 --- /dev/null +++ b/tests/headers/extern-const-struct.h @@ -0,0 +1,5 @@ +struct nsFoo { + float details[400]; +}; + +extern const struct nsFoo gDetails; From 710cbc0b2d3839659c302b521c93dfd77f42b02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 20 Oct 2018 07:08:50 +0200 Subject: [PATCH 2/2] Version bump, plus actually bump the required cexpr version. --- Cargo.lock | 2 +- Cargo.toml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8f1a2b519e..f2dd804328 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,7 +26,7 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.42.3" +version = "0.43.0" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "cexpr 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 0b5ad56f20..4901d780d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ readme = "README.md" repository = "https://github.com/rust-lang-nursery/rust-bindgen" documentation = "https://docs.rs/bindgen" homepage = "https://rust-lang-nursery.github.io/rust-bindgen/" -version = "0.42.3" +version = "0.43.0" build = "build.rs" include = [ @@ -44,7 +44,7 @@ shlex = "0.1" [dependencies] bitflags = "1.0.3" -cexpr = "0.3" +cexpr = "0.3.3" cfg-if = "0.1.0" # This kinda sucks: https://github.com/rust-lang/cargo/issues/1982 clap = "2"