Skip to content

Commit 54e70b3

Browse files
committed
This fixes a crash when using non-deductible auto types.
1 parent 9fb016e commit 54e70b3

File tree

3 files changed

+66
-16
lines changed

3 files changed

+66
-16
lines changed

src/clang.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -931,18 +931,37 @@ impl Type {
931931
unsafe { clang_isConstQualifiedType(self.x) != 0 }
932932
}
933933

934+
#[inline]
935+
fn is_non_deductible_auto_type(&self) -> bool {
936+
self.kind() == CXType_Auto && self.canonical_type() == *self
937+
}
938+
939+
#[inline]
940+
fn clang_size_of(&self) -> c_longlong {
941+
if self.is_non_deductible_auto_type() {
942+
return -6; // Work-around https://bugs.llvm.org/show_bug.cgi?id=40813
943+
}
944+
unsafe { clang_Type_getSizeOf(self.x) }
945+
}
946+
947+
#[inline]
948+
fn clang_align_of(&self) -> c_longlong {
949+
if self.is_non_deductible_auto_type() {
950+
return -6; // Work-around https://bugs.llvm.org/show_bug.cgi?id=40813
951+
}
952+
unsafe { clang_Type_getAlignOf(self.x) }
953+
}
954+
934955
/// What is the size of this type? Paper over invalid types by returning `0`
935956
/// for them.
936957
pub fn size(&self) -> usize {
937-
unsafe {
938-
let val = clang_Type_getSizeOf(self.x);
939-
if val < 0 { 0 } else { val as usize }
940-
}
958+
let val = self.clang_size_of();
959+
if val < 0 { 0 } else { val as usize }
941960
}
942961

943962
/// What is the size of this type?
944963
pub fn fallible_size(&self) -> Result<usize, LayoutError> {
945-
let val = unsafe { clang_Type_getSizeOf(self.x) };
964+
let val = self.clang_size_of();
946965
if val < 0 {
947966
Err(LayoutError::from(val as i32))
948967
} else {
@@ -953,21 +972,17 @@ impl Type {
953972
/// What is the alignment of this type? Paper over invalid types by
954973
/// returning `0`.
955974
pub fn align(&self) -> usize {
956-
unsafe {
957-
let val = clang_Type_getAlignOf(self.x);
958-
if val < 0 { 0 } else { val as usize }
959-
}
975+
let val = self.clang_align_of();
976+
if val < 0 { 0 } else { val as usize }
960977
}
961978

962979
/// What is the alignment of this type?
963980
pub fn fallible_align(&self) -> Result<usize, LayoutError> {
964-
unsafe {
965-
let val = clang_Type_getAlignOf(self.x);
966-
if val < 0 {
967-
Err(LayoutError::from(val as i32))
968-
} else {
969-
Ok(val as usize)
970-
}
981+
let val = self.clang_align_of();
982+
if val < 0 {
983+
Err(LayoutError::from(val as i32))
984+
} else {
985+
Ok(val as usize)
971986
}
972987
}
973988

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#[repr(C)]
11+
#[derive(Debug, Default, Copy, Clone)]
12+
pub struct BrowsingContext {
13+
pub _address: u8,
14+
}
15+
#[test]
16+
fn bindgen_test_layout_BrowsingContext() {
17+
assert_eq!(
18+
::std::mem::size_of::<BrowsingContext>(),
19+
1usize,
20+
concat!("Size of: ", stringify!(BrowsingContext))
21+
);
22+
assert_eq!(
23+
::std::mem::align_of::<BrowsingContext>(),
24+
1usize,
25+
concat!("Alignment of ", stringify!(BrowsingContext))
26+
);
27+
}

tests/headers/bug-1529681.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// bindgen-flags: -- -std=c++14
2+
//
3+
// https://bugzilla.mozilla.org/show_bug.cgi?id=1529681
4+
// https://bugs.llvm.org/show_bug.cgi?id=40813
5+
6+
class BrowsingContext {
7+
auto Tie(void* aUnused) const;
8+
};

0 commit comments

Comments
 (0)