Skip to content

Commit 8fb4cf5

Browse files
authored
transpile: Allow ternary operator in const context (#1796)
This was allowed all the way back in Rust 1.46.
2 parents 832d4bd + 21722d2 commit 8fb4cf5

3 files changed

Lines changed: 12 additions & 14 deletions

File tree

c2rust-transpile/src/translator/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3310,12 +3310,6 @@ impl<'c> Translation<'c> {
33103310
}
33113311

33123312
Conditional(ty, cond, lhs, rhs) => {
3313-
if ctx.is_const {
3314-
return Err(format_translation_err!(
3315-
self.ast_context.display_loc(src_loc),
3316-
"Constants cannot contain ternary expressions in Rust",
3317-
));
3318-
}
33193313
let cond = self.convert_condition(ctx, true, cond)?;
33203314

33213315
let lhs = self.convert_expr(ctx, lhs, Some(override_ty.unwrap_or(ty)))?;

c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.snap

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,15 @@ pub static mut fns: fn_ptrs = fn_ptrs {
373373
pub static mut p: *const fn_ptrs = unsafe { &raw const fns };
374374
pub const ZSTD_WINDOWLOG_MAX_32: ::core::ffi::c_int = 30 as ::core::ffi::c_int;
375375
pub const ZSTD_WINDOWLOG_MAX_64: ::core::ffi::c_int = 31 as ::core::ffi::c_int;
376-
#[no_mangle]
377-
pub unsafe extern "C" fn test_zstd() -> U64 {
378-
return (if ::core::mem::size_of::<zstd_platform_dependent_type>() as usize == 4 as usize {
376+
pub const ZSTD_WINDOWLOG_MAX: ::core::ffi::c_int =
377+
if ::core::mem::size_of::<zstd_platform_dependent_type>() as usize == 4 as usize {
379378
ZSTD_WINDOWLOG_MAX_32
380379
} else {
381380
ZSTD_WINDOWLOG_MAX_64
382-
}) as U64;
381+
};
382+
#[no_mangle]
383+
pub unsafe extern "C" fn test_zstd() -> U64 {
384+
return ZSTD_WINDOWLOG_MAX as U64;
383385
}
384386
#[no_mangle]
385387
pub unsafe extern "C" fn stmt_expr_inc() -> ::core::ffi::c_int {

c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.snap

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,15 @@ pub static mut fns: fn_ptrs = fn_ptrs {
373373
pub static mut p: *const fn_ptrs = &raw const fns;
374374
pub const ZSTD_WINDOWLOG_MAX_32: ::core::ffi::c_int = 30 as ::core::ffi::c_int;
375375
pub const ZSTD_WINDOWLOG_MAX_64: ::core::ffi::c_int = 31 as ::core::ffi::c_int;
376-
#[unsafe(no_mangle)]
377-
pub unsafe extern "C" fn test_zstd() -> U64 {
378-
return (if ::core::mem::size_of::<zstd_platform_dependent_type>() as usize == 4 as usize {
376+
pub const ZSTD_WINDOWLOG_MAX: ::core::ffi::c_int =
377+
if ::core::mem::size_of::<zstd_platform_dependent_type>() as usize == 4 as usize {
379378
ZSTD_WINDOWLOG_MAX_32
380379
} else {
381380
ZSTD_WINDOWLOG_MAX_64
382-
}) as U64;
381+
};
382+
#[unsafe(no_mangle)]
383+
pub unsafe extern "C" fn test_zstd() -> U64 {
384+
return ZSTD_WINDOWLOG_MAX as U64;
383385
}
384386
#[unsafe(no_mangle)]
385387
pub unsafe extern "C" fn stmt_expr_inc() -> ::core::ffi::c_int {

0 commit comments

Comments
 (0)