Skip to content

Commit e09602b

Browse files
committed
var: Constant arrays of const elements should not generate static muts.
Fixes #1727
1 parent 56b18d6 commit e09602b

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

src/ir/var.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,12 @@ impl ClangSubItemParser for Var {
232232

233233
let ty = cursor.cur_type();
234234

235-
// XXX this is redundant, remove!
236-
let is_const = ty.is_const();
235+
// TODO(emilio): do we have to special-case constant arrays in
236+
// some other places?
237+
let is_const = ty.is_const() ||
238+
(ty.kind() == CXType_ConstantArray &&
239+
ty.elem_type()
240+
.map_or(false, |element| element.is_const()));
237241

238242
let ty = match Item::from_ty(&ty, cursor, None, ctx) {
239243
Ok(ty) => ty,
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
extern "C" {
11+
pub static foo: [::std::os::raw::c_int; 1usize];
12+
}
13+
extern "C" {
14+
pub static mut bar: [::std::os::raw::c_int; 1usize];
15+
}

tests/headers/const_array.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
extern const int foo[1];
2+
extern int bar[1];

tests/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,10 @@ fn test_clang_env_args() {
353353
"/* automatically generated by rust-bindgen */
354354
355355
extern \"C\" {
356-
pub static mut x: [::std::os::raw::c_int; 1usize];
356+
pub static x: [::std::os::raw::c_int; 1usize];
357357
}
358358
extern \"C\" {
359-
pub static mut y: [::std::os::raw::c_int; 1usize];
359+
pub static y: [::std::os::raw::c_int; 1usize];
360360
}
361361
"
362362
.to_string(),

0 commit comments

Comments
 (0)