Skip to content

Commit 74dcb20

Browse files
author
bors-servo
authored
Auto merge of #1311 - cynecx:canonicalize_pointer_ty, r=emilio
Canonicalize a type (pointer type) first before checking for constness This fixes cases like: ```cpp int sodium_munlock(int* const addr); // addr itself is const but the pointer points to mutable data ``` Before fix: ```rust extern "C" { pub fn sodium_munlock( addr: *const ::std::os::raw::c_int, ) -> ::std::os::raw::c_int; } ``` After this patch: ```rust extern "C" { pub fn sodium_munlock( addr: *mut ::std::os::raw::c_int, ) -> ::std::os::raw::c_int; } ```
2 parents 515ca97 + 1fc8172 commit 74dcb20

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

src/ir/ty.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,23 @@ impl Type {
11941194
};
11951195

11961196
let name = if name.is_empty() { None } else { Some(name) };
1197-
let is_const = ty.is_const();
1197+
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+
};
11981214

11991215
let ty = Type::new(name, layout, kind, is_const);
12001216
// TODO: maybe declaration.canonical()?

tests/expectations/tests/const_tparam.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
#[repr(C)]
86
#[derive(Debug, Copy, Clone)]
97
pub struct C<T> {
108
pub foo: *const T,
11-
pub bar: *mut T,
9+
pub bar: *const T,
1210
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
1311
}
1412
impl<T> Default for C<T> {

tests/expectations/tests/derive-hash-struct-with-pointer.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
7-
85
/// Pointers can derive Hash/PartialOrd/Ord/PartialEq/Eq
96
#[repr(C)]
107
#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
118
pub struct ConstPtrMutObj {
12-
pub bar: *const ::std::os::raw::c_int,
9+
pub bar: *mut ::std::os::raw::c_int,
1310
}
1411
#[test]
1512
fn bindgen_test_layout_ConstPtrMutObj() {

tests/expectations/tests/issue-511.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
4+
5+
extern "C" {
6+
#[link_name = "\u{1}a"]
7+
pub static mut a: *mut ::std::os::raw::c_char;
8+
}
9+
extern "C" {
10+
#[link_name = "\u{1}b"]
11+
pub static mut b: *const ::std::os::raw::c_char;
12+
}
13+
extern "C" {
14+
#[link_name = "\u{1}c"]
15+
pub static mut c: *mut ::std::os::raw::c_char;
16+
}
17+
extern "C" {
18+
#[link_name = "\u{1}d"]
19+
pub static mut d: *const ::std::os::raw::c_char;
20+
}

tests/expectations/tests/layout_array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub type rte_mempool_free_t = ::std::option::Option<unsafe extern "C" fn(mp: *mu
2727
pub type rte_mempool_enqueue_t = ::std::option::Option<
2828
unsafe extern "C" fn(
2929
mp: *mut rte_mempool,
30-
obj_table: *const *const ::std::os::raw::c_void,
30+
obj_table: *const *mut ::std::os::raw::c_void,
3131
n: ::std::os::raw::c_uint,
3232
) -> ::std::os::raw::c_int,
3333
>;

tests/headers/issue-511.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
char * a;
2+
const char * b;
3+
char * const c;
4+
const char * const d;

0 commit comments

Comments
 (0)