Skip to content

Commit 8979d07

Browse files
committed
transpile: const macros: skip fn ptr types, as they don't work with portable types
The workaround for non-portable types for const macros is to insert a use-site cast to the portable type, but this works differently for fn ptrs, so while #1321 is fixed, just skip fn ptr const macros. This should get #1384 to pass CI by working around this issue there, and it should also workaround #1366, although #1366 in `libxml2` appears to have been fixed/worked around from some other change, as #1385 now passes CI.
1 parent 1c197eb commit 8979d07

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

c2rust-transpile/src/translator/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,14 @@ impl<'c> Translation<'c> {
21732173
// Join ty and cur_ty to the smaller of the two types. If the
21742174
// types are not cast-compatible, abort the fold.
21752175
let ty_kind = self.ast_context.resolve_type(ty).kind.clone();
2176+
if matches!(ty_kind, CTypeKind::Function(..)) {
2177+
// TODO This is a temporary workaround for #1321 (portable types in const macros).
2178+
// The workaround for most types is to create a const macro with non-portable type
2179+
// and insert casts at use sites, but this doesn't work the same for fn ptr types.
2180+
// So don't translate fn ptr const macros yet until #1321 is fixed.
2181+
return Err(format_err!("fn ptr const macros not yet supported due to non-portable types; see #1321"));
2182+
}
2183+
21762184
if let Some((canon_val, canon_ty)) = canonical {
21772185
let canon_ty_kind = self.ast_context.resolve_type(canon_ty).kind.clone();
21782186
if let Some(smaller_ty) =

c2rust-transpile/tests/snapshots/os-specific/macros.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int size_of_dynamic(int n) {
1919
}
2020
#endif
2121

22-
// From Lua's `lobject.c`.
22+
// From `lua`'s `lobject.c`.
2323

2424
#define POS "\"]"
2525
/* number of chars of a literal string without the ending \0 */
@@ -28,3 +28,12 @@ int size_of_dynamic(int n) {
2828
void memcpy_str_literal(char *out) {
2929
memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
3030
}
31+
32+
// From `python2`'s `bytes_methods.c`.
33+
34+
#define Py_MEMCPY memcpy
35+
36+
void f(void *x) {
37+
size_t len = 0;
38+
Py_MEMCPY(x, x, len);
39+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut core::ffi::c_char) {
4444
.wrapping_mul(::core::mem::size_of::<core::ffi::c_char>() as size_t),
4545
);
4646
}
47+
#[no_mangle]
48+
pub unsafe extern "C" fn f(mut x: *mut core::ffi::c_void) {
49+
let mut len: size_t = 0 as size_t;
50+
memcpy(x, x, len);
51+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut core::ffi::c_char) {
4545
.wrapping_mul(::core::mem::size_of::<core::ffi::c_char>() as size_t),
4646
);
4747
}
48+
#[no_mangle]
49+
pub unsafe extern "C" fn f(mut x: *mut core::ffi::c_void) {
50+
let mut len: size_t = 0 as size_t;
51+
memcpy(x, x, len);
52+
}

0 commit comments

Comments
 (0)