Skip to content

Commit 731bf4e

Browse files
committed
transpile: force UnTypeOps (sizeof, alignof, etc.) to be CTypeKind::Size (size_t)
1 parent 9f4c341 commit 731bf4e

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,19 @@ impl TypedAstContext {
887887
CExprKind::Paren(_ty, e) => {
888888
self.ast_context.c_exprs[&e].kind.get_qual_type()
889889
}
890+
CExprKind::UnaryType(_, op, _, _) => {
891+
// All of these `UnTypeOp`s should return `size_t`.
892+
let kind = match op {
893+
UnTypeOp::SizeOf => CTypeKind::Size,
894+
UnTypeOp::AlignOf => CTypeKind::Size,
895+
UnTypeOp::PreferredAlignOf => CTypeKind::Size,
896+
};
897+
let ty = self
898+
.ast_context
899+
.type_for_kind(&kind)
900+
.expect("CTypeKind::Size should be size_t");
901+
Some(CQualTypeId::new(ty))
902+
}
890903
_ => return,
891904
};
892905
if let (Some(ty), Some(new_ty)) = (

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ pub const ZSTD_WINDOWLOG_MAX_32: std::ffi::c_int = unsafe { 30 as std::ffi::c_in
344344
pub const ZSTD_WINDOWLOG_MAX_64: std::ffi::c_int = unsafe { 31 as std::ffi::c_int };
345345
#[no_mangle]
346346
pub unsafe extern "C" fn test_zstd() -> U64 {
347-
return (if ::core::mem::size_of::<zstd_platform_dependent_type>() as std::ffi::c_ulong
348-
== 4 as std::ffi::c_ulong
349-
{
347+
return (if ::core::mem::size_of::<zstd_platform_dependent_type>() as usize == 4 as usize {
350348
ZSTD_WINDOWLOG_MAX_32
351349
} else {
352350
ZSTD_WINDOWLOG_MAX_64
@@ -413,7 +411,7 @@ pub unsafe extern "C" fn size_of_const() -> std::ffi::c_int {
413411
let mut a: [std::ffi::c_int; 10] = [0; 10];
414412
return SIZE as std::ffi::c_int;
415413
}
416-
pub const SIZE: std::ffi::c_ulong = unsafe { ::core::mem::size_of::<[std::ffi::c_int; 10]>() };
414+
pub const SIZE: usize = unsafe { ::core::mem::size_of::<[std::ffi::c_int; 10]>() };
417415
unsafe extern "C" fn run_static_initializers() {
418416
global_static_const_ptr_arithmetic = PTR_ARITHMETIC;
419417
global_static_const_indexing =

0 commit comments

Comments
 (0)