Skip to content

Commit cdc637c

Browse files
authored
transpile: replace pref_align_of with align_of in edition 2024 (#1656)
Since `core::intrinsics::pref_align_of` was removed, we can't fully support `__alignof`/`__alignof__` anymore, but using normal `core::mem::align_of` should still be a valid value. This also updates `tests/unit/misc` to edition 2024, as its usage of `__alignof__` was the only thing holding it back.
2 parents 669198c + a3bd2a6 commit cdc637c

11 files changed

Lines changed: 36 additions & 13 deletions

File tree

c2rust-transpile/src/translator/mod.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,17 +3379,36 @@ impl<'c> Translation<'c> {
33793379
&self,
33803380
mut type_id: CTypeId,
33813381
preferred: bool,
3382+
src_loc: &Option<SrcSpan>,
33823383
) -> TranslationResult<WithStmts<Box<Expr>>> {
33833384
type_id = self.variable_array_base_type(type_id);
33843385

33853386
let ty = self.convert_type(type_id)?;
33863387
let tys = vec![ty];
33873388
let mut path = vec![mk().path_segment("core")];
3388-
if preferred {
3389+
// `core::intrinsics::pref_align_of` was removed in Rust 1.89
3390+
// (https://github.com/rust-lang/rust/pull/141803).
3391+
// There is no longer a notion of preferred alignment in Rust,
3392+
// so in edition 2024, we no longer support
3393+
// the non-standard `__alignof`/`__alignof__` extensions.
3394+
// Normal alignment should always be a valid preferred alignment,
3395+
// even if in a few cases, it is smaller,
3396+
// so rather than having a hard error here,
3397+
// we polyfill it with normal alignment.
3398+
if preferred && self.tcfg.edition < Edition2024 {
33893399
self.use_feature("core_intrinsics");
33903400
path.push(mk().path_segment("intrinsics"));
33913401
path.push(mk().path_segment_with_args("pref_align_of", mk().angle_bracketed_args(tys)));
33923402
} else {
3403+
if preferred {
3404+
let msg = "using `core::mem::align_of` instead of `core::intrinsics::pref_align_of` \
3405+
for preferred alignment (`__alignof`/`__alignof__`) as the latter has been removed in Rust";
3406+
if let Some(loc) = self.ast_context.display_loc(src_loc) {
3407+
warn!("{loc}: {msg}");
3408+
} else {
3409+
warn!("{msg}");
3410+
}
3411+
}
33933412
path.push(mk().path_segment("mem"));
33943413
path.push(mk().path_segment_with_args("align_of", mk().angle_bracketed_args(tys)));
33953414
}
@@ -3521,8 +3540,12 @@ impl<'c> Translation<'c> {
35213540
}
35223541
}
35233542
},
3524-
UnTypeOp::AlignOf => self.compute_align_of_type(arg_ty.ctype, false)?,
3525-
UnTypeOp::PreferredAlignOf => self.compute_align_of_type(arg_ty.ctype, true)?,
3543+
UnTypeOp::AlignOf => {
3544+
self.compute_align_of_type(arg_ty.ctype, false, src_loc)?
3545+
}
3546+
UnTypeOp::PreferredAlignOf => {
3547+
self.compute_align_of_type(arg_ty.ctype, true, src_loc)?
3548+
}
35263549
};
35273550

35283551
Ok(result)

tests/unit/misc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "misc-tests"
33
version = "0.1.0"
4-
edition = "2021"
4+
edition = "2024"
55

66
[dependencies]

tests/unit/misc/src/test_call_only_once.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::call_only_once::rust_assert_call_only_once;
22
use std::ffi::c_int;
33

44
#[link(name = "test")]
5-
extern "C" {
5+
unsafe extern "C" {
66
fn assert_call_only_once() -> c_int;
77
}
88

tests/unit/misc/src/test_exprs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::exprs::rust_exprs;
44
use std::ffi::{c_int, c_uint};
55

66
#[link(name = "test")]
7-
extern "C" {
7+
unsafe extern "C" {
88
fn exprs(_: c_uint, _: *mut c_int);
99
}
1010

tests/unit/misc/src/test_lvalues.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::lvalues::rust_lvalue;
22
use std::ffi::c_int;
33

44
#[link(name = "test")]
5-
extern "C" {
5+
unsafe extern "C" {
66
fn lvalue(_: *mut c_int);
77
}
88

tests/unit/misc/src/test_memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::strings_h::rust_setmem;
33
use std::ffi::{c_int, c_uint};
44

55
#[link(name = "test")]
6-
extern "C" {
6+
unsafe extern "C" {
77
fn malloc_test(_: c_uint, _: *mut c_int);
88

99
fn setmem(_: c_uint, _: *mut c_int);

tests/unit/misc/src/test_quicksort.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::qsort::{rust_partition, rust_quickSort, rust_swap};
44
use std::ffi::c_int;
55

66
#[link(name = "test")]
7-
extern "C" {
7+
unsafe extern "C" {
88
fn swap(_: *mut c_int, _: *mut c_int);
99

1010
fn partition(_: *mut c_int, _: c_int, _: c_int);

tests/unit/misc/src/test_shadowing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::shadowing::{rust_shadow, rust_twice};
22
use std::ffi::{c_int, c_uint};
33

44
#[link(name = "test")]
5-
extern "C" {
5+
unsafe extern "C" {
66
fn twice(_: c_int) -> c_int;
77

88
fn shadow(_: c_uint, _: *mut c_int);

tests/unit/misc/src/test_sizeofs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::sizeofs::rust_sizeofs;
44
use std::ffi::{c_int, c_uint};
55

66
#[link(name = "test")]
7-
extern "C" {
7+
unsafe extern "C" {
88
fn sizeofs(_: c_uint, _: *mut c_int);
99
}
1010

tests/unit/misc/src/test_typedef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::typedef::{int_ptr, my_int, rust_entry};
55
use std::ffi::c_int;
66

77
#[link(name = "test")]
8-
extern "C" {
8+
unsafe extern "C" {
99
fn entry() -> c_int;
1010
}
1111

0 commit comments

Comments
 (0)