Skip to content

Commit 0068c1a

Browse files
committed
Handle unsigned integer constants greater than u32::MAX in codegen
We were not checking signed-ness and emitting the appropriate types. Fixes #1040
1 parent 52fe951 commit 0068c1a

File tree

6 files changed

+40
-1
lines changed

6 files changed

+40
-1
lines changed

src/codegen/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,19 @@ impl CodeGenerator for Var {
478478
});
479479
}
480480
VarType::Int(val) => {
481-
let val = helpers::ast_ty::int_expr(val);
481+
let int_kind = self.ty()
482+
.into_resolver()
483+
.through_type_aliases()
484+
.through_type_refs()
485+
.resolve(ctx)
486+
.expect_type()
487+
.as_integer()
488+
.unwrap();
489+
let val = if int_kind.is_signed() {
490+
helpers::ast_ty::int_expr(val)
491+
} else {
492+
helpers::ast_ty::uint_expr(val as _)
493+
};
482494
result.push(quote! {
483495
pub const #canonical_ident : #ty = #val ;
484496
});

src/ir/ty.rs

+9
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ impl Type {
194194
}
195195
}
196196

197+
/// Cast this type to an integer kind, or `None` if it is not an integer
198+
/// type.
199+
pub fn as_integer(&self) -> Option<IntKind> {
200+
match self.kind {
201+
TypeKind::Int(int_kind) => Some(int_kind),
202+
_ => None,
203+
}
204+
}
205+
197206
/// Is this a `const` qualified type?
198207
pub fn is_const(&self) -> bool {
199208
self.is_const
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
pub const a: u32 = 18446744073709551611;
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
pub const g_107: ::std::os::raw::c_ulonglong = 18446744073709551615;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
typedef unsigned int uint32_t;
2+
3+
uint32_t a = 18446744073709551611;

tests/headers/issue-1040.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unsigned long long g_107 = 18446744073709551615UL;

0 commit comments

Comments
 (0)