Skip to content

Commit 716d53b

Browse files
committed
Handle pointer constness at the right level.
1 parent 35fb113 commit 716d53b

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

src/codegen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3054,7 +3054,7 @@ impl TryToRustTy for Type {
30543054
}
30553055
TypeKind::Pointer(inner) |
30563056
TypeKind::Reference(inner) => {
3057-
let is_const = self.is_const() || ctx.resolve_type(inner).is_const();
3057+
let is_const = ctx.resolve_type(inner).is_const();
30583058

30593059
let inner = inner.into_resolver().through_type_refs().resolve(ctx);
30603060
let inner_ty = inner.expect_type();

src/ir/ty.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,22 +1195,7 @@ impl Type {
11951195

11961196
let name = if name.is_empty() { None } else { Some(name) };
11971197

1198-
// Just using ty.is_const() is wrong here, because when we declare an
1199-
// argument like 'int* const arg0', arg0 is considered
1200-
// const but the pointer itself points to mutable data.
1201-
//
1202-
// Without canonicalizing the type to the pointer type, we'll get the
1203-
// following mapping:
1204-
//
1205-
// arg0: *const c_int
1206-
//
1207-
// So by canonicalizing the type first, we can check constness by
1208-
// calling is_const() on the pointer type.
1209-
let is_const = if let Some(pty) = ty.pointee_type() {
1210-
pty.is_const()
1211-
} else {
1212-
ty.is_const()
1213-
};
1198+
let is_const = ty.is_const();
12141199

12151200
let ty = Type::new(name, layout, kind, is_const);
12161201
// TODO: maybe declaration.canonical()?
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
4+
5+
#[repr(C)]
6+
#[derive(Debug, Copy, Clone)]
7+
pub struct foo {
8+
pub bar: *const *const *mut *const ::std::os::raw::c_int,
9+
}
10+
#[test]
11+
fn bindgen_test_layout_foo() {
12+
assert_eq!(
13+
::std::mem::size_of::<foo>(),
14+
8usize,
15+
concat!("Size of: ", stringify!(foo))
16+
);
17+
assert_eq!(
18+
::std::mem::align_of::<foo>(),
19+
8usize,
20+
concat!("Alignment of ", stringify!(foo))
21+
);
22+
assert_eq!(
23+
unsafe { &(*(::std::ptr::null::<foo>())).bar as *const _ as usize },
24+
0usize,
25+
concat!("Offset of field: ", stringify!(foo), "::", stringify!(bar))
26+
);
27+
}
28+
impl Default for foo {
29+
fn default() -> Self {
30+
unsafe { ::std::mem::zeroed() }
31+
}
32+
}

tests/headers/const-const-mut-ptr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct foo {
2+
const int** const* const* bar;
3+
};

0 commit comments

Comments
 (0)