From ab0eda325543afedc0809e90ecb68b8252427b30 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 14:39:13 -0800 Subject: [PATCH 01/39] ci: run `cargo nextest run` with `--no-fail-fast` in CI --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ecac15ddd7..b2b53577ab 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -126,7 +126,7 @@ jobs: run: | export RUSTFLAGS="$RUSTFLAGS -D warnings" export RUSTDOCFLAGS="-D warnings" - cargo nextest run --release --workspace + cargo nextest run --release --workspace --no-fail-fast - name: Test translator run: | # `test_translator.py` compiles translated code, @@ -212,6 +212,6 @@ jobs: - run: cargo build --release - - run: cargo nextest run --release --no-tests warn + - run: cargo nextest run --release --no-tests warn --no-fail-fast - run: cargo doc --document-private-items --no-deps From c8f2aff9605da414313bb21839b9c0c7fe86990e Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 18:24:13 -0800 Subject: [PATCH 02/39] transpile: add `--edition`, defaulting to edition 2021 for now --- Cargo.lock | 2 + c2rust-refactor/tests/snapshots.rs | 18 ++++- c2rust-rust-tools/Cargo.toml | 1 + c2rust-rust-tools/src/lib.rs | 68 ++++++++++++++++--- .../src/build_files/Cargo.toml.hbs | 2 +- c2rust-transpile/src/build_files/mod.rs | 5 +- c2rust-transpile/src/lib.rs | 5 +- c2rust-transpile/tests/snapshots.rs | 10 ++- c2rust/Cargo.toml | 1 + c2rust/src/bin/c2rust-transpile.rs | 7 ++ 10 files changed, 102 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78ef031bef..5480a9c957 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -275,6 +275,7 @@ version = "0.22.1" dependencies = [ "anyhow", "c2rust-build-paths", + "c2rust-rust-tools", "c2rust-transpile", "clap 3.2.25", "env_logger 0.10.0", @@ -477,6 +478,7 @@ name = "c2rust-rust-tools" version = "0.22.1" dependencies = [ "fs-err", + "itertools 0.14.0", "log", ] diff --git a/c2rust-refactor/tests/snapshots.rs b/c2rust-refactor/tests/snapshots.rs index a6326dd508..b12bae251e 100644 --- a/c2rust-refactor/tests/snapshots.rs +++ b/c2rust-refactor/tests/snapshots.rs @@ -6,7 +6,7 @@ use c2rust_refactor::RustcArgSource; use c2rust_rust_tools::rustc; use c2rust_rust_tools::rustfmt; use c2rust_rust_tools::sanitize_file_name; -use c2rust_rust_tools::EDITION; +use c2rust_rust_tools::RustEdition; use insta::assert_snapshot; use itertools::Itertools; use std::path::Path; @@ -16,6 +16,7 @@ struct RefactorTest<'a> { command: &'a str, command_args: &'a [&'a str], path: Option<&'a str>, + edition: RustEdition, old_expect_format_error: bool, new_expect_format_error: bool, old_expect_compile_error: bool, @@ -27,6 +28,7 @@ fn refactor(command: &str) -> RefactorTest { command, command_args: &[], path: None, + edition: Default::default(), old_expect_format_error: false, new_expect_format_error: false, old_expect_compile_error: false, @@ -50,6 +52,11 @@ impl<'a> RefactorTest<'a> { } } + #[allow(unused)] // TODO remove once `c2rust-refactor` is upgraded to edition 2024. + pub fn edition(self, edition: RustEdition) -> Self { + Self { edition, ..self } + } + pub fn old_expect_format_error(self, expect_error: bool) -> Self { Self { old_expect_format_error: expect_error, @@ -93,6 +100,7 @@ impl<'a> RefactorTest<'a> { command, path, command_args, + edition, old_expect_format_error, new_expect_format_error, old_expect_compile_error, @@ -110,6 +118,7 @@ impl<'a> RefactorTest<'a> { command, command_args, path, + edition, old_expect_format_error, new_expect_format_error, old_expect_compile_error, @@ -122,6 +131,7 @@ fn test_refactor( command: &str, command_args: &[&str], path: &str, + edition: RustEdition, old_expect_format_error: bool, new_expect_format_error: bool, old_expect_compile_error: bool, @@ -131,17 +141,19 @@ fn test_refactor( let old_path = tests_dir.join(path); rustfmt(&old_path) + .edition(edition) .check(true) .expect_error(old_expect_format_error) .run(); rustc(&old_path) + .edition(edition) .expect_error(old_expect_compile_error) .run(); let new_path = old_path.with_extension("new"); // Output from `alongside`. let old_path = old_path.to_str().unwrap(); - let rustc_args = [old_path, "--edition", EDITION]; + let rustc_args = [old_path, "--edition", edition.as_str()]; lib_main(Options { rewrite_modes: vec![OutputMode::Alongside], @@ -164,9 +176,11 @@ fn test_refactor( // TODO Run `rustfmt` by default as part of `c2rust-refactor` // with the same `--disable-rustfmt` flag that `c2rust-transpile` has. rustfmt(&new_path) + .edition(edition) .expect_error(new_expect_format_error) .run(); rustc(&new_path) + .edition(edition) .expect_error(new_expect_compile_error) .run(); diff --git a/c2rust-rust-tools/Cargo.toml b/c2rust-rust-tools/Cargo.toml index f91de6710b..c9eef790e8 100644 --- a/c2rust-rust-tools/Cargo.toml +++ b/c2rust-rust-tools/Cargo.toml @@ -13,4 +13,5 @@ categories.workspace = true [dependencies] fs-err = "3.3.0" +itertools = "0.14.0" log = "=0.4.28" # "0.4.29" requires Rust 1.68 diff --git a/c2rust-rust-tools/src/lib.rs b/c2rust-rust-tools/src/lib.rs index 2806c6b901..79870877ee 100644 --- a/c2rust-rust-tools/src/lib.rs +++ b/c2rust-rust-tools/src/lib.rs @@ -1,13 +1,52 @@ +use itertools::Itertools; use log::warn; +use std::fmt; +use std::fmt::Display; +use std::fmt::Formatter; use std::path::Path; use std::process::Command; +use std::str::FromStr; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)] +pub enum RustEdition { + /// The default is edition 2021 because `c2rust-refactor`, + /// based on `nightly-2022-08-08`, only understands up to edition 2021. + #[default] + Rust2021, + Rust2024, +} + +impl RustEdition { + pub const fn as_str(&self) -> &'static str { + match self { + Self::Rust2021 => "2021", + Self::Rust2024 => "2024", + } + } +} + +impl Display for RustEdition { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{}", self.as_str()) + } +} -/// The Rust edition used by code emitted by `c2rust`. -pub const EDITION: &str = "2021"; +impl FromStr for RustEdition { + type Err = String; + + fn from_str(s: &str) -> Result { + let choices = [Self::Rust2021, Self::Rust2024]; + choices + .into_iter() + .find(|choice| choice.as_str() == s) + .ok_or_else(|| format!("{s} not one of {}", choices.iter().join(" ,"))) + } +} #[must_use] pub struct Rustfmt<'a> { rs_path: &'a Path, + edition: RustEdition, check: bool, expect_error: bool, } @@ -15,12 +54,17 @@ pub struct Rustfmt<'a> { pub fn rustfmt(rs_path: &Path) -> Rustfmt { Rustfmt { rs_path, + edition: Default::default(), check: false, expect_error: false, } } impl<'a> Rustfmt<'a> { + pub fn edition(self, edition: RustEdition) -> Self { + Self { edition, ..self } + } + pub fn check(self, check: bool) -> Self { Self { check, ..self } } @@ -35,19 +79,20 @@ impl<'a> Rustfmt<'a> { pub fn run(self) { let Self { rs_path, + edition, check, expect_error, } = self; let check = if expect_error { true } else { check }; - run_rustfmt(rs_path, check, expect_error) + run_rustfmt(rs_path, edition, check, expect_error) } } -fn run_rustfmt(rs_path: &Path, check: bool, expect_error: bool) { +fn run_rustfmt(rs_path: &Path, edition: RustEdition, check: bool, expect_error: bool) { assert!(!expect_error || check); let mut cmd = Command::new("rustfmt"); - cmd.args(["--edition", EDITION]); + cmd.args(["--edition", edition.as_str()]); cmd.arg(rs_path); if check { cmd.arg("--check"); @@ -80,6 +125,7 @@ fn run_rustfmt(rs_path: &Path, check: bool, expect_error: bool) { #[must_use] pub struct Rustc<'a> { rs_path: &'a Path, + edition: RustEdition, crate_name: Option<&'a str>, expect_error: bool, } @@ -87,12 +133,17 @@ pub struct Rustc<'a> { pub fn rustc(rs_path: &Path) -> Rustc { Rustc { rs_path, + edition: Default::default(), crate_name: None, expect_error: false, } } impl<'a> Rustc<'a> { + pub fn edition(self, edition: RustEdition) -> Self { + Self { edition, ..self } + } + pub fn crate_name(self, crate_name: &'a str) -> Self { Self { crate_name: Some(crate_name), @@ -110,16 +161,17 @@ impl<'a> Rustc<'a> { pub fn run(self) { let Self { rs_path, + edition, crate_name, expect_error, } = self; let crate_name = crate_name.unwrap_or_else(|| rs_path.file_stem().unwrap().to_str().unwrap()); - run_rustc(rs_path, crate_name, expect_error); + run_rustc(rs_path, edition, crate_name, expect_error); } } -fn run_rustc(rs_path: &Path, crate_name: &str, expect_error: bool) { +fn run_rustc(rs_path: &Path, edition: RustEdition, crate_name: &str, expect_error: bool) { // There's no good way to not create an output with `rustc`, // so just create an `.rlib` and then delete it immediately. let rlib_path = rs_path.with_file_name(format!("lib{crate_name}.rlib")); @@ -129,7 +181,7 @@ fn run_rustc(rs_path: &Path, crate_name: &str, expect_error: bool) { "--crate-type", "lib", "--edition", - EDITION, + edition.as_str(), "--crate-name", crate_name, "-Awarnings", // Disable warnings. diff --git a/c2rust-transpile/src/build_files/Cargo.toml.hbs b/c2rust-transpile/src/build_files/Cargo.toml.hbs index e9e21cbe0a..db53bea56b 100644 --- a/c2rust-transpile/src/build_files/Cargo.toml.hbs +++ b/c2rust-transpile/src/build_files/Cargo.toml.hbs @@ -12,7 +12,7 @@ name = "{{crate_name}}" authors = ["C2Rust"] version = "0.0.0" publish = false -edition = "2021" +edition = "{{edition}}" autobins = false {{#if is_library~}} diff --git a/c2rust-transpile/src/build_files/mod.rs b/c2rust-transpile/src/build_files/mod.rs index 870b764fdd..0356b88dda 100644 --- a/c2rust-transpile/src/build_files/mod.rs +++ b/c2rust-transpile/src/build_files/mod.rs @@ -235,7 +235,7 @@ fn emit_build_rs( let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?; if !tcfg.disable_rustfmt { - rustfmt(&output_path).run(); + rustfmt(&output_path).edition(tcfg.edition).run(); } Some(path) @@ -279,7 +279,7 @@ fn emit_lib_rs( let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?; if !tcfg.disable_rustfmt { - rustfmt(&output_path).run(); + rustfmt(&output_path).edition(tcfg.edition).run(); } Some(path) @@ -319,6 +319,7 @@ fn emit_cargo_toml<'lcmd>( let crate_json = json!({ "crate_name": ccfg.crate_name, "crate_rust_name": ccfg.crate_name.replace('-', "_"), + "edition": tcfg.edition.as_str(), "crate_types": ccfg.link_cmd.r#type.as_cargo_types(), "is_library": ccfg.link_cmd.r#type.is_library(), "lib_rs_file": get_lib_rs_file_name(tcfg), diff --git a/c2rust-transpile/src/lib.rs b/c2rust-transpile/src/lib.rs index 3efe8bcd8d..a6ef0042fa 100644 --- a/c2rust-transpile/src/lib.rs +++ b/c2rust-transpile/src/lib.rs @@ -20,7 +20,7 @@ use std::process::Command; use std::{env, io}; use crate::compile_cmds::CompileCmd; -use c2rust_rust_tools::rustfmt; +use c2rust_rust_tools::{rustfmt, RustEdition}; use failure::{format_err, Error}; use itertools::Itertools; use log::{info, warn}; @@ -106,6 +106,7 @@ pub struct TranspilerConfig { pub disable_refactoring: bool, pub preserve_unused_functions: bool, pub log_level: log::LevelFilter, + pub edition: RustEdition, /// Run `c2rust-postprocess` after transpiling and potentially refactoring. pub postprocess: bool, @@ -755,7 +756,7 @@ fn transpile_single( }; if !tcfg.disable_rustfmt { - rustfmt(&output_path).run(); + rustfmt(&output_path).edition(tcfg.edition).run(); } Ok((output_path, pragmas, crates)) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 80dd6f2e87..13e3bb81d1 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -51,6 +51,7 @@ fn config() -> TranspilerConfig { disable_refactoring: false, preserve_unused_functions: false, log_level: log::LevelFilter::Warn, + edition: Default::default(), postprocess: false, emit_build_files: false, binaries: Vec::new(), @@ -91,7 +92,9 @@ fn compile_and_transpile_file(c_path: &Path, config: TranspilerConfig) { /// For outputs that vary in different environments, `platform` can be any platform-specific string. /// It could be the `target_arch`, `target_os`, some combination, or something else. fn transpile_snapshot(platform: Option<&str>, c_path: &Path) { - compile_and_transpile_file(c_path, config()); + let cfg = config(); + let edition = cfg.edition; + compile_and_transpile_file(c_path, cfg); let cwd = current_dir().unwrap(); // The crate name can't have `.`s in it, so use the file stem. // This is also why we set it explicitly with `--crate-name`, @@ -137,7 +140,10 @@ fn transpile_snapshot(platform: Option<&str>, c_path: &Path) { return; } - rustc(&rs_path).crate_name(crate_name).run(); + rustc(&rs_path) + .edition(edition) + .crate_name(crate_name) + .run(); } #[must_use] diff --git a/c2rust/Cargo.toml b/c2rust/Cargo.toml index a8e344c487..3353cfed8b 100644 --- a/c2rust/Cargo.toml +++ b/c2rust/Cargo.toml @@ -21,6 +21,7 @@ log = "0.4" regex = "1.3" shlex = "1.3" c2rust-transpile = { path = "../c2rust-transpile", version = "0.22.1" } +c2rust-rust-tools = { path = "../c2rust-rust-tools", version = "0.22.1" } [build-dependencies] c2rust-build-paths = { path = "../c2rust-build-paths", version = "0.22.1" } diff --git a/c2rust/src/bin/c2rust-transpile.rs b/c2rust/src/bin/c2rust-transpile.rs index e123f74af2..eaf33a5f18 100644 --- a/c2rust/src/bin/c2rust-transpile.rs +++ b/c2rust/src/bin/c2rust-transpile.rs @@ -3,6 +3,7 @@ use log::LevelFilter; use regex::Regex; use std::{ffi::OsStr, fs, path::PathBuf}; +use c2rust_rust_tools::RustEdition; use c2rust_transpile::{Diagnostic, ReplaceMode, TranspilerConfig}; #[derive(Debug, Parser)] @@ -97,6 +98,11 @@ struct Args { #[clap(long, value_enum, default_value_t = InvalidCodes::CompileError)] invalid_code: InvalidCodes, + /// Rust edition to target. + /// 2021 and 2024 are supported. + #[clap(long, default_value_t)] + edition: RustEdition, + /// Emit .rs files as modules instead of crates, excluding the crate preambles #[clap(long)] emit_modules: bool, @@ -303,6 +309,7 @@ fn main() { emit_no_std: args.emit_no_std, enabled_warnings: args.warn.into_iter().collect(), log_level: args.log_level, + edition: args.edition, }; // binaries imply emit-build-files if !tcfg.binaries.is_empty() { From a4f2ba17bcd9827ec61c321f990deba3e5869261 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 18:42:55 -0800 Subject: [PATCH 03/39] transpile: only emit `[profile.release] strip = "debuginfo"` before edition 2024 where it wasn't the default --- c2rust-transpile/src/build_files/Cargo.toml.hbs | 2 ++ c2rust-transpile/src/build_files/mod.rs | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/c2rust-transpile/src/build_files/Cargo.toml.hbs b/c2rust-transpile/src/build_files/Cargo.toml.hbs index db53bea56b..efdd0413ed 100644 --- a/c2rust-transpile/src/build_files/Cargo.toml.hbs +++ b/c2rust-transpile/src/build_files/Cargo.toml.hbs @@ -56,5 +56,7 @@ version = "*" {{~/if}} {{~/if}} +{{#if strip_debuginfo_release~}} [profile.release] strip = "debuginfo" +{{~/if}} diff --git a/c2rust-transpile/src/build_files/mod.rs b/c2rust-transpile/src/build_files/mod.rs index 0356b88dda..327b9629c3 100644 --- a/c2rust-transpile/src/build_files/mod.rs +++ b/c2rust-transpile/src/build_files/mod.rs @@ -4,7 +4,7 @@ use std::io::Write; use std::path::{Path, PathBuf}; use std::str::FromStr; -use c2rust_rust_tools::rustfmt; +use c2rust_rust_tools::{rustfmt, RustEdition}; use handlebars::Handlebars; use pathdiff::diff_paths; use serde_derive::Serialize; @@ -320,6 +320,9 @@ fn emit_cargo_toml<'lcmd>( "crate_name": ccfg.crate_name, "crate_rust_name": ccfg.crate_name.replace('-', "_"), "edition": tcfg.edition.as_str(), + // This is already the default in Rust 1.77, + // and edition 2024 was released in Rust 1.85. + "strip_debuginfo_release": tcfg.edition < RustEdition::Rust2024, "crate_types": ccfg.link_cmd.r#type.as_cargo_types(), "is_library": ccfg.link_cmd.r#type.is_library(), "lib_rs_file": get_lib_rs_file_name(tcfg), From 1036717c710a3782eb5b467dbdb07ba8aa418d80 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 20:01:47 -0800 Subject: [PATCH 04/39] transpile: tests: add `expect_compile_error` This will help as I'm upgrading to edition 2024 and fix errors one by one. --- c2rust-transpile/tests/snapshots.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 13e3bb81d1..9ed1b99aae 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -91,7 +91,7 @@ fn compile_and_transpile_file(c_path: &Path, config: TranspilerConfig) { /// Transpile one input and compare output against the corresponding snapshot. /// For outputs that vary in different environments, `platform` can be any platform-specific string. /// It could be the `target_arch`, `target_os`, some combination, or something else. -fn transpile_snapshot(platform: Option<&str>, c_path: &Path) { +fn transpile_snapshot(platform: Option<&str>, c_path: &Path, expect_compile_error: bool) { let cfg = config(); let edition = cfg.edition; compile_and_transpile_file(c_path, cfg); @@ -143,6 +143,7 @@ fn transpile_snapshot(platform: Option<&str>, c_path: &Path) { rustc(&rs_path) .edition(edition) .crate_name(crate_name) + .expect_error(expect_compile_error) .run(); } @@ -151,6 +152,7 @@ struct TranspileTest<'a> { c_file_name: &'a str, arch_specific: bool, os_specific: bool, + expect_compile_error: bool, } fn transpile(c_file_name: &str) -> TranspileTest { @@ -158,6 +160,7 @@ fn transpile(c_file_name: &str) -> TranspileTest { c_file_name, arch_specific: false, os_specific: false, + expect_compile_error: false, } } @@ -176,11 +179,20 @@ impl<'a> TranspileTest<'a> { } } + #[allow(unused)] // TODO remove once used + pub fn expect_compile_error(self, expect_compile_error: bool) -> Self { + Self { + expect_compile_error, + ..self + } + } + pub fn run(self) { let Self { c_file_name, arch_specific, os_specific, + expect_compile_error, } = self; let specific_dir_prefix = [arch_specific.then_some("arch"), os_specific.then_some("os")] @@ -230,7 +242,7 @@ impl<'a> TranspileTest<'a> { platform => Some(platform), }; - transpile_snapshot(platform, &c_path); + transpile_snapshot(platform, &c_path, expect_compile_error); } } From 9693972e0d2d650b7d8b501a1a3958004b1c876c Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 20:08:05 -0800 Subject: [PATCH 05/39] transpile: tests: add `edition` to `TranspileTest` --- c2rust-transpile/tests/snapshots.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 9ed1b99aae..a8aba99d41 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -6,11 +6,12 @@ use std::process::Command; use c2rust_rust_tools::rustc; use c2rust_rust_tools::sanitize_file_name; +use c2rust_rust_tools::RustEdition; use c2rust_transpile::convert_type::RESERVED_NAMES; use c2rust_transpile::{ReplaceMode, TranspilerConfig}; use itertools::Itertools; -fn config() -> TranspilerConfig { +fn config(edition: RustEdition) -> TranspilerConfig { TranspilerConfig { dump_untyped_context: false, dump_typed_context: false, @@ -51,7 +52,7 @@ fn config() -> TranspilerConfig { disable_refactoring: false, preserve_unused_functions: false, log_level: log::LevelFilter::Warn, - edition: Default::default(), + edition, postprocess: false, emit_build_files: false, binaries: Vec::new(), @@ -91,9 +92,13 @@ fn compile_and_transpile_file(c_path: &Path, config: TranspilerConfig) { /// Transpile one input and compare output against the corresponding snapshot. /// For outputs that vary in different environments, `platform` can be any platform-specific string. /// It could be the `target_arch`, `target_os`, some combination, or something else. -fn transpile_snapshot(platform: Option<&str>, c_path: &Path, expect_compile_error: bool) { - let cfg = config(); - let edition = cfg.edition; +fn transpile_snapshot( + platform: Option<&str>, + c_path: &Path, + edition: RustEdition, + expect_compile_error: bool, +) { + let cfg = config(edition); compile_and_transpile_file(c_path, cfg); let cwd = current_dir().unwrap(); // The crate name can't have `.`s in it, so use the file stem. @@ -150,6 +155,7 @@ fn transpile_snapshot(platform: Option<&str>, c_path: &Path, expect_compile_erro #[must_use] struct TranspileTest<'a> { c_file_name: &'a str, + edition: RustEdition, arch_specific: bool, os_specific: bool, expect_compile_error: bool, @@ -158,6 +164,7 @@ struct TranspileTest<'a> { fn transpile(c_file_name: &str) -> TranspileTest { TranspileTest { c_file_name, + edition: Default::default(), arch_specific: false, os_specific: false, expect_compile_error: false, @@ -165,6 +172,11 @@ fn transpile(c_file_name: &str) -> TranspileTest { } impl<'a> TranspileTest<'a> { + #[allow(unused)] // TODO remove once used + pub fn edition(self, edition: RustEdition) -> Self { + Self { edition, ..self } + } + pub fn arch_specific(self, arch_specific: bool) -> Self { Self { arch_specific, @@ -190,6 +202,7 @@ impl<'a> TranspileTest<'a> { pub fn run(self) { let Self { c_file_name, + edition, arch_specific, os_specific, expect_compile_error, @@ -242,7 +255,7 @@ impl<'a> TranspileTest<'a> { platform => Some(platform), }; - transpile_snapshot(platform, &c_path, expect_compile_error); + transpile_snapshot(platform, &c_path, edition, expect_compile_error); } } @@ -438,7 +451,7 @@ fn test_varargs() { } fn transpile_with_c_decl_map_snapshot(c_path: &Path) { - compile_and_transpile_file(c_path, config()); + compile_and_transpile_file(c_path, config(Default::default())); let c_decls_path = c_path.with_extension("c_decls.json"); let snapshot_name = format!("c_decls-{}", c_path.file_name().unwrap().to_str().unwrap()); From a1095535141852f8cf53ca4d01eccbd8c0f03c40 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 20:09:06 -0800 Subject: [PATCH 06/39] rust-tools: use a recent nightly for edition 2024 for `rustfmt` and `rustc` `nightly-2023-04-15` doesn't know about edition 2024 yet. Previously, `rustfmt` wasn't using a specific toolchain, so it would just default to whatever `rust-toolchain.toml` was there. Now it properly is overridden like `rustc` was being. --- .github/workflows/ci.yml | 2 ++ c2rust-rust-tools/src/lib.rs | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b2b53577ab..fe002c1a3a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,6 +60,8 @@ jobs: --profile minimal --component rustfmt,rustc-dev rustup toolchain install nightly-2023-04-15 \ --profile minimal --component rustfmt + rustup toolchain install nightly-2026-03-03 \ + --profile minimal --component rustfmt - uses: taiki-e/install-action@nextest diff --git a/c2rust-rust-tools/src/lib.rs b/c2rust-rust-tools/src/lib.rs index 79870877ee..899a1cd362 100644 --- a/c2rust-rust-tools/src/lib.rs +++ b/c2rust-rust-tools/src/lib.rs @@ -23,6 +23,17 @@ impl RustEdition { Self::Rust2024 => "2024", } } + + /// The toolchain to use for this edition. + /// This is returned in the `+{toolchain}` format + /// that can be passed to `rustup` wrappers. + pub const fn toolchain(&self) -> &'static str { + match self { + Self::Rust2021 => "+nightly-2023-04-15", + // This doesn't really need to be pinned, but pin it for stability. + Self::Rust2024 => "+nightly-2026-03-03", + } + } } impl Display for RustEdition { @@ -92,7 +103,7 @@ fn run_rustfmt(rs_path: &Path, edition: RustEdition, check: bool, expect_error: assert!(!expect_error || check); let mut cmd = Command::new("rustfmt"); - cmd.args(["--edition", edition.as_str()]); + cmd.args([edition.toolchain(), "--edition", edition.as_str()]); cmd.arg(rs_path); if check { cmd.arg("--check"); @@ -177,7 +188,7 @@ fn run_rustc(rs_path: &Path, edition: RustEdition, crate_name: &str, expect_erro let rlib_path = rs_path.with_file_name(format!("lib{crate_name}.rlib")); let mut cmd = Command::new("rustc"); cmd.args([ - "+nightly-2023-04-15", + edition.toolchain(), "--crate-type", "lib", "--edition", From 5a5fa4b33e60cd00cc9b137d595f1bc1ccd5b208 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 13:43:54 -0800 Subject: [PATCH 07/39] transpile: make `fn maybe_write_file` take `output` as a `&str` not `String` --- c2rust-transpile/src/build_files/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/c2rust-transpile/src/build_files/mod.rs b/c2rust-transpile/src/build_files/mod.rs index 327b9629c3..24fa756ab9 100644 --- a/c2rust-transpile/src/build_files/mod.rs +++ b/c2rust-transpile/src/build_files/mod.rs @@ -232,7 +232,7 @@ fn emit_build_rs( }); let output = reg.render("build.rs", &json).unwrap(); let output_path = build_dir.join("build.rs"); - let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?; + let path = maybe_write_to_file(&output_path, &output, tcfg.overwrite_existing)?; if !tcfg.disable_rustfmt { rustfmt(&output_path).edition(tcfg.edition).run(); @@ -276,7 +276,7 @@ fn emit_lib_rs( let output_path = build_dir.join(file_name); let output = reg.render("lib.rs", &json).unwrap(); - let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?; + let path = maybe_write_to_file(&output_path, &output, tcfg.overwrite_existing)?; if !tcfg.disable_rustfmt { rustfmt(&output_path).edition(tcfg.edition).run(); @@ -290,7 +290,7 @@ fn emit_lib_rs( fn emit_rust_toolchain(tcfg: &TranspilerConfig, build_dir: &Path) { let output_path = build_dir.join("rust-toolchain.toml"); let output = include_str!("generated-rust-toolchain.toml").to_string(); - maybe_write_to_file(&output_path, output, tcfg.overwrite_existing); + maybe_write_to_file(&output_path, &output, tcfg.overwrite_existing); } fn emit_cargo_toml<'lcmd>( @@ -342,10 +342,10 @@ fn emit_cargo_toml<'lcmd>( let file_name = "Cargo.toml"; let output_path = build_dir.join(file_name); let output = reg.render(file_name, &json).unwrap(); - maybe_write_to_file(&output_path, output, tcfg.overwrite_existing); + maybe_write_to_file(&output_path, &output, tcfg.overwrite_existing); } -fn maybe_write_to_file(output_path: &Path, output: String, overwrite: bool) -> Option { +fn maybe_write_to_file(output_path: &Path, output: &str, overwrite: bool) -> Option { if output_path.exists() && !overwrite { eprintln!("Skipping existing file {}", output_path.display()); return None; From b89e837e11539f6e02eb9989a36b53acb7e9d9ce Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 13:57:54 -0800 Subject: [PATCH 08/39] transpile: remove `generated-rust-toolchain.toml` and generate it from scratch with the right toolchain `generated-rust-toolchain.toml` is very simple, so embedding it should be fine. We previously didn't since `test_translator.py` just copied it, since it doesn't use `--emit-build-files`, for *reasons*. But now that the toolchain depends on the edition, we can't do that. So we just duplicate the simple logic in `fn emit_rust_toolchain`. --- c2rust-rust-tools/src/lib.rs | 1 + .../build_files/generated-rust-toolchain.toml | 3 --- c2rust-transpile/src/build_files/mod.rs | 12 ++++++++++-- scripts/test_translator.py | 17 +++++++++++++---- 4 files changed, 24 insertions(+), 9 deletions(-) delete mode 100644 c2rust-transpile/src/build_files/generated-rust-toolchain.toml diff --git a/c2rust-rust-tools/src/lib.rs b/c2rust-rust-tools/src/lib.rs index 899a1cd362..3eed6d99c3 100644 --- a/c2rust-rust-tools/src/lib.rs +++ b/c2rust-rust-tools/src/lib.rs @@ -29,6 +29,7 @@ impl RustEdition { /// that can be passed to `rustup` wrappers. pub const fn toolchain(&self) -> &'static str { match self { + // 1.70 (1.68 for syn v2.0, 1.70 for sparse registry) Self::Rust2021 => "+nightly-2023-04-15", // This doesn't really need to be pinned, but pin it for stability. Self::Rust2024 => "+nightly-2026-03-03", diff --git a/c2rust-transpile/src/build_files/generated-rust-toolchain.toml b/c2rust-transpile/src/build_files/generated-rust-toolchain.toml deleted file mode 100644 index fecea06965..0000000000 --- a/c2rust-transpile/src/build_files/generated-rust-toolchain.toml +++ /dev/null @@ -1,3 +0,0 @@ -[toolchain] -channel = "nightly-2023-04-15" # 1.70 (1.68 for syn v2.0, 1.70 for sparse registry) -components = ["rustfmt"] diff --git a/c2rust-transpile/src/build_files/mod.rs b/c2rust-transpile/src/build_files/mod.rs index 24fa756ab9..360abec323 100644 --- a/c2rust-transpile/src/build_files/mod.rs +++ b/c2rust-transpile/src/build_files/mod.rs @@ -289,8 +289,16 @@ fn emit_lib_rs( /// on a nightly toolchain until the `c_variadics` feature is stable. fn emit_rust_toolchain(tcfg: &TranspilerConfig, build_dir: &Path) { let output_path = build_dir.join("rust-toolchain.toml"); - let output = include_str!("generated-rust-toolchain.toml").to_string(); - maybe_write_to_file(&output_path, &output, tcfg.overwrite_existing); + let toolchain = tcfg.edition.toolchain().strip_prefix("+").unwrap(); + let output = format!( + " +[toolchain] +channel = \"{toolchain}\" +components = [\"rustfmt\"] +" + ); + let output = output.trim_start(); + maybe_write_to_file(&output_path, output, tcfg.overwrite_existing); } fn emit_cargo_toml<'lcmd>( diff --git a/scripts/test_translator.py b/scripts/test_translator.py index efaa6746ea..648f3a0b45 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -504,14 +504,23 @@ def run(self) -> List[TestOutcome]: self.generated_files["rust_src"].append(lib_file) - # Copy `generated-rust-toolchain.toml`. # We `c2rust-transpile` `*.c` files individually, so `--emit-build-files` doesn't work # (if it's generated, it's in the wrong directory and may be different for each transpiled file). # We could also change things to transpile all `*.c` files at once, but that's more involved. - generated_rust_toolchain = Path(c.TRANSPILE_CRATE_DIR) / "src/build_files/generated-rust-toolchain.toml" + # This logic needs to stay in sync with `fn emit_rust_toolchain`. + edition = "2021" + match edition: + case "2021": + toolchain = "nightly-2023-04-15" + case "2024": + toolchain = "nightly-2026-03-03" + rust_toolchain_toml = f"""\ +[toolchain] +channel = "{toolchain}" +components = ["rustfmt"] +""" rust_toolchain = Path(self.full_path) / "rust-toolchain.toml" - rust_toolchain.unlink(missing_ok=True) - rust_toolchain.symlink_to(generated_rust_toolchain) + rust_toolchain.write_text(rust_toolchain_toml) self.generated_files["rust_src"].append(str(rust_toolchain)) # Build From 549d8b3a076910d2c2bda9c4d5e70c3f46b4f54a Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 20:12:14 -0800 Subject: [PATCH 09/39] transpile: tests: upgrade tests that require 0 changes to edition 2024 These are `incomplete_arrays.c` and `main_fn.c`. --- c2rust-transpile/tests/snapshots.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index a8aba99d41..dec6b7274a 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -172,7 +172,6 @@ fn transpile(c_file_name: &str) -> TranspileTest { } impl<'a> TranspileTest<'a> { - #[allow(unused)] // TODO remove once used pub fn edition(self, edition: RustEdition) -> Self { Self { edition, ..self } } @@ -322,7 +321,9 @@ fn test_gotos() { #[test] fn test_incomplete_arrays() { - transpile("incomplete_arrays.c").run(); + transpile("incomplete_arrays.c") + .edition(RustEdition::Rust2024) + .run(); } #[test] @@ -348,7 +349,7 @@ fn test_macros() { #[test] fn test_main_fn() { - transpile("main_fn.c").run(); + transpile("main_fn.c").edition(RustEdition::Rust2024).run(); } #[test] From 744e8d60af745bdd3bf6d7cdd08277685e3beaec Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 21:18:40 -0800 Subject: [PATCH 10/39] transpile: emit `#[unsafe(no_mangle)]` for edition 2024 --- c2rust-transpile/src/translator/mod.rs | 22 ++++-- c2rust-transpile/tests/snapshots.rs | 77 ++++++++++++++----- ...snapshots__transpile-aarch64@vm_x86.c.snap | 2 +- ...s__transpile-linux@out_of_range_lit.c.snap | 2 +- .../snapshots__transpile-linux@rotate.c.snap | 4 +- .../snapshots__transpile-linux@sigign.c.snap | 2 +- ...apshots__transpile-linux@typedefidx.c.snap | 2 +- .../snapshots__transpile-linux@types.c.snap | 32 ++++---- ...shots__transpile-linux@wide_strings.c.snap | 8 +- ...s__transpile-macos@out_of_range_lit.c.snap | 2 +- .../snapshots__transpile-macos@rotate.c.snap | 4 +- .../snapshots__transpile-macos@sigign.c.snap | 2 +- ...apshots__transpile-macos@typedefidx.c.snap | 2 +- .../snapshots__transpile-macos@types.c.snap | 32 ++++---- ...shots__transpile-macos@wide_strings.c.snap | 8 +- .../snapshots__transpile-x86_64@vm_x86.c.snap | 2 +- .../snapshots__transpile@alloca.c.snap | 4 +- .../snapshots__transpile@arrays.c.snap | 10 +-- ...pshots__transpile@compound_literals.c.snap | 12 +-- .../snapshots__transpile@empty_init.c.snap | 2 +- .../snapshots__transpile@factorial.c.snap | 2 +- .../snapshots__transpile@gotos.c.snap | 2 +- .../snapshots__transpile@insertion.c.snap | 2 +- .../snapshots__transpile@macrocase.c.snap | 2 +- .../snapshots__transpile@predefined.c.snap | 4 +- .../snapshots__transpile@records.c.snap | 4 +- .../snapshots__transpile@ref_ub.c.snap | 2 +- .../snapshots__transpile@rotate.c.snap | 12 +-- .../snapshots__transpile@static_assert.c.snap | 2 +- .../snapshots__transpile@str_init.c.snap | 22 +++--- 30 files changed, 166 insertions(+), 119 deletions(-) diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index c014935ccf..4a63594d7c 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -6,6 +6,7 @@ use std::ops::Index; use std::path::{Path, PathBuf}; use std::rc::Rc; +use c2rust_rust_tools::RustEdition; use dtoa; use failure::{err_msg, format_err, Fail}; use indexmap::indexmap; @@ -371,10 +372,17 @@ pub fn stmts_block(mut stmts: Vec) -> Block { } /// Generate link attributes needed to ensure that the generated Rust libraries have the right symbol values. -fn mk_linkage(in_extern_block: bool, new_name: &str, old_name: &str) -> Builder { +fn mk_linkage( + in_extern_block: bool, + new_name: &str, + old_name: &str, + edition: RustEdition, +) -> Builder { if new_name == old_name { if in_extern_block { mk() // There is no mangling by default in extern blocks anymore + } else if edition >= RustEdition::Rust2024 { + mk().call_attr("unsafe", vec!["no_mangle"]) } else { mk().single_attr("no_mangle") // Don't touch my name Rust! } @@ -2119,7 +2127,7 @@ impl<'c> Translation<'c> { .expect("Variables should already be renamed"); let ConvertedVariable { ty, mutbl, init: _ } = self.convert_variable(ctx.static_().const_(), None, typ)?; - let mut extern_item = mk_linkage(true, &new_name, ident) + let mut extern_item = mk_linkage(true, &new_name, ident, self.tcfg.edition) .span(span) .set_mutbl(mutbl); @@ -2218,7 +2226,9 @@ impl<'c> Translation<'c> { }; let static_def = if is_externally_visible { - mk_linkage(false, new_name, ident).pub_().extern_("C") + mk_linkage(false, new_name, ident, self.tcfg.edition) + .pub_() + .extern_("C") } else if self.cur_file.get().is_some() { mk().pub_() } else { @@ -2556,7 +2566,9 @@ impl<'c> Translation<'c> { // but strings have to do for now self.mk_cross_check(mk(), vec!["entry(djb2=\"main\")", "exit(djb2=\"main\")"]) } else if (is_global && !is_inline) || is_extern_inline { - mk_linkage(false, new_name, name).extern_("C").pub_() + mk_linkage(false, new_name, name, self.tcfg.edition) + .extern_("C") + .pub_() } else if self.cur_file.get().is_some() { mk().extern_("C").pub_() } else { @@ -2603,7 +2615,7 @@ impl<'c> Translation<'c> { )) } else { // Translating an extern function declaration - let mut mk_ = mk_linkage(true, new_name, name).span(span); + let mut mk_ = mk_linkage(true, new_name, name, self.tcfg.edition).span(span); // When putting extern fns into submodules, they need to be public to be accessible if self.tcfg.reorganize_definitions { diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index dec6b7274a..f941334d12 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -281,12 +281,12 @@ fn generate_keywords_test() { #[test] fn test_alloca() { - transpile("alloca.c").run(); + transpile("alloca.c").edition(RustEdition::Rust2024).run(); } #[test] fn test_arrays() { - transpile("arrays.c").run(); + transpile("arrays.c").edition(RustEdition::Rust2024).run(); } #[test] @@ -296,12 +296,16 @@ fn test_atomics() { #[test] fn test_compound_literals() { - transpile("compound_literals.c").run(); + transpile("compound_literals.c") + .edition(RustEdition::Rust2024) + .run(); } #[test] fn test_empty_init() { - transpile("empty_init.c").run(); + transpile("empty_init.c") + .edition(RustEdition::Rust2024) + .run(); } #[test] @@ -311,12 +315,14 @@ fn test_exprs() { #[test] fn test_factorial() { - transpile("factorial.c").run(); + transpile("factorial.c") + .edition(RustEdition::Rust2024) + .run(); } #[test] fn test_gotos() { - transpile("gotos.c").run(); + transpile("gotos.c").edition(RustEdition::Rust2024).run(); } #[test] @@ -328,7 +334,9 @@ fn test_incomplete_arrays() { #[test] fn test_insertion() { - transpile("insertion.c").run(); + transpile("insertion.c") + .edition(RustEdition::Rust2024) + .run(); } #[test] @@ -339,7 +347,9 @@ fn test_keywords() { #[test] fn test_macrocase() { - transpile("macrocase.c").run(); + transpile("macrocase.c") + .edition(RustEdition::Rust2024) + .run(); } #[test] @@ -354,32 +364,36 @@ fn test_main_fn() { #[test] fn test_predefined() { - transpile("predefined.c").run(); + transpile("predefined.c") + .edition(RustEdition::Rust2024) + .run(); } #[test] fn test_records() { - transpile("records.c").run(); + transpile("records.c").edition(RustEdition::Rust2024).run(); } #[test] fn test_ref_ub() { - transpile("ref_ub.c").run(); + transpile("ref_ub.c").edition(RustEdition::Rust2024).run(); } #[test] fn test_rotate() { - transpile("rotate.c").run(); + transpile("rotate.c").edition(RustEdition::Rust2024).run(); } #[test] fn test_static_assert() { - transpile("static_assert.c").run(); + transpile("static_assert.c") + .edition(RustEdition::Rust2024) + .run(); } #[test] fn test_str_init() { - transpile("str_init.c").run(); + transpile("str_init.c").edition(RustEdition::Rust2024).run(); } // arch-specific @@ -391,7 +405,10 @@ fn test_spin() { #[test] fn test_vm_x86() { - transpile("vm_x86.c").arch_specific(true).run(); + transpile("vm_x86.c") + .edition(RustEdition::Rust2024) + .arch_specific(true) + .run(); } // os-specific @@ -408,7 +425,10 @@ fn test_macros_os_specific() { #[test] fn test_out_of_range_lit() { - transpile("out_of_range_lit.c").os_specific(true).run(); + transpile("out_of_range_lit.c") + .edition(RustEdition::Rust2024) + .os_specific(true) + .run(); } #[test] @@ -418,27 +438,42 @@ fn test_rnd() { #[test] fn test_rotate_os_specific() { - transpile("rotate.c").os_specific(true).run(); + transpile("rotate.c") + .edition(RustEdition::Rust2024) + .os_specific(true) + .run(); } #[test] fn test_sigign() { - transpile("sigign.c").os_specific(true).run(); + transpile("sigign.c") + .edition(RustEdition::Rust2024) + .os_specific(true) + .run(); } #[test] fn test_typedefidx() { - transpile("typedefidx.c").os_specific(true).run(); + transpile("typedefidx.c") + .edition(RustEdition::Rust2024) + .os_specific(true) + .run(); } #[test] fn test_types() { - transpile("types.c").os_specific(true).run(); + transpile("types.c") + .edition(RustEdition::Rust2024) + .os_specific(true) + .run(); } #[test] fn test_wide_strings() { - transpile("wide_strings.c").os_specific(true).run(); + transpile("wide_strings.c") + .edition(RustEdition::Rust2024) + .os_specific(true) + .run(); } // arch-os-specific diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap index 1885166bcf..7d5ac74dce 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap @@ -22,7 +22,7 @@ pub struct vm_t { } pub type byte = ::core::ffi::c_uchar; pub const MAX_VMMAIN_ARGS: ::core::ffi::c_int = 50 as ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn VM_CallCompiled( mut vm: *mut vm_t, mut args: *mut ::core::ffi::c_int, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@out_of_range_lit.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@out_of_range_lit.c.snap index c684a98f66..e192665b01 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@out_of_range_lit.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@out_of_range_lit.c.snap @@ -12,7 +12,7 @@ expression: cat tests/snapshots/os-specific/out_of_range_lit.linux.rs )] pub type __int32_t = i32; pub type int32_t = __int32_t; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn f() { let mut a: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; let mut b: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap index 0e2e0fefcc..54865ac254 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/os-specific/rotate.linux.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_left_64( mut x: ::core::ffi::c_ulonglong, ) -> ::core::ffi::c_ulonglong { @@ -18,7 +18,7 @@ pub unsafe extern "C" fn rotate_left_64( .rotate_left(4 as ::core::ffi::c_int as ::core::ffi::c_ulong as u32) as ::core::ffi::c_ulonglong; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_right_64( mut x: ::core::ffi::c_ulonglong, ) -> ::core::ffi::c_ulonglong { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@sigign.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@sigign.c.snap index afb7d1e8a3..2234f8a3de 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@sigign.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@sigign.c.snap @@ -11,7 +11,7 @@ expression: cat tests/snapshots/os-specific/sigign.linux.rs unused_mut )] pub type __sighandler_t = Option ()>; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn foo() { let mut handler: Option ()> = ::core::mem::transmute::< diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@typedefidx.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@typedefidx.c.snap index 26ebfca502..0c1ceda062 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@typedefidx.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@typedefidx.c.snap @@ -13,7 +13,7 @@ expression: cat tests/snapshots/os-specific/typedefidx.linux.rs pub type size_t = usize; pub type __uint64_t = u64; pub type uint64_t = __uint64_t; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn index_typedef(mut arr: *mut uint64_t) { let mut foo3: size_t = *arr.offset(25 as ::core::ffi::c_int as isize) as size_t; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@types.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@types.c.snap index 677da7e207..316d7842db 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@types.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@types.c.snap @@ -33,35 +33,35 @@ pub type uintptr_t = usize; pub type intmax_t = ::libc::intmax_t; pub type uintmax_t = ::libc::uintmax_t; pub type ptrdiff_t = isize; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut intvar: ::core::ffi::c_int = 0 as ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut sizevar: size_t = 0 as size_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut ssizevar: ssize_t = 0 as ssize_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut intptrvar: intptr_t = 0 as intptr_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uintptrvar: uintptr_t = 0 as uintptr_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut ptrdiffvar: ptrdiff_t = 0 as ptrdiff_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uint8var: uint8_t = 0 as uint8_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uint16var: uint16_t = 0 as uint16_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uint32var: uint32_t = 0 as uint32_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uint64var: uint64_t = 0 as uint64_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut int8var: int8_t = 0 as int8_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut int16var: int16_t = 0 as int16_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut int32var: int32_t = 0 as int32_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut int64var: int64_t = 0 as int64_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut maxvar: intmax_t = 0 as intmax_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut umaxvar: uintmax_t = 0 as uintmax_t; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap index df369f0b9b..6ca3cc9c08 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap @@ -16,16 +16,16 @@ extern "C" { } pub type size_t = usize; pub type wchar_t = ::libc::wchar_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_array: [wchar_t; 2] = unsafe { ::core::mem::transmute::<[u8; 8], [wchar_t; 2]>(*b"x\0\0\0\0\0\0\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_array_longer: [wchar_t; 3] = unsafe { ::core::mem::transmute::<[u8; 12], [wchar_t; 3]>(*b"x\0\0\0\0\0\0\0\0\0\0\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_array_shorter: [wchar_t; 1] = unsafe { ::core::mem::transmute::<[u8; 4], [wchar_t; 1]>(*b"x\0\0\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn func() { let mut array: [wchar_t; 2] = ::core::mem::transmute::<[u8; 8], [wchar_t; 2]>(*b"x\0\0\0\0\0\0\0"); diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@out_of_range_lit.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@out_of_range_lit.c.snap index 6f632476d4..1910c4733f 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@out_of_range_lit.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@out_of_range_lit.c.snap @@ -11,7 +11,7 @@ expression: cat tests/snapshots/out_of_range_lit.rs unused_mut )] pub type int32_t = i32; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn f() { let mut a: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; let mut b: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap index a6e6004c1f..828ce24563 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap @@ -10,13 +10,13 @@ expression: cat tests/snapshots/os-specific/rotate.macos.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_left_64( mut x: ::core::ffi::c_ulonglong, ) -> ::core::ffi::c_ulonglong { return x.rotate_left(4 as ::core::ffi::c_int as ::core::ffi::c_ulonglong as u32); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_right_64( mut x: ::core::ffi::c_ulonglong, ) -> ::core::ffi::c_ulonglong { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@sigign.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@sigign.c.snap index 0b3b245e7c..8ecc14df01 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@sigign.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@sigign.c.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/os-specific/sigign.macos.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn foo() { let mut handler: Option ()> = ::core::mem::transmute::< diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@typedefidx.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@typedefidx.c.snap index 990a16e2d5..44d1433be1 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@typedefidx.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@typedefidx.c.snap @@ -13,7 +13,7 @@ expression: cat tests/snapshots/os-specific/typedefidx.macos.rs pub type __darwin_size_t = usize; pub type size_t = __darwin_size_t; pub type uint64_t = u64; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn index_typedef(mut arr: *mut uint64_t) { let mut foo3: size_t = *arr.offset(25 as ::core::ffi::c_int as isize) as size_t; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@types.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@types.c.snap index 72902d4ff0..417aa26cc8 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@types.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@types.c.snap @@ -28,35 +28,35 @@ pub type intmax_t = ::libc::intmax_t; pub type uintmax_t = ::libc::uintmax_t; pub type ptrdiff_t = __darwin_ptrdiff_t; pub type ssize_t = __darwin_ssize_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut intvar: ::core::ffi::c_int = 0 as ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut sizevar: size_t = 0 as size_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut ssizevar: ssize_t = 0 as ssize_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut intptrvar: intptr_t = 0 as intptr_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uintptrvar: uintptr_t = 0 as uintptr_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut ptrdiffvar: ptrdiff_t = 0 as ptrdiff_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uint8var: uint8_t = 0 as uint8_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uint16var: uint16_t = 0 as uint16_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uint32var: uint32_t = 0 as uint32_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut uint64var: uint64_t = 0 as uint64_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut int8var: int8_t = 0 as int8_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut int16var: int16_t = 0 as int16_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut int32var: int32_t = 0 as int32_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut int64var: int64_t = 0 as int64_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut maxvar: intmax_t = 0 as intmax_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut umaxvar: uintmax_t = 0 as uintmax_t; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap index 0c7143b160..536113669b 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap @@ -18,16 +18,16 @@ pub type __darwin_size_t = usize; pub type __darwin_wchar_t = ::libc::wchar_t; pub type size_t = __darwin_size_t; pub type wchar_t = __darwin_wchar_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_array: [wchar_t; 2] = unsafe { ::core::mem::transmute::<[u8; 8], [wchar_t; 2]>(*b"x\0\0\0\0\0\0\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_array_longer: [wchar_t; 3] = unsafe { ::core::mem::transmute::<[u8; 12], [wchar_t; 3]>(*b"x\0\0\0\0\0\0\0\0\0\0\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_array_shorter: [wchar_t; 1] = unsafe { ::core::mem::transmute::<[u8; 4], [wchar_t; 1]>(*b"x\0\0\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn func() { let mut array: [wchar_t; 2] = ::core::mem::transmute::<[u8; 8], [wchar_t; 2]>(*b"x\0\0\0\0\0\0\0"); diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap index 13d29217fb..d829592033 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap @@ -23,7 +23,7 @@ pub struct vm_t { } pub type byte = ::core::ffi::c_uchar; pub const MAX_VMMAIN_ARGS: ::core::ffi::c_int = 50 as ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn VM_CallCompiled( mut vm: *mut vm_t, mut args: *mut ::core::ffi::c_int, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap index e9af932706..2523ccb6ae 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap @@ -10,9 +10,9 @@ expression: cat tests/snapshots/alloca.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub static mut TRUE: ::core::ffi::c_int = 1 as ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn alloca_sum( mut val1: ::core::ffi::c_int, mut val2: ::core::ffi::c_int, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap index 4ab9348929..74e29c7b60 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap @@ -34,16 +34,16 @@ pub struct C2Rust_Unnamed_2 { pub x: ::core::ffi::c_short, pub y: ::core::ffi::c_int, } -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_char_array: [::core::ffi::c_char; 9] = unsafe { ::core::mem::transmute::<[u8; 9], [::core::ffi::c_char; 9]>(*b"mystring\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_char_ptr: *mut ::core::ffi::c_char = b"mystring\0".as_ptr() as *const ::core::ffi::c_char as *mut ::core::ffi::c_char; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_void_ptr: *mut ::core::ffi::c_void = unsafe { &raw const static_char_array as *mut ::core::ffi::c_char as *mut ::core::ffi::c_void }; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn entry() { let mut int_2d: [[::core::ffi::c_int; 1]; 1] = [[1 as ::core::ffi::c_int]]; int_2d[0 as ::core::ffi::c_int as usize][0 as ::core::ffi::c_int as usize] += @@ -102,7 +102,7 @@ pub unsafe extern "C" fn entry() { as *mut ::core::ffi::c_char; past_end = static_char_ptr.offset(8 as ::core::ffi::c_int as isize) as *mut ::core::ffi::c_char; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn short_initializer() { let mut empty_brackets: [::core::ffi::c_int; 16] = [0; 16]; let mut brackets_with_zero: [::core::ffi::c_int; 16] = [0 as ::core::ffi::c_int; 16]; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.snap index aac31e1256..54720d61a5 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.snap @@ -11,18 +11,18 @@ expression: cat tests/snapshots/compound_literals.rs unused_mut )] #![feature(raw_ref_op)] -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_single_int: ::core::ffi::c_int = 42; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_single_int_ptr: *mut ::core::ffi::c_int = &42 as *const ::core::ffi::c_int as *mut ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_int_ptr_to_array: *mut ::core::ffi::c_int = [42, 9001].as_ptr() as *mut ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_volatile_int_ptr_to_array: *mut ::core::ffi::c_int = [42, 9001].as_ptr() as *mut ::core::ffi::c_int as *mut ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut static_int_array_ptr: *mut [::core::ffi::c_int; 2] = &[42, 9001] as *const [::core::ffi::c_int; 2] as *mut [::core::ffi::c_int; 2]; pub const SINGLE_INT: ::core::ffi::c_int = 42 as ::core::ffi::c_int; @@ -30,7 +30,7 @@ pub const INT_ARRAY: [::core::ffi::c_int; 2] = [42 as ::core::ffi::c_int, 9001 as ::core::ffi::c_int]; pub const CHAR_ARRAY: [::core::ffi::c_char; 6] = unsafe { ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn local_compound_literals() { let mut single_int: ::core::ffi::c_int = 42 as ::core::ffi::c_int; let mut c2rust_fresh0: ::core::ffi::c_int = 42 as ::core::ffi::c_int; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap index f0a7c1a55e..ba486bc4d9 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap @@ -15,7 +15,7 @@ expression: cat tests/snapshots/empty_init.rs pub struct Scope { pub next: *mut Scope, } -#[no_mangle] +#[unsafe(no_mangle)] pub static mut scope: *mut Scope = &Scope { next: ::core::ptr::null::() as *mut Scope, } as *const Scope as *mut Scope; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap index 62f06f5636..06f036e617 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/factorial.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn factorial(mut n: ::core::ffi::c_ushort) -> ::core::ffi::c_ushort { let mut result: ::core::ffi::c_ushort = 1 as ::core::ffi::c_ushort; let mut i: ::core::ffi::c_ushort = 1 as ::core::ffi::c_ushort; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap index 8ca5e31889..565b1c8e05 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/gotos.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn sum(mut count: ::core::ffi::c_int) -> ::core::ffi::c_int { let mut x: ::core::ffi::c_int = 0 as ::core::ffi::c_int; while !(count <= 0 as ::core::ffi::c_int) { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.snap index 99c3a56063..1e0c9e98f5 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/insertion.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn insertion_sort(n: ::core::ffi::c_int, p: *mut ::core::ffi::c_int) { let mut i: ::core::ffi::c_int = 1 as ::core::ffi::c_int; while i < n { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap index 341adbc290..1ef0062235 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap @@ -14,7 +14,7 @@ pub type ZSTD_dParameter = ::core::ffi::c_uint; pub const ZSTD_d_experimentalParam1: ZSTD_dParameter = 1000; pub const ZSTD_d_windowLogMax: ZSTD_dParameter = 100; pub const ZSTD_d_format: ::core::ffi::c_uint = 1000 as ::core::ffi::c_uint; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn ZSTD_dParam_getBounds(mut dParam: ZSTD_dParameter) -> ::core::ffi::c_int { let mut bounds: ::core::ffi::c_int = 0 as ::core::ffi::c_int; match dParam as ::core::ffi::c_uint { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.snap index d7973f9f3e..a580bdf83b 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/predefined.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn predefined() { let mut line: ::core::ffi::c_int = 2 as ::core::ffi::c_int; let mut file_name: *const ::core::ffi::c_char = @@ -18,7 +18,7 @@ pub unsafe extern "C" fn predefined() { let mut func: *const ::core::ffi::c_char = b"predefined\0".as_ptr() as *const ::core::ffi::c_char; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn extension_operator() -> *const ::core::ffi::c_char { return b"const char *extension_operator()\0".as_ptr() as *const ::core::ffi::c_char; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.snap index 1bfdda5099..5becb36fb8 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.snap @@ -63,7 +63,7 @@ pub union NestedStructInUnion { pub struct InsideUnion { pub yup: ::core::ffi::c_int, } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn struct_declaration() { let mut value: ::core::ffi::c_int = VALUE2 as ::core::ffi::c_int; let mut a: AnonEnumInStruct = AnonEnumInStruct {}; @@ -74,7 +74,7 @@ pub unsafe extern "C" fn struct_declaration() { let mut c: NestedStructInStruct = NestedStructInStruct {}; let mut d: InsideStruct = InsideStruct { yup: 0 }; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn union_declaration() { let mut value: ::core::ffi::c_int = VALUE4 as ::core::ffi::c_int; let mut a: AnonEnumInUnion = AnonEnumInUnion { a: 0 }; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap index 84fbe244f2..3a07799afc 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap @@ -16,7 +16,7 @@ expression: cat tests/snapshots/ref_ub.rs pub struct Foo { pub len: ::core::ffi::c_int, } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dec(mut f: *mut Foo) { (*f).len -= 1; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap index 2b69069fd2..0064e22f34 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap @@ -10,27 +10,27 @@ expression: cat tests/snapshots/rotate.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_left_8(mut x: ::core::ffi::c_uchar) -> ::core::ffi::c_uchar { return x.rotate_left(1 as ::core::ffi::c_int as ::core::ffi::c_uchar as u32); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_left_16(mut x: ::core::ffi::c_ushort) -> ::core::ffi::c_ushort { return x.rotate_left(2 as ::core::ffi::c_int as ::core::ffi::c_ushort as u32); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_left_32(mut x: ::core::ffi::c_uint) -> ::core::ffi::c_uint { return x.rotate_left(3 as ::core::ffi::c_int as ::core::ffi::c_uint as u32); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_right_8(mut x: ::core::ffi::c_uchar) -> ::core::ffi::c_uchar { return x.rotate_right(1 as ::core::ffi::c_int as ::core::ffi::c_uchar as u32); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_right_16(mut x: ::core::ffi::c_ushort) -> ::core::ffi::c_ushort { return x.rotate_right(2 as ::core::ffi::c_int as ::core::ffi::c_ushort as u32); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn rotate_right_32(mut x: ::core::ffi::c_uint) -> ::core::ffi::c_uint { return x.rotate_right(3 as ::core::ffi::c_int as ::core::ffi::c_uint as u32); } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.snap index f5bee77eb3..bfb484796e 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.snap @@ -10,5 +10,5 @@ expression: cat tests/snapshots/static_assert.rs unused_assignments, unused_mut )] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn static_assert() {} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap index c2ec0cabd9..0236052297 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap @@ -21,21 +21,21 @@ pub struct alpn_spec { pub entries: [[::core::ffi::c_char; 10]; 3], pub count: ::core::ffi::c_uint, } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn ptr() { let mut _s: *const ::core::ffi::c_char = b"hello\0".as_ptr() as *const ::core::ffi::c_char; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn array_deduced_length() { let mut _s: [::core::ffi::c_char; 6] = ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0"); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn array() { let mut _s: [::core::ffi::c_char; 10] = ::core::mem::transmute::<[u8; 10], [::core::ffi::c_char; 10]>(*b"hello\0\0\0\0\0"); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn int_array_extra_braces() { let mut _a: [[::core::ffi::c_int; 10]; 3] = [ [ @@ -54,16 +54,16 @@ pub unsafe extern "C" fn int_array_extra_braces() { [0; 10], ]; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn ptr_extra_braces() { let mut _s: *const ::core::ffi::c_char = b"hello\0".as_ptr() as *const ::core::ffi::c_char; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn array_extra_braces() { let _s: [::core::ffi::c_char; 10] = ::core::mem::transmute::<[u8; 10], [::core::ffi::c_char; 10]>(*b"hello\0\0\0\0\0"); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn array_of_ptrs() { let mut _s: [*const ::core::ffi::c_char; 3] = [ b"hello\0".as_ptr() as *const ::core::ffi::c_char, @@ -71,7 +71,7 @@ pub unsafe extern "C" fn array_of_ptrs() { ::core::ptr::null::<::core::ffi::c_char>(), ]; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn array_of_arrays() { let mut _s: [[::core::ffi::c_char; 10]; 3] = [ ::core::mem::transmute::<[u8; 10], [::core::ffi::c_char; 10]>(*b"hello\0\0\0\0\0"), @@ -79,7 +79,7 @@ pub unsafe extern "C" fn array_of_arrays() { [0; 10], ]; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn array_of_arrays_static() { static mut _S: [[::core::ffi::c_char; 10]; 3] = unsafe { [ @@ -89,7 +89,7 @@ pub unsafe extern "C" fn array_of_arrays_static() { ] }; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn array_of_arrays_static_struct() { static mut _S: s = unsafe { s { @@ -101,7 +101,7 @@ pub unsafe extern "C" fn array_of_arrays_static_struct() { } }; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn curl_alpn_spec() { static mut _ALPN_SPEC_H11: alpn_spec = alpn_spec { entries: [ALPN_HTTP_1_1, [0; 10], [0; 10]], From ec99a1b2de9ed0b9a6ce68f11a818087fd027eab Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 23:15:18 -0800 Subject: [PATCH 11/39] transpile: emit `unsafe extern "C" }` for edition 2024 --- c2rust-ast-builder/src/builder.rs | 2 +- c2rust-transpile/src/translator/mod.rs | 28 +++++++++++++++++-- c2rust-transpile/tests/snapshots.rs | 17 ++++++++--- ...ots__transpile-linux@call_only_once.c.snap | 4 +-- .../snapshots__transpile-linux@macros.c.snap | 8 +++--- .../snapshots__transpile-linux@rnd.c.snap | 8 +++--- ...shots__transpile-linux@wide_strings.c.snap | 2 +- ...ots__transpile-macos@call_only_once.c.snap | 4 +-- .../snapshots__transpile-macos@macros.c.snap | 8 +++--- .../snapshots__transpile-macos@rnd.c.snap | 8 +++--- ...shots__transpile-macos@wide_strings.c.snap | 2 +- .../snapshots__transpile@exprs.c.snap | 10 +++---- 12 files changed, 66 insertions(+), 35 deletions(-) diff --git a/c2rust-ast-builder/src/builder.rs b/c2rust-ast-builder/src/builder.rs index 431173af23..316044d4cf 100644 --- a/c2rust-ast-builder/src/builder.rs +++ b/c2rust-ast-builder/src/builder.rs @@ -1684,7 +1684,7 @@ impl Builder { Box::new(Item::ForeignMod(ItemForeignMod { attrs: self.attrs, - unsafety: None, + unsafety: self.unsafety.to_token(), brace_token: token::Brace(self.span), items, abi, diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 4a63594d7c..612fc5895d 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -371,6 +371,15 @@ pub fn stmts_block(mut stmts: Vec) -> Block { mk().block(stmts) } +/// Whether `extern` blocks can be `unsafe` in this edition. +fn extern_block_unsafety(edition: RustEdition) -> Unsafety { + if edition >= RustEdition::Rust2024 { + Unsafety::Unsafe + } else { + Unsafety::Normal + } +} + /// Generate link attributes needed to ensure that the generated Rust libraries have the right symbol values. fn mk_linkage( in_extern_block: bool, @@ -1013,6 +1022,7 @@ pub fn translate( &mut new_uses, &t.mod_names, tcfg.reorganize_definitions, + tcfg.edition, ); let comments = t.comment_context.get_remaining_comments(*file_id); submodule.set_span(match t.comment_store.borrow_mut().add_comments(&comments) { @@ -1084,7 +1094,11 @@ pub fn translate( all_items.extend(new_uses.into_items()); if !foreign_items.is_empty() { - all_items.push(mk().extern_("C").foreign_items(foreign_items)); + all_items.push( + mk().unsafety(extern_block_unsafety(t.tcfg.edition)) + .extern_("C") + .foreign_items(foreign_items), + ); } // Add the items accumulated @@ -1235,6 +1249,7 @@ fn make_submodule( use_item_store: &mut ItemStore, mod_names: &RefCell>, reorganize_definitions: bool, + edition: RustEdition, ) -> Box { let (mut items, foreign_items, uses) = item_store.drain(); let file_path = ast_context.get_file_path(file_id); @@ -1307,7 +1322,11 @@ fn make_submodule( } if !foreign_items.is_empty() { - items.push(mk().extern_("C").foreign_items(foreign_items)); + items.push( + mk().unsafety(extern_block_unsafety(edition)) + .extern_("C") + .foreign_items(foreign_items), + ); } let module_builder = mk().pub_(); @@ -3021,7 +3040,10 @@ impl<'c> Translation<'c> { let items = match self.convert_decl(ctx, decl_id)? { Item(item) => vec![item], ForeignItem(item) => { - vec![mk().extern_("C").foreign_items(vec![*item])] + vec![mk() + .unsafety(extern_block_unsafety(self.tcfg.edition)) + .extern_("C") + .foreign_items(vec![*item])] } Items(items) => items, NoItem => return Ok(cfg::DeclStmtInfo::empty()), diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index f941334d12..dba884c127 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -310,7 +310,7 @@ fn test_empty_init() { #[test] fn test_exprs() { - transpile("exprs.c").run(); + transpile("exprs.c").edition(RustEdition::Rust2024).run(); } #[test] @@ -415,12 +415,18 @@ fn test_vm_x86() { #[test] fn test_call_only_once() { - transpile("call_only_once.c").os_specific(true).run(); + transpile("call_only_once.c") + .edition(RustEdition::Rust2024) + .os_specific(true) + .run(); } #[test] fn test_macros_os_specific() { - transpile("macros.c").os_specific(true).run(); + transpile("macros.c") + .edition(RustEdition::Rust2024) + .os_specific(true) + .run(); } #[test] @@ -433,7 +439,10 @@ fn test_out_of_range_lit() { #[test] fn test_rnd() { - transpile("rnd.c").os_specific(true).run(); + transpile("rnd.c") + .edition(RustEdition::Rust2024) + .os_specific(true) + .run(); } #[test] diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@call_only_once.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@call_only_once.c.snap index 435701bb75..cd9bdc4fed 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@call_only_once.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@call_only_once.c.snap @@ -11,7 +11,7 @@ expression: cat tests/snapshots/os-specific/call_only_once.linux.rs unused_mut )] #![feature(label_break_value)] -extern "C" { +unsafe extern "C" { fn __assert_fail( __assertion: *const ::core::ffi::c_char, __file: *const ::core::ffi::c_char, @@ -29,7 +29,7 @@ unsafe extern "C" fn called_only_once() -> ::core::ffi::c_int { called += 1; return 1 as ::core::ffi::c_int; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn assert_call_only_once() -> ::core::ffi::c_int { '_c2rust_label: { if called_only_once() != 0 { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap index e50cdbea74..ea833a9cff 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/os-specific/macros.linux.rs unused_assignments, unused_mut )] -extern "C" { +unsafe extern "C" { fn __errno_location() -> *mut ::core::ffi::c_int; fn memcpy( __dest: *mut ::core::ffi::c_void, @@ -19,11 +19,11 @@ extern "C" { ) -> *mut ::core::ffi::c_void; } pub type size_t = usize; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn errno_is_error() -> bool { return *__errno_location() != 0 as ::core::ffi::c_int; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn size_of_const() -> ::core::ffi::c_int { let mut a: [::core::ffi::c_int; 10] = [0; 10]; return SIZE as ::core::ffi::c_int; @@ -31,7 +31,7 @@ pub unsafe extern "C" fn size_of_const() -> ::core::ffi::c_int { pub const SIZE: usize = ::core::mem::size_of::<[::core::ffi::c_int; 10]>(); pub const POS: [::core::ffi::c_char; 3] = unsafe { ::core::mem::transmute::<[u8; 3], [::core::ffi::c_char; 3]>(*b"\"]\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut ::core::ffi::c_char) { memcpy( out as *mut ::core::ffi::c_void, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap index 3bbc7b3efd..6810985c93 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap @@ -10,20 +10,20 @@ expression: cat tests/snapshots/os-specific/rnd.linux.rs unused_assignments, unused_mut )] -extern "C" { +unsafe extern "C" { fn abs(__x: ::core::ffi::c_int) -> ::core::ffi::c_int; } pub type __int32_t = i32; pub type __uint32_t = u32; pub type int32_t = __int32_t; pub type uint32_t = __uint32_t; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut cur_rand_seed: uint32_t = 0 as uint32_t; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { cur_rand_seed = s; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn get_rand_seed() -> uint32_t { let INCREMENT: uint32_t = 1 as uint32_t; let MULTIPLIER: uint32_t = 0x15a4e35 as uint32_t; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap index 6ca3cc9c08..f630ebd673 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap @@ -11,7 +11,7 @@ expression: cat tests/snapshots/os-specific/wide_strings.linux.rs unused_mut )] #![feature(raw_ref_op)] -extern "C" { +unsafe extern "C" { fn wcslen(__s: *const wchar_t) -> ::core::ffi::c_ulong; } pub type size_t = usize; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@call_only_once.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@call_only_once.c.snap index ed291d834f..415ccb92b0 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@call_only_once.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@call_only_once.c.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/os-specific/call_only_once.macos.rs unused_assignments, unused_mut )] -extern "C" { +unsafe extern "C" { fn __assert_rtn( _: *const ::core::ffi::c_char, _: *const ::core::ffi::c_char, @@ -23,7 +23,7 @@ unsafe extern "C" fn called_only_once() -> ::core::ffi::c_int { called += 1; return 1 as ::core::ffi::c_int; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn assert_call_only_once() -> ::core::ffi::c_int { if (called_only_once() == 0) as ::core::ffi::c_int as ::core::ffi::c_long != 0 { __assert_rtn( diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap index 8bbcc1d383..8c0118c15e 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/os-specific/macros.macos.rs unused_assignments, unused_mut )] -extern "C" { +unsafe extern "C" { fn __error() -> *mut ::core::ffi::c_int; fn memcpy( __dst: *mut ::core::ffi::c_void, @@ -20,11 +20,11 @@ extern "C" { } pub type __darwin_size_t = usize; pub type size_t = __darwin_size_t; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn errno_is_error() -> bool { return *__error() != 0 as ::core::ffi::c_int; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn size_of_const() -> ::core::ffi::c_int { let mut a: [::core::ffi::c_int; 10] = [0; 10]; return SIZE as ::core::ffi::c_int; @@ -32,7 +32,7 @@ pub unsafe extern "C" fn size_of_const() -> ::core::ffi::c_int { pub const SIZE: usize = ::core::mem::size_of::<[::core::ffi::c_int; 10]>(); pub const POS: [::core::ffi::c_char; 3] = unsafe { ::core::mem::transmute::<[u8; 3], [::core::ffi::c_char; 3]>(*b"\"]\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut ::core::ffi::c_char) { memcpy( out as *mut ::core::ffi::c_void, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap index 991b0eab39..fac56a445c 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap @@ -10,18 +10,18 @@ expression: cat tests/snapshots/os-specific/rnd.macos.rs unused_assignments, unused_mut )] -extern "C" { +unsafe extern "C" { fn abs(_: ::core::ffi::c_int) -> ::core::ffi::c_int; } pub type int32_t = i32; pub type uint32_t = u32; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut cur_rand_seed: uint32_t = 0 as uint32_t; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { cur_rand_seed = s; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn get_rand_seed() -> uint32_t { let INCREMENT: uint32_t = 1 as uint32_t; let MULTIPLIER: uint32_t = 0x15a4e35 as uint32_t; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap index 536113669b..b1e1075406 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap @@ -11,7 +11,7 @@ expression: cat tests/snapshots/os-specific/wide_strings.macos.rs unused_mut )] #![feature(raw_ref_op)] -extern "C" { +unsafe extern "C" { fn wcslen(_: *const wchar_t) -> ::core::ffi::c_ulong; } pub type __darwin_size_t = usize; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap index 587de397a2..73bfee8e43 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap @@ -11,7 +11,7 @@ expression: cat tests/snapshots/exprs.rs unused_mut )] #![feature(label_break_value, raw_ref_op)] -extern "C" { +unsafe extern "C" { fn puts(str: *const ::core::ffi::c_char) -> ::core::ffi::c_int; } pub type C2Rust_Unnamed = ::core::ffi::c_uint; @@ -22,7 +22,7 @@ unsafe extern "C" fn side_effect() -> ::core::ffi::c_int { puts(b"the return of side effect\0".as_ptr() as *const ::core::ffi::c_char); return 10 as ::core::ffi::c_int; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn unary_without_side_effect() { let mut i: ::core::ffi::c_int = 5 as ::core::ffi::c_int; -i; @@ -36,7 +36,7 @@ pub unsafe extern "C" fn unary_without_side_effect() { i -= 1; i += 1; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn unary_with_side_effect() { let mut arr: [*mut ::core::ffi::c_char; 1] = [::core::ptr::null_mut::<::core::ffi::c_char>()]; -side_effect(); @@ -51,11 +51,11 @@ pub unsafe extern "C" fn unary_with_side_effect() { arr[side_effect() as usize] = arr[side_effect() as usize].offset(1); arr[side_effect() as usize] = arr[side_effect() as usize].offset(-1); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn compound_literal() { let mut i: ::core::ffi::c_int = B as ::core::ffi::c_int; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn statement_expr() { '_c2rust_label: { puts(b"should execute\0".as_ptr() as *const ::core::ffi::c_char); From e30779a59974b4812fbc2b5996ab4d7d1ea87348 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 23:20:20 -0800 Subject: [PATCH 12/39] transpile: emit `#[unsafe(export_name = "")]` and `#[unsafe(link_section = "")]` in edition 2024 This reworks the previous basic support for `#[unsafe(no_mangle)]` to make it more robust. `fn Builder::meta` is added, which checks `self.unsafety`, and if it's `unsafe`, adds a wrapper `unsafe()` around the meta. This is then used in all of the `mk().meta_*` functions to ensure they use the `unsafe` wrapping. It has to be done at this level because sometimes these `unsafe`s aren't at the top-level, like `#[cfg_attr(target_os = "linux", unsafe(link_section = ".init_array"))]`. --- c2rust-ast-builder/src/builder.rs | 41 ++++--- c2rust-transpile/src/translator/mod.rs | 32 ++++-- c2rust-transpile/tests/snapshots.rs | 2 +- .../snapshots__transpile@macros.c.snap | 104 +++++++++--------- 4 files changed, 100 insertions(+), 79 deletions(-) diff --git a/c2rust-ast-builder/src/builder.rs b/c2rust-ast-builder/src/builder.rs index 316044d4cf..d377d0c940 100644 --- a/c2rust-ast-builder/src/builder.rs +++ b/c2rust-ast-builder/src/builder.rs @@ -1,11 +1,11 @@ //! Helpers for building AST nodes. Normally used by calling `mk().some_node(args...)`. -use std::str; - use itertools::intersperse; use proc_macro2::{Literal, Punct, Spacing, Span, TokenStream, TokenTree}; use std::default::Default; use std::iter::FromIterator; +use std::mem; +use std::str; use syn::{__private::ToTokens, punctuated::Punctuated, *}; pub mod properties { @@ -49,11 +49,12 @@ pub mod properties { } } - #[derive(Debug, Clone)] + #[derive(Debug, Clone, Copy)] pub enum Unsafety { Normal, Unsafe, } + impl ToToken for Unsafety { type Token = Token![unsafe]; fn to_token(&self) -> Option { @@ -513,7 +514,7 @@ impl Builder { K: Make, V: Make, { - let meta = mk().meta_namevalue(key, value); + let meta = self.clone().meta_namevalue(key, value); self.prepared_attr(meta) } @@ -521,7 +522,7 @@ impl Builder { where K: Make, { - let meta = mk().meta_path(key); + let meta = self.clone().meta_path(key); self.prepared_attr(meta) } @@ -530,7 +531,7 @@ impl Builder { K: Make, V: Make, { - let meta = mk().meta_list(func, arguments); + let meta = self.clone().meta_list(func, arguments); self.prepared_attr(meta) } @@ -1951,6 +1952,13 @@ impl Builder { } } + pub fn meta(self, meta: Meta) -> Meta { + match self.unsafety { + Unsafety::Normal => meta, + Unsafety::Unsafe => mk().meta_list("unsafe", vec![meta]), + } + } + /// makes a meta item with just a path /// # Examples /// @@ -1960,7 +1968,7 @@ impl Builder { Pa: Make, { let path = path.make(&self); - Meta::Path(path) + self.meta(Meta::Path(path)) } /// makes a meta item with the given path and some arguments @@ -1974,18 +1982,19 @@ impl Builder { { let path = path.make(&self); let args = args.make(&self); - Meta::List(MetaList { + let span = self.span; + self.meta(Meta::List(MetaList { path, - delimiter: MacroDelimiter::Paren(token::Paren(self.span)), + delimiter: MacroDelimiter::Paren(token::Paren(span)), tokens: args, - }) + })) } /// makes a meta item with key value argument /// # Examples /// /// mk().meta_namevalue("target_os", "linux") // -> `target_os = "linux"` - pub fn meta_namevalue(self, key: K, value: V) -> Meta + pub fn meta_namevalue(mut self, key: K, value: V) -> Meta where K: Make, V: Make, @@ -1993,15 +2002,15 @@ impl Builder { let key = key.make(&self); let lit = value.make(&self); let value = Expr::Lit(ExprLit { - attrs: self.attrs, + attrs: mem::take(&mut self.attrs), lit, }); - - Meta::NameValue(MetaNameValue { + let span = self.span; + self.meta(Meta::NameValue(MetaNameValue { path: key, - eq_token: Token![=](self.span), + eq_token: Token![=](span), value, - }) + })) } pub fn empty_mac(self, path: Pa, delim: MacroDelimiter) -> Macro diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 612fc5895d..106e0b3d05 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -380,6 +380,15 @@ fn extern_block_unsafety(edition: RustEdition) -> Unsafety { } } +/// Whether attributes can be `unsafe` in this edition. +fn attr_unsafety(edition: RustEdition) -> Unsafety { + if edition >= RustEdition::Rust2024 { + Unsafety::Unsafe + } else { + Unsafety::Normal + } +} + /// Generate link attributes needed to ensure that the generated Rust libraries have the right symbol values. fn mk_linkage( in_extern_block: bool, @@ -390,15 +399,15 @@ fn mk_linkage( if new_name == old_name { if in_extern_block { mk() // There is no mangling by default in extern blocks anymore - } else if edition >= RustEdition::Rust2024 { - mk().call_attr("unsafe", vec!["no_mangle"]) } else { - mk().single_attr("no_mangle") // Don't touch my name Rust! + mk().unsafety(attr_unsafety(edition)) + .single_attr("no_mangle") // Don't touch my name Rust! } } else if in_extern_block { mk().str_attr("link_name", old_name) // Look for this name } else { - mk().str_attr("export_name", old_name) // Make sure you actually name it this + mk().unsafety(attr_unsafety(edition)) + .str_attr("export_name", old_name) // Make sure you actually name it this } } @@ -1856,21 +1865,24 @@ impl<'c> Translation<'c> { "cfg_attr", vec![ mk().meta_namevalue("target_os", "linux"), - mk().meta_namevalue("link_section", ".init_array"), + mk().unsafety(attr_unsafety(self.tcfg.edition)) + .meta_namevalue("link_section", ".init_array"), ], ) .call_attr( "cfg_attr", vec![ mk().meta_namevalue("target_os", "windows"), - mk().meta_namevalue("link_section", ".CRT$XIB"), + mk().unsafety(attr_unsafety(self.tcfg.edition)) + .meta_namevalue("link_section", ".CRT$XIB"), ], ) .call_attr( "cfg_attr", vec![ mk().meta_namevalue("target_os", "macos"), - mk().meta_namevalue("link_section", "__DATA,__mod_init_func"), + mk().unsafety(attr_unsafety(self.tcfg.edition)) + .meta_namevalue("link_section", "__DATA,__mod_init_func"), ], ); let static_array_size = mk().lit_expr(mk().int_unsuffixed_lit(1)); @@ -2265,9 +2277,9 @@ impl<'c> Translation<'c> { for attr in attrs { static_def = match attr { c_ast::Attribute::Used => static_def.single_attr("used"), - c_ast::Attribute::Section(name) => { - static_def.str_attr("link_section", name) - } + c_ast::Attribute::Section(name) => static_def + .unsafety(attr_unsafety(self.tcfg.edition)) + .str_attr("link_section", name), _ => continue, } } diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index dba884c127..fdabe0f1de 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -354,7 +354,7 @@ fn test_macrocase() { #[test] fn test_macros() { - transpile("macros.c").run(); + transpile("macros.c").edition(RustEdition::Rust2024).run(); } #[test] diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap index 8220f6880e..244389b91c 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap @@ -11,7 +11,7 @@ expression: cat tests/snapshots/macros.rs unused_mut )] #![feature(raw_ref_op)] -extern "C" { +unsafe extern "C" { fn extern_fn() -> ::core::ffi::c_int; } pub type uintptr_t = usize; @@ -60,7 +60,7 @@ pub const PTR_ARITHMETIC: *const ::core::ffi::c_char = unsafe { }; pub const WIDENING_CAST: ::core::ffi::c_ulonglong = LITERAL_INT as ::core::ffi::c_ulonglong; pub const CONVERSION_CAST: ::core::ffi::c_double = LITERAL_INT as ::core::ffi::c_double; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn local_muts() { let mut literal_int: ::core::ffi::c_int = LITERAL_INT; let mut literal_bool: bool = LITERAL_BOOL != 0; @@ -132,7 +132,7 @@ pub unsafe extern "C" fn local_muts() { mixed }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn local_consts() { let literal_int: ::core::ffi::c_int = LITERAL_INT; let literal_bool: bool = LITERAL_BOOL != 0; @@ -255,91 +255,91 @@ static mut global_static_const_ref_indexing: *const ::core::ffi::c_char = static mut global_static_const_ref_struct: *const S = &LITERAL_STRUCT as *const S as *mut S; static mut global_static_const_ternary: ::core::ffi::c_int = 0; static mut global_static_const_member: ::core::ffi::c_int = 0; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn global_static_consts() {} -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_literal_int: ::core::ffi::c_int = LITERAL_INT; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_literal_bool: bool = LITERAL_BOOL != 0; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_literal_float: ::core::ffi::c_float = LITERAL_FLOAT as ::core::ffi::c_float; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr(); -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_literal_str: [::core::ffi::c_char; 6] = LITERAL_STR; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_literal_array: [::core::ffi::c_int; 3] = [ 1 as ::core::ffi::c_int, 2 as ::core::ffi::c_int, 3 as ::core::ffi::c_int, ]; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_literal_struct: S = LITERAL_STRUCT; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_nested_int: ::core::ffi::c_int = NESTED_INT; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_nested_bool: bool = NESTED_BOOL != 0; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_nested_float: ::core::ffi::c_float = NESTED_FLOAT as ::core::ffi::c_float; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr(); -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_nested_str: [::core::ffi::c_char; 6] = NESTED_STR; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_nested_array: [::core::ffi::c_int; 3] = [ 1 as ::core::ffi::c_int, 2 as ::core::ffi::c_int, 3 as ::core::ffi::c_int, ]; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_nested_struct: S = NESTED_STRUCT; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double - true_0 as ::core::ffi::c_double) as ::core::ffi::c_float; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_parens: ::core::ffi::c_int = PARENS; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_ptr_arithmetic: *const ::core::ffi::c_char = ::core::ptr::null::<::core::ffi::c_char>(); -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_widening_cast: ::core::ffi::c_ulonglong = WIDENING_CAST; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_narrowing_cast: ::core::ffi::c_char = LITERAL_INT as ::core::ffi::c_char; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_conversion_cast: ::core::ffi::c_double = CONVERSION_CAST; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_indexing: ::core::ffi::c_char = 0; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_str_concatenation_ptr: *const ::core::ffi::c_char = b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_str_concatenation: [::core::ffi::c_char; 18] = unsafe { ::core::mem::transmute::<[u8; 18], [::core::ffi::c_char; 18]>(*b"hello hello world\0") }; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_builtin: ::core::ffi::c_int = (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_ref_indexing: *const ::core::ffi::c_char = ::core::ptr::null::<::core::ffi::c_char>(); -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_ref_struct: *const S = &LITERAL_STRUCT as *const S as *mut S; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_ternary: ::core::ffi::c_int = 0; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut global_const_member: ::core::ffi::c_int = 0; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn test_fn_macro(mut x: ::core::ffi::c_int) -> ::core::ffi::c_int { return x * x; } @@ -348,7 +348,7 @@ pub const TEST_NESTED: ::core::ffi::c_int = 2 as ::core::ffi::c_int; pub const TEST_CONST2: ::core::ffi::c_int = TEST_NESTED; pub const TEST_PARENS: ::core::ffi::c_int = (TEST_CONST2 + 1 as ::core::ffi::c_int) * 3 as ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn reference_define() -> ::core::ffi::c_int { let mut x: ::core::ffi::c_int = TEST_CONST1; x += TEST_CONST2; @@ -357,17 +357,17 @@ pub unsafe extern "C" fn reference_define() -> ::core::ffi::c_int { } return x; } -#[no_mangle] +#[unsafe(no_mangle)] pub static mut fns: fn_ptrs = fn_ptrs { v: ::core::ptr::null::<::core::ffi::c_void>() as *mut ::core::ffi::c_void, fn1: None, fn2: None, }; -#[no_mangle] +#[unsafe(no_mangle)] pub static mut p: *const fn_ptrs = unsafe { &raw const fns }; pub const ZSTD_WINDOWLOG_MAX_32: ::core::ffi::c_int = 30 as ::core::ffi::c_int; pub const ZSTD_WINDOWLOG_MAX_64: ::core::ffi::c_int = 31 as ::core::ffi::c_int; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn test_zstd() -> U64 { return (if ::core::mem::size_of::() as usize == 4 as usize { ZSTD_WINDOWLOG_MAX_32 @@ -375,7 +375,7 @@ pub unsafe extern "C" fn test_zstd() -> U64 { ZSTD_WINDOWLOG_MAX_64 }) as U64; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn stmt_expr_inc() -> ::core::ffi::c_int { let mut a: ::core::ffi::c_int = 0 as ::core::ffi::c_int; let mut b: *mut ::core::ffi::c_int = &raw mut a; @@ -388,7 +388,7 @@ pub unsafe extern "C" fn stmt_expr_inc() -> ::core::ffi::c_int { *b }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn test_switch(mut x: ::core::ffi::c_int) -> ::core::ffi::c_int { match x { TEST_CONST1 => return 10 as ::core::ffi::c_int, @@ -399,36 +399,36 @@ pub unsafe extern "C" fn test_switch(mut x: ::core::ffi::c_int) -> ::core::ffi:: } pub const silk_int16_MIN: ::core::ffi::c_short = 0x8000 as ::core::ffi::c_int as ::core::ffi::c_short; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn test_silk_int16_MIN() -> ::core::ffi::c_int { let mut _null: ::core::ffi::c_char = ::core::mem::transmute::<[u8; 1], [::core::ffi::c_char; 1]>(*b"\0") [(silk_int16_MIN as ::core::ffi::c_int + 0x8000 as ::core::ffi::c_int) as usize]; return silk_int16_MIN as ::core::ffi::c_int; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn use_extern_value() -> ::core::ffi::c_int { return extern_fn(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn local_fn() -> ::core::ffi::c_int { return 1234 as ::core::ffi::c_int; } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn use_local_value() -> ::core::ffi::c_int { return local_fn(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn use_portable_type(mut len: uintptr_t) -> bool { return len <= (UINTPTR_MAX as uintptr_t).wrapping_div(2 as ::core::ffi::c_int as uintptr_t); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn ntlm_v2_blob_len(mut ntlm: *mut ntlmdata) -> ::core::ffi::c_uint { return ((44 as ::core::ffi::c_int - 16 as ::core::ffi::c_int) as ::core::ffi::c_uint) .wrapping_add((*ntlm).target_info_len) .wrapping_add(4 as ::core::ffi::c_uint); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn late_init_var() -> ::core::ffi::c_int { return ({ let mut i: ::core::ffi::c_int = 0; @@ -463,7 +463,7 @@ unsafe extern "C" fn c2rust_run_static_initializers() { global_const_member = LITERAL_STRUCT.i; } #[used] -#[cfg_attr(target_os = "linux", link_section = ".init_array")] -#[cfg_attr(target_os = "windows", link_section = ".CRT$XIB")] -#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")] +#[cfg_attr(target_os = "linux", unsafe(link_section = ".init_array"))] +#[cfg_attr(target_os = "windows", unsafe(link_section = ".CRT$XIB"))] +#[cfg_attr(target_os = "macos", unsafe(link_section = "__DATA,__mod_init_func"))] static INIT_ARRAY: [unsafe extern "C" fn(); 1] = [c2rust_run_static_initializers]; From 9849caef78328d6a5d3bb18e7a6f68a3ce0f61e2 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 23:30:07 -0800 Subject: [PATCH 13/39] transpile: fix some comment typos for `__builtin_arm_yield` --- c2rust-transpile/src/translator/builtins.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c2rust-transpile/src/translator/builtins.rs b/c2rust-transpile/src/translator/builtins.rs index c9dfef4ba3..31ca604dc3 100644 --- a/c2rust-transpile/src/translator/builtins.rs +++ b/c2rust-transpile/src/translator/builtins.rs @@ -416,8 +416,8 @@ impl<'c> Translation<'c> { let fn_name = "__yield"; self.use_feature("stdsimd"); // TODO See #1298. - // In Rust 1.7, `#![feature(stdsimd)]` was removed and split into (at least): - // `#![feature("stdarch_arm_hints")]` and + // In Rust 1.78, `#![feature(stdsimd)]` was removed and split into (at least): + // `#![feature(stdarch_arm_hints)]` and // `#![cfg_attr(target_arch = "arm", feature(stdarch_arm_neon_intrinsics))]`. // self.use_feature("stdarch_arm_hints"); // self.use_feature("stdarch_arm_neon_intrinsics"); // TODO need to add `cfg_attr` support. From c5b7cbc3f71966bb1568e6465fb1e4e5018f2059 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Thu, 5 Mar 2026 23:32:58 -0800 Subject: [PATCH 14/39] transpile: remove/split `#![feature(stdsimd)]` in edition 2024 * Fixes #1298. Luckily, I left comments saying how to do this in more recent Rust versions. We still haven't fixed the missing `cfg_attr` in `#![cfg_attr(target_arch = "arm", stdarch_arm_neon_intrinsics)]` when using `self.use_feature`, but we're not testing `target_arch = "arm"` at the moment. --- c2rust-transpile/src/translator/builtins.rs | 17 ++++++++++------- c2rust-transpile/src/translator/simd.rs | 9 ++++++--- c2rust-transpile/tests/snapshots.rs | 5 ++++- .../snapshots__transpile-aarch64@spin.c.snap | 4 ++-- .../snapshots__transpile-x86_64@spin.c.snap | 3 +-- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/c2rust-transpile/src/translator/builtins.rs b/c2rust-transpile/src/translator/builtins.rs index 31ca604dc3..3de0c7aef1 100644 --- a/c2rust-transpile/src/translator/builtins.rs +++ b/c2rust-transpile/src/translator/builtins.rs @@ -414,13 +414,16 @@ impl<'c> Translation<'c> { "__builtin_arm_yield" => { let fn_name = "__yield"; - self.use_feature("stdsimd"); - // TODO See #1298. - // In Rust 1.78, `#![feature(stdsimd)]` was removed and split into (at least): - // `#![feature(stdarch_arm_hints)]` and - // `#![cfg_attr(target_arch = "arm", feature(stdarch_arm_neon_intrinsics))]`. - // self.use_feature("stdarch_arm_hints"); - // self.use_feature("stdarch_arm_neon_intrinsics"); // TODO need to add `cfg_attr` support. + if self.tcfg.edition < RustEdition::Rust2024 { + self.use_feature("stdsimd"); + } else { + // Edition 2024 was release in Rust 1.85. + // In Rust 1.78, `#![feature(stdsimd)]` was removed and split into (at least): + // `#![feature(stdarch_arm_hints)]` and + // `#![cfg_attr(target_arch = "arm", feature(stdarch_arm_neon_intrinsics))]`. + self.use_feature("stdarch_arm_hints"); + // self.use_feature("stdarch_arm_neon_intrinsics"); // TODO need to add `cfg_attr` support. + } self.import_arch_function("arm", fn_name); self.import_arch_function("aarch64", fn_name); let ident = mk().ident_expr(fn_name); diff --git a/c2rust-transpile/src/translator/simd.rs b/c2rust-transpile/src/translator/simd.rs index 51bb10b3fb..1b3d993066 100644 --- a/c2rust-transpile/src/translator/simd.rs +++ b/c2rust-transpile/src/translator/simd.rs @@ -160,9 +160,12 @@ impl<'c> Translation<'c> { .into()); } - // The majority of x86/64 SIMD is stable, however there are still some - // bits that are behind a feature gate. - self.use_feature("stdsimd"); + if self.tcfg.edition < RustEdition::Rust2024 { + // Edition 2024 was release in Rust 1.85. + // In Rust 1.78, `#![feature(stdsimd)]` was removed and split into individual features. + // All of the x86_64 parts (that we use at least) were stabilized. + self.use_feature("stdsimd"); + } self.with_cur_file_item_store(|item_store| { // REVIEW: Also a linear lookup diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index fdabe0f1de..98ee91835c 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -400,7 +400,10 @@ fn test_str_init() { #[test] fn test_spin() { - transpile("spin.c").arch_specific(true).run(); + transpile("spin.c") + .edition(RustEdition::Rust2024) + .arch_specific(true) + .run(); } #[test] diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@spin.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@spin.c.snap index d14c9d9203..f007e3271a 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@spin.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@spin.c.snap @@ -10,12 +10,12 @@ expression: cat tests/snapshots/arch-specific/spin.aarch64.rs unused_assignments, unused_mut )] -#![feature(stdsimd)] +#![feature(stdarch_arm_hints)] #[cfg(target_arch = "aarch64")] pub use ::core::arch::aarch64::__yield; #[cfg(target_arch = "arm")] pub use ::core::arch::arm::__yield; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn spin() { __yield(); } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@spin.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@spin.c.snap index 7b3453e7bf..bb98231811 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@spin.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@spin.c.snap @@ -10,12 +10,11 @@ expression: cat tests/snapshots/arch-specific/spin.x86_64.rs unused_assignments, unused_mut )] -#![feature(stdsimd)] #[cfg(target_arch = "x86")] pub use ::core::arch::x86::_mm_pause; #[cfg(target_arch = "x86_64")] pub use ::core::arch::x86_64::_mm_pause; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn spin() { _mm_pause(); } From 4a5538936b1f108be3ba93612441a5935b84706f Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 00:06:29 -0800 Subject: [PATCH 15/39] transpile: tests: make the snapshot default edition 2024 now that most tests are switched to it --- c2rust-transpile/tests/snapshots.rs | 114 ++++++++-------------------- 1 file changed, 33 insertions(+), 81 deletions(-) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 98ee91835c..b9302c69ba 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -164,7 +164,7 @@ struct TranspileTest<'a> { fn transpile(c_file_name: &str) -> TranspileTest { TranspileTest { c_file_name, - edition: Default::default(), + edition: RustEdition::Rust2024, arch_specific: false, os_specific: false, expect_compile_error: false, @@ -281,211 +281,162 @@ fn generate_keywords_test() { #[test] fn test_alloca() { - transpile("alloca.c").edition(RustEdition::Rust2024).run(); + transpile("alloca.c").run(); } #[test] fn test_arrays() { - transpile("arrays.c").edition(RustEdition::Rust2024).run(); + transpile("arrays.c").run(); } #[test] fn test_atomics() { - transpile("atomics.c").run(); + transpile("atomics.c").edition(RustEdition::Rust2021).run(); } #[test] fn test_compound_literals() { - transpile("compound_literals.c") - .edition(RustEdition::Rust2024) - .run(); + transpile("compound_literals.c").run(); } #[test] fn test_empty_init() { - transpile("empty_init.c") - .edition(RustEdition::Rust2024) - .run(); + transpile("empty_init.c").run(); } #[test] fn test_exprs() { - transpile("exprs.c").edition(RustEdition::Rust2024).run(); + transpile("exprs.c").run(); } #[test] fn test_factorial() { - transpile("factorial.c") - .edition(RustEdition::Rust2024) - .run(); + transpile("factorial.c").run(); } #[test] fn test_gotos() { - transpile("gotos.c").edition(RustEdition::Rust2024).run(); + transpile("gotos.c").run(); } #[test] fn test_incomplete_arrays() { - transpile("incomplete_arrays.c") - .edition(RustEdition::Rust2024) - .run(); + transpile("incomplete_arrays.c").run(); } #[test] fn test_insertion() { - transpile("insertion.c") - .edition(RustEdition::Rust2024) - .run(); + transpile("insertion.c").run(); } #[test] fn test_keywords() { generate_keywords_test(); - transpile("keywords.c").run(); + transpile("keywords.c").edition(RustEdition::Rust2021).run(); } #[test] fn test_macrocase() { - transpile("macrocase.c") - .edition(RustEdition::Rust2024) - .run(); + transpile("macrocase.c").run(); } #[test] fn test_macros() { - transpile("macros.c").edition(RustEdition::Rust2024).run(); + transpile("macros.c").run(); } #[test] fn test_main_fn() { - transpile("main_fn.c").edition(RustEdition::Rust2024).run(); + transpile("main_fn.c").run(); } #[test] fn test_predefined() { - transpile("predefined.c") - .edition(RustEdition::Rust2024) - .run(); + transpile("predefined.c").run(); } #[test] fn test_records() { - transpile("records.c").edition(RustEdition::Rust2024).run(); + transpile("records.c").run(); } #[test] fn test_ref_ub() { - transpile("ref_ub.c").edition(RustEdition::Rust2024).run(); + transpile("ref_ub.c").run(); } #[test] fn test_rotate() { - transpile("rotate.c").edition(RustEdition::Rust2024).run(); + transpile("rotate.c").run(); } #[test] fn test_static_assert() { - transpile("static_assert.c") - .edition(RustEdition::Rust2024) - .run(); + transpile("static_assert.c").run(); } #[test] fn test_str_init() { - transpile("str_init.c").edition(RustEdition::Rust2024).run(); + transpile("str_init.c").run(); } // arch-specific #[test] fn test_spin() { - transpile("spin.c") - .edition(RustEdition::Rust2024) - .arch_specific(true) - .run(); + transpile("spin.c").arch_specific(true).run(); } #[test] fn test_vm_x86() { - transpile("vm_x86.c") - .edition(RustEdition::Rust2024) - .arch_specific(true) - .run(); + transpile("vm_x86.c").arch_specific(true).run(); } // os-specific #[test] fn test_call_only_once() { - transpile("call_only_once.c") - .edition(RustEdition::Rust2024) - .os_specific(true) - .run(); + transpile("call_only_once.c").os_specific(true).run(); } #[test] fn test_macros_os_specific() { - transpile("macros.c") - .edition(RustEdition::Rust2024) - .os_specific(true) - .run(); + transpile("macros.c").os_specific(true).run(); } #[test] fn test_out_of_range_lit() { - transpile("out_of_range_lit.c") - .edition(RustEdition::Rust2024) - .os_specific(true) - .run(); + transpile("out_of_range_lit.c").os_specific(true).run(); } #[test] fn test_rnd() { - transpile("rnd.c") - .edition(RustEdition::Rust2024) - .os_specific(true) - .run(); + transpile("rnd.c").os_specific(true).run(); } #[test] fn test_rotate_os_specific() { - transpile("rotate.c") - .edition(RustEdition::Rust2024) - .os_specific(true) - .run(); + transpile("rotate.c").os_specific(true).run(); } #[test] fn test_sigign() { - transpile("sigign.c") - .edition(RustEdition::Rust2024) - .os_specific(true) - .run(); + transpile("sigign.c").os_specific(true).run(); } #[test] fn test_typedefidx() { - transpile("typedefidx.c") - .edition(RustEdition::Rust2024) - .os_specific(true) - .run(); + transpile("typedefidx.c").os_specific(true).run(); } #[test] fn test_types() { - transpile("types.c") - .edition(RustEdition::Rust2024) - .os_specific(true) - .run(); + transpile("types.c").os_specific(true).run(); } #[test] fn test_wide_strings() { - transpile("wide_strings.c") - .edition(RustEdition::Rust2024) - .os_specific(true) - .run(); + transpile("wide_strings.c").os_specific(true).run(); } // arch-os-specific @@ -493,6 +444,7 @@ fn test_wide_strings() { #[test] fn test_varargs() { transpile("varargs.c") + .edition(RustEdition::Rust2021) .arch_specific(true) .os_specific(true) .run(); From f955267c3a1f5e488c61d33e4357c9c82afc9bb4 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 13:06:05 -0800 Subject: [PATCH 16/39] transpile: tests: compute `snapshot_prefix` with `.flatten()` to simplify --- c2rust-transpile/tests/snapshots.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index b9302c69ba..78d40b5e3b 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -124,10 +124,10 @@ fn transpile_snapshot( // Replace real paths with placeholders let rs = rs.replace(cwd.to_str().unwrap(), "."); - let snapshot_prefix = match platform { - None => "transpile".into(), - Some(platform) => format!("transpile-{platform}"), - }; + let snapshot_prefix = [Some("transpile"), platform] + .into_iter() + .flatten() + .join("-"); let c_file_name = c_path.file_name().unwrap().to_str().unwrap(); let c_file_name = sanitize_file_name(&c_file_name); let snapshot_name = format!("{snapshot_prefix}@{c_file_name}"); From dd1cef9a45464e843025a0ac5246a10a6a86bfa6 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 13:10:33 -0800 Subject: [PATCH 17/39] transpile: tests: make `platform` a `&[&str]` instead of `Option<&str>` --- c2rust-transpile/tests/snapshots.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 78d40b5e3b..348a8523ad 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -90,10 +90,11 @@ fn compile_and_transpile_file(c_path: &Path, config: TranspilerConfig) { } /// Transpile one input and compare output against the corresponding snapshot. -/// For outputs that vary in different environments, `platform` can be any platform-specific string. -/// It could be the `target_arch`, `target_os`, some combination, or something else. +/// For outputs that vary in different environments, +/// `platform` should be a slice of the platform-specific parts, +/// such as the `target_arch` or `target_os` or both. fn transpile_snapshot( - platform: Option<&str>, + platform: &[&str], c_path: &Path, edition: RustEdition, expect_compile_error: bool, @@ -110,8 +111,9 @@ fn transpile_snapshot( // We need to move the `.rs` file to a platform-specific name // so that they don't overwrite each other. let rs_path = match platform { - None => rs_path, - Some(platform) => { + &[] => rs_path, + platform => { + let platform = platform.join("-"); let platform_rs_path = rs_path.with_extension(format!("{platform}.rs")); fs::rename(&rs_path, &platform_rs_path).unwrap(); platform_rs_path @@ -124,7 +126,7 @@ fn transpile_snapshot( // Replace real paths with placeholders let rs = rs.replace(cwd.to_str().unwrap(), "."); - let snapshot_prefix = [Some("transpile"), platform] + let snapshot_prefix = [&["transpile"][..], platform] .into_iter() .flatten() .join("-"); @@ -248,13 +250,9 @@ impl<'a> TranspileTest<'a> { let platform = [arch_specific.then_some(arch), os_specific.then_some(os)] .into_iter() .flatten() - .join("-"); - let platform = match platform.as_str() { - "" => None, - platform => Some(platform), - }; + .collect::>(); - transpile_snapshot(platform, &c_path, edition, expect_compile_error); + transpile_snapshot(&platform, &c_path, edition, expect_compile_error); } } From d4f49e31fd4c6bc7e1e2d9ac32495bc3c9803105 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 13:10:57 -0800 Subject: [PATCH 18/39] transpile: tests: separate platform-specific parts with `.` instead of `-` in `.rs` names --- c2rust-transpile/tests/snapshots.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 348a8523ad..6a49f6d3b5 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -113,7 +113,7 @@ fn transpile_snapshot( let rs_path = match platform { &[] => rs_path, platform => { - let platform = platform.join("-"); + let platform = platform.join("."); let platform_rs_path = rs_path.with_extension(format!("{platform}.rs")); fs::rename(&rs_path, &platform_rs_path).unwrap(); platform_rs_path From 3e401802ae9b9684a5bafd1888542c2f26984aeb Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 13:27:09 -0800 Subject: [PATCH 19/39] transpile: tests: rename snapshots so that platform-specific parts are suffixes so that related files are next to each --- c2rust-transpile/tests/snapshots.rs | 10 +++++----- ...> snapshots__transpile@call_only_once.c.linux.snap} | 0 ...> snapshots__transpile@call_only_once.c.macos.snap} | 0 ...c.snap => snapshots__transpile@macros.c.linux.snap} | 0 ...c.snap => snapshots__transpile@macros.c.macos.snap} | 0 ...snapshots__transpile@out_of_range_lit.c.linux.snap} | 0 ...snapshots__transpile@out_of_range_lit.c.macos.snap} | 0 ...nd.c.snap => snapshots__transpile@rnd.c.linux.snap} | 0 ...nd.c.snap => snapshots__transpile@rnd.c.macos.snap} | 0 ...c.snap => snapshots__transpile@rotate.c.linux.snap} | 0 ...c.snap => snapshots__transpile@rotate.c.macos.snap} | 0 ...c.snap => snapshots__transpile@sigign.c.linux.snap} | 0 ...c.snap => snapshots__transpile@sigign.c.macos.snap} | 0 ...c.snap => snapshots__transpile@spin.c.aarch64.snap} | 0 ....c.snap => snapshots__transpile@spin.c.x86_64.snap} | 0 ...ap => snapshots__transpile@typedefidx.c.linux.snap} | 0 ...ap => snapshots__transpile@typedefidx.c.macos.snap} | 0 ....c.snap => snapshots__transpile@types.c.linux.snap} | 0 ....c.snap => snapshots__transpile@types.c.macos.snap} | 0 ... snapshots__transpile@varargs.c.aarch64.macos.snap} | 0 ...> snapshots__transpile@varargs.c.x86_64.linux.snap} | 2 +- ...snap => snapshots__transpile@vm_x86.c.aarch64.snap} | 0 ....snap => snapshots__transpile@vm_x86.c.x86_64.snap} | 0 ... => snapshots__transpile@wide_strings.c.linux.snap} | 0 ... => snapshots__transpile@wide_strings.c.macos.snap} | 0 25 files changed, 6 insertions(+), 6 deletions(-) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-linux@call_only_once.c.snap => snapshots__transpile@call_only_once.c.linux.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-macos@call_only_once.c.snap => snapshots__transpile@call_only_once.c.macos.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-linux@macros.c.snap => snapshots__transpile@macros.c.linux.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-macos@macros.c.snap => snapshots__transpile@macros.c.macos.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-linux@out_of_range_lit.c.snap => snapshots__transpile@out_of_range_lit.c.linux.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-macos@out_of_range_lit.c.snap => snapshots__transpile@out_of_range_lit.c.macos.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-linux@rnd.c.snap => snapshots__transpile@rnd.c.linux.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-macos@rnd.c.snap => snapshots__transpile@rnd.c.macos.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-linux@rotate.c.snap => snapshots__transpile@rotate.c.linux.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-macos@rotate.c.snap => snapshots__transpile@rotate.c.macos.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-linux@sigign.c.snap => snapshots__transpile@sigign.c.linux.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-macos@sigign.c.snap => snapshots__transpile@sigign.c.macos.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-aarch64@spin.c.snap => snapshots__transpile@spin.c.aarch64.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-x86_64@spin.c.snap => snapshots__transpile@spin.c.x86_64.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-linux@typedefidx.c.snap => snapshots__transpile@typedefidx.c.linux.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-macos@typedefidx.c.snap => snapshots__transpile@typedefidx.c.macos.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-linux@types.c.snap => snapshots__transpile@types.c.linux.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-macos@types.c.snap => snapshots__transpile@types.c.macos.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-aarch64-macos@varargs.c.snap => snapshots__transpile@varargs.c.aarch64.macos.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-x86_64-linux@varargs.c.snap => snapshots__transpile@varargs.c.x86_64.linux.snap} (98%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-aarch64@vm_x86.c.snap => snapshots__transpile@vm_x86.c.aarch64.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-x86_64@vm_x86.c.snap => snapshots__transpile@vm_x86.c.x86_64.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-linux@wide_strings.c.snap => snapshots__transpile@wide_strings.c.linux.snap} (100%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile-macos@wide_strings.c.snap => snapshots__transpile@wide_strings.c.macos.snap} (100%) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 6a49f6d3b5..d9a70cba6a 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -126,13 +126,13 @@ fn transpile_snapshot( // Replace real paths with placeholders let rs = rs.replace(cwd.to_str().unwrap(), "."); - let snapshot_prefix = [&["transpile"][..], platform] - .into_iter() - .flatten() - .join("-"); let c_file_name = c_path.file_name().unwrap().to_str().unwrap(); let c_file_name = sanitize_file_name(&c_file_name); - let snapshot_name = format!("{snapshot_prefix}@{c_file_name}"); + let snapshot_suffix = [&[c_file_name.as_str()][..], platform] + .into_iter() + .flatten() + .join("."); + let snapshot_name = format!("transpile@{snapshot_suffix}"); insta::assert_snapshot!(snapshot_name, &rs, &debug_expr); diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@call_only_once.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.linux.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-linux@call_only_once.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.linux.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@call_only_once.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-macos@call_only_once.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.macos.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.linux.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-linux@macros.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.linux.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-macos@macros.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.macos.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@out_of_range_lit.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.linux.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-linux@out_of_range_lit.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.linux.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@out_of_range_lit.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-macos@out_of_range_lit.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.macos.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.linux.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rnd.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.linux.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rnd.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.macos.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.linux.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-linux@rotate.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.linux.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-macos@rotate.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.macos.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@sigign.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.linux.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-linux@sigign.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.linux.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@sigign.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-macos@sigign.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.macos.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@spin.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.aarch64.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@spin.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.aarch64.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@spin.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.x86_64.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@spin.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.x86_64.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@typedefidx.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.linux.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-linux@typedefidx.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.linux.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@typedefidx.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-macos@typedefidx.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.macos.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@types.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.linux.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-linux@types.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.linux.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@types.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-macos@types.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.macos.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64-macos@varargs.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.aarch64.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64-macos@varargs.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.aarch64.macos.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64-linux@varargs.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.x86_64.linux.snap similarity index 98% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64-linux@varargs.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.x86_64.linux.snap index c176f7ad34..c86a25c979 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64-linux@varargs.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.x86_64.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/arch-os-specific/varargs.x86_64-linux.rs +expression: cat tests/snapshots/arch-os-specific/varargs.x86_64.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.aarch64.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.aarch64.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.x86_64.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.x86_64.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.linux.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-linux@wide_strings.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.linux.snap diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.macos.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__transpile-macos@wide_strings.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.macos.snap From 253e66d18c12ece4149bb94b2da9332c2b812786 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 13:28:24 -0800 Subject: [PATCH 20/39] transpile: tests: use `@` as the test name and file name separator for `c-decls`, too --- c2rust-transpile/tests/snapshots.rs | 2 +- ...napshots__c_decls-nh.c.snap => snapshots__c_decls@nh.c.snap} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename c2rust-transpile/tests/snapshots/{snapshots__c_decls-nh.c.snap => snapshots__c_decls@nh.c.snap} (100%) diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index d9a70cba6a..05b08cafe8 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -452,7 +452,7 @@ fn transpile_with_c_decl_map_snapshot(c_path: &Path) { compile_and_transpile_file(c_path, config(Default::default())); let c_decls_path = c_path.with_extension("c_decls.json"); - let snapshot_name = format!("c_decls-{}", c_path.file_name().unwrap().to_str().unwrap()); + let snapshot_name = format!("c_decls@{}", c_path.file_name().unwrap().to_str().unwrap()); let debug_expr = format!("cat {}", c_decls_path.display()); let json: serde_json::Value = serde_json::from_str(&fs::read_to_string(&c_decls_path).unwrap()).unwrap(); diff --git a/c2rust-transpile/tests/snapshots/snapshots__c_decls-nh.c.snap b/c2rust-transpile/tests/snapshots/snapshots__c_decls@nh.c.snap similarity index 100% rename from c2rust-transpile/tests/snapshots/snapshots__c_decls-nh.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__c_decls@nh.c.snap From 512b0c6590e739323f0d17b238067f9a3c575ed8 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 13:33:39 -0800 Subject: [PATCH 21/39] transpile: tests: test on both editions This easy to do, gives better test coverage, and allows us to see the failing snapshots on edition 2024 for tests that haven't been converted yet. --- c2rust-rust-tools/src/lib.rs | 2 + c2rust-transpile/tests/snapshots.rs | 80 +-- .../snapshots__transpile@alloca.c.2021.snap | 42 ++ ...> snapshots__transpile@alloca.c.2024.snap} | 2 +- .../snapshots__transpile@arrays.c.2021.snap | 125 +++++ ...> snapshots__transpile@arrays.c.2024.snap} | 2 +- ... snapshots__transpile@atomics.c.2021.snap} | 2 +- .../snapshots__transpile@atomics.c.2024.snap | 39 ++ ...transpile@call_only_once.c.2021.linux.snap | 47 ++ ...transpile@call_only_once.c.2021.macos.snap | 38 ++ ...ranspile@call_only_once.c.2024.linux.snap} | 2 +- ...ranspile@call_only_once.c.2024.macos.snap} | 2 +- ...s__transpile@compound_literals.c.2021.snap | 61 +++ ...__transpile@compound_literals.c.2024.snap} | 2 +- ...napshots__transpile@empty_init.c.2021.snap | 21 + ...apshots__transpile@empty_init.c.2024.snap} | 2 +- .../snapshots__transpile@exprs.c.2021.snap | 65 +++ ...=> snapshots__transpile@exprs.c.2024.snap} | 2 +- ...snapshots__transpile@factorial.c.2021.snap | 22 + ...napshots__transpile@factorial.c.2024.snap} | 2 +- .../snapshots__transpile@gotos.c.2021.snap | 21 + ...=> snapshots__transpile@gotos.c.2024.snap} | 2 +- ...__transpile@incomplete_arrays.c.2021.snap} | 2 +- ...s__transpile@incomplete_arrays.c.2024.snap | 12 + ...snapshots__transpile@insertion.c.2021.snap | 27 + ...napshots__transpile@insertion.c.2024.snap} | 2 +- ...snapshots__transpile@keywords.c.2021.snap} | 2 +- .../snapshots__transpile@keywords.c.2024.snap | 180 +++++++ ...snapshots__transpile@macrocase.c.2021.snap | 32 ++ ...napshots__transpile@macrocase.c.2024.snap} | 2 +- ...pshots__transpile@macros.c.2021.linux.snap | 45 ++ ...pshots__transpile@macros.c.2021.macos.snap | 46 ++ .../snapshots__transpile@macros.c.2021.snap | 469 ++++++++++++++++++ ...shots__transpile@macros.c.2024.linux.snap} | 2 +- ...shots__transpile@macros.c.2024.macos.snap} | 2 +- ...> snapshots__transpile@macros.c.2024.snap} | 2 +- ... snapshots__transpile@main_fn.c.2021.snap} | 2 +- .../snapshots__transpile@main_fn.c.2024.snap | 50 ++ ...anspile@out_of_range_lit.c.2021.linux.snap | 21 + ...anspile@out_of_range_lit.c.2021.macos.snap | 20 + ...nspile@out_of_range_lit.c.2024.linux.snap} | 2 +- ...nspile@out_of_range_lit.c.2024.macos.snap} | 2 +- ...napshots__transpile@predefined.c.2021.snap | 24 + ...apshots__transpile@predefined.c.2024.snap} | 2 +- .../snapshots__transpile@records.c.2021.snap | 87 ++++ ... snapshots__transpile@records.c.2024.snap} | 2 +- .../snapshots__transpile@ref_ub.c.2021.snap | 35 ++ ...> snapshots__transpile@ref_ub.c.2024.snap} | 2 +- ...snapshots__transpile@rnd.c.2021.linux.snap | 35 ++ ...snapshots__transpile@rnd.c.2021.macos.snap | 33 ++ ...napshots__transpile@rnd.c.2024.linux.snap} | 2 +- ...napshots__transpile@rnd.c.2024.macos.snap} | 2 +- ...pshots__transpile@rotate.c.2021.linux.snap | 28 ++ ...pshots__transpile@rotate.c.2021.macos.snap | 24 + .../snapshots__transpile@rotate.c.2021.snap | 36 ++ ...shots__transpile@rotate.c.2024.linux.snap} | 2 +- ...shots__transpile@rotate.c.2024.macos.snap} | 2 +- ...> snapshots__transpile@rotate.c.2024.snap} | 2 +- ...pshots__transpile@sigign.c.2021.linux.snap | 21 + ...pshots__transpile@sigign.c.2021.macos.snap | 20 + ...shots__transpile@sigign.c.2024.linux.snap} | 2 +- ...shots__transpile@sigign.c.2024.macos.snap} | 2 +- ...pshots__transpile@spin.c.2021.aarch64.snap | 21 + ...apshots__transpile@spin.c.2021.x86_64.snap | 21 + ...shots__transpile@spin.c.2024.aarch64.snap} | 2 +- ...pshots__transpile@spin.c.2024.x86_64.snap} | 2 +- ...shots__transpile@static_assert.c.2021.snap | 14 + ...hots__transpile@static_assert.c.2024.snap} | 2 +- .../snapshots__transpile@str_init.c.2021.snap | 112 +++++ ...snapshots__transpile@str_init.c.2024.snap} | 2 +- ...ts__transpile@typedefidx.c.2021.linux.snap | 19 + ...ts__transpile@typedefidx.c.2021.macos.snap | 19 + ...s__transpile@typedefidx.c.2024.linux.snap} | 2 +- ...s__transpile@typedefidx.c.2024.macos.snap} | 2 +- ...apshots__transpile@types.c.2021.linux.snap | 67 +++ ...apshots__transpile@types.c.2021.macos.snap | 62 +++ ...pshots__transpile@types.c.2024.linux.snap} | 2 +- ...pshots__transpile@types.c.2024.macos.snap} | 2 +- ...anspile@varargs.c.2021.aarch64.macos.snap} | 2 +- ...ranspile@varargs.c.2021.x86_64.linux.snap} | 2 +- ...ranspile@varargs.c.2024.aarch64.macos.snap | 23 + ...transpile@varargs.c.2024.x86_64.linux.snap | 167 +++++++ ...hots__transpile@vm_x86.c.2021.aarch64.snap | 71 +++ ...shots__transpile@vm_x86.c.2021.x86_64.snap | 81 +++ ...ots__transpile@vm_x86.c.2024.aarch64.snap} | 2 +- ...hots__transpile@vm_x86.c.2024.x86_64.snap} | 2 +- ...__transpile@wide_strings.c.2021.linux.snap | 37 ++ ...__transpile@wide_strings.c.2021.macos.snap | 39 ++ ..._transpile@wide_strings.c.2024.linux.snap} | 2 +- ..._transpile@wide_strings.c.2024.macos.snap} | 2 +- 90 files changed, 2574 insertions(+), 75 deletions(-) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@alloca.c.snap => snapshots__transpile@alloca.c.2024.snap} (96%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@arrays.c.snap => snapshots__transpile@arrays.c.2024.snap} (99%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@atomics.c.snap => snapshots__transpile@atomics.c.2021.snap} (97%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2024.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2021.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2021.macos.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@call_only_once.c.linux.snap => snapshots__transpile@call_only_once.c.2024.linux.snap} (94%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@call_only_once.c.macos.snap => snapshots__transpile@call_only_once.c.2024.macos.snap} (93%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@compound_literals.c.snap => snapshots__transpile@compound_literals.c.2024.snap} (98%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@empty_init.c.snap => snapshots__transpile@empty_init.c.2024.snap} (89%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@exprs.c.snap => snapshots__transpile@exprs.c.2024.snap} (97%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@factorial.c.snap => snapshots__transpile@factorial.c.2024.snap} (93%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@gotos.c.snap => snapshots__transpile@gotos.c.2024.snap} (91%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@incomplete_arrays.c.snap => snapshots__transpile@incomplete_arrays.c.2021.snap} (76%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.2024.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@insertion.c.snap => snapshots__transpile@insertion.c.2024.snap} (93%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@keywords.c.snap => snapshots__transpile@keywords.c.2021.snap} (98%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2024.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@macrocase.c.snap => snapshots__transpile@macrocase.c.2024.snap} (94%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.macos.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@macros.c.linux.snap => snapshots__transpile@macros.c.2024.linux.snap} (95%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@macros.c.macos.snap => snapshots__transpile@macros.c.2024.macos.snap} (95%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@macros.c.snap => snapshots__transpile@macros.c.2024.snap} (99%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@main_fn.c.snap => snapshots__transpile@main_fn.c.2021.snap} (97%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.2024.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2021.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2021.macos.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@out_of_range_lit.c.linux.snap => snapshots__transpile@out_of_range_lit.c.2024.linux.snap} (88%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@out_of_range_lit.c.macos.snap => snapshots__transpile@out_of_range_lit.c.2024.macos.snap} (88%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@predefined.c.snap => snapshots__transpile@predefined.c.2024.snap} (93%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@records.c.snap => snapshots__transpile@records.c.2024.snap} (98%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@ref_ub.c.snap => snapshots__transpile@ref_ub.c.2024.snap} (93%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2021.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2021.macos.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@rnd.c.linux.snap => snapshots__transpile@rnd.c.2024.linux.snap} (93%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@rnd.c.macos.snap => snapshots__transpile@rnd.c.2024.macos.snap} (93%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.macos.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@rotate.c.linux.snap => snapshots__transpile@rotate.c.2024.linux.snap} (92%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@rotate.c.macos.snap => snapshots__transpile@rotate.c.2024.macos.snap} (90%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@rotate.c.snap => snapshots__transpile@rotate.c.2024.snap} (96%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2021.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2021.macos.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@sigign.c.linux.snap => snapshots__transpile@sigign.c.2024.linux.snap} (90%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@sigign.c.macos.snap => snapshots__transpile@sigign.c.2024.macos.snap} (88%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2021.aarch64.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2021.x86_64.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@spin.c.aarch64.snap => snapshots__transpile@spin.c.2024.aarch64.snap} (86%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@spin.c.x86_64.snap => snapshots__transpile@spin.c.2024.x86_64.snap} (85%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@static_assert.c.snap => snapshots__transpile@static_assert.c.2024.snap} (82%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.2021.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@str_init.c.snap => snapshots__transpile@str_init.c.2024.snap} (98%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2021.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2021.macos.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@typedefidx.c.linux.snap => snapshots__transpile@typedefidx.c.2024.linux.snap} (86%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@typedefidx.c.macos.snap => snapshots__transpile@typedefidx.c.2024.macos.snap} (86%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2021.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2021.macos.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@types.c.linux.snap => snapshots__transpile@types.c.2024.linux.snap} (96%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@types.c.macos.snap => snapshots__transpile@types.c.2024.macos.snap} (96%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@varargs.c.aarch64.macos.snap => snapshots__transpile@varargs.c.2021.aarch64.macos.snap} (85%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@varargs.c.x86_64.linux.snap => snapshots__transpile@varargs.c.2021.x86_64.linux.snap} (98%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.aarch64.macos.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.x86_64.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2021.aarch64.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2021.x86_64.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@vm_x86.c.aarch64.snap => snapshots__transpile@vm_x86.c.2024.aarch64.snap} (97%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@vm_x86.c.x86_64.snap => snapshots__transpile@vm_x86.c.2024.x86_64.snap} (98%) create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2021.linux.snap create mode 100644 c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2021.macos.snap rename c2rust-transpile/tests/snapshots/{snapshots__transpile@wide_strings.c.linux.snap => snapshots__transpile@wide_strings.c.2024.linux.snap} (94%) rename c2rust-transpile/tests/snapshots/{snapshots__transpile@wide_strings.c.macos.snap => snapshots__transpile@wide_strings.c.2024.macos.snap} (95%) diff --git a/c2rust-rust-tools/src/lib.rs b/c2rust-rust-tools/src/lib.rs index 3eed6d99c3..916098f84f 100644 --- a/c2rust-rust-tools/src/lib.rs +++ b/c2rust-rust-tools/src/lib.rs @@ -17,6 +17,8 @@ pub enum RustEdition { } impl RustEdition { + pub const ALL: &[Self] = &[Self::Rust2021, Self::Rust2024]; + pub const fn as_str(&self) -> &'static str { match self { Self::Rust2021 => "2021", diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 05b08cafe8..4112b50c1c 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -108,17 +108,15 @@ fn transpile_snapshot( // the file name won't be valid anymore. let crate_name = c_path.file_stem().unwrap().to_str().unwrap(); let rs_path = c_path.with_extension("rs"); - // We need to move the `.rs` file to a platform-specific name + // We need to move the `.rs` file to a platform/edition-specific name // so that they don't overwrite each other. - let rs_path = match platform { - &[] => rs_path, - platform => { - let platform = platform.join("."); - let platform_rs_path = rs_path.with_extension(format!("{platform}.rs")); - fs::rename(&rs_path, &platform_rs_path).unwrap(); - platform_rs_path - } - }; + let ext = [&[edition.as_str()][..], platform] + .into_iter() + .flatten() + .join("."); + let old_rs_path = rs_path; + let rs_path = old_rs_path.with_extension(format!("{ext}.rs")); + fs::rename(&old_rs_path, &rs_path).unwrap(); let rs = fs::read_to_string(&rs_path).unwrap(); let debug_expr = format!("cat {}", rs_path.display()); @@ -128,11 +126,7 @@ fn transpile_snapshot( let c_file_name = c_path.file_name().unwrap().to_str().unwrap(); let c_file_name = sanitize_file_name(&c_file_name); - let snapshot_suffix = [&[c_file_name.as_str()][..], platform] - .into_iter() - .flatten() - .join("."); - let snapshot_name = format!("transpile@{snapshot_suffix}"); + let snapshot_name = format!("transpile@{c_file_name}.{ext}"); insta::assert_snapshot!(snapshot_name, &rs, &debug_expr); @@ -157,27 +151,23 @@ fn transpile_snapshot( #[must_use] struct TranspileTest<'a> { c_file_name: &'a str, - edition: RustEdition, arch_specific: bool, os_specific: bool, - expect_compile_error: bool, + expect_compile_error_edition_2021: bool, + expect_compile_error_edition_2024: bool, } fn transpile(c_file_name: &str) -> TranspileTest { TranspileTest { c_file_name, - edition: RustEdition::Rust2024, arch_specific: false, os_specific: false, - expect_compile_error: false, + expect_compile_error_edition_2021: false, + expect_compile_error_edition_2024: false, } } impl<'a> TranspileTest<'a> { - pub fn edition(self, edition: RustEdition) -> Self { - Self { edition, ..self } - } - pub fn arch_specific(self, arch_specific: bool) -> Self { Self { arch_specific, @@ -193,9 +183,22 @@ impl<'a> TranspileTest<'a> { } #[allow(unused)] // TODO remove once used - pub fn expect_compile_error(self, expect_compile_error: bool) -> Self { + pub fn expect_compile_error_edition_2021( + self, + expect_compile_error_edition_2021: bool, + ) -> Self { + Self { + expect_compile_error_edition_2021, + ..self + } + } + + pub fn expect_compile_error_edition_2024( + self, + expect_compile_error_edition_2024: bool, + ) -> Self { Self { - expect_compile_error, + expect_compile_error_edition_2024, ..self } } @@ -203,10 +206,10 @@ impl<'a> TranspileTest<'a> { pub fn run(self) { let Self { c_file_name, - edition, arch_specific, os_specific, - expect_compile_error, + expect_compile_error_edition_2021, + expect_compile_error_edition_2024, } = self; let specific_dir_prefix = [arch_specific.then_some("arch"), os_specific.then_some("os")] @@ -252,7 +255,18 @@ impl<'a> TranspileTest<'a> { .flatten() .collect::>(); - transpile_snapshot(&platform, &c_path, edition, expect_compile_error); + transpile_snapshot( + &platform, + &c_path, + RustEdition::Rust2021, + expect_compile_error_edition_2021, + ); + transpile_snapshot( + &platform, + &c_path, + RustEdition::Rust2024, + expect_compile_error_edition_2024, + ); } } @@ -289,7 +303,9 @@ fn test_arrays() { #[test] fn test_atomics() { - transpile("atomics.c").edition(RustEdition::Rust2021).run(); + transpile("atomics.c") + .expect_compile_error_edition_2024(true) + .run(); } #[test] @@ -330,7 +346,9 @@ fn test_insertion() { #[test] fn test_keywords() { generate_keywords_test(); - transpile("keywords.c").edition(RustEdition::Rust2021).run(); + transpile("keywords.c") + .expect_compile_error_edition_2024(true) + .run(); } #[test] @@ -442,7 +460,7 @@ fn test_wide_strings() { #[test] fn test_varargs() { transpile("varargs.c") - .edition(RustEdition::Rust2021) + .expect_compile_error_edition_2024(cfg!(target_os = "linux")) .arch_specific(true) .os_specific(true) .run(); diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.2021.snap new file mode 100644 index 0000000000..fec256ea0e --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.2021.snap @@ -0,0 +1,42 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/alloca.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub static mut TRUE: ::core::ffi::c_int = 1 as ::core::ffi::c_int; +#[no_mangle] +pub unsafe extern "C" fn alloca_sum( + mut val1: ::core::ffi::c_int, + mut val2: ::core::ffi::c_int, +) -> ::core::ffi::c_int { + let mut c2rust_alloca_allocations: Vec> = Vec::new(); + let mut alloca1: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>(); + let mut alloca2: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>(); + if TRUE != 0 { + c2rust_alloca_allocations.push(::std::vec::from_elem( + 0, + ::core::mem::size_of::<::core::ffi::c_int>() as usize, + )); + alloca1 = c2rust_alloca_allocations.last_mut().unwrap().as_mut_ptr() + as *mut ::core::ffi::c_void as *mut ::core::ffi::c_int; + *alloca1 = val1; + } + if TRUE != 0 { + c2rust_alloca_allocations.push(::std::vec::from_elem( + 0, + ::core::mem::size_of::<::core::ffi::c_int>() as usize, + )); + alloca2 = c2rust_alloca_allocations.last_mut().unwrap().as_mut_ptr() + as *mut ::core::ffi::c_void as *mut ::core::ffi::c_int; + *alloca2 = val2; + } + return *alloca1 + *alloca2; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.2024.snap similarity index 96% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.2024.snap index 2523ccb6ae..d0767787e8 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/alloca.rs +expression: cat tests/snapshots/alloca.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2021.snap new file mode 100644 index 0000000000..df10d4395c --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2021.snap @@ -0,0 +1,125 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/arrays.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(raw_ref_op)] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct C2Rust_Unnamed { + pub y: ::core::ffi::c_int, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct C2Rust_Unnamed_0 { + pub x: *mut ::core::ffi::c_char, + pub y: ::core::ffi::c_int, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct C2Rust_Unnamed_1 { + pub x: ::core::ffi::c_short, + pub y: ::core::ffi::c_int, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct C2Rust_Unnamed_2 { + pub x: ::core::ffi::c_short, + pub y: ::core::ffi::c_int, +} +#[no_mangle] +pub static mut static_char_array: [::core::ffi::c_char; 9] = + unsafe { ::core::mem::transmute::<[u8; 9], [::core::ffi::c_char; 9]>(*b"mystring\0") }; +#[no_mangle] +pub static mut static_char_ptr: *mut ::core::ffi::c_char = + b"mystring\0".as_ptr() as *const ::core::ffi::c_char as *mut ::core::ffi::c_char; +#[no_mangle] +pub static mut static_void_ptr: *mut ::core::ffi::c_void = + unsafe { &raw const static_char_array as *mut ::core::ffi::c_char as *mut ::core::ffi::c_void }; +#[no_mangle] +pub unsafe extern "C" fn entry() { + let mut int_2d: [[::core::ffi::c_int; 1]; 1] = [[1 as ::core::ffi::c_int]]; + int_2d[0 as ::core::ffi::c_int as usize][0 as ::core::ffi::c_int as usize] += + 9 as ::core::ffi::c_int; + let mut int_empty_init: [::core::ffi::c_int; 16] = [0; 16]; + int_empty_init[15 as ::core::ffi::c_int as usize] += 9 as ::core::ffi::c_int; + let mut int_too_long: [::core::ffi::c_int; 2] = + [1 as ::core::ffi::c_int, 2 as ::core::ffi::c_int]; + let mut int_zero: [::core::ffi::c_int; 0] = [0; 0]; + let mut int_too_short: [::core::ffi::c_int; 16] = [0 as ::core::ffi::c_int; 16]; + int_too_short[15 as ::core::ffi::c_int as usize] += 9 as ::core::ffi::c_int; + let mut struct_init_too_short: [C2Rust_Unnamed_0; 1] = [C2Rust_Unnamed_0 { + x: ::core::ptr::null_mut::<::core::ffi::c_char>(), + y: 0, + }; 1]; + struct_init_too_short[0 as ::core::ffi::c_int as usize].y += 9 as ::core::ffi::c_int; + let mut struct_init_too_long: [C2Rust_Unnamed; 1] = [C2Rust_Unnamed { + y: 1 as ::core::ffi::c_int, + }]; + struct_init_too_long[0 as ::core::ffi::c_int as usize].y += 9 as ::core::ffi::c_int; + let mut char_with_string: [::core::ffi::c_char; 4] = + ::core::mem::transmute::<[u8; 4], [::core::ffi::c_char; 4]>(*b"abc\0"); + let mut char_with_chars: [::core::ffi::c_char; 3] = [ + 'd' as i32 as ::core::ffi::c_char, + 'e' as i32 as ::core::ffi::c_char, + 'f' as i32 as ::core::ffi::c_char, + ]; + let mut char_with_ints: [::core::ffi::c_char; 2] = + [1 as ::core::ffi::c_int as ::core::ffi::c_char, 0]; + let mut char_with_init: [::core::ffi::c_char; 5] = + ::core::mem::transmute::<[u8; 5], [::core::ffi::c_char; 5]>(*b"abcd\0"); + let mut char_too_long: [::core::ffi::c_char; 3] = + ::core::mem::transmute::<[u8; 3], [::core::ffi::c_char; 3]>(*b"abc"); + let mut char_too_short: [::core::ffi::c_char; 20] = + ::core::mem::transmute::<[u8; 20], [::core::ffi::c_char; 20]>( + *b"abc\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", + ); + let mut int_var_ptr: *mut ::core::ffi::c_int = + &raw mut int_empty_init as *mut ::core::ffi::c_int; + let mut int_var_array_ptr: *mut [::core::ffi::c_int; 16] = &raw mut int_empty_init; + let mut char_var_ptr: *mut ::core::ffi::c_char = + &raw mut char_with_string as *mut ::core::ffi::c_char; + let mut char_var_array_ptr: *mut [::core::ffi::c_char; 4] = &raw mut char_with_string; + let mut const_char_lit_ptr: *const ::core::ffi::c_char = + b"abc\0".as_ptr() as *const ::core::ffi::c_char; + let mut const_char_lit_array_ptr: *const [::core::ffi::c_char; 4] = + b"abc\0" as *const [u8; 4] as *const [::core::ffi::c_char; 4]; + let mut char_lit_ptr: *mut ::core::ffi::c_char = + b"abc\0".as_ptr() as *const ::core::ffi::c_char as *mut ::core::ffi::c_char; + let mut char_lit_array_ptr: *mut [::core::ffi::c_char; 4] = b"abc\0" as *const [u8; 4] + as *const [::core::ffi::c_char; 4] + as *mut [::core::ffi::c_char; 4]; + let mut past_end: *mut ::core::ffi::c_char = (&raw mut static_char_array + as *mut ::core::ffi::c_char) + .offset(::core::mem::size_of::<[::core::ffi::c_char; 9]>() as isize) + as *mut ::core::ffi::c_char; + past_end = static_char_ptr.offset(8 as ::core::ffi::c_int as isize) as *mut ::core::ffi::c_char; +} +#[no_mangle] +pub unsafe extern "C" fn short_initializer() { + let mut empty_brackets: [::core::ffi::c_int; 16] = [0; 16]; + let mut brackets_with_zero: [::core::ffi::c_int; 16] = [0 as ::core::ffi::c_int; 16]; + let mut brackets_with_one: [::core::ffi::c_int; 4] = [1 as ::core::ffi::c_int, 0, 0, 0]; + let mut excess_elements_1: [::core::ffi::c_int; 2] = + [1 as ::core::ffi::c_int, 2 as ::core::ffi::c_int]; + let mut excess_elements_2: [::core::ffi::c_int; 0] = [0; 0]; + let mut single_struct: [C2Rust_Unnamed_2; 1] = [C2Rust_Unnamed_2 { + x: 1 as ::core::ffi::c_short, + y: 2 as ::core::ffi::c_int, + }]; + let mut many_struct: [C2Rust_Unnamed_1; 3] = [ + C2Rust_Unnamed_1 { + x: 1 as ::core::ffi::c_short, + y: 2 as ::core::ffi::c_int, + }, + C2Rust_Unnamed_1 { x: 0, y: 0 }, + C2Rust_Unnamed_1 { x: 0, y: 0 }, + ]; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2024.snap similarity index 99% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2024.snap index 74e29c7b60..4de9f12cc5 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/arrays.rs +expression: cat tests/snapshots/arrays.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2021.snap similarity index 97% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2021.snap index 1cd7b2c804..d60e753cb2 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2021.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/atomics.rs +expression: cat tests/snapshots/atomics.2021.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2024.snap new file mode 100644 index 0000000000..108ae33e86 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2024.snap @@ -0,0 +1,39 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/atomics.2024.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(core_intrinsics, raw_ref_op)] +#[unsafe(no_mangle)] +pub unsafe extern "C" fn c11_atomics(mut x: ::core::ffi::c_int) -> ::core::ffi::c_int { + *&raw mut x = 0 as ::core::ffi::c_int; + ::core::intrinsics::atomic_store_seqcst(&raw mut x, 1 as ::core::ffi::c_int); + ::core::intrinsics::atomic_load_seqcst(&raw mut x); + ::core::intrinsics::atomic_xadd_seqcst(&raw mut x, 2 as ::core::ffi::c_int); + ::core::intrinsics::atomic_xsub_seqcst(&raw mut x, 1 as ::core::ffi::c_int); + ::core::intrinsics::atomic_and_seqcst(&raw mut x, 0xf as ::core::ffi::c_int); + ::core::intrinsics::atomic_or_seqcst(&raw mut x, 0x10 as ::core::ffi::c_int); + ::core::intrinsics::atomic_nand_seqcst(&raw mut x, 0xff as ::core::ffi::c_int); + ::core::intrinsics::atomic_xchg_seqcst(&raw mut x, 42 as ::core::ffi::c_int); + let mut expected: ::core::ffi::c_int = 42 as ::core::ffi::c_int; + let mut desired: ::core::ffi::c_int = 100 as ::core::ffi::c_int; + let c2rust_fresh0 = + ::core::intrinsics::atomic_cxchg_seqcst_seqcst(&raw mut x, *&raw mut expected, desired); + *&raw mut expected = c2rust_fresh0.0; + c2rust_fresh0.1; + expected = 100 as ::core::ffi::c_int; + desired = 200 as ::core::ffi::c_int; + let c2rust_fresh1 = + ::core::intrinsics::atomic_cxchgweak_seqcst_seqcst(&raw mut x, *&raw mut expected, desired); + *&raw mut expected = c2rust_fresh1.0; + c2rust_fresh1.1; + return x; +} +pub const __ATOMIC_SEQ_CST: ::core::ffi::c_int = 5 as ::core::ffi::c_int; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2021.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2021.linux.snap new file mode 100644 index 0000000000..7bb9e6bddc --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2021.linux.snap @@ -0,0 +1,47 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/call_only_once.2021.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(label_break_value)] +extern "C" { + fn __assert_fail( + __assertion: *const ::core::ffi::c_char, + __file: *const ::core::ffi::c_char, + __line: ::core::ffi::c_uint, + __function: *const ::core::ffi::c_char, + ) -> !; +} +pub const __ASSERT_FUNCTION: [::core::ffi::c_char; 32] = unsafe { + ::core::mem::transmute::<[u8; 32], [::core::ffi::c_char; 32]>( + *b"int assert_call_only_once(void)\0", + ) +}; +static mut called: ::core::ffi::c_int = 0 as ::core::ffi::c_int; +unsafe extern "C" fn called_only_once() -> ::core::ffi::c_int { + called += 1; + return 1 as ::core::ffi::c_int; +} +#[no_mangle] +pub unsafe extern "C" fn assert_call_only_once() -> ::core::ffi::c_int { + '_c2rust_label: { + if called_only_once() != 0 { + } else { + __assert_fail( + b"called_only_once()\0".as_ptr() as *const ::core::ffi::c_char, + b"./tests/snapshots/os-specific/call_only_once.c\0" + .as_ptr() as *const ::core::ffi::c_char, + 12 as ::core::ffi::c_uint, + __ASSERT_FUNCTION.as_ptr(), + ); + } + }; + return called; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2021.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2021.macos.snap new file mode 100644 index 0000000000..0d9abd911d --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2021.macos.snap @@ -0,0 +1,38 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/call_only_once.2021.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +extern "C" { + fn __assert_rtn( + _: *const ::core::ffi::c_char, + _: *const ::core::ffi::c_char, + _: ::core::ffi::c_int, + _: *const ::core::ffi::c_char, + ) -> !; +} +static mut called: ::core::ffi::c_int = 0 as ::core::ffi::c_int; +unsafe extern "C" fn called_only_once() -> ::core::ffi::c_int { + called += 1; + return 1 as ::core::ffi::c_int; +} +#[no_mangle] +pub unsafe extern "C" fn assert_call_only_once() -> ::core::ffi::c_int { + if (called_only_once() == 0) as ::core::ffi::c_int as ::core::ffi::c_long != 0 { + __assert_rtn( + b"assert_call_only_once\0".as_ptr() as *const ::core::ffi::c_char, + b"call_only_once.c\0".as_ptr() as *const ::core::ffi::c_char, + 12 as ::core::ffi::c_int, + b"called_only_once()\0".as_ptr() as *const ::core::ffi::c_char, + ); + } else { + }; + return called; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.linux.snap similarity index 94% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.linux.snap index cd9bdc4fed..a6f0d8f6c1 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/call_only_once.linux.rs +expression: cat tests/snapshots/os-specific/call_only_once.2024.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.macos.snap similarity index 93% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.macos.snap index 415ccb92b0..b9036251d2 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/call_only_once.macos.rs +expression: cat tests/snapshots/os-specific/call_only_once.2024.macos.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2021.snap new file mode 100644 index 0000000000..b92f5ff312 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2021.snap @@ -0,0 +1,61 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/compound_literals.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(raw_ref_op)] +#[no_mangle] +pub static mut static_single_int: ::core::ffi::c_int = 42; +#[no_mangle] +pub static mut static_single_int_ptr: *mut ::core::ffi::c_int = + &42 as *const ::core::ffi::c_int as *mut ::core::ffi::c_int; +#[no_mangle] +pub static mut static_int_ptr_to_array: *mut ::core::ffi::c_int = + [42, 9001].as_ptr() as *mut ::core::ffi::c_int; +#[no_mangle] +pub static mut static_volatile_int_ptr_to_array: *mut ::core::ffi::c_int = + [42, 9001].as_ptr() as *mut ::core::ffi::c_int as *mut ::core::ffi::c_int; +#[no_mangle] +pub static mut static_int_array_ptr: *mut [::core::ffi::c_int; 2] = + &[42, 9001] as *const [::core::ffi::c_int; 2] as *mut [::core::ffi::c_int; 2]; +pub const SINGLE_INT: ::core::ffi::c_int = 42 as ::core::ffi::c_int; +pub const INT_ARRAY: [::core::ffi::c_int; 2] = + [42 as ::core::ffi::c_int, 9001 as ::core::ffi::c_int]; +pub const CHAR_ARRAY: [::core::ffi::c_char; 6] = + unsafe { ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0") }; +#[no_mangle] +pub unsafe extern "C" fn local_compound_literals() { + let mut single_int: ::core::ffi::c_int = 42 as ::core::ffi::c_int; + let mut c2rust_fresh0: ::core::ffi::c_int = 42 as ::core::ffi::c_int; + let mut single_int_ptr: *mut ::core::ffi::c_int = &raw mut c2rust_fresh0; + let mut c2rust_fresh1: [::core::ffi::c_int; 2] = + [42 as ::core::ffi::c_int, 9001 as ::core::ffi::c_int]; + let mut int_ptr_to_array: *mut ::core::ffi::c_int = + &raw mut c2rust_fresh1 as *mut ::core::ffi::c_int; + let mut c2rust_fresh2: [::core::ffi::c_char; 6] = + ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0"); + let mut char_ptr_to_array: *mut ::core::ffi::c_char = + &raw mut c2rust_fresh2 as *mut ::core::ffi::c_char; + let mut c2rust_fresh3: [::core::ffi::c_int; 2] = + [42 as ::core::ffi::c_int, 9001 as ::core::ffi::c_int]; + let mut int_array_ptr: *mut [::core::ffi::c_int; 2] = &raw mut c2rust_fresh3; + let mut c2rust_fresh4: [::core::ffi::c_char; 6] = + ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0"); + let mut char_array_ptr: *mut [::core::ffi::c_char; 6] = &raw mut c2rust_fresh4; + let mut macro_single_int: ::core::ffi::c_int = SINGLE_INT; + let mut macro_single_int_ptr: *mut ::core::ffi::c_int = + &mut SINGLE_INT as *mut ::core::ffi::c_int; + let mut macro_int_ptr_to_array: *mut ::core::ffi::c_int = INT_ARRAY.as_mut_ptr(); + let mut macro_char_ptr_to_array: *mut ::core::ffi::c_char = CHAR_ARRAY.as_mut_ptr(); + let mut macro_int_array_ptr: *mut [::core::ffi::c_int; 2] = + &mut INT_ARRAY as *mut [::core::ffi::c_int; 2]; + let mut macro_char_array_ptr: *mut [::core::ffi::c_char; 6] = + &mut CHAR_ARRAY as *mut [::core::ffi::c_char; 6]; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2024.snap similarity index 98% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2024.snap index 54720d61a5..94082044de 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/compound_literals.rs +expression: cat tests/snapshots/compound_literals.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.2021.snap new file mode 100644 index 0000000000..e9a235f2f4 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.2021.snap @@ -0,0 +1,21 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/empty_init.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct Scope { + pub next: *mut Scope, +} +#[no_mangle] +pub static mut scope: *mut Scope = &Scope { + next: ::core::ptr::null::() as *mut Scope, +} as *const Scope as *mut Scope; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.2024.snap similarity index 89% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.2024.snap index ba486bc4d9..e26fedcef7 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/empty_init.rs +expression: cat tests/snapshots/empty_init.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2021.snap new file mode 100644 index 0000000000..026724f05d --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2021.snap @@ -0,0 +1,65 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/exprs.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(label_break_value, raw_ref_op)] +extern "C" { + fn puts(str: *const ::core::ffi::c_char) -> ::core::ffi::c_int; +} +pub type C2Rust_Unnamed = ::core::ffi::c_uint; +pub const C: C2Rust_Unnamed = 2; +pub const B: C2Rust_Unnamed = 1; +pub const A: C2Rust_Unnamed = 0; +unsafe extern "C" fn side_effect() -> ::core::ffi::c_int { + puts(b"the return of side effect\0".as_ptr() as *const ::core::ffi::c_char); + return 10 as ::core::ffi::c_int; +} +#[no_mangle] +pub unsafe extern "C" fn unary_without_side_effect() { + let mut i: ::core::ffi::c_int = 5 as ::core::ffi::c_int; + -i; + i; + !i; + (i == 0) as ::core::ffi::c_int; + &raw mut i; + i; + i += 1; + i -= 1; + i -= 1; + i += 1; +} +#[no_mangle] +pub unsafe extern "C" fn unary_with_side_effect() { + let mut arr: [*mut ::core::ffi::c_char; 1] = [::core::ptr::null_mut::<::core::ffi::c_char>()]; + -side_effect(); + side_effect(); + !side_effect(); + (side_effect() == 0) as ::core::ffi::c_int; + (b"\0".as_ptr() as *const ::core::ffi::c_char).offset(side_effect() as isize) + as *const ::core::ffi::c_char; + *arr[side_effect() as usize]; + arr[side_effect() as usize] = arr[side_effect() as usize].offset(1); + arr[side_effect() as usize] = arr[side_effect() as usize].offset(-1); + arr[side_effect() as usize] = arr[side_effect() as usize].offset(1); + arr[side_effect() as usize] = arr[side_effect() as usize].offset(-1); +} +#[no_mangle] +pub unsafe extern "C" fn compound_literal() { + let mut i: ::core::ffi::c_int = B as ::core::ffi::c_int; +} +#[no_mangle] +pub unsafe extern "C" fn statement_expr() { + '_c2rust_label: { + puts(b"should execute\0".as_ptr() as *const ::core::ffi::c_char); + return; + }; + puts(b"should be unreachable!\0".as_ptr() as *const ::core::ffi::c_char); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap similarity index 97% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap index 73bfee8e43..bb3c476a29 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/exprs.rs +expression: cat tests/snapshots/exprs.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.2021.snap new file mode 100644 index 0000000000..3096dcc8b9 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.2021.snap @@ -0,0 +1,22 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/factorial.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn factorial(mut n: ::core::ffi::c_ushort) -> ::core::ffi::c_ushort { + let mut result: ::core::ffi::c_ushort = 1 as ::core::ffi::c_ushort; + let mut i: ::core::ffi::c_ushort = 1 as ::core::ffi::c_ushort; + while (i as ::core::ffi::c_int) < n as ::core::ffi::c_int { + result = (result as ::core::ffi::c_int * i as ::core::ffi::c_int) as ::core::ffi::c_ushort; + i = i.wrapping_add(1); + } + return result; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.2024.snap similarity index 93% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.2024.snap index 06f036e617..730e49e9d7 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@factorial.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/factorial.rs +expression: cat tests/snapshots/factorial.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.2021.snap new file mode 100644 index 0000000000..5eee811d42 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.2021.snap @@ -0,0 +1,21 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/gotos.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn sum(mut count: ::core::ffi::c_int) -> ::core::ffi::c_int { + let mut x: ::core::ffi::c_int = 0 as ::core::ffi::c_int; + while !(count <= 0 as ::core::ffi::c_int) { + x += count; + count -= 1; + } + return x; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.2024.snap similarity index 91% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.2024.snap index 565b1c8e05..bd7a61bb12 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@gotos.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/gotos.rs +expression: cat tests/snapshots/gotos.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.2021.snap similarity index 76% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.2021.snap index 375172ac22..9c644c24e6 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.2021.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/incomplete_arrays.rs +expression: cat tests/snapshots/incomplete_arrays.2021.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.2024.snap new file mode 100644 index 0000000000..f22b2222c3 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@incomplete_arrays.c.2024.snap @@ -0,0 +1,12 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/incomplete_arrays.2024.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.2021.snap new file mode 100644 index 0000000000..a26bd7a4f7 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.2021.snap @@ -0,0 +1,27 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/insertion.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn insertion_sort(n: ::core::ffi::c_int, p: *mut ::core::ffi::c_int) { + let mut i: ::core::ffi::c_int = 1 as ::core::ffi::c_int; + while i < n { + let tmp: ::core::ffi::c_int = *p.offset(i as isize); + let mut j: ::core::ffi::c_int = i; + while j > 0 as ::core::ffi::c_int && *p.offset((j - 1 as ::core::ffi::c_int) as isize) > tmp + { + *p.offset(j as isize) = *p.offset((j - 1 as ::core::ffi::c_int) as isize); + j -= 1; + } + *p.offset(j as isize) = tmp; + i += 1; + } +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.2024.snap similarity index 93% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.2024.snap index 1e0c9e98f5..e046d3b88b 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@insertion.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/insertion.rs +expression: cat tests/snapshots/insertion.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2021.snap similarity index 98% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2021.snap index 84b54c788f..6a13d0db95 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2021.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/keywords.rs +expression: cat tests/snapshots/keywords.2021.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2024.snap new file mode 100644 index 0000000000..4858e9e619 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@keywords.c.2024.snap @@ -0,0 +1,180 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/keywords.2024.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[unsafe(export_name = "as")] +pub unsafe extern "C" fn as_0() {} +#[unsafe(export_name = "async")] +pub unsafe extern "C" fn async_0() {} +#[unsafe(export_name = "crate")] +pub unsafe extern "C" fn crate_0() {} +#[unsafe(export_name = "dyn")] +pub unsafe extern "C" fn dyn_0() {} +#[unsafe(export_name = "false")] +pub unsafe extern "C" fn false_0() {} +#[unsafe(export_name = "fn")] +pub unsafe extern "C" fn fn_0() {} +#[unsafe(export_name = "impl")] +pub unsafe extern "C" fn impl_0() {} +#[unsafe(export_name = "in")] +pub unsafe extern "C" fn in_0() {} +#[unsafe(export_name = "let")] +pub unsafe extern "C" fn let_0() {} +#[unsafe(export_name = "loop")] +pub unsafe extern "C" fn loop_0() {} +#[unsafe(export_name = "match")] +pub unsafe extern "C" fn match_0() {} +#[unsafe(export_name = "mod")] +pub unsafe extern "C" fn mod_0() {} +#[unsafe(export_name = "move")] +pub unsafe extern "C" fn move_0() {} +#[unsafe(export_name = "mut")] +pub unsafe extern "C" fn mut_0() {} +#[unsafe(export_name = "pub")] +pub unsafe extern "C" fn pub_0() {} +#[unsafe(export_name = "ref")] +pub unsafe extern "C" fn ref_0() {} +#[unsafe(export_name = "self")] +pub unsafe extern "C" fn self_0() {} +#[unsafe(export_name = "Self")] +pub unsafe extern "C" fn Self_0() {} +#[unsafe(export_name = "super")] +pub unsafe extern "C" fn super_0() {} +#[unsafe(export_name = "trait")] +pub unsafe extern "C" fn trait_0() {} +#[unsafe(export_name = "true")] +pub unsafe extern "C" fn true_0() {} +#[unsafe(export_name = "type")] +pub unsafe extern "C" fn type_0() {} +#[unsafe(export_name = "unsafe")] +pub unsafe extern "C" fn unsafe_0() {} +#[unsafe(export_name = "use")] +pub unsafe extern "C" fn use_0() {} +#[unsafe(export_name = "where")] +pub unsafe extern "C" fn where_0() {} +#[unsafe(export_name = "abstract")] +pub unsafe extern "C" fn abstract_0() {} +#[unsafe(export_name = "become")] +pub unsafe extern "C" fn become_0() {} +#[unsafe(export_name = "box")] +pub unsafe extern "C" fn box_0() {} +#[unsafe(export_name = "final")] +pub unsafe extern "C" fn final_0() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn gen() {} +#[unsafe(export_name = "macro")] +pub unsafe extern "C" fn macro_0() {} +#[unsafe(export_name = "override")] +pub unsafe extern "C" fn override_0() {} +#[unsafe(export_name = "priv")] +pub unsafe extern "C" fn priv_0() {} +#[unsafe(export_name = "try")] +pub unsafe extern "C" fn try_0() {} +#[unsafe(export_name = "unsized")] +pub unsafe extern "C" fn unsized_0() {} +#[unsafe(export_name = "virtual")] +pub unsafe extern "C" fn virtual_0() {} +#[unsafe(export_name = "yield")] +pub unsafe extern "C" fn yield_0() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Copy() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Send() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Sized() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Sync() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Drop() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Fn() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn FnMut() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn FnOnce() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Box() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn ToOwned() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Clone() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn PartialEq() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn PartialOrd() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Eq() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Ord() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn AsRef() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn AsMut() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Into() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn From() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Default() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Iterator() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Extend() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn IntoIterator() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn DoubleEndedIterator() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn ExactSizeIterator() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Option() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Result() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn SliceConcatExt() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn String() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn ToString() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn Vec() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn bool() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn f32() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn f64() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn i8() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn i16() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn i32() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn i64() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn i128() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn isize() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn u8() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn u16() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn u32() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn u64() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn u128() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn usize() {} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn str() {} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.2021.snap new file mode 100644 index 0000000000..3695088a01 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.2021.snap @@ -0,0 +1,32 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/macrocase.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +pub type ZSTD_dParameter = ::core::ffi::c_uint; +pub const ZSTD_d_experimentalParam1: ZSTD_dParameter = 1000; +pub const ZSTD_d_windowLogMax: ZSTD_dParameter = 100; +pub const ZSTD_d_format: ::core::ffi::c_uint = 1000 as ::core::ffi::c_uint; +#[no_mangle] +pub unsafe extern "C" fn ZSTD_dParam_getBounds(mut dParam: ZSTD_dParameter) -> ::core::ffi::c_int { + let mut bounds: ::core::ffi::c_int = 0 as ::core::ffi::c_int; + match dParam as ::core::ffi::c_uint { + 100 => { + bounds = 1 as ::core::ffi::c_int; + return bounds; + } + 1000 => { + bounds = 5 as ::core::ffi::c_int; + return bounds; + } + _ => {} + } + return bounds; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.2024.snap similarity index 94% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.2024.snap index 1ef0062235..0d9a10200b 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macrocase.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/macrocase.rs +expression: cat tests/snapshots/macrocase.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.linux.snap new file mode 100644 index 0000000000..a0853cd25f --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.linux.snap @@ -0,0 +1,45 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/macros.2021.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +extern "C" { + fn __errno_location() -> *mut ::core::ffi::c_int; + fn memcpy( + __dest: *mut ::core::ffi::c_void, + __src: *const ::core::ffi::c_void, + __n: size_t, + ) -> *mut ::core::ffi::c_void; +} +pub type size_t = usize; +#[no_mangle] +pub unsafe extern "C" fn errno_is_error() -> bool { + return *__errno_location() != 0 as ::core::ffi::c_int; +} +#[no_mangle] +pub unsafe extern "C" fn size_of_const() -> ::core::ffi::c_int { + let mut a: [::core::ffi::c_int; 10] = [0; 10]; + return SIZE as ::core::ffi::c_int; +} +pub const SIZE: usize = ::core::mem::size_of::<[::core::ffi::c_int; 10]>(); +pub const POS: [::core::ffi::c_char; 3] = + unsafe { ::core::mem::transmute::<[u8; 3], [::core::ffi::c_char; 3]>(*b"\"]\0") }; +#[no_mangle] +pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut ::core::ffi::c_char) { + memcpy( + out as *mut ::core::ffi::c_void, + POS.as_ptr() as *const ::core::ffi::c_void, + (::core::mem::size_of::<[::core::ffi::c_char; 3]>() as size_t) + .wrapping_div(::core::mem::size_of::<::core::ffi::c_char>() as size_t) + .wrapping_sub(1 as size_t) + .wrapping_add(1 as size_t) + .wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>() as size_t), + ); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.macos.snap new file mode 100644 index 0000000000..2984ee2740 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.macos.snap @@ -0,0 +1,46 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/macros.2021.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +extern "C" { + fn __error() -> *mut ::core::ffi::c_int; + fn memcpy( + __dst: *mut ::core::ffi::c_void, + __src: *const ::core::ffi::c_void, + __n: size_t, + ) -> *mut ::core::ffi::c_void; +} +pub type __darwin_size_t = usize; +pub type size_t = __darwin_size_t; +#[no_mangle] +pub unsafe extern "C" fn errno_is_error() -> bool { + return *__error() != 0 as ::core::ffi::c_int; +} +#[no_mangle] +pub unsafe extern "C" fn size_of_const() -> ::core::ffi::c_int { + let mut a: [::core::ffi::c_int; 10] = [0; 10]; + return SIZE as ::core::ffi::c_int; +} +pub const SIZE: usize = ::core::mem::size_of::<[::core::ffi::c_int; 10]>(); +pub const POS: [::core::ffi::c_char; 3] = + unsafe { ::core::mem::transmute::<[u8; 3], [::core::ffi::c_char; 3]>(*b"\"]\0") }; +#[no_mangle] +pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut ::core::ffi::c_char) { + memcpy( + out as *mut ::core::ffi::c_void, + POS.as_ptr() as *const ::core::ffi::c_void, + (::core::mem::size_of::<[::core::ffi::c_char; 3]>() as size_t) + .wrapping_div(::core::mem::size_of::<::core::ffi::c_char>() as size_t) + .wrapping_sub(1 as size_t) + .wrapping_add(1 as size_t) + .wrapping_mul(::core::mem::size_of::<::core::ffi::c_char>() as size_t), + ); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.snap new file mode 100644 index 0000000000..58f0f4b295 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.snap @@ -0,0 +1,469 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/macros.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(raw_ref_op)] +extern "C" { + fn extern_fn() -> ::core::ffi::c_int; +} +pub type uintptr_t = usize; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct S { + pub i: ::core::ffi::c_int, +} +pub type U64 = ::core::ffi::c_ulonglong; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct fn_ptrs { + pub v: *mut ::core::ffi::c_void, + pub fn1: Option ::core::ffi::c_int>, + pub fn2: Option ::core::ffi::c_int>, +} +pub type zstd_platform_dependent_type = ::core::ffi::c_long; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct ntlmdata { + pub target_info_len: ::core::ffi::c_uint, +} +pub const true_0: ::core::ffi::c_int = 1 as ::core::ffi::c_int; +pub const UINTPTR_MAX: ::core::ffi::c_ulong = 18446744073709551615 as ::core::ffi::c_ulong; +pub const LITERAL_INT: ::core::ffi::c_int = 0xffff as ::core::ffi::c_int; +pub const LITERAL_BOOL: ::core::ffi::c_int = true_0; +pub const LITERAL_FLOAT: ::core::ffi::c_double = 3.14f64; +pub const LITERAL_CHAR: ::core::ffi::c_int = 'x' as i32; +pub const LITERAL_STR: [::core::ffi::c_char; 6] = + unsafe { ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0") }; +pub const LITERAL_STRUCT: S = S { + i: 5 as ::core::ffi::c_int, +}; +pub const NESTED_INT: ::core::ffi::c_int = LITERAL_INT; +pub const NESTED_BOOL: ::core::ffi::c_int = LITERAL_BOOL; +pub const NESTED_FLOAT: ::core::ffi::c_double = LITERAL_FLOAT; +pub const NESTED_CHAR: ::core::ffi::c_int = LITERAL_CHAR; +pub const NESTED_STR: [::core::ffi::c_char; 6] = LITERAL_STR; +pub const NESTED_STRUCT: S = LITERAL_STRUCT; +pub const PARENS: ::core::ffi::c_int = NESTED_INT * (LITERAL_CHAR + true_0); +pub const PTR_ARITHMETIC: *const ::core::ffi::c_char = unsafe { + LITERAL_STR + .as_ptr() + .offset(5 as ::core::ffi::c_int as isize) + .offset(-(3 as ::core::ffi::c_int as isize)) +}; +pub const WIDENING_CAST: ::core::ffi::c_ulonglong = LITERAL_INT as ::core::ffi::c_ulonglong; +pub const CONVERSION_CAST: ::core::ffi::c_double = LITERAL_INT as ::core::ffi::c_double; +#[no_mangle] +pub unsafe extern "C" fn local_muts() { + let mut literal_int: ::core::ffi::c_int = LITERAL_INT; + let mut literal_bool: bool = LITERAL_BOOL != 0; + let mut literal_float: ::core::ffi::c_float = LITERAL_FLOAT as ::core::ffi::c_float; + let mut literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char; + let mut literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr(); + let mut literal_str: [::core::ffi::c_char; 6] = LITERAL_STR; + let mut literal_array: [::core::ffi::c_int; 3] = [ + 1 as ::core::ffi::c_int, + 2 as ::core::ffi::c_int, + 3 as ::core::ffi::c_int, + ]; + let mut literal_struct: S = LITERAL_STRUCT; + let mut nested_int: ::core::ffi::c_int = NESTED_INT; + let mut nested_bool: bool = NESTED_BOOL != 0; + let mut nested_float: ::core::ffi::c_float = NESTED_FLOAT as ::core::ffi::c_float; + let mut nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char; + let mut nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr(); + let mut nested_str: [::core::ffi::c_char; 6] = NESTED_STR; + let mut nested_array: [::core::ffi::c_int; 3] = [ + 1 as ::core::ffi::c_int, + 2 as ::core::ffi::c_int, + 3 as ::core::ffi::c_int, + ]; + let mut nested_struct: S = NESTED_STRUCT; + let mut int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; + let mut mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double + - true_0 as ::core::ffi::c_double) + as ::core::ffi::c_float; + let mut parens: ::core::ffi::c_int = PARENS; + let mut ptr_arithmetic: *const ::core::ffi::c_char = PTR_ARITHMETIC; + let mut widening_cast: ::core::ffi::c_ulonglong = WIDENING_CAST; + let mut narrowing_cast: ::core::ffi::c_char = LITERAL_INT as ::core::ffi::c_char; + let mut conversion_cast: ::core::ffi::c_double = CONVERSION_CAST; + let mut indexing: ::core::ffi::c_char = + NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; + let mut str_concatenation_ptr: *const ::core::ffi::c_char = + b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char; + let mut str_concatenation: [::core::ffi::c_char; 18] = + ::core::mem::transmute::<[u8; 18], [::core::ffi::c_char; 18]>(*b"hello hello world\0"); + let mut builtin: ::core::ffi::c_int = + (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32; + let mut ref_indexing: *const ::core::ffi::c_char = NESTED_STR + .as_ptr() + .offset(LITERAL_FLOAT as ::core::ffi::c_int as isize) + as *const ::core::ffi::c_char; + let mut ref_struct: *const S = &mut LITERAL_STRUCT as *mut S; + let mut ternary: ::core::ffi::c_int = if LITERAL_BOOL != 0 { + 1 as ::core::ffi::c_int + } else { + 2 as ::core::ffi::c_int + }; + let mut member: ::core::ffi::c_int = LITERAL_STRUCT.i; + let mut stmt_expr: ::core::ffi::c_float = ({ + let mut builtin_0: ::core::ffi::c_int = + (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32; + let mut indexing_0: ::core::ffi::c_char = + NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; + let mut mixed: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double + - true_0 as ::core::ffi::c_double) + as ::core::ffi::c_float; + let mut i: ::core::ffi::c_int = 0 as ::core::ffi::c_int; + while i < builtin_0 { + mixed += indexing_0 as ::core::ffi::c_float; + i += 1; + } + mixed + }); +} +#[no_mangle] +pub unsafe extern "C" fn local_consts() { + let literal_int: ::core::ffi::c_int = LITERAL_INT; + let literal_bool: bool = LITERAL_BOOL != 0; + let literal_float: ::core::ffi::c_float = LITERAL_FLOAT as ::core::ffi::c_float; + let literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char; + let literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr(); + let literal_str: [::core::ffi::c_char; 6] = LITERAL_STR; + let literal_array: [::core::ffi::c_int; 3] = [ + 1 as ::core::ffi::c_int, + 2 as ::core::ffi::c_int, + 3 as ::core::ffi::c_int, + ]; + let literal_struct: S = LITERAL_STRUCT; + let nested_int: ::core::ffi::c_int = NESTED_INT; + let nested_bool: bool = NESTED_BOOL != 0; + let nested_float: ::core::ffi::c_float = NESTED_FLOAT as ::core::ffi::c_float; + let nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char; + let nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr(); + let nested_str: [::core::ffi::c_char; 6] = NESTED_STR; + let nested_array: [::core::ffi::c_int; 3] = [ + 1 as ::core::ffi::c_int, + 2 as ::core::ffi::c_int, + 3 as ::core::ffi::c_int, + ]; + let nested_struct: S = NESTED_STRUCT; + let int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; + let mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double + - true_0 as ::core::ffi::c_double) + as ::core::ffi::c_float; + let parens: ::core::ffi::c_int = PARENS; + let ptr_arithmetic: *const ::core::ffi::c_char = PTR_ARITHMETIC; + let widening_cast: ::core::ffi::c_ulonglong = WIDENING_CAST; + let narrowing_cast: ::core::ffi::c_char = LITERAL_INT as ::core::ffi::c_char; + let conversion_cast: ::core::ffi::c_double = CONVERSION_CAST; + let indexing: ::core::ffi::c_char = NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; + let str_concatenation_ptr: *const ::core::ffi::c_char = + b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char; + let str_concatenation: [::core::ffi::c_char; 18] = + ::core::mem::transmute::<[u8; 18], [::core::ffi::c_char; 18]>(*b"hello hello world\0"); + let builtin: ::core::ffi::c_int = (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32; + let ref_indexing: *const ::core::ffi::c_char = NESTED_STR + .as_ptr() + .offset(LITERAL_FLOAT as ::core::ffi::c_int as isize) + as *const ::core::ffi::c_char; + let ref_struct: *const S = &mut LITERAL_STRUCT as *mut S; + let ternary: ::core::ffi::c_int = if LITERAL_BOOL != 0 { + 1 as ::core::ffi::c_int + } else { + 2 as ::core::ffi::c_int + }; + let member: ::core::ffi::c_int = LITERAL_STRUCT.i; + let stmt_expr: ::core::ffi::c_float = ({ + let mut builtin_0: ::core::ffi::c_int = + (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32; + let mut indexing_0: ::core::ffi::c_char = + NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; + let mut mixed: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double + + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double + - true_0 as ::core::ffi::c_double) + as ::core::ffi::c_float; + let mut i: ::core::ffi::c_int = 0 as ::core::ffi::c_int; + while i < builtin_0 { + mixed += indexing_0 as ::core::ffi::c_float; + i += 1; + } + mixed + }); +} +static mut global_static_const_literal_int: ::core::ffi::c_int = LITERAL_INT; +static mut global_static_const_literal_bool: bool = LITERAL_BOOL != 0; +static mut global_static_const_literal_float: ::core::ffi::c_float = + LITERAL_FLOAT as ::core::ffi::c_float; +static mut global_static_const_literal_char: ::core::ffi::c_char = + LITERAL_CHAR as ::core::ffi::c_char; +static mut global_static_const_literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr(); +static mut global_static_const_literal_str: [::core::ffi::c_char; 6] = LITERAL_STR; +static mut global_static_const_literal_array: [::core::ffi::c_int; 3] = [ + 1 as ::core::ffi::c_int, + 2 as ::core::ffi::c_int, + 3 as ::core::ffi::c_int, +]; +static mut global_static_const_literal_struct: S = LITERAL_STRUCT; +static mut global_static_const_nested_int: ::core::ffi::c_int = NESTED_INT; +static mut global_static_const_nested_bool: bool = NESTED_BOOL != 0; +static mut global_static_const_nested_float: ::core::ffi::c_float = + NESTED_FLOAT as ::core::ffi::c_float; +static mut global_static_const_nested_char: ::core::ffi::c_char = + NESTED_CHAR as ::core::ffi::c_char; +static mut global_static_const_nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr(); +static mut global_static_const_nested_str: [::core::ffi::c_char; 6] = NESTED_STR; +static mut global_static_const_nested_array: [::core::ffi::c_int; 3] = [ + 1 as ::core::ffi::c_int, + 2 as ::core::ffi::c_int, + 3 as ::core::ffi::c_int, +]; +static mut global_static_const_nested_struct: S = NESTED_STRUCT; +static mut global_static_const_int_arithmetic: ::core::ffi::c_int = + NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; +static mut global_static_const_mixed_arithmetic: ::core::ffi::c_float = + (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double + - true_0 as ::core::ffi::c_double) as ::core::ffi::c_float; +static mut global_static_const_parens: ::core::ffi::c_int = PARENS; +static mut global_static_const_ptr_arithmetic: *const ::core::ffi::c_char = + ::core::ptr::null::<::core::ffi::c_char>(); +static mut global_static_const_widening_cast: ::core::ffi::c_ulonglong = WIDENING_CAST; +static mut global_static_const_narrowing_cast: ::core::ffi::c_char = + LITERAL_INT as ::core::ffi::c_char; +static mut global_static_const_conversion_cast: ::core::ffi::c_double = CONVERSION_CAST; +static mut global_static_const_indexing: ::core::ffi::c_char = 0; +static mut global_static_const_str_concatenation_ptr: *const ::core::ffi::c_char = + b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char; +static mut global_static_const_str_concatenation: [::core::ffi::c_char; 18] = unsafe { + ::core::mem::transmute::<[u8; 18], [::core::ffi::c_char; 18]>(*b"hello hello world\0") +}; +static mut global_static_const_builtin: ::core::ffi::c_int = + (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32; +static mut global_static_const_ref_indexing: *const ::core::ffi::c_char = + ::core::ptr::null::<::core::ffi::c_char>(); +static mut global_static_const_ref_struct: *const S = &LITERAL_STRUCT as *const S as *mut S; +static mut global_static_const_ternary: ::core::ffi::c_int = 0; +static mut global_static_const_member: ::core::ffi::c_int = 0; +#[no_mangle] +pub unsafe extern "C" fn global_static_consts() {} +#[no_mangle] +pub static mut global_const_literal_int: ::core::ffi::c_int = LITERAL_INT; +#[no_mangle] +pub static mut global_const_literal_bool: bool = LITERAL_BOOL != 0; +#[no_mangle] +pub static mut global_const_literal_float: ::core::ffi::c_float = + LITERAL_FLOAT as ::core::ffi::c_float; +#[no_mangle] +pub static mut global_const_literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char; +#[no_mangle] +pub static mut global_const_literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr(); +#[no_mangle] +pub static mut global_const_literal_str: [::core::ffi::c_char; 6] = LITERAL_STR; +#[no_mangle] +pub static mut global_const_literal_array: [::core::ffi::c_int; 3] = [ + 1 as ::core::ffi::c_int, + 2 as ::core::ffi::c_int, + 3 as ::core::ffi::c_int, +]; +#[no_mangle] +pub static mut global_const_literal_struct: S = LITERAL_STRUCT; +#[no_mangle] +pub static mut global_const_nested_int: ::core::ffi::c_int = NESTED_INT; +#[no_mangle] +pub static mut global_const_nested_bool: bool = NESTED_BOOL != 0; +#[no_mangle] +pub static mut global_const_nested_float: ::core::ffi::c_float = + NESTED_FLOAT as ::core::ffi::c_float; +#[no_mangle] +pub static mut global_const_nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char; +#[no_mangle] +pub static mut global_const_nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr(); +#[no_mangle] +pub static mut global_const_nested_str: [::core::ffi::c_char; 6] = NESTED_STR; +#[no_mangle] +pub static mut global_const_nested_array: [::core::ffi::c_int; 3] = [ + 1 as ::core::ffi::c_int, + 2 as ::core::ffi::c_int, + 3 as ::core::ffi::c_int, +]; +#[no_mangle] +pub static mut global_const_nested_struct: S = NESTED_STRUCT; +#[no_mangle] +pub static mut global_const_int_arithmetic: ::core::ffi::c_int = + NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int; +#[no_mangle] +pub static mut global_const_mixed_arithmetic: ::core::ffi::c_float = + (LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double + - true_0 as ::core::ffi::c_double) as ::core::ffi::c_float; +#[no_mangle] +pub static mut global_const_parens: ::core::ffi::c_int = PARENS; +#[no_mangle] +pub static mut global_const_ptr_arithmetic: *const ::core::ffi::c_char = + ::core::ptr::null::<::core::ffi::c_char>(); +#[no_mangle] +pub static mut global_const_widening_cast: ::core::ffi::c_ulonglong = WIDENING_CAST; +#[no_mangle] +pub static mut global_const_narrowing_cast: ::core::ffi::c_char = + LITERAL_INT as ::core::ffi::c_char; +#[no_mangle] +pub static mut global_const_conversion_cast: ::core::ffi::c_double = CONVERSION_CAST; +#[no_mangle] +pub static mut global_const_indexing: ::core::ffi::c_char = 0; +#[no_mangle] +pub static mut global_const_str_concatenation_ptr: *const ::core::ffi::c_char = + b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char; +#[no_mangle] +pub static mut global_const_str_concatenation: [::core::ffi::c_char; 18] = unsafe { + ::core::mem::transmute::<[u8; 18], [::core::ffi::c_char; 18]>(*b"hello hello world\0") +}; +#[no_mangle] +pub static mut global_const_builtin: ::core::ffi::c_int = + (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32; +#[no_mangle] +pub static mut global_const_ref_indexing: *const ::core::ffi::c_char = + ::core::ptr::null::<::core::ffi::c_char>(); +#[no_mangle] +pub static mut global_const_ref_struct: *const S = &LITERAL_STRUCT as *const S as *mut S; +#[no_mangle] +pub static mut global_const_ternary: ::core::ffi::c_int = 0; +#[no_mangle] +pub static mut global_const_member: ::core::ffi::c_int = 0; +#[no_mangle] +pub unsafe extern "C" fn test_fn_macro(mut x: ::core::ffi::c_int) -> ::core::ffi::c_int { + return x * x; +} +pub const TEST_CONST1: ::core::ffi::c_int = 1 as ::core::ffi::c_int; +pub const TEST_NESTED: ::core::ffi::c_int = 2 as ::core::ffi::c_int; +pub const TEST_CONST2: ::core::ffi::c_int = TEST_NESTED; +pub const TEST_PARENS: ::core::ffi::c_int = + (TEST_CONST2 + 1 as ::core::ffi::c_int) * 3 as ::core::ffi::c_int; +#[no_mangle] +pub unsafe extern "C" fn reference_define() -> ::core::ffi::c_int { + let mut x: ::core::ffi::c_int = TEST_CONST1; + x += TEST_CONST2; + if (3 as ::core::ffi::c_int) < TEST_PARENS { + x += TEST_PARENS; + } + return x; +} +#[no_mangle] +pub static mut fns: fn_ptrs = fn_ptrs { + v: ::core::ptr::null::<::core::ffi::c_void>() as *mut ::core::ffi::c_void, + fn1: None, + fn2: None, +}; +#[no_mangle] +pub static mut p: *const fn_ptrs = unsafe { &raw const fns }; +pub const ZSTD_WINDOWLOG_MAX_32: ::core::ffi::c_int = 30 as ::core::ffi::c_int; +pub const ZSTD_WINDOWLOG_MAX_64: ::core::ffi::c_int = 31 as ::core::ffi::c_int; +#[no_mangle] +pub unsafe extern "C" fn test_zstd() -> U64 { + return (if ::core::mem::size_of::() as usize == 4 as usize { + ZSTD_WINDOWLOG_MAX_32 + } else { + ZSTD_WINDOWLOG_MAX_64 + }) as U64; +} +#[no_mangle] +pub unsafe extern "C" fn stmt_expr_inc() -> ::core::ffi::c_int { + let mut a: ::core::ffi::c_int = 0 as ::core::ffi::c_int; + let mut b: *mut ::core::ffi::c_int = &raw mut a; + ({ + *b += 1; + *b; + }); + return ({ + *b += 1; + *b + }); +} +#[no_mangle] +pub unsafe extern "C" fn test_switch(mut x: ::core::ffi::c_int) -> ::core::ffi::c_int { + match x { + TEST_CONST1 => return 10 as ::core::ffi::c_int, + TEST_NESTED => return 20 as ::core::ffi::c_int, + _ => {} + } + return 0 as ::core::ffi::c_int; +} +pub const silk_int16_MIN: ::core::ffi::c_short = + 0x8000 as ::core::ffi::c_int as ::core::ffi::c_short; +#[no_mangle] +pub unsafe extern "C" fn test_silk_int16_MIN() -> ::core::ffi::c_int { + let mut _null: ::core::ffi::c_char = + ::core::mem::transmute::<[u8; 1], [::core::ffi::c_char; 1]>(*b"\0") + [(silk_int16_MIN as ::core::ffi::c_int + 0x8000 as ::core::ffi::c_int) as usize]; + return silk_int16_MIN as ::core::ffi::c_int; +} +#[no_mangle] +pub unsafe extern "C" fn use_extern_value() -> ::core::ffi::c_int { + return extern_fn(); +} +#[no_mangle] +pub unsafe extern "C" fn local_fn() -> ::core::ffi::c_int { + return 1234 as ::core::ffi::c_int; +} +#[no_mangle] +pub unsafe extern "C" fn use_local_value() -> ::core::ffi::c_int { + return local_fn(); +} +#[no_mangle] +pub unsafe extern "C" fn use_portable_type(mut len: uintptr_t) -> bool { + return len <= (UINTPTR_MAX as uintptr_t).wrapping_div(2 as ::core::ffi::c_int as uintptr_t); +} +#[no_mangle] +pub unsafe extern "C" fn ntlm_v2_blob_len(mut ntlm: *mut ntlmdata) -> ::core::ffi::c_uint { + return ((44 as ::core::ffi::c_int - 16 as ::core::ffi::c_int) as ::core::ffi::c_uint) + .wrapping_add((*ntlm).target_info_len) + .wrapping_add(4 as ::core::ffi::c_uint); +} +#[no_mangle] +pub unsafe extern "C" fn late_init_var() -> ::core::ffi::c_int { + return ({ + let mut i: ::core::ffi::c_int = 0; + i = 1 as ::core::ffi::c_int; + i + }); +} +unsafe extern "C" fn c2rust_run_static_initializers() { + global_static_const_ptr_arithmetic = PTR_ARITHMETIC; + global_static_const_indexing = NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; + global_static_const_ref_indexing = NESTED_STR + .as_ptr() + .offset(LITERAL_FLOAT as ::core::ffi::c_int as isize) + as *const ::core::ffi::c_char; + global_static_const_ternary = if LITERAL_BOOL != 0 { + 1 as ::core::ffi::c_int + } else { + 2 as ::core::ffi::c_int + }; + global_static_const_member = LITERAL_STRUCT.i; + global_const_ptr_arithmetic = PTR_ARITHMETIC; + global_const_indexing = NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize]; + global_const_ref_indexing = NESTED_STR + .as_ptr() + .offset(LITERAL_FLOAT as ::core::ffi::c_int as isize) + as *const ::core::ffi::c_char; + global_const_ternary = if LITERAL_BOOL != 0 { + 1 as ::core::ffi::c_int + } else { + 2 as ::core::ffi::c_int + }; + global_const_member = LITERAL_STRUCT.i; +} +#[used] +#[cfg_attr(target_os = "linux", link_section = ".init_array")] +#[cfg_attr(target_os = "windows", link_section = ".CRT$XIB")] +#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")] +static INIT_ARRAY: [unsafe extern "C" fn(); 1] = [c2rust_run_static_initializers]; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.linux.snap similarity index 95% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.linux.snap index ea833a9cff..eddd0fc591 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/macros.linux.rs +expression: cat tests/snapshots/os-specific/macros.2024.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.macos.snap similarity index 95% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.macos.snap index 8c0118c15e..c4547a9d95 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/macros.macos.rs +expression: cat tests/snapshots/os-specific/macros.2024.macos.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.snap similarity index 99% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.snap index 244389b91c..15fcf4470a 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/macros.rs +expression: cat tests/snapshots/macros.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.2021.snap similarity index 97% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.2021.snap index 0579dc8c28..36326178ed 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.2021.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/main_fn.rs +expression: cat tests/snapshots/main_fn.2021.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.2024.snap new file mode 100644 index 0000000000..c41213fac0 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@main_fn.c.2024.snap @@ -0,0 +1,50 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/main_fn.2024.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +unsafe fn main_0( + mut argc: ::core::ffi::c_int, + mut argv: *mut *mut ::core::ffi::c_char, + mut envp: *mut *mut ::core::ffi::c_char, +) -> ::core::ffi::c_int { + return 0 as ::core::ffi::c_int; +} +pub fn main() { + let mut args_strings: Vec> = ::std::env::args() + .map(|arg| { + ::std::ffi::CString::new(arg) + .expect("Failed to convert argument into CString.") + .into_bytes_with_nul() + }) + .collect(); + let mut args_ptrs: Vec<*mut ::core::ffi::c_char> = args_strings + .iter_mut() + .map(|arg| arg.as_mut_ptr() as *mut ::core::ffi::c_char) + .chain(::core::iter::once(::core::ptr::null_mut())) + .collect(); + let mut vars: Vec<*mut ::core::ffi::c_char> = Vec::new(); + for (var_name, var_value) in ::std::env::vars() { + let var: String = format!("{}={}", var_name, var_value); + vars.push( + ::std::ffi::CString::new(var) + .expect("Failed to convert environment variable into CString.") + .into_raw(), + ); + } + vars.push(::core::ptr::null_mut()); + unsafe { + ::std::process::exit(main_0( + (args_ptrs.len() - 1) as ::core::ffi::c_int, + args_ptrs.as_mut_ptr() as *mut *mut ::core::ffi::c_char, + vars.as_mut_ptr() as *mut *mut ::core::ffi::c_char, + ) as i32) + } +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2021.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2021.linux.snap new file mode 100644 index 0000000000..2720b14218 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2021.linux.snap @@ -0,0 +1,21 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/out_of_range_lit.2021.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +pub type __int32_t = i32; +pub type int32_t = __int32_t; +#[no_mangle] +pub unsafe extern "C" fn f() { + let mut a: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; + let mut b: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; + let mut c: int32_t = 0x8000000000000000 as ::core::ffi::c_ulong as int32_t; + let mut d: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2021.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2021.macos.snap new file mode 100644 index 0000000000..5d14ef80ef --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2021.macos.snap @@ -0,0 +1,20 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/out_of_range_lit.2021.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +pub type int32_t = i32; +#[no_mangle] +pub unsafe extern "C" fn f() { + let mut a: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; + let mut b: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; + let mut c: int32_t = 0x8000000000000000 as ::core::ffi::c_ulong as int32_t; + let mut d: int32_t = 0x80000000 as ::core::ffi::c_uint as int32_t; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2024.linux.snap similarity index 88% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2024.linux.snap index e192665b01..42390110bb 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2024.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/out_of_range_lit.linux.rs +expression: cat tests/snapshots/os-specific/out_of_range_lit.2024.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2024.macos.snap similarity index 88% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2024.macos.snap index 1910c4733f..e1fd8a9f51 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@out_of_range_lit.c.2024.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/out_of_range_lit.rs +expression: cat tests/snapshots/os-specific/out_of_range_lit.2024.macos.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.2021.snap new file mode 100644 index 0000000000..63030276ac --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.2021.snap @@ -0,0 +1,24 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/predefined.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn predefined() { + let mut line: ::core::ffi::c_int = 2 as ::core::ffi::c_int; + let mut file_name: *const ::core::ffi::c_char = + b"predefined.c\0".as_ptr() as *const ::core::ffi::c_char; + let mut func: *const ::core::ffi::c_char = + b"predefined\0".as_ptr() as *const ::core::ffi::c_char; +} +#[no_mangle] +pub unsafe extern "C" fn extension_operator() -> *const ::core::ffi::c_char { + return b"const char *extension_operator()\0".as_ptr() as *const ::core::ffi::c_char; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.2024.snap similarity index 93% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.2024.snap index a580bdf83b..44f46ff5f8 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@predefined.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/predefined.rs +expression: cat tests/snapshots/predefined.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.2021.snap new file mode 100644 index 0000000000..969f6cc77c --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.2021.snap @@ -0,0 +1,87 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/records.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct AnonEnumInStruct {} +pub type C2Rust_Unnamed = ::core::ffi::c_uint; +pub const VALUE2: C2Rust_Unnamed = 1; +pub const VALUE1: C2Rust_Unnamed = 0; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct AnonStructInStruct { + pub c2rust_unnamed: C2Rust_Unnamed_0, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct C2Rust_Unnamed_0 { + pub some_number: ::core::ffi::c_int, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct NestedStructInStruct {} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct InsideStruct { + pub yup: ::core::ffi::c_int, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union AnonEnumInUnion { + pub a: ::core::ffi::c_int, +} +pub type C2Rust_Unnamed_1 = ::core::ffi::c_uint; +pub const VALUE4: C2Rust_Unnamed_1 = 1; +pub const VALUE3: C2Rust_Unnamed_1 = 0; +#[derive(Copy, Clone)] +#[repr(C)] +pub union AnonStructInUnion { + pub c2rust_unnamed: C2Rust_Unnamed_2, + pub a: ::core::ffi::c_int, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct C2Rust_Unnamed_2 { + pub some_number: ::core::ffi::c_int, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union NestedStructInUnion { + pub a: ::core::ffi::c_int, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct InsideUnion { + pub yup: ::core::ffi::c_int, +} +#[no_mangle] +pub unsafe extern "C" fn struct_declaration() { + let mut value: ::core::ffi::c_int = VALUE2 as ::core::ffi::c_int; + let mut a: AnonEnumInStruct = AnonEnumInStruct {}; + let mut b: AnonStructInStruct = AnonStructInStruct { + c2rust_unnamed: C2Rust_Unnamed_0 { some_number: 0 }, + }; + b.c2rust_unnamed.some_number = 7 as ::core::ffi::c_int; + let mut c: NestedStructInStruct = NestedStructInStruct {}; + let mut d: InsideStruct = InsideStruct { yup: 0 }; +} +#[no_mangle] +pub unsafe extern "C" fn union_declaration() { + let mut value: ::core::ffi::c_int = VALUE4 as ::core::ffi::c_int; + let mut a: AnonEnumInUnion = AnonEnumInUnion { a: 0 }; + let mut b: AnonStructInUnion = AnonStructInUnion { + c2rust_unnamed: C2Rust_Unnamed_2 { some_number: 0 }, + }; + b.c2rust_unnamed.some_number = 99 as ::core::ffi::c_int; + let mut c: NestedStructInUnion = NestedStructInUnion { a: 0 }; + let mut d: InsideUnion = InsideUnion { yup: 0 }; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.2024.snap similarity index 98% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.2024.snap index 5becb36fb8..6b8215a92d 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@records.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/records.rs +expression: cat tests/snapshots/records.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2021.snap new file mode 100644 index 0000000000..7b1b1f2a58 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2021.snap @@ -0,0 +1,35 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/ref_ub.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(raw_ref_op)] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct Foo { + pub len: ::core::ffi::c_int, +} +#[no_mangle] +pub unsafe extern "C" fn dec(mut f: *mut Foo) { + (*f).len -= 1; +} +unsafe fn main_0() -> ::core::ffi::c_int { + let mut f: Foo = Foo { + len: 5 as ::core::ffi::c_int, + }; + let mut fp: *mut Foo = &raw mut f; + dec(fp); + f.len = 6 as ::core::ffi::c_int; + dec(fp); + return 0; +} +pub fn main() { + unsafe { ::std::process::exit(main_0() as i32) } +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2024.snap similarity index 93% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2024.snap index 3a07799afc..2e4c3b904d 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/ref_ub.rs +expression: cat tests/snapshots/ref_ub.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2021.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2021.linux.snap new file mode 100644 index 0000000000..86c26a329c --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2021.linux.snap @@ -0,0 +1,35 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/rnd.2021.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +extern "C" { + fn abs(__x: ::core::ffi::c_int) -> ::core::ffi::c_int; +} +pub type __int32_t = i32; +pub type __uint32_t = u32; +pub type int32_t = __int32_t; +pub type uint32_t = __uint32_t; +#[no_mangle] +pub static mut cur_rand_seed: uint32_t = 0 as uint32_t; +#[no_mangle] +pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { + cur_rand_seed = s; +} +#[no_mangle] +pub unsafe extern "C" fn get_rand_seed() -> uint32_t { + let INCREMENT: uint32_t = 1 as uint32_t; + let MULTIPLIER: uint32_t = 0x15a4e35 as uint32_t; + cur_rand_seed = MULTIPLIER + .wrapping_mul(cur_rand_seed) + .wrapping_add(INCREMENT); + let mut ret: uint32_t = abs(cur_rand_seed as ::core::ffi::c_int) as uint32_t; + return ret; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2021.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2021.macos.snap new file mode 100644 index 0000000000..a84684aea4 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2021.macos.snap @@ -0,0 +1,33 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/rnd.2021.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +extern "C" { + fn abs(_: ::core::ffi::c_int) -> ::core::ffi::c_int; +} +pub type int32_t = i32; +pub type uint32_t = u32; +#[no_mangle] +pub static mut cur_rand_seed: uint32_t = 0 as uint32_t; +#[no_mangle] +pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { + cur_rand_seed = s; +} +#[no_mangle] +pub unsafe extern "C" fn get_rand_seed() -> uint32_t { + let INCREMENT: uint32_t = 1 as uint32_t; + let MULTIPLIER: uint32_t = 0x15a4e35 as uint32_t; + cur_rand_seed = MULTIPLIER + .wrapping_mul(cur_rand_seed) + .wrapping_add(INCREMENT); + let mut ret: uint32_t = abs(cur_rand_seed as ::core::ffi::c_int) as uint32_t; + return ret; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2024.linux.snap similarity index 93% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2024.linux.snap index 6810985c93..730711eaeb 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2024.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/rnd.linux.rs +expression: cat tests/snapshots/os-specific/rnd.2024.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2024.macos.snap similarity index 93% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2024.macos.snap index fac56a445c..bc6a60ba3d 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rnd.c.2024.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/rnd.macos.rs +expression: cat tests/snapshots/os-specific/rnd.2024.macos.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.linux.snap new file mode 100644 index 0000000000..b09b5d3b5a --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.linux.snap @@ -0,0 +1,28 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/rotate.2021.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn rotate_left_64( + mut x: ::core::ffi::c_ulonglong, +) -> ::core::ffi::c_ulonglong { + return (x as ::core::ffi::c_ulong) + .rotate_left(4 as ::core::ffi::c_int as ::core::ffi::c_ulong as u32) + as ::core::ffi::c_ulonglong; +} +#[no_mangle] +pub unsafe extern "C" fn rotate_right_64( + mut x: ::core::ffi::c_ulonglong, +) -> ::core::ffi::c_ulonglong { + return (x as ::core::ffi::c_ulong) + .rotate_right(4 as ::core::ffi::c_int as ::core::ffi::c_ulong as u32) + as ::core::ffi::c_ulonglong; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.macos.snap new file mode 100644 index 0000000000..9aab0b8016 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.macos.snap @@ -0,0 +1,24 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/rotate.2021.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn rotate_left_64( + mut x: ::core::ffi::c_ulonglong, +) -> ::core::ffi::c_ulonglong { + return x.rotate_left(4 as ::core::ffi::c_int as ::core::ffi::c_ulonglong as u32); +} +#[no_mangle] +pub unsafe extern "C" fn rotate_right_64( + mut x: ::core::ffi::c_ulonglong, +) -> ::core::ffi::c_ulonglong { + return x.rotate_right(4 as ::core::ffi::c_int as ::core::ffi::c_ulonglong as u32); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.snap new file mode 100644 index 0000000000..109644c782 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2021.snap @@ -0,0 +1,36 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/rotate.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn rotate_left_8(mut x: ::core::ffi::c_uchar) -> ::core::ffi::c_uchar { + return x.rotate_left(1 as ::core::ffi::c_int as ::core::ffi::c_uchar as u32); +} +#[no_mangle] +pub unsafe extern "C" fn rotate_left_16(mut x: ::core::ffi::c_ushort) -> ::core::ffi::c_ushort { + return x.rotate_left(2 as ::core::ffi::c_int as ::core::ffi::c_ushort as u32); +} +#[no_mangle] +pub unsafe extern "C" fn rotate_left_32(mut x: ::core::ffi::c_uint) -> ::core::ffi::c_uint { + return x.rotate_left(3 as ::core::ffi::c_int as ::core::ffi::c_uint as u32); +} +#[no_mangle] +pub unsafe extern "C" fn rotate_right_8(mut x: ::core::ffi::c_uchar) -> ::core::ffi::c_uchar { + return x.rotate_right(1 as ::core::ffi::c_int as ::core::ffi::c_uchar as u32); +} +#[no_mangle] +pub unsafe extern "C" fn rotate_right_16(mut x: ::core::ffi::c_ushort) -> ::core::ffi::c_ushort { + return x.rotate_right(2 as ::core::ffi::c_int as ::core::ffi::c_ushort as u32); +} +#[no_mangle] +pub unsafe extern "C" fn rotate_right_32(mut x: ::core::ffi::c_uint) -> ::core::ffi::c_uint { + return x.rotate_right(3 as ::core::ffi::c_int as ::core::ffi::c_uint as u32); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2024.linux.snap similarity index 92% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2024.linux.snap index 54865ac254..8c6a0f0f13 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2024.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/rotate.linux.rs +expression: cat tests/snapshots/os-specific/rotate.2024.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2024.macos.snap similarity index 90% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2024.macos.snap index 828ce24563..eababf8dfa 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2024.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/rotate.macos.rs +expression: cat tests/snapshots/os-specific/rotate.2024.macos.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2024.snap similarity index 96% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2024.snap index 0064e22f34..ff1cdfb321 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@rotate.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/rotate.rs +expression: cat tests/snapshots/rotate.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2021.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2021.linux.snap new file mode 100644 index 0000000000..1230331540 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2021.linux.snap @@ -0,0 +1,21 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/sigign.2021.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +pub type __sighandler_t = Option ()>; +#[no_mangle] +pub unsafe extern "C" fn foo() { + let mut handler: Option ()> = + ::core::mem::transmute::< + ::libc::intptr_t, + Option ()>, + >(1 as ::core::ffi::c_int as ::libc::intptr_t); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2021.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2021.macos.snap new file mode 100644 index 0000000000..477d8828fa --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2021.macos.snap @@ -0,0 +1,20 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/sigign.2021.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn foo() { + let mut handler: Option ()> = + ::core::mem::transmute::< + ::libc::intptr_t, + Option ()>, + >(1 as ::core::ffi::c_int as ::libc::intptr_t); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2024.linux.snap similarity index 90% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2024.linux.snap index 2234f8a3de..6b7a3324d9 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2024.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/sigign.linux.rs +expression: cat tests/snapshots/os-specific/sigign.2024.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2024.macos.snap similarity index 88% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2024.macos.snap index 8ecc14df01..341657dc35 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@sigign.c.2024.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/sigign.macos.rs +expression: cat tests/snapshots/os-specific/sigign.2024.macos.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2021.aarch64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2021.aarch64.snap new file mode 100644 index 0000000000..003c5e5511 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2021.aarch64.snap @@ -0,0 +1,21 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/arch-specific/spin.2021.aarch64.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(stdsimd)] +#[cfg(target_arch = "aarch64")] +pub use ::core::arch::aarch64::__yield; +#[cfg(target_arch = "arm")] +pub use ::core::arch::arm::__yield; +#[no_mangle] +pub unsafe extern "C" fn spin() { + __yield(); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2021.x86_64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2021.x86_64.snap new file mode 100644 index 0000000000..55a9c9d4c9 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2021.x86_64.snap @@ -0,0 +1,21 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/arch-specific/spin.2021.x86_64.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(stdsimd)] +#[cfg(target_arch = "x86")] +pub use ::core::arch::x86::_mm_pause; +#[cfg(target_arch = "x86_64")] +pub use ::core::arch::x86_64::_mm_pause; +#[no_mangle] +pub unsafe extern "C" fn spin() { + _mm_pause(); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.aarch64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2024.aarch64.snap similarity index 86% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.aarch64.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2024.aarch64.snap index f007e3271a..56f1c100cf 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.aarch64.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2024.aarch64.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/arch-specific/spin.aarch64.rs +expression: cat tests/snapshots/arch-specific/spin.2024.aarch64.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.x86_64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2024.x86_64.snap similarity index 85% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.x86_64.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2024.x86_64.snap index bb98231811..fa2daf5d58 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.x86_64.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@spin.c.2024.x86_64.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/arch-specific/spin.x86_64.rs +expression: cat tests/snapshots/arch-specific/spin.2024.x86_64.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.2021.snap new file mode 100644 index 0000000000..dab981ee2b --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.2021.snap @@ -0,0 +1,14 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/static_assert.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn static_assert() {} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.2024.snap similarity index 82% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.2024.snap index bfb484796e..9aa593638f 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@static_assert.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/static_assert.rs +expression: cat tests/snapshots/static_assert.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.2021.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.2021.snap new file mode 100644 index 0000000000..916c053b1e --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.2021.snap @@ -0,0 +1,112 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/str_init.2021.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct s { + pub entries: [[::core::ffi::c_char; 10]; 3], +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct alpn_spec { + pub entries: [[::core::ffi::c_char; 10]; 3], + pub count: ::core::ffi::c_uint, +} +#[no_mangle] +pub unsafe extern "C" fn ptr() { + let mut _s: *const ::core::ffi::c_char = b"hello\0".as_ptr() as *const ::core::ffi::c_char; +} +#[no_mangle] +pub unsafe extern "C" fn array_deduced_length() { + let mut _s: [::core::ffi::c_char; 6] = + ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0"); +} +#[no_mangle] +pub unsafe extern "C" fn array() { + let mut _s: [::core::ffi::c_char; 10] = + ::core::mem::transmute::<[u8; 10], [::core::ffi::c_char; 10]>(*b"hello\0\0\0\0\0"); +} +#[no_mangle] +pub unsafe extern "C" fn int_array_extra_braces() { + let mut _a: [[::core::ffi::c_int; 10]; 3] = [ + [ + 1 as ::core::ffi::c_int, + 2 as ::core::ffi::c_int, + 3 as ::core::ffi::c_int, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + [0; 10], + [0; 10], + ]; +} +#[no_mangle] +pub unsafe extern "C" fn ptr_extra_braces() { + let mut _s: *const ::core::ffi::c_char = b"hello\0".as_ptr() as *const ::core::ffi::c_char; +} +#[no_mangle] +pub unsafe extern "C" fn array_extra_braces() { + let _s: [::core::ffi::c_char; 10] = + ::core::mem::transmute::<[u8; 10], [::core::ffi::c_char; 10]>(*b"hello\0\0\0\0\0"); +} +#[no_mangle] +pub unsafe extern "C" fn array_of_ptrs() { + let mut _s: [*const ::core::ffi::c_char; 3] = [ + b"hello\0".as_ptr() as *const ::core::ffi::c_char, + ::core::ptr::null::<::core::ffi::c_char>(), + ::core::ptr::null::<::core::ffi::c_char>(), + ]; +} +#[no_mangle] +pub unsafe extern "C" fn array_of_arrays() { + let mut _s: [[::core::ffi::c_char; 10]; 3] = [ + ::core::mem::transmute::<[u8; 10], [::core::ffi::c_char; 10]>(*b"hello\0\0\0\0\0"), + [0; 10], + [0; 10], + ]; +} +#[no_mangle] +pub unsafe extern "C" fn array_of_arrays_static() { + static mut _S: [[::core::ffi::c_char; 10]; 3] = unsafe { + [ + ::core::mem::transmute::<[u8; 10], [::core::ffi::c_char; 10]>(*b"hello\0\0\0\0\0"), + [0; 10], + [0; 10], + ] + }; +} +#[no_mangle] +pub unsafe extern "C" fn array_of_arrays_static_struct() { + static mut _S: s = unsafe { + s { + entries: [ + ::core::mem::transmute::<[u8; 10], [::core::ffi::c_char; 10]>(*b"hello\0\0\0\0\0"), + [0; 10], + [0; 10], + ], + } + }; +} +#[no_mangle] +pub unsafe extern "C" fn curl_alpn_spec() { + static mut _ALPN_SPEC_H11: alpn_spec = alpn_spec { + entries: [ALPN_HTTP_1_1, [0; 10], [0; 10]], + count: 1 as ::core::ffi::c_uint, + }; +} +pub const ALPN_HTTP_1_1: [::core::ffi::c_char; 10] = + unsafe { ::core::mem::transmute::<[u8; 10], [::core::ffi::c_char; 10]>(*b"http/1.1\0\0") }; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.2024.snap similarity index 98% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.2024.snap index 0236052297..219e8d2347 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.2024.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/str_init.rs +expression: cat tests/snapshots/str_init.2024.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2021.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2021.linux.snap new file mode 100644 index 0000000000..da70489241 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2021.linux.snap @@ -0,0 +1,19 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/typedefidx.2021.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +pub type size_t = usize; +pub type __uint64_t = u64; +pub type uint64_t = __uint64_t; +#[no_mangle] +pub unsafe extern "C" fn index_typedef(mut arr: *mut uint64_t) { + let mut foo3: size_t = *arr.offset(25 as ::core::ffi::c_int as isize) as size_t; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2021.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2021.macos.snap new file mode 100644 index 0000000000..1a8c83c1e6 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2021.macos.snap @@ -0,0 +1,19 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/typedefidx.2021.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +pub type __darwin_size_t = usize; +pub type size_t = __darwin_size_t; +pub type uint64_t = u64; +#[no_mangle] +pub unsafe extern "C" fn index_typedef(mut arr: *mut uint64_t) { + let mut foo3: size_t = *arr.offset(25 as ::core::ffi::c_int as isize) as size_t; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2024.linux.snap similarity index 86% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2024.linux.snap index 0c1ceda062..33278a502b 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2024.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/typedefidx.linux.rs +expression: cat tests/snapshots/os-specific/typedefidx.2024.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2024.macos.snap similarity index 86% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2024.macos.snap index 44d1433be1..5d9fe0a391 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@typedefidx.c.2024.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/typedefidx.macos.rs +expression: cat tests/snapshots/os-specific/typedefidx.2024.macos.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2021.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2021.linux.snap new file mode 100644 index 0000000000..35dccc6116 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2021.linux.snap @@ -0,0 +1,67 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/types.2021.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +pub type size_t = usize; +pub type __int8_t = i8; +pub type __uint8_t = u8; +pub type __int16_t = i16; +pub type __uint16_t = u16; +pub type __int32_t = i32; +pub type __uint32_t = u32; +pub type __int64_t = i64; +pub type __uint64_t = u64; +pub type ssize_t = isize; +pub type int8_t = __int8_t; +pub type int16_t = __int16_t; +pub type int32_t = __int32_t; +pub type int64_t = __int64_t; +pub type uint8_t = __uint8_t; +pub type uint16_t = __uint16_t; +pub type uint32_t = __uint32_t; +pub type uint64_t = __uint64_t; +pub type intptr_t = isize; +pub type uintptr_t = usize; +pub type intmax_t = ::libc::intmax_t; +pub type uintmax_t = ::libc::uintmax_t; +pub type ptrdiff_t = isize; +#[no_mangle] +pub static mut intvar: ::core::ffi::c_int = 0 as ::core::ffi::c_int; +#[no_mangle] +pub static mut sizevar: size_t = 0 as size_t; +#[no_mangle] +pub static mut ssizevar: ssize_t = 0 as ssize_t; +#[no_mangle] +pub static mut intptrvar: intptr_t = 0 as intptr_t; +#[no_mangle] +pub static mut uintptrvar: uintptr_t = 0 as uintptr_t; +#[no_mangle] +pub static mut ptrdiffvar: ptrdiff_t = 0 as ptrdiff_t; +#[no_mangle] +pub static mut uint8var: uint8_t = 0 as uint8_t; +#[no_mangle] +pub static mut uint16var: uint16_t = 0 as uint16_t; +#[no_mangle] +pub static mut uint32var: uint32_t = 0 as uint32_t; +#[no_mangle] +pub static mut uint64var: uint64_t = 0 as uint64_t; +#[no_mangle] +pub static mut int8var: int8_t = 0 as int8_t; +#[no_mangle] +pub static mut int16var: int16_t = 0 as int16_t; +#[no_mangle] +pub static mut int32var: int32_t = 0 as int32_t; +#[no_mangle] +pub static mut int64var: int64_t = 0 as int64_t; +#[no_mangle] +pub static mut maxvar: intmax_t = 0 as intmax_t; +#[no_mangle] +pub static mut umaxvar: uintmax_t = 0 as uintmax_t; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2021.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2021.macos.snap new file mode 100644 index 0000000000..72214ca083 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2021.macos.snap @@ -0,0 +1,62 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/types.2021.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +pub type __darwin_ptrdiff_t = isize; +pub type __darwin_size_t = usize; +pub type __darwin_ssize_t = isize; +pub type int8_t = i8; +pub type int16_t = i16; +pub type int32_t = i32; +pub type int64_t = i64; +pub type intptr_t = isize; +pub type uintptr_t = usize; +pub type size_t = __darwin_size_t; +pub type uint8_t = u8; +pub type uint16_t = u16; +pub type uint32_t = u32; +pub type uint64_t = u64; +pub type intmax_t = ::libc::intmax_t; +pub type uintmax_t = ::libc::uintmax_t; +pub type ptrdiff_t = __darwin_ptrdiff_t; +pub type ssize_t = __darwin_ssize_t; +#[no_mangle] +pub static mut intvar: ::core::ffi::c_int = 0 as ::core::ffi::c_int; +#[no_mangle] +pub static mut sizevar: size_t = 0 as size_t; +#[no_mangle] +pub static mut ssizevar: ssize_t = 0 as ssize_t; +#[no_mangle] +pub static mut intptrvar: intptr_t = 0 as intptr_t; +#[no_mangle] +pub static mut uintptrvar: uintptr_t = 0 as uintptr_t; +#[no_mangle] +pub static mut ptrdiffvar: ptrdiff_t = 0 as ptrdiff_t; +#[no_mangle] +pub static mut uint8var: uint8_t = 0 as uint8_t; +#[no_mangle] +pub static mut uint16var: uint16_t = 0 as uint16_t; +#[no_mangle] +pub static mut uint32var: uint32_t = 0 as uint32_t; +#[no_mangle] +pub static mut uint64var: uint64_t = 0 as uint64_t; +#[no_mangle] +pub static mut int8var: int8_t = 0 as int8_t; +#[no_mangle] +pub static mut int16var: int16_t = 0 as int16_t; +#[no_mangle] +pub static mut int32var: int32_t = 0 as int32_t; +#[no_mangle] +pub static mut int64var: int64_t = 0 as int64_t; +#[no_mangle] +pub static mut maxvar: intmax_t = 0 as intmax_t; +#[no_mangle] +pub static mut umaxvar: uintmax_t = 0 as uintmax_t; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2024.linux.snap similarity index 96% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2024.linux.snap index 316d7842db..f903f38610 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2024.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/types.linux.rs +expression: cat tests/snapshots/os-specific/types.2024.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2024.macos.snap similarity index 96% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2024.macos.snap index 417aa26cc8..fc816e220c 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@types.c.2024.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/types.macos.rs +expression: cat tests/snapshots/os-specific/types.2024.macos.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.aarch64.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2021.aarch64.macos.snap similarity index 85% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.aarch64.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2021.aarch64.macos.snap index e86442201e..de291bb1a8 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.aarch64.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2021.aarch64.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/arch-os-specific/varargs.aarch64-macos.rs +expression: cat tests/snapshots/arch-os-specific/varargs.2021.aarch64.macos.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.x86_64.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2021.x86_64.linux.snap similarity index 98% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.x86_64.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2021.x86_64.linux.snap index c86a25c979..65b357c6aa 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.x86_64.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2021.x86_64.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/arch-os-specific/varargs.x86_64.linux.rs +expression: cat tests/snapshots/arch-os-specific/varargs.2021.x86_64.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.aarch64.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.aarch64.macos.snap new file mode 100644 index 0000000000..07c66948c8 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.aarch64.macos.snap @@ -0,0 +1,23 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/arch-os-specific/varargs.2024.aarch64.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +unsafe extern "C" { + fn printf(_: *const ::core::ffi::c_char, ...) -> ::core::ffi::c_int; +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn call_printf() { + printf( + b"%d, %f\n\0".as_ptr() as *const ::core::ffi::c_char, + 10 as ::core::ffi::c_int, + 1.5f64, + ); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.x86_64.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.x86_64.linux.snap new file mode 100644 index 0000000000..4fa352f39f --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.x86_64.linux.snap @@ -0,0 +1,167 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/arch-os-specific/varargs.2024.x86_64.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(c_variadic, raw_ref_op)] +unsafe extern "C" { + fn printf(__format: *const ::core::ffi::c_char, ...) -> ::core::ffi::c_int; + fn vprintf( + __format: *const ::core::ffi::c_char, + __arg: ::core::ffi::VaList, + ) -> ::core::ffi::c_int; + fn putchar(__c: ::core::ffi::c_int) -> ::core::ffi::c_int; +} +pub type __builtin_va_list = [__va_list_tag; 1]; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct __va_list_tag { + pub gp_offset: ::core::ffi::c_uint, + pub fp_offset: ::core::ffi::c_uint, + pub overflow_arg_area: *mut ::core::ffi::c_void, + pub reg_save_area: *mut ::core::ffi::c_void, +} +pub type va_list = __builtin_va_list; +pub type size_t = usize; +#[derive()] +#[repr(C)] +pub struct vastruct<'a> { + pub args: ::core::ffi::VaListImpl<'a>, +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn call_printf() { + printf( + b"%d, %f\n\0".as_ptr() as *const ::core::ffi::c_char, + 10 as ::core::ffi::c_int, + 1.5f64, + ); +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn my_vprintf( + mut format: *const ::core::ffi::c_char, + mut ap: ::core::ffi::VaList, +) { + vprintf(format, ap.as_va_list()); +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn call_vprintf( + mut format: *const ::core::ffi::c_char, + mut c2rust_args: ... +) { + let mut ap: ::core::ffi::VaListImpl; + ap = c2rust_args.clone(); + my_vprintf(format, ap.as_va_list()); +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn my_printf(mut fmt: *const ::core::ffi::c_char, mut c2rust_args: ...) { + let mut ap: ::core::ffi::VaListImpl; + ap = c2rust_args.clone(); + while *fmt != 0 { + match *fmt as ::core::ffi::c_int { + 37 => { + fmt = fmt.offset(1); + if !(*fmt == 0) { + match *fmt as ::core::ffi::c_int { + 105 | 100 => { + printf( + b"%d\0".as_ptr() as *const ::core::ffi::c_char, + ap.arg::<::core::ffi::c_int>(), + ); + } + 102 => { + printf( + b"%f\0".as_ptr() as *const ::core::ffi::c_char, + ap.arg::<::core::ffi::c_double>(), + ); + } + 115 => { + printf( + b"%s\0".as_ptr() as *const ::core::ffi::c_char, + ap.arg::<*mut ::core::ffi::c_char>(), + ); + } + _ => {} + } + } + } + _ => { + putchar(*fmt as ::core::ffi::c_int); + } + } + fmt = fmt.offset(1); + } +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn simple_vacopy(mut fmt: *const ::core::ffi::c_char, mut c2rust_args: ...) { + let mut ap: ::core::ffi::VaListImpl; + let mut aq: ::core::ffi::VaListImpl; + ap = c2rust_args.clone(); + aq = ap.clone(); + vprintf(fmt, ap.as_va_list()); + vprintf(fmt, aq.as_va_list()); +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn valist_struct_member( + mut fmt: *const ::core::ffi::c_char, + mut c2rust_args: ... +) { + let mut a: vastruct = vastruct { + args: ::core::mem::MaybeUninit::uninit().assume_init(), + }; + let mut b: vastruct = vastruct { + args: ::core::mem::MaybeUninit::uninit().assume_init(), + }; + a.args = c2rust_args.clone(); + b.args = a.args.clone(); + vprintf(fmt, a.args.as_va_list()); + vprintf(fmt, b.args.as_va_list()); +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn valist_struct_pointer_member( + mut fmt: *const ::core::ffi::c_char, + mut c2rust_args: ... +) { + let mut a: vastruct = vastruct { + args: ::core::mem::MaybeUninit::uninit().assume_init(), + }; + let mut b: vastruct = vastruct { + args: ::core::mem::MaybeUninit::uninit().assume_init(), + }; + let mut p: *mut vastruct = &raw mut a; + let mut q: *mut vastruct = &raw mut b; + (*p).args = c2rust_args.clone(); + (*q).args = (*p).args.clone(); + vprintf(fmt, (*p).args.as_va_list()); + vprintf(fmt, (*q).args.as_va_list()); +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn restart_valist(mut fmt: *const ::core::ffi::c_char, mut c2rust_args: ...) { + let mut ap: ::core::ffi::VaListImpl; + ap = c2rust_args.clone(); + vprintf(fmt, ap.as_va_list()); + ap = c2rust_args.clone(); + vprintf(fmt, ap.as_va_list()); +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn print_int(mut ap: *mut ::core::ffi::VaListImpl) { + printf( + b"%d\0".as_ptr() as *const ::core::ffi::c_char, + (*ap).arg::<::core::ffi::c_int>(), + ); +} +#[unsafe(no_mangle)] +pub unsafe extern "C" fn borrowed_valist(mut count: size_t, mut c2rust_args: ...) { + let mut ap: ::core::ffi::VaListImpl; + ap = c2rust_args.clone(); + while count > 0 as size_t { + print_int(&raw mut ap); + count = count.wrapping_sub(1); + } +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2021.aarch64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2021.aarch64.snap new file mode 100644 index 0000000000..aa0f14aa98 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2021.aarch64.snap @@ -0,0 +1,71 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/arch-specific/vm_x86.2021.aarch64.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(raw_ref_op)] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct vm_t { + pub programStack: ::core::ffi::c_int, + pub entryOfs: ::core::ffi::c_int, + pub dataBase: *mut ::core::ffi::c_void, + pub codeBase: *mut ::core::ffi::c_void, + pub instructionPointers: *mut ::core::ffi::c_ulong, +} +pub type byte = ::core::ffi::c_uchar; +pub const MAX_VMMAIN_ARGS: ::core::ffi::c_int = 50 as ::core::ffi::c_int; +#[no_mangle] +pub unsafe extern "C" fn VM_CallCompiled( + mut vm: *mut vm_t, + mut args: *mut ::core::ffi::c_int, +) -> ::core::ffi::c_int { + let mut stack: [byte; 271] = [0; 271]; + let mut entryPoint: *mut ::core::ffi::c_void = ::core::ptr::null_mut::<::core::ffi::c_void>(); + let mut programStack: ::core::ffi::c_int = 0; + let mut stackOnEntry: ::core::ffi::c_int = 0; + let mut image: *mut byte = ::core::ptr::null_mut::(); + let mut opStack: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>(); + let mut opStackOfs: ::core::ffi::c_int = 0; + let mut arg: ::core::ffi::c_int = 0; + let mut currentVM: *mut vm_t = vm; + stackOnEntry = (*vm).programStack; + programStack = stackOnEntry; + image = (*vm).dataBase as *mut byte; + programStack -= 8 as ::core::ffi::c_int + 4 as ::core::ffi::c_int * MAX_VMMAIN_ARGS; + arg = 0 as ::core::ffi::c_int; + while arg < MAX_VMMAIN_ARGS { + *(image.offset( + (programStack + 8 as ::core::ffi::c_int + arg * 4 as ::core::ffi::c_int) as isize, + ) as *mut byte as *mut ::core::ffi::c_int) = *args.offset(arg as isize); + arg += 1; + } + *(image.offset((programStack + 4 as ::core::ffi::c_int) as isize) as *mut byte + as *mut ::core::ffi::c_int) = 0 as ::core::ffi::c_int; + *(image.offset(programStack as isize) as *mut byte as *mut ::core::ffi::c_int) = + -(1 as ::core::ffi::c_int); + entryPoint = (*vm).codeBase.offset((*vm).entryOfs as isize); + opStack = (&raw mut stack as *mut byte as *mut ::core::ffi::c_int) + .offset(16 as ::core::ffi::c_int as isize); + *opStack = 0 as ::core::ffi::c_int; + opStackOfs = 0 as ::core::ffi::c_int; + if opStackOfs != 1 as ::core::ffi::c_int + || *opStack as ::core::ffi::c_uint != 0xdeadbeef as ::core::ffi::c_uint + { + return 0 as ::core::ffi::c_int; + } + if programStack + != stackOnEntry - (8 as ::core::ffi::c_int + 4 as ::core::ffi::c_int * MAX_VMMAIN_ARGS) + { + return 0 as ::core::ffi::c_int; + } + (*vm).programStack = stackOnEntry; + return *opStack.offset(opStackOfs as isize); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2021.x86_64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2021.x86_64.snap new file mode 100644 index 0000000000..71dab70049 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2021.x86_64.snap @@ -0,0 +1,81 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/arch-specific/vm_x86.2021.x86_64.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(asm, raw_ref_op)] +use ::core::arch::asm; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct vm_t { + pub programStack: ::core::ffi::c_int, + pub entryOfs: ::core::ffi::c_int, + pub dataBase: *mut ::core::ffi::c_void, + pub codeBase: *mut ::core::ffi::c_void, + pub instructionPointers: *mut ::core::ffi::c_ulong, +} +pub type byte = ::core::ffi::c_uchar; +pub const MAX_VMMAIN_ARGS: ::core::ffi::c_int = 50 as ::core::ffi::c_int; +#[no_mangle] +pub unsafe extern "C" fn VM_CallCompiled( + mut vm: *mut vm_t, + mut args: *mut ::core::ffi::c_int, +) -> ::core::ffi::c_int { + let mut stack: [byte; 271] = [0; 271]; + let mut entryPoint: *mut ::core::ffi::c_void = ::core::ptr::null_mut::<::core::ffi::c_void>(); + let mut programStack: ::core::ffi::c_int = 0; + let mut stackOnEntry: ::core::ffi::c_int = 0; + let mut image: *mut byte = ::core::ptr::null_mut::(); + let mut opStack: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>(); + let mut opStackOfs: ::core::ffi::c_int = 0; + let mut arg: ::core::ffi::c_int = 0; + let mut currentVM: *mut vm_t = vm; + stackOnEntry = (*vm).programStack; + programStack = stackOnEntry; + image = (*vm).dataBase as *mut byte; + programStack -= 8 as ::core::ffi::c_int + 4 as ::core::ffi::c_int * MAX_VMMAIN_ARGS; + arg = 0 as ::core::ffi::c_int; + while arg < MAX_VMMAIN_ARGS { + *(image.offset( + (programStack + 8 as ::core::ffi::c_int + arg * 4 as ::core::ffi::c_int) as isize, + ) as *mut byte as *mut ::core::ffi::c_int) = *args.offset(arg as isize); + arg += 1; + } + *(image.offset((programStack + 4 as ::core::ffi::c_int) as isize) as *mut byte + as *mut ::core::ffi::c_int) = 0 as ::core::ffi::c_int; + *(image.offset(programStack as isize) as *mut byte as *mut ::core::ffi::c_int) = + -(1 as ::core::ffi::c_int); + entryPoint = (*vm).codeBase.offset((*vm).entryOfs as isize); + opStack = (&raw mut stack as *mut byte as *mut ::core::ffi::c_int) + .offset(16 as ::core::ffi::c_int as isize); + *opStack = 0 as ::core::ffi::c_int; + opStackOfs = 0 as ::core::ffi::c_int; + asm!( + "movq ({2}), %rax\n", "movq ({0}), %r8\n", "movq ({1}), %r9\n", "push %r15\n", + "push %r14\n", "push %r13\n", "push %r12\n", "callq *%rax\n", "pop %r12\n", + "pop %r13\n", "pop %r14\n", "pop %r15\n", "\n", "mov {restmp0:x}, %bx\n", in + (reg) & (* vm).instructionPointers, in (reg) & (* vm).dataBase, in (reg) & + entryPoint, restmp0 = inlateout(reg) opStackOfs, inlateout("di") opStack, + inlateout("si") programStack, out("rax") _, out("rcx") _, out("rdx") _, out("r8") + _, out("r9") _, out("r10") _, out("r11") _, options(att_syntax) + ); + if opStackOfs != 1 as ::core::ffi::c_int + || *opStack as ::core::ffi::c_uint != 0xdeadbeef as ::core::ffi::c_uint + { + return 0 as ::core::ffi::c_int; + } + if programStack + != stackOnEntry - (8 as ::core::ffi::c_int + 4 as ::core::ffi::c_int * MAX_VMMAIN_ARGS) + { + return 0 as ::core::ffi::c_int; + } + (*vm).programStack = stackOnEntry; + return *opStack.offset(opStackOfs as isize); +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.aarch64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.aarch64.snap similarity index 97% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.aarch64.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.aarch64.snap index 7d5ac74dce..69d91bd7d9 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.aarch64.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.aarch64.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/arch-specific/vm_x86.x86_64.rs +expression: cat tests/snapshots/arch-specific/vm_x86.2024.aarch64.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.x86_64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap similarity index 98% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.x86_64.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap index d829592033..763c5c1ebc 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.x86_64.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/arch-specific/vm_x86.x86_64.rs +expression: cat tests/snapshots/arch-specific/vm_x86.2024.x86_64.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2021.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2021.linux.snap new file mode 100644 index 0000000000..a9c0fc18ec --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2021.linux.snap @@ -0,0 +1,37 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/wide_strings.2021.linux.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(raw_ref_op)] +extern "C" { + fn wcslen(__s: *const wchar_t) -> ::core::ffi::c_ulong; +} +pub type size_t = usize; +pub type wchar_t = ::libc::wchar_t; +#[no_mangle] +pub static mut static_array: [wchar_t; 2] = + unsafe { ::core::mem::transmute::<[u8; 8], [wchar_t; 2]>(*b"x\0\0\0\0\0\0\0") }; +#[no_mangle] +pub static mut static_array_longer: [wchar_t; 3] = + unsafe { ::core::mem::transmute::<[u8; 12], [wchar_t; 3]>(*b"x\0\0\0\0\0\0\0\0\0\0\0") }; +#[no_mangle] +pub static mut static_array_shorter: [wchar_t; 1] = + unsafe { ::core::mem::transmute::<[u8; 4], [wchar_t; 1]>(*b"x\0\0\0") }; +#[no_mangle] +pub unsafe extern "C" fn func() { + let mut array: [wchar_t; 2] = + ::core::mem::transmute::<[u8; 8], [wchar_t; 2]>(*b"x\0\0\0\0\0\0\0"); + let mut array_longer: [wchar_t; 3] = + ::core::mem::transmute::<[u8; 12], [wchar_t; 3]>(*b"x\0\0\0\0\0\0\0\0\0\0\0"); + let mut array_shorter: [wchar_t; 1] = + ::core::mem::transmute::<[u8; 4], [wchar_t; 1]>(*b"x\0\0\0"); + let mut len: size_t = wcslen(&raw mut array as *mut wchar_t) as size_t; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2021.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2021.macos.snap new file mode 100644 index 0000000000..76f2282935 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2021.macos.snap @@ -0,0 +1,39 @@ +--- +source: c2rust-transpile/tests/snapshots.rs +expression: cat tests/snapshots/os-specific/wide_strings.2021.macos.rs +--- +#![allow( + dead_code, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#![feature(raw_ref_op)] +extern "C" { + fn wcslen(_: *const wchar_t) -> ::core::ffi::c_ulong; +} +pub type __darwin_size_t = usize; +pub type __darwin_wchar_t = ::libc::wchar_t; +pub type size_t = __darwin_size_t; +pub type wchar_t = __darwin_wchar_t; +#[no_mangle] +pub static mut static_array: [wchar_t; 2] = + unsafe { ::core::mem::transmute::<[u8; 8], [wchar_t; 2]>(*b"x\0\0\0\0\0\0\0") }; +#[no_mangle] +pub static mut static_array_longer: [wchar_t; 3] = + unsafe { ::core::mem::transmute::<[u8; 12], [wchar_t; 3]>(*b"x\0\0\0\0\0\0\0\0\0\0\0") }; +#[no_mangle] +pub static mut static_array_shorter: [wchar_t; 1] = + unsafe { ::core::mem::transmute::<[u8; 4], [wchar_t; 1]>(*b"x\0\0\0") }; +#[no_mangle] +pub unsafe extern "C" fn func() { + let mut array: [wchar_t; 2] = + ::core::mem::transmute::<[u8; 8], [wchar_t; 2]>(*b"x\0\0\0\0\0\0\0"); + let mut array_longer: [wchar_t; 3] = + ::core::mem::transmute::<[u8; 12], [wchar_t; 3]>(*b"x\0\0\0\0\0\0\0\0\0\0\0"); + let mut array_shorter: [wchar_t; 1] = + ::core::mem::transmute::<[u8; 4], [wchar_t; 1]>(*b"x\0\0\0"); + let mut len: size_t = wcslen(&raw mut array as *mut wchar_t) as size_t; +} diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.linux.snap similarity index 94% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.linux.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.linux.snap index f630ebd673..4f0fdf74f3 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.linux.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/wide_strings.linux.rs +expression: cat tests/snapshots/os-specific/wide_strings.2024.linux.rs --- #![allow( dead_code, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.macos.snap similarity index 95% rename from c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.macos.snap rename to c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.macos.snap index b1e1075406..ab9dd47e95 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.macos.snap @@ -1,6 +1,6 @@ --- source: c2rust-transpile/tests/snapshots.rs -expression: cat tests/snapshots/os-specific/wide_strings.macos.rs +expression: cat tests/snapshots/os-specific/wide_strings.2024.macos.rs --- #![allow( dead_code, From 1c9a6d1d2f0cebf885a4331809eeeb94a498bdd9 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 14:27:10 -0800 Subject: [PATCH 22/39] transpile: don't emit now stabilized `#![feature(raw_ref_op)]` in edition 2024 --- c2rust-transpile/src/translator/pointers.rs | 5 ++++- .../tests/snapshots/snapshots__transpile@arrays.c.2024.snap | 1 - .../tests/snapshots/snapshots__transpile@atomics.c.2024.snap | 2 +- .../snapshots__transpile@compound_literals.c.2024.snap | 1 - .../tests/snapshots/snapshots__transpile@exprs.c.2024.snap | 2 +- .../tests/snapshots/snapshots__transpile@macros.c.2024.snap | 1 - .../tests/snapshots/snapshots__transpile@ref_ub.c.2024.snap | 1 - .../snapshots__transpile@varargs.c.2024.x86_64.linux.snap | 2 +- .../snapshots__transpile@vm_x86.c.2024.aarch64.snap | 1 - .../snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap | 2 +- .../snapshots__transpile@wide_strings.c.2024.linux.snap | 1 - .../snapshots__transpile@wide_strings.c.2024.macos.snap | 1 - 12 files changed, 8 insertions(+), 12 deletions(-) diff --git a/c2rust-transpile/src/translator/pointers.rs b/c2rust-transpile/src/translator/pointers.rs index 763aefe6e2..b91de91e0b 100644 --- a/c2rust-transpile/src/translator/pointers.rs +++ b/c2rust-transpile/src/translator/pointers.rs @@ -2,6 +2,7 @@ use std::ops::Index; use c2rust_ast_builder::{mk, properties::Mutability}; use c2rust_ast_exporter::clang_ast::LRValue; +use c2rust_rust_tools::RustEdition; use failure::{err_msg, format_err}; use syn::{BinOp, Expr, Type, UnOp}; @@ -175,7 +176,9 @@ impl<'c> Translation<'c> { } } } else { - self.use_feature("raw_ref_op"); + if self.tcfg.edition < RustEdition::Rust2024 { + self.use_feature("raw_ref_op"); + } val = val.map(|val| mk().set_mutbl(mutbl).raw_borrow_expr(val)); if is_array_decay { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2024.snap index 4de9f12cc5..0cfba528c3 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2024.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.2024.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/arrays.2024.rs unused_assignments, unused_mut )] -#![feature(raw_ref_op)] #[derive(Copy, Clone)] #[repr(C)] pub struct C2Rust_Unnamed { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2024.snap index 108ae33e86..9e1dab07a6 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2024.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.2024.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/atomics.2024.rs unused_assignments, unused_mut )] -#![feature(core_intrinsics, raw_ref_op)] +#![feature(core_intrinsics)] #[unsafe(no_mangle)] pub unsafe extern "C" fn c11_atomics(mut x: ::core::ffi::c_int) -> ::core::ffi::c_int { *&raw mut x = 0 as ::core::ffi::c_int; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2024.snap index 94082044de..a265b1c4e5 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2024.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.2024.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/compound_literals.2024.rs unused_assignments, unused_mut )] -#![feature(raw_ref_op)] #[unsafe(no_mangle)] pub static mut static_single_int: ::core::ffi::c_int = 42; #[unsafe(no_mangle)] diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap index bb3c476a29..3196147d1d 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/exprs.2024.rs unused_assignments, unused_mut )] -#![feature(label_break_value, raw_ref_op)] +#![feature(label_break_value)] unsafe extern "C" { fn puts(str: *const ::core::ffi::c_char) -> ::core::ffi::c_int; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.snap index 15fcf4470a..1db3600a36 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/macros.2024.rs unused_assignments, unused_mut )] -#![feature(raw_ref_op)] unsafe extern "C" { fn extern_fn() -> ::core::ffi::c_int; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2024.snap index 2e4c3b904d..720f40be73 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2024.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@ref_ub.c.2024.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/ref_ub.2024.rs unused_assignments, unused_mut )] -#![feature(raw_ref_op)] #[derive(Copy, Clone)] #[repr(C)] pub struct Foo { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.x86_64.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.x86_64.linux.snap index 4fa352f39f..0e4b8d26fb 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.x86_64.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@varargs.c.2024.x86_64.linux.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/arch-os-specific/varargs.2024.x86_64.linux.rs unused_assignments, unused_mut )] -#![feature(c_variadic, raw_ref_op)] +#![feature(c_variadic)] unsafe extern "C" { fn printf(__format: *const ::core::ffi::c_char, ...) -> ::core::ffi::c_int; fn vprintf( diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.aarch64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.aarch64.snap index 69d91bd7d9..3c098477c3 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.aarch64.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.aarch64.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/arch-specific/vm_x86.2024.aarch64.rs unused_assignments, unused_mut )] -#![feature(raw_ref_op)] #[derive(Copy, Clone)] #[repr(C)] pub struct vm_t { diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap index 763c5c1ebc..0594e098f5 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap @@ -10,7 +10,7 @@ expression: cat tests/snapshots/arch-specific/vm_x86.2024.x86_64.rs unused_assignments, unused_mut )] -#![feature(asm, raw_ref_op)] +#![feature(asm)] use ::core::arch::asm; #[derive(Copy, Clone)] #[repr(C)] diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.linux.snap index 4f0fdf74f3..8beddbe79f 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.linux.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/os-specific/wide_strings.2024.linux.rs unused_assignments, unused_mut )] -#![feature(raw_ref_op)] unsafe extern "C" { fn wcslen(__s: *const wchar_t) -> ::core::ffi::c_ulong; } diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.macos.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.macos.snap index ab9dd47e95..64bfcde90b 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.macos.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@wide_strings.c.2024.macos.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/os-specific/wide_strings.2024.macos.rs unused_assignments, unused_mut )] -#![feature(raw_ref_op)] unsafe extern "C" { fn wcslen(_: *const wchar_t) -> ::core::ffi::c_ulong; } From 97902eb9e22a1790708bdc1dd3095da3aae798e9 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 14:34:43 -0800 Subject: [PATCH 23/39] transpile: move the stabilized feature in edition 2024 check to inside `fn use_feature` --- c2rust-transpile/src/translator/mod.rs | 5 +++++ c2rust-transpile/src/translator/pointers.rs | 5 +---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 106e0b3d05..0364a7f216 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -1594,6 +1594,11 @@ impl<'c> Translation<'c> { /// Called when translation makes use of a language feature that will require a feature-gate. pub fn use_feature(&self, feature: &'static str) { + if matches!(feature, "raw_ref_op") + && self.tcfg.edition >= RustEdition::Rust2024 + { + return; + } self.features.borrow_mut().insert(feature); } diff --git a/c2rust-transpile/src/translator/pointers.rs b/c2rust-transpile/src/translator/pointers.rs index b91de91e0b..763aefe6e2 100644 --- a/c2rust-transpile/src/translator/pointers.rs +++ b/c2rust-transpile/src/translator/pointers.rs @@ -2,7 +2,6 @@ use std::ops::Index; use c2rust_ast_builder::{mk, properties::Mutability}; use c2rust_ast_exporter::clang_ast::LRValue; -use c2rust_rust_tools::RustEdition; use failure::{err_msg, format_err}; use syn::{BinOp, Expr, Type, UnOp}; @@ -176,9 +175,7 @@ impl<'c> Translation<'c> { } } } else { - if self.tcfg.edition < RustEdition::Rust2024 { - self.use_feature("raw_ref_op"); - } + self.use_feature("raw_ref_op"); val = val.map(|val| mk().set_mutbl(mutbl).raw_borrow_expr(val)); if is_array_decay { From 1b5050e589157c3c4b21d4ee063c6ba6da8b7bcd Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 14:36:41 -0800 Subject: [PATCH 24/39] transpile: don't emit now stabilized `#![feature(label_break_value)]` in edition 2024 --- c2rust-transpile/src/translator/mod.rs | 2 +- .../snapshots__transpile@call_only_once.c.2024.linux.snap | 1 - .../tests/snapshots/snapshots__transpile@exprs.c.2024.snap | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 0364a7f216..e689958595 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -1594,7 +1594,7 @@ impl<'c> Translation<'c> { /// Called when translation makes use of a language feature that will require a feature-gate. pub fn use_feature(&self, feature: &'static str) { - if matches!(feature, "raw_ref_op") + if matches!(feature, "label_break_value" | "raw_ref_op") && self.tcfg.edition >= RustEdition::Rust2024 { return; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.linux.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.linux.snap index a6f0d8f6c1..736f519afb 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.linux.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@call_only_once.c.2024.linux.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/os-specific/call_only_once.2024.linux.rs unused_assignments, unused_mut )] -#![feature(label_break_value)] unsafe extern "C" { fn __assert_fail( __assertion: *const ::core::ffi::c_char, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap index 3196147d1d..bb0d780951 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/exprs.2024.rs unused_assignments, unused_mut )] -#![feature(label_break_value)] unsafe extern "C" { fn puts(str: *const ::core::ffi::c_char) -> ::core::ffi::c_int; } From a23298a407d6f6b42e86fb51b3532ad048d45093 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 14:37:43 -0800 Subject: [PATCH 25/39] transpile: don't emit now stabilized `#![feature(asm)]` in edition 2024 --- c2rust-transpile/src/translator/mod.rs | 2 +- .../snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index e689958595..6f86c4c523 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -1594,7 +1594,7 @@ impl<'c> Translation<'c> { /// Called when translation makes use of a language feature that will require a feature-gate. pub fn use_feature(&self, feature: &'static str) { - if matches!(feature, "label_break_value" | "raw_ref_op") + if matches!(feature, "asm" | "label_break_value" | "raw_ref_op") && self.tcfg.edition >= RustEdition::Rust2024 { return; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap index 0594e098f5..d9d82879f0 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@vm_x86.c.2024.x86_64.snap @@ -10,7 +10,6 @@ expression: cat tests/snapshots/arch-specific/vm_x86.2024.x86_64.rs unused_assignments, unused_mut )] -#![feature(asm)] use ::core::arch::asm; #[derive(Copy, Clone)] #[repr(C)] From 64cac44bec0795fd493434f2bcb949845e21936b Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 15:06:04 -0800 Subject: [PATCH 26/39] transpile: remove `#![feature(stdsimd)]` for `__m64`, stabilized in Rust 1.29 --- c2rust-transpile/src/translator/simd.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/c2rust-transpile/src/translator/simd.rs b/c2rust-transpile/src/translator/simd.rs index 1b3d993066..34dbe97607 100644 --- a/c2rust-transpile/src/translator/simd.rs +++ b/c2rust-transpile/src/translator/simd.rs @@ -254,12 +254,7 @@ impl<'c> Translation<'c> { (Double, 4) => ("_mm256_setzero_pd", 32), (Char, 16) | (Int, 4) | (LongLong, 2) => ("_mm_setzero_si128", 16), (Char, 32) | (Int, 8) | (LongLong, 4) => ("_mm256_setzero_si256", 32), - (Char, 8) | (Int, 2) | (LongLong, 1) => { - // __m64 is still unstable as of rust 1.29 - self.use_feature("stdsimd"); - - ("_mm_setzero_si64", 8) - } + (Char, 8) | (Int, 2) | (LongLong, 1) => ("_mm_setzero_si64", 8), (kind, len) => { return Err(format_err!( "Unsupported vector default initializer: {:?} x {}", From 33610212c3115a80785b86447d67dd69f8c3f1bc Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 15:06:55 -0800 Subject: [PATCH 27/39] tests/unit: change `edition` to an `int` instead of a `str`, as this is ordered --- scripts/test_translator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index 648f3a0b45..fa40fa12f2 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -508,11 +508,11 @@ def run(self) -> List[TestOutcome]: # (if it's generated, it's in the wrong directory and may be different for each transpiled file). # We could also change things to transpile all `*.c` files at once, but that's more involved. # This logic needs to stay in sync with `fn emit_rust_toolchain`. - edition = "2021" + edition = 2021 match edition: - case "2021": + case 2021: toolchain = "nightly-2023-04-15" - case "2024": + case 2024: toolchain = "nightly-2026-03-03" rust_toolchain_toml = f"""\ [toolchain] From 4013b66c7cdb957b261cf59a5c20909c522a9f3d Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 15:08:37 -0800 Subject: [PATCH 28/39] tests/unit: hoise `edition = 2021` and gate `#![feature(stdsimd)]` behind `edition < 2021` --- scripts/test_translator.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index fa40fa12f2..db7f972fe5 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -364,6 +364,8 @@ def _generate_cc_db(self, c_file_path: str) -> None: fh.write(compile_commands) def run(self) -> List[TestOutcome]: + edition = 2021 + if self.target and not rustc_has_target(self.target): self.print_status(Colors.OKBLUE, "SKIPPED", "building test {} because the {} target is not installed" @@ -417,10 +419,13 @@ def run(self) -> List[TestOutcome]: rust_file_builder.add_features([ "extern_types", "simd_ffi", - "stdsimd", "linkage", "register_tool", ]) + if edition < 2024: + rust_file_builder.add_features([ + "stdsimd", + ]) rust_file_builder.add_pragma("register_tool", ["c2rust"]) # Ensure that path to rustc's lib dir is in`LD_LIBRARY_PATH` @@ -508,7 +513,6 @@ def run(self) -> List[TestOutcome]: # (if it's generated, it's in the wrong directory and may be different for each transpiled file). # We could also change things to transpile all `*.c` files at once, but that's more involved. # This logic needs to stay in sync with `fn emit_rust_toolchain`. - edition = 2021 match edition: case 2021: toolchain = "nightly-2023-04-15" From 6546e916439c90ba394c81040b49c49f31da5dc6 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Fri, 6 Mar 2026 15:10:45 -0800 Subject: [PATCH 29/39] tests/unit: enable `--color always` for `cargo build`s --- scripts/test_translator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index db7f972fe5..d81e0c96ad 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -529,7 +529,7 @@ def run(self) -> List[TestOutcome]: # Build with pb.local.cwd(self.full_path): - args = ["build"] + args = ["build", "--color", "always"] if c.BUILD_TYPE == 'release': args.append('--release') From 09ebf4b4e1a191ea6cd9372086be9617ff5b8a91 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 05:41:51 -0800 Subject: [PATCH 30/39] tests/unit: use named args for `c_file.translate` --- scripts/test_translator.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index d81e0c96ad..ae37be2770 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -446,9 +446,11 @@ def run(self) -> List[TestOutcome]: try: logging.debug("translating %s", c_file_short) - translated_rust_file = c_file.translate(self.generated_files["cc_db"][0], - ld_lib_path, - extra_args=target_args(self.target)) + translated_rust_file = c_file.translate( + cc_db=self.generated_files["cc_db"][0], + ld_lib_path=ld_lib_path, + extra_args=target_args(self.target), + ) except NonZeroReturn as exception: self.print_status(Colors.FAIL, "FAILED", "translate " + c_file_short) From d6f5376b1e7f9940732a60605e28467a261ffcd6 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 06:38:02 -0800 Subject: [PATCH 31/39] tests/unit: add support for `--edition 2021` and `--edition 2024` in `test_*.rs` files This allows us to move individual test directories to edition 2024. --- scripts/test_translator.py | 40 ++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index ae37be2770..d19b99425c 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -1,4 +1,5 @@ #!/usr/bin/env -S uv run +from collections import defaultdict import errno import os @@ -28,7 +29,7 @@ RustMod, RustVisibility, ) -from typing import Any, Dict, Generator, List, Optional, Set, Iterable +from typing import Any, Dict, Generator, List, Optional, Set, Iterable, Literal # Tools we will need clang = get_cmd_or_die("clang") @@ -54,6 +55,9 @@ class TestOutcome(Enum): UnexpectedSuccess = "unexpected successes" +RustEdition = Literal[2021] | Literal[2024] + + class CStaticLibrary: def __init__(self, path: str, link_name: str, obj_files: List[str]) -> None: @@ -73,7 +77,7 @@ def __init__(self, log_level: str, path: str, flags: Set[str] = set()) -> None: self.reorganize_definitions = "reorganize_definitions" in flags self.emit_build_files = "emit_build_files" in flags - def translate(self, cc_db: str, ld_lib_path: str, extra_args: List[str] = []) -> RustFile: + def translate(self, cc_db: str, edition: RustEdition, ld_lib_path: str, extra_args: List[str] = []) -> RustFile: extensionless_file, _ = os.path.splitext(self.path) # run the transpiler @@ -84,6 +88,8 @@ def translate(self, cc_db: str, ld_lib_path: str, extra_args: List[str] = []) -> "--prefix-function-names", "rust_", "--overwrite-existing", + "--edition", + str(edition), ] # return nonzero if translation fails @@ -214,10 +220,13 @@ def __init__(self, path: str, test_functions: Optional[List[TestFunction]] = Non self.pass_expected = "xfail" not in flags self.extern_crates = {flag[13:] for flag in flags if flag.startswith("extern_crate_")} self.features = {flag[8:] for flag in flags if flag.startswith("feature_")} + self.edition_2021 = "--edition 2021" in flags + self.edition_2024 = "--edition 2024" in flags class TestDirectory: rs_test_files: list[TestFile] + edition: RustEdition def __init__(self, full_path: str, files: 're.Pattern', keep: List[str], log_level: str) -> None: self.c_files = [] @@ -288,6 +297,26 @@ def __init__(self, full_path: str, files: 're.Pattern', keep: List[str], log_lev rs_test_file = self._read_rust_test_file(path) self.rs_test_files.append(rs_test_file) + editions: defaultdict[RustEdition, list[TestFile]] = defaultdict(list) + for test_file in self.rs_test_files: + assert not (test_file.edition_2021 and test_file.edition_2024), ( + f"{test_file.path} has can only have one --edition flag" + ) + edition = 2021 # default + if test_file.edition_2021: + edition = 2021 + elif test_file.edition_2024: + edition = 2024 + editions[edition].append(test_file) + editions_rendered = { + f"--edition {edition}": [test_file.path for test_file in test_files] + for edition, test_files in editions.items() + } + assert len(editions) <= 1, ( + f"multiple editions in a TestDirectory not allowed: {editions_rendered}" + ) + self.edition = next(iter(editions.keys())) + def _read_c_file(self, path: str) -> Optional[CFile]: file_config = None file_flags = set() @@ -364,8 +393,6 @@ def _generate_cc_db(self, c_file_path: str) -> None: fh.write(compile_commands) def run(self) -> List[TestOutcome]: - edition = 2021 - if self.target and not rustc_has_target(self.target): self.print_status(Colors.OKBLUE, "SKIPPED", "building test {} because the {} target is not installed" @@ -422,7 +449,7 @@ def run(self) -> List[TestOutcome]: "linkage", "register_tool", ]) - if edition < 2024: + if self.edition < 2024: rust_file_builder.add_features([ "stdsimd", ]) @@ -448,6 +475,7 @@ def run(self) -> List[TestOutcome]: logging.debug("translating %s", c_file_short) translated_rust_file = c_file.translate( cc_db=self.generated_files["cc_db"][0], + edition=self.edition, ld_lib_path=ld_lib_path, extra_args=target_args(self.target), ) @@ -515,7 +543,7 @@ def run(self) -> List[TestOutcome]: # (if it's generated, it's in the wrong directory and may be different for each transpiled file). # We could also change things to transpile all `*.c` files at once, but that's more involved. # This logic needs to stay in sync with `fn emit_rust_toolchain`. - match edition: + match self.edition: case 2021: toolchain = "nightly-2023-04-15" case 2024: From faf532db36450dd8e231707a258286c90d194f67 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 16:11:58 -0800 Subject: [PATCH 32/39] tests/unit: switch from `--edition` flags to parsing `Cargo.toml`'s `package.edition` --- scripts/test_translator.py | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index d19b99425c..11d067ae11 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -1,5 +1,5 @@ #!/usr/bin/env -S uv run -from collections import defaultdict +import toml import errno import os @@ -29,7 +29,7 @@ RustMod, RustVisibility, ) -from typing import Any, Dict, Generator, List, Optional, Set, Iterable, Literal +from typing import Any, Dict, Generator, List, Optional, Set, Iterable, Literal, cast # Tools we will need clang = get_cmd_or_die("clang") @@ -220,8 +220,6 @@ def __init__(self, path: str, test_functions: Optional[List[TestFunction]] = Non self.pass_expected = "xfail" not in flags self.extern_crates = {flag[13:] for flag in flags if flag.startswith("extern_crate_")} self.features = {flag[8:] for flag in flags if flag.startswith("feature_")} - self.edition_2021 = "--edition 2021" in flags - self.edition_2024 = "--edition 2024" in flags class TestDirectory: @@ -244,6 +242,15 @@ def __init__(self, full_path: str, files: 're.Pattern', keep: List[str], log_lev "cc_db": [], } + cargo_toml_path = Path(full_path) / "Cargo.toml" + cargo_toml = toml.loads(cargo_toml_path.read_text()) + edition = int(cargo_toml["package"]["edition"]) + match edition: + case 2021 | 2024: + self.edition = cast(RustEdition, edition) + case _: + raise ValueError(f"unsupported Rust edition: {edition}") + # if the test is arch-specific, check if we can run it natively; if not, # set self.target to a known-working target tuple for it self.target = None @@ -297,26 +304,6 @@ def __init__(self, full_path: str, files: 're.Pattern', keep: List[str], log_lev rs_test_file = self._read_rust_test_file(path) self.rs_test_files.append(rs_test_file) - editions: defaultdict[RustEdition, list[TestFile]] = defaultdict(list) - for test_file in self.rs_test_files: - assert not (test_file.edition_2021 and test_file.edition_2024), ( - f"{test_file.path} has can only have one --edition flag" - ) - edition = 2021 # default - if test_file.edition_2021: - edition = 2021 - elif test_file.edition_2024: - edition = 2024 - editions[edition].append(test_file) - editions_rendered = { - f"--edition {edition}": [test_file.path for test_file in test_files] - for edition, test_files in editions.items() - } - assert len(editions) <= 1, ( - f"multiple editions in a TestDirectory not allowed: {editions_rendered}" - ) - self.edition = next(iter(editions.keys())) - def _read_c_file(self, path: str) -> Optional[CFile]: file_config = None file_flags = set() From 0a34f33b7de754f84251cf567adc01e4d419cac7 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 16:26:02 -0800 Subject: [PATCH 33/39] tests/unit: enable `--color always` for `cargo test`s --- scripts/test_translator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index 11d067ae11..dbb0a1ea62 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -569,7 +569,7 @@ def run(self) -> List[TestOutcome]: # Test with pb.local.cwd(self.full_path): - args = ["test"] + args = ["test", "--color", "always"] if c.BUILD_TYPE == 'release': args.append('--release') From 5d9c36b8ee75589673c26990e35aba7b10440f41 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 16:33:24 -0800 Subject: [PATCH 34/39] tests/unit: `#![allow(unsafe_op_in_unsafe_fn)]` until we add proper `unsafe` blocks in each `unsafe fn` --- scripts/test_translator.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index dbb0a1ea62..9bca47b0df 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -441,6 +441,9 @@ def run(self) -> List[TestOutcome]: "stdsimd", ]) rust_file_builder.add_pragma("register_tool", ["c2rust"]) + if self.edition >= 2024: + # TODO We should emit `unsafe` blocks for this instead of silencing the warning. + rust_file_builder.add_pragma("allow", ["unsafe_op_in_unsafe_fn"]) # Ensure that path to rustc's lib dir is in`LD_LIBRARY_PATH` ld_lib_path = get_rust_toolchain_libpath() From 6043bee79632ee2c5fd46ded73328e978f6f40e8 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 16:33:44 -0800 Subject: [PATCH 35/39] tests/unit: update most tests to edition 2024 --- tests/unit/README.md | 2 +- tests/unit/arrays/Cargo.toml | 2 +- tests/unit/arrays/src/test_arrays.rs | 7 ++++--- tests/unit/asm.aarch64/Cargo.toml | 2 +- tests/unit/asm.aarch64/src/test_asm.rs | 2 +- tests/unit/asm.arm/Cargo.toml | 2 +- tests/unit/asm.arm/src/test_asm.rs | 2 +- tests/unit/asm.x86_64/Cargo.toml | 2 +- tests/unit/asm.x86_64/src/test_asm.rs | 2 +- tests/unit/casts/Cargo.toml | 2 +- tests/unit/casts/src/test_casts.rs | 2 +- tests/unit/comments/Cargo.toml | 2 +- tests/unit/conditionals/Cargo.toml | 2 +- tests/unit/conditionals/src/test_conditionals.rs | 4 +--- tests/unit/enums/Cargo.toml | 2 +- tests/unit/enums/src/test_enums.rs | 2 +- tests/unit/example/Cargo.toml | 2 +- tests/unit/example/src/test_add.rs | 2 +- tests/unit/example/src/test_sub.rs | 2 +- tests/unit/floats/Cargo.toml | 2 +- tests/unit/floats/src/test_no_wrapping_neg.rs | 2 +- tests/unit/gotos/Cargo.toml | 2 +- tests/unit/gotos/src/test_irreducible.rs | 2 +- tests/unit/ints/Cargo.toml | 2 +- tests/unit/ints/src/test_arithmetic.rs | 2 +- tests/unit/ints/src/test_compound_assignment.rs | 2 +- tests/unit/ints/src/test_const.rs | 2 +- tests/unit/ints/src/test_implicit_ints.rs | 2 +- tests/unit/ints/src/test_ints.rs | 2 +- tests/unit/ints/src/test_sieve_of_eratosthenes.rs | 2 +- tests/unit/ints/src/test_volatile.rs | 2 +- tests/unit/loops/Cargo.toml | 2 +- tests/unit/loops/src/test_goto.rs | 2 +- tests/unit/loops/src/test_loops.rs | 2 +- tests/unit/loops/src/test_switch.rs | 2 +- tests/unit/macros/Cargo.toml | 2 +- tests/unit/macros/src/test_define.rs | 2 +- tests/unit/modules/Cargo.toml | 2 +- tests/unit/modules/src/test_modules.rs | 2 +- tests/unit/unions/Cargo.toml | 2 +- tests/unit/unions/src/test_unions.rs | 2 +- 41 files changed, 44 insertions(+), 45 deletions(-) diff --git a/tests/unit/README.md b/tests/unit/README.md index ac9799de60..675fc64d1c 100644 --- a/tests/unit/README.md +++ b/tests/unit/README.md @@ -16,7 +16,7 @@ use crate::c_file::rust_example; use std::ffi::c_int; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn example(_: c_uint, _: *mut c_int); } diff --git a/tests/unit/arrays/Cargo.toml b/tests/unit/arrays/Cargo.toml index 647a5100bd..1e97c51081 100644 --- a/tests/unit/arrays/Cargo.toml +++ b/tests/unit/arrays/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "array-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] libc = "0.2" diff --git a/tests/unit/arrays/src/test_arrays.rs b/tests/unit/arrays/src/test_arrays.rs index e71b408651..109b21d245 100644 --- a/tests/unit/arrays/src/test_arrays.rs +++ b/tests/unit/arrays/src/test_arrays.rs @@ -6,7 +6,7 @@ use crate::variable_arrays::{rust_alloca_arrays, rust_variable_arrays}; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry(_: c_uint, _: *mut c_int); fn entry2(_: c_uint, _: *mut c_int); @@ -20,9 +20,10 @@ extern "C" { fn check_some_ints() -> bool; } -#[no_mangle] +#[unsafe(no_mangle)] pub static SOME_INTS: [u32; 4] = [2, 0, 1, 8]; -#[no_mangle] + +#[unsafe(no_mangle)] pub static rust_SOME_INTS: [u32; 4] = [2, 0, 1, 8]; const BUFFER_SIZE: usize = 49; diff --git a/tests/unit/asm.aarch64/Cargo.toml b/tests/unit/asm.aarch64/Cargo.toml index 83fac5531a..051b8c2073 100644 --- a/tests/unit/asm.aarch64/Cargo.toml +++ b/tests/unit/asm.aarch64/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "asm-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] c2rust-asm-casts = { path = "../../../c2rust-asm-casts", version = "0.22.1" } diff --git a/tests/unit/asm.aarch64/src/test_asm.rs b/tests/unit/asm.aarch64/src/test_asm.rs index da5e9798d8..4059cd2f70 100644 --- a/tests/unit/asm.aarch64/src/test_asm.rs +++ b/tests/unit/asm.aarch64/src/test_asm.rs @@ -4,7 +4,7 @@ use crate::asm::rust_entry; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry(_: c_uint, _: *mut c_int); } diff --git a/tests/unit/asm.arm/Cargo.toml b/tests/unit/asm.arm/Cargo.toml index c795abf12a..07c9bfc30b 100644 --- a/tests/unit/asm.arm/Cargo.toml +++ b/tests/unit/asm.arm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "asm-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] diff --git a/tests/unit/asm.arm/src/test_asm.rs b/tests/unit/asm.arm/src/test_asm.rs index e50ca091b0..e24e7fb791 100644 --- a/tests/unit/asm.arm/src/test_asm.rs +++ b/tests/unit/asm.arm/src/test_asm.rs @@ -2,7 +2,7 @@ use crate::asm::rust_entry; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry(_: c_uint, _: *mut c_int); } diff --git a/tests/unit/asm.x86_64/Cargo.toml b/tests/unit/asm.x86_64/Cargo.toml index 83fac5531a..051b8c2073 100644 --- a/tests/unit/asm.x86_64/Cargo.toml +++ b/tests/unit/asm.x86_64/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "asm-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] c2rust-asm-casts = { path = "../../../c2rust-asm-casts", version = "0.22.1" } diff --git a/tests/unit/asm.x86_64/src/test_asm.rs b/tests/unit/asm.x86_64/src/test_asm.rs index 883ccfb151..2edfbcadb6 100644 --- a/tests/unit/asm.x86_64/src/test_asm.rs +++ b/tests/unit/asm.x86_64/src/test_asm.rs @@ -4,7 +4,7 @@ use crate::asm::rust_entry; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry(_: c_uint, _: *mut c_int); } diff --git a/tests/unit/casts/Cargo.toml b/tests/unit/casts/Cargo.toml index d92d514b58..42e964ff18 100644 --- a/tests/unit/casts/Cargo.toml +++ b/tests/unit/casts/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cast-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] libc = "0.2" diff --git a/tests/unit/casts/src/test_casts.rs b/tests/unit/casts/src/test_casts.rs index 1d13d37c97..8d7bf05aa1 100644 --- a/tests/unit/casts/src/test_casts.rs +++ b/tests/unit/casts/src/test_casts.rs @@ -8,7 +8,7 @@ use std::ffi::{c_int, c_uint, c_void}; use std::mem::transmute; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn cast_stuff(); fn identity(_: c_int) -> c_int; diff --git a/tests/unit/comments/Cargo.toml b/tests/unit/comments/Cargo.toml index 63a0f17bb2..605f55d95f 100644 --- a/tests/unit/comments/Cargo.toml +++ b/tests/unit/comments/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "comments-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] libc = "0.2" diff --git a/tests/unit/conditionals/Cargo.toml b/tests/unit/conditionals/Cargo.toml index 437f3246da..872ddfe7cb 100644 --- a/tests/unit/conditionals/Cargo.toml +++ b/tests/unit/conditionals/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "conditional-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] diff --git a/tests/unit/conditionals/src/test_conditionals.rs b/tests/unit/conditionals/src/test_conditionals.rs index d18af7666e..174b4b1a48 100644 --- a/tests/unit/conditionals/src/test_conditionals.rs +++ b/tests/unit/conditionals/src/test_conditionals.rs @@ -1,5 +1,3 @@ -//! feature_raw_ref_op - use crate::binary_conditional::rust_entry3; use crate::conditional::rust_entry; use crate::conditionals::{rust_entry2, rust_ternaries}; @@ -10,7 +8,7 @@ use crate::unused_conditionals::{ use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry(_: c_uint, _: *mut c_int); fn entry2(_: c_uint, _: *mut c_int); diff --git a/tests/unit/enums/Cargo.toml b/tests/unit/enums/Cargo.toml index 64c7fa0267..0d685de3d3 100644 --- a/tests/unit/enums/Cargo.toml +++ b/tests/unit/enums/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "enum-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] diff --git a/tests/unit/enums/src/test_enums.rs b/tests/unit/enums/src/test_enums.rs index 8449fb460c..1e373fd354 100644 --- a/tests/unit/enums/src/test_enums.rs +++ b/tests/unit/enums/src/test_enums.rs @@ -12,7 +12,7 @@ use crate::top_enum::{rust_entry4, E as otherE}; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry(_: c_uint, _: *mut c_int); fn entry2(_: c_uint, _: *mut c_int); diff --git a/tests/unit/example/Cargo.toml b/tests/unit/example/Cargo.toml index ed7d2417cc..dc54505ae8 100644 --- a/tests/unit/example/Cargo.toml +++ b/tests/unit/example/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "example-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] diff --git a/tests/unit/example/src/test_add.rs b/tests/unit/example/src/test_add.rs index 2344f4e5f1..705af4ef36 100644 --- a/tests/unit/example/src/test_add.rs +++ b/tests/unit/example/src/test_add.rs @@ -2,7 +2,7 @@ use crate::add::rust_add; use std::ffi::c_uint; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn add(left: c_uint, right: c_uint) -> c_uint; } diff --git a/tests/unit/example/src/test_sub.rs b/tests/unit/example/src/test_sub.rs index a41e650eb3..008ea2b876 100644 --- a/tests/unit/example/src/test_sub.rs +++ b/tests/unit/example/src/test_sub.rs @@ -2,7 +2,7 @@ use crate::sub::rust_sub; use std::ffi::c_uint; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn sub(left: c_uint, right: c_uint) -> c_uint; } diff --git a/tests/unit/floats/Cargo.toml b/tests/unit/floats/Cargo.toml index cef145b90e..233c775f42 100644 --- a/tests/unit/floats/Cargo.toml +++ b/tests/unit/floats/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "float-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] diff --git a/tests/unit/floats/src/test_no_wrapping_neg.rs b/tests/unit/floats/src/test_no_wrapping_neg.rs index c5f462b240..843bcafadd 100644 --- a/tests/unit/floats/src/test_no_wrapping_neg.rs +++ b/tests/unit/floats/src/test_no_wrapping_neg.rs @@ -2,7 +2,7 @@ use crate::no_float_wrapping_neg::{rust_double_inc_dec, rust_float_inc_dec, rust use std::ffi::{c_double, c_float}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn no_wrapping_neg() -> c_double; fn float_inc_dec() -> c_float; fn double_inc_dec() -> c_double; diff --git a/tests/unit/gotos/Cargo.toml b/tests/unit/gotos/Cargo.toml index 0f7f65c2d7..4d5d2432bb 100644 --- a/tests/unit/gotos/Cargo.toml +++ b/tests/unit/gotos/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "goto-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] diff --git a/tests/unit/gotos/src/test_irreducible.rs b/tests/unit/gotos/src/test_irreducible.rs index e15eba2b56..1865b2b0ce 100644 --- a/tests/unit/gotos/src/test_irreducible.rs +++ b/tests/unit/gotos/src/test_irreducible.rs @@ -2,7 +2,7 @@ use crate::irreducible::rust_irreducible; use std::ffi::c_int; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn irreducible(_: c_int) -> c_int; } diff --git a/tests/unit/ints/Cargo.toml b/tests/unit/ints/Cargo.toml index ae816f01a0..fc6f6f647b 100644 --- a/tests/unit/ints/Cargo.toml +++ b/tests/unit/ints/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "int-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] diff --git a/tests/unit/ints/src/test_arithmetic.rs b/tests/unit/ints/src/test_arithmetic.rs index 2cb0a61bd7..15021806a4 100644 --- a/tests/unit/ints/src/test_arithmetic.rs +++ b/tests/unit/ints/src/test_arithmetic.rs @@ -4,7 +4,7 @@ use crate::arithmetic::rust_entry2; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry2(_: c_uint, _: *mut c_int); } diff --git a/tests/unit/ints/src/test_compound_assignment.rs b/tests/unit/ints/src/test_compound_assignment.rs index e8ae6c23fa..a9ba4f4cea 100644 --- a/tests/unit/ints/src/test_compound_assignment.rs +++ b/tests/unit/ints/src/test_compound_assignment.rs @@ -3,7 +3,7 @@ use crate::compound_assignment::rust_compound_assignment; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn compound_assignment(_: c_uint, _: *mut c_int); } diff --git a/tests/unit/ints/src/test_const.rs b/tests/unit/ints/src/test_const.rs index 41e8e440ee..81c7fe6166 100644 --- a/tests/unit/ints/src/test_const.rs +++ b/tests/unit/ints/src/test_const.rs @@ -2,7 +2,7 @@ use crate::const_test::rust_entry4; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry4(_: c_uint, _: *mut c_int); } diff --git a/tests/unit/ints/src/test_implicit_ints.rs b/tests/unit/ints/src/test_implicit_ints.rs index b948ba3e56..d85ee7888a 100644 --- a/tests/unit/ints/src/test_implicit_ints.rs +++ b/tests/unit/ints/src/test_implicit_ints.rs @@ -3,7 +3,7 @@ use crate::implicit_int::{identity as rust_identity, implicit_int as rust_implicit_int}; use std::ffi::{c_int, c_uint}; -extern "C" { +unsafe extern "C" { fn identity(_: c_int) -> c_int; fn implicit_int(); diff --git a/tests/unit/ints/src/test_ints.rs b/tests/unit/ints/src/test_ints.rs index e273785093..8889ee1318 100644 --- a/tests/unit/ints/src/test_ints.rs +++ b/tests/unit/ints/src/test_ints.rs @@ -3,7 +3,7 @@ use crate::size_t::rust_entry; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry(_: c_uint, _: *mut c_int); fn multibyte_chars(_: c_uint, _: *mut c_int) -> c_int; diff --git a/tests/unit/ints/src/test_sieve_of_eratosthenes.rs b/tests/unit/ints/src/test_sieve_of_eratosthenes.rs index 6abc1ed565..981b0aa33a 100644 --- a/tests/unit/ints/src/test_sieve_of_eratosthenes.rs +++ b/tests/unit/ints/src/test_sieve_of_eratosthenes.rs @@ -2,7 +2,7 @@ use crate::sieve_of_eratosthenes::rust_sieve_of_eratosthenes; use std::ffi::c_int; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn sieve_of_eratosthenes(_: *mut c_int); } diff --git a/tests/unit/ints/src/test_volatile.rs b/tests/unit/ints/src/test_volatile.rs index c761936a5a..ee1f70477f 100644 --- a/tests/unit/ints/src/test_volatile.rs +++ b/tests/unit/ints/src/test_volatile.rs @@ -2,7 +2,7 @@ use crate::volatile::rust_entry3; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry3(_: c_uint, _: *mut c_int); } diff --git a/tests/unit/loops/Cargo.toml b/tests/unit/loops/Cargo.toml index 1383c75cde..765e9700a3 100644 --- a/tests/unit/loops/Cargo.toml +++ b/tests/unit/loops/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "loops-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] diff --git a/tests/unit/loops/src/test_goto.rs b/tests/unit/loops/src/test_goto.rs index 65730fc8fc..c740201c28 100644 --- a/tests/unit/loops/src/test_goto.rs +++ b/tests/unit/loops/src/test_goto.rs @@ -5,7 +5,7 @@ use crate::goto_switch_cf::rust_goto_switch; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn goto_linear(_: c_uint, _: *mut c_int); fn goto_loop(_: c_uint, _: *mut c_int); diff --git a/tests/unit/loops/src/test_loops.rs b/tests/unit/loops/src/test_loops.rs index 60e8b70edb..5b98f32a99 100644 --- a/tests/unit/loops/src/test_loops.rs +++ b/tests/unit/loops/src/test_loops.rs @@ -2,7 +2,7 @@ use crate::break_continue::rust_entry; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry(_: c_uint, _: *mut c_int); } diff --git a/tests/unit/loops/src/test_switch.rs b/tests/unit/loops/src/test_switch.rs index e5ed70ae4d..16e09a8b6a 100644 --- a/tests/unit/loops/src/test_switch.rs +++ b/tests/unit/loops/src/test_switch.rs @@ -2,7 +2,7 @@ use crate::switch::rust_switch_val; use std::ffi::c_int; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn switch_val(_: c_int) -> c_int; } diff --git a/tests/unit/macros/Cargo.toml b/tests/unit/macros/Cargo.toml index b72eb58d5c..9655519310 100644 --- a/tests/unit/macros/Cargo.toml +++ b/tests/unit/macros/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "macros-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] libc = "0.2" diff --git a/tests/unit/macros/src/test_define.rs b/tests/unit/macros/src/test_define.rs index 39d37ae4cd..70f917c05c 100644 --- a/tests/unit/macros/src/test_define.rs +++ b/tests/unit/macros/src/test_define.rs @@ -6,7 +6,7 @@ use crate::define::{rust_test_zstd, ZSTD_WINDOWLOG_MAX_32, ZSTD_WINDOWLOG_MAX_64 use std::ffi::{c_int, c_uint, c_ulong}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn reference_define() -> c_uint; } diff --git a/tests/unit/modules/Cargo.toml b/tests/unit/modules/Cargo.toml index aaf2e24247..e9a64efbb1 100644 --- a/tests/unit/modules/Cargo.toml +++ b/tests/unit/modules/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "modules-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] libc = "0.2" diff --git a/tests/unit/modules/src/test_modules.rs b/tests/unit/modules/src/test_modules.rs index 04a205cda0..9901f23fc9 100644 --- a/tests/unit/modules/src/test_modules.rs +++ b/tests/unit/modules/src/test_modules.rs @@ -2,7 +2,7 @@ use crate::modules::rust_modules; use std::ffi::c_uint; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn modules(); } diff --git a/tests/unit/unions/Cargo.toml b/tests/unit/unions/Cargo.toml index f591e878b2..0ca1c43eba 100644 --- a/tests/unit/unions/Cargo.toml +++ b/tests/unit/unions/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "union-tests" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] diff --git a/tests/unit/unions/src/test_unions.rs b/tests/unit/unions/src/test_unions.rs index 0a2733b3e8..6566dd7804 100644 --- a/tests/unit/unions/src/test_unions.rs +++ b/tests/unit/unions/src/test_unions.rs @@ -2,7 +2,7 @@ use crate::unions::rust_entry; use std::ffi::{c_int, c_uint}; #[link(name = "test")] -extern "C" { +unsafe extern "C" { fn entry(_: c_uint, _: *mut c_int); } From 3b1424e0b19d7a3ae1b7ae430edcb7430e1aff53 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 16:45:52 -0800 Subject: [PATCH 36/39] rust-tools: rename `RustEdition::{Rust => Edition}{2021,2024}` `Edition2024` more closely matches what we normally call it. --- c2rust-rust-tools/src/lib.rs | 16 ++++++++-------- c2rust-transpile/src/build_files/mod.rs | 2 +- c2rust-transpile/src/translator/builtins.rs | 2 +- c2rust-transpile/src/translator/mod.rs | 6 +++--- c2rust-transpile/src/translator/simd.rs | 2 +- c2rust-transpile/tests/snapshots.rs | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/c2rust-rust-tools/src/lib.rs b/c2rust-rust-tools/src/lib.rs index 916098f84f..7a507b6138 100644 --- a/c2rust-rust-tools/src/lib.rs +++ b/c2rust-rust-tools/src/lib.rs @@ -12,17 +12,17 @@ pub enum RustEdition { /// The default is edition 2021 because `c2rust-refactor`, /// based on `nightly-2022-08-08`, only understands up to edition 2021. #[default] - Rust2021, - Rust2024, + Edition2021, + Edition2024, } impl RustEdition { - pub const ALL: &[Self] = &[Self::Rust2021, Self::Rust2024]; + pub const ALL: &[Self] = &[Self::Edition2021, Self::Edition2024]; pub const fn as_str(&self) -> &'static str { match self { - Self::Rust2021 => "2021", - Self::Rust2024 => "2024", + Self::Edition2021 => "2021", + Self::Edition2024 => "2024", } } @@ -32,9 +32,9 @@ impl RustEdition { pub const fn toolchain(&self) -> &'static str { match self { // 1.70 (1.68 for syn v2.0, 1.70 for sparse registry) - Self::Rust2021 => "+nightly-2023-04-15", + Self::Edition2021 => "+nightly-2023-04-15", // This doesn't really need to be pinned, but pin it for stability. - Self::Rust2024 => "+nightly-2026-03-03", + Self::Edition2024 => "+nightly-2026-03-03", } } } @@ -49,7 +49,7 @@ impl FromStr for RustEdition { type Err = String; fn from_str(s: &str) -> Result { - let choices = [Self::Rust2021, Self::Rust2024]; + let choices = [Self::Edition2021, Self::Edition2024]; choices .into_iter() .find(|choice| choice.as_str() == s) diff --git a/c2rust-transpile/src/build_files/mod.rs b/c2rust-transpile/src/build_files/mod.rs index 360abec323..a106ae8f42 100644 --- a/c2rust-transpile/src/build_files/mod.rs +++ b/c2rust-transpile/src/build_files/mod.rs @@ -330,7 +330,7 @@ fn emit_cargo_toml<'lcmd>( "edition": tcfg.edition.as_str(), // This is already the default in Rust 1.77, // and edition 2024 was released in Rust 1.85. - "strip_debuginfo_release": tcfg.edition < RustEdition::Rust2024, + "strip_debuginfo_release": tcfg.edition < RustEdition::Edition2024, "crate_types": ccfg.link_cmd.r#type.as_cargo_types(), "is_library": ccfg.link_cmd.r#type.is_library(), "lib_rs_file": get_lib_rs_file_name(tcfg), diff --git a/c2rust-transpile/src/translator/builtins.rs b/c2rust-transpile/src/translator/builtins.rs index 3de0c7aef1..48594d9f89 100644 --- a/c2rust-transpile/src/translator/builtins.rs +++ b/c2rust-transpile/src/translator/builtins.rs @@ -414,7 +414,7 @@ impl<'c> Translation<'c> { "__builtin_arm_yield" => { let fn_name = "__yield"; - if self.tcfg.edition < RustEdition::Rust2024 { + if self.tcfg.edition < RustEdition::Edition2024 { self.use_feature("stdsimd"); } else { // Edition 2024 was release in Rust 1.85. diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 6f86c4c523..177748edb2 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -373,7 +373,7 @@ pub fn stmts_block(mut stmts: Vec) -> Block { /// Whether `extern` blocks can be `unsafe` in this edition. fn extern_block_unsafety(edition: RustEdition) -> Unsafety { - if edition >= RustEdition::Rust2024 { + if edition >= RustEdition::Edition2024 { Unsafety::Unsafe } else { Unsafety::Normal @@ -382,7 +382,7 @@ fn extern_block_unsafety(edition: RustEdition) -> Unsafety { /// Whether attributes can be `unsafe` in this edition. fn attr_unsafety(edition: RustEdition) -> Unsafety { - if edition >= RustEdition::Rust2024 { + if edition >= RustEdition::Edition2024 { Unsafety::Unsafe } else { Unsafety::Normal @@ -1595,7 +1595,7 @@ impl<'c> Translation<'c> { /// Called when translation makes use of a language feature that will require a feature-gate. pub fn use_feature(&self, feature: &'static str) { if matches!(feature, "asm" | "label_break_value" | "raw_ref_op") - && self.tcfg.edition >= RustEdition::Rust2024 + && self.tcfg.edition >= RustEdition::Edition2024 { return; } diff --git a/c2rust-transpile/src/translator/simd.rs b/c2rust-transpile/src/translator/simd.rs index 34dbe97607..62a15e89a2 100644 --- a/c2rust-transpile/src/translator/simd.rs +++ b/c2rust-transpile/src/translator/simd.rs @@ -160,7 +160,7 @@ impl<'c> Translation<'c> { .into()); } - if self.tcfg.edition < RustEdition::Rust2024 { + if self.tcfg.edition < RustEdition::Edition2024 { // Edition 2024 was release in Rust 1.85. // In Rust 1.78, `#![feature(stdsimd)]` was removed and split into individual features. // All of the x86_64 parts (that we use at least) were stabilized. diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 4112b50c1c..f2a37917d1 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -258,13 +258,13 @@ impl<'a> TranspileTest<'a> { transpile_snapshot( &platform, &c_path, - RustEdition::Rust2021, + RustEdition::Edition2021, expect_compile_error_edition_2021, ); transpile_snapshot( &platform, &c_path, - RustEdition::Rust2024, + RustEdition::Edition2024, expect_compile_error_edition_2024, ); } From 4470f88d273f712424e408cef0435987efc5f2bc Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 16:47:55 -0800 Subject: [PATCH 37/39] rust-tools: use `RustEditon::ALL` in `impl FromStr` --- c2rust-rust-tools/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/c2rust-rust-tools/src/lib.rs b/c2rust-rust-tools/src/lib.rs index 7a507b6138..251e52a7f9 100644 --- a/c2rust-rust-tools/src/lib.rs +++ b/c2rust-rust-tools/src/lib.rs @@ -49,9 +49,10 @@ impl FromStr for RustEdition { type Err = String; fn from_str(s: &str) -> Result { - let choices = [Self::Edition2021, Self::Edition2024]; + let choices = Self::ALL; choices .into_iter() + .copied() .find(|choice| choice.as_str() == s) .ok_or_else(|| format!("{s} not one of {}", choices.iter().join(" ,"))) } From f277687fdb50dd64bba5d72ac5fda0c6fd4bd948 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 16:50:46 -0800 Subject: [PATCH 38/39] transpile: import `Edition{2021,2024}` directly --- c2rust-rust-tools/src/lib.rs | 13 ++++++++----- c2rust-transpile/src/build_files/mod.rs | 5 +++-- c2rust-transpile/src/translator/builtins.rs | 4 +++- c2rust-transpile/src/translator/mod.rs | 7 ++++--- c2rust-transpile/src/translator/simd.rs | 2 +- c2rust-transpile/tests/snapshots.rs | 9 ++++++--- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/c2rust-rust-tools/src/lib.rs b/c2rust-rust-tools/src/lib.rs index 251e52a7f9..6222c8ed38 100644 --- a/c2rust-rust-tools/src/lib.rs +++ b/c2rust-rust-tools/src/lib.rs @@ -7,6 +7,9 @@ use std::path::Path; use std::process::Command; use std::str::FromStr; +use crate::RustEdition::Edition2021; +use crate::RustEdition::Edition2024; + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)] pub enum RustEdition { /// The default is edition 2021 because `c2rust-refactor`, @@ -17,12 +20,12 @@ pub enum RustEdition { } impl RustEdition { - pub const ALL: &[Self] = &[Self::Edition2021, Self::Edition2024]; + pub const ALL: &[Self] = &[Edition2021, Edition2024]; pub const fn as_str(&self) -> &'static str { match self { - Self::Edition2021 => "2021", - Self::Edition2024 => "2024", + Edition2021 => "2021", + Edition2024 => "2024", } } @@ -32,9 +35,9 @@ impl RustEdition { pub const fn toolchain(&self) -> &'static str { match self { // 1.70 (1.68 for syn v2.0, 1.70 for sparse registry) - Self::Edition2021 => "+nightly-2023-04-15", + Edition2021 => "+nightly-2023-04-15", // This doesn't really need to be pinned, but pin it for stability. - Self::Edition2024 => "+nightly-2026-03-03", + Edition2024 => "+nightly-2026-03-03", } } } diff --git a/c2rust-transpile/src/build_files/mod.rs b/c2rust-transpile/src/build_files/mod.rs index a106ae8f42..95251400f6 100644 --- a/c2rust-transpile/src/build_files/mod.rs +++ b/c2rust-transpile/src/build_files/mod.rs @@ -4,7 +4,8 @@ use std::io::Write; use std::path::{Path, PathBuf}; use std::str::FromStr; -use c2rust_rust_tools::{rustfmt, RustEdition}; +use c2rust_rust_tools::rustfmt; +use c2rust_rust_tools::RustEdition::Edition2024; use handlebars::Handlebars; use pathdiff::diff_paths; use serde_derive::Serialize; @@ -330,7 +331,7 @@ fn emit_cargo_toml<'lcmd>( "edition": tcfg.edition.as_str(), // This is already the default in Rust 1.77, // and edition 2024 was released in Rust 1.85. - "strip_debuginfo_release": tcfg.edition < RustEdition::Edition2024, + "strip_debuginfo_release": tcfg.edition < Edition2024, "crate_types": ccfg.link_cmd.r#type.as_cargo_types(), "is_library": ccfg.link_cmd.r#type.is_library(), "lib_rs_file": get_lib_rs_file_name(tcfg), diff --git a/c2rust-transpile/src/translator/builtins.rs b/c2rust-transpile/src/translator/builtins.rs index 48594d9f89..521cd2c5ee 100644 --- a/c2rust-transpile/src/translator/builtins.rs +++ b/c2rust-transpile/src/translator/builtins.rs @@ -5,6 +5,8 @@ use crate::format_translation_err; use super::*; +use c2rust_rust_tools::RustEdition::Edition2024; + /// The argument type for a libc builtin function #[derive(Copy, Clone, PartialEq)] enum LibcFnArgType { @@ -414,7 +416,7 @@ impl<'c> Translation<'c> { "__builtin_arm_yield" => { let fn_name = "__yield"; - if self.tcfg.edition < RustEdition::Edition2024 { + if self.tcfg.edition < Edition2024 { self.use_feature("stdsimd"); } else { // Edition 2024 was release in Rust 1.85. diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 177748edb2..4693b538c6 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -7,6 +7,7 @@ use std::path::{Path, PathBuf}; use std::rc::Rc; use c2rust_rust_tools::RustEdition; +use c2rust_rust_tools::RustEdition::Edition2024; use dtoa; use failure::{err_msg, format_err, Fail}; use indexmap::indexmap; @@ -373,7 +374,7 @@ pub fn stmts_block(mut stmts: Vec) -> Block { /// Whether `extern` blocks can be `unsafe` in this edition. fn extern_block_unsafety(edition: RustEdition) -> Unsafety { - if edition >= RustEdition::Edition2024 { + if edition >= Edition2024 { Unsafety::Unsafe } else { Unsafety::Normal @@ -382,7 +383,7 @@ fn extern_block_unsafety(edition: RustEdition) -> Unsafety { /// Whether attributes can be `unsafe` in this edition. fn attr_unsafety(edition: RustEdition) -> Unsafety { - if edition >= RustEdition::Edition2024 { + if edition >= Edition2024 { Unsafety::Unsafe } else { Unsafety::Normal @@ -1595,7 +1596,7 @@ impl<'c> Translation<'c> { /// Called when translation makes use of a language feature that will require a feature-gate. pub fn use_feature(&self, feature: &'static str) { if matches!(feature, "asm" | "label_break_value" | "raw_ref_op") - && self.tcfg.edition >= RustEdition::Edition2024 + && self.tcfg.edition >= Edition2024 { return; } diff --git a/c2rust-transpile/src/translator/simd.rs b/c2rust-transpile/src/translator/simd.rs index 62a15e89a2..303381cf70 100644 --- a/c2rust-transpile/src/translator/simd.rs +++ b/c2rust-transpile/src/translator/simd.rs @@ -160,7 +160,7 @@ impl<'c> Translation<'c> { .into()); } - if self.tcfg.edition < RustEdition::Edition2024 { + if self.tcfg.edition < Edition2024 { // Edition 2024 was release in Rust 1.85. // In Rust 1.78, `#![feature(stdsimd)]` was removed and split into individual features. // All of the x86_64 parts (that we use at least) were stabilized. diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index f2a37917d1..234acf61d3 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -7,8 +7,11 @@ use std::process::Command; use c2rust_rust_tools::rustc; use c2rust_rust_tools::sanitize_file_name; use c2rust_rust_tools::RustEdition; +use c2rust_rust_tools::RustEdition::Edition2021; +use c2rust_rust_tools::RustEdition::Edition2024; use c2rust_transpile::convert_type::RESERVED_NAMES; -use c2rust_transpile::{ReplaceMode, TranspilerConfig}; +use c2rust_transpile::ReplaceMode; +use c2rust_transpile::TranspilerConfig; use itertools::Itertools; fn config(edition: RustEdition) -> TranspilerConfig { @@ -258,13 +261,13 @@ impl<'a> TranspileTest<'a> { transpile_snapshot( &platform, &c_path, - RustEdition::Edition2021, + Edition2021, expect_compile_error_edition_2021, ); transpile_snapshot( &platform, &c_path, - RustEdition::Edition2024, + Edition2024, expect_compile_error_edition_2024, ); } From 9bbfccc0b7d28959bd1dcf88f54869813e7c9109 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Sat, 7 Mar 2026 16:56:32 -0800 Subject: [PATCH 39/39] tests/unit: remove now-stabilized `#![feature(...)]`s in edition 2024 tests --- tests/unit/arrays/src/test_arrays.rs | 2 -- tests/unit/casts/src/test_casts.rs | 2 -- tests/unit/gotos/src/test_translation_only.rs | 2 -- tests/unit/ints/src/test_arithmetic.rs | 2 -- tests/unit/macros/src/test_define.rs | 2 -- 5 files changed, 10 deletions(-) diff --git a/tests/unit/arrays/src/test_arrays.rs b/tests/unit/arrays/src/test_arrays.rs index 109b21d245..c728b0a110 100644 --- a/tests/unit/arrays/src/test_arrays.rs +++ b/tests/unit/arrays/src/test_arrays.rs @@ -1,5 +1,3 @@ -//! feature_raw_ref_op - use crate::arrays::rust_entry; use crate::incomplete_arrays::{rust_check_some_ints, rust_entry2, rust_test_sized_array}; use crate::variable_arrays::{rust_alloca_arrays, rust_variable_arrays}; diff --git a/tests/unit/casts/src/test_casts.rs b/tests/unit/casts/src/test_casts.rs index 8d7bf05aa1..6f0c7509ea 100644 --- a/tests/unit/casts/src/test_casts.rs +++ b/tests/unit/casts/src/test_casts.rs @@ -1,5 +1,3 @@ -//! feature_raw_ref_op - use crate::cast_funptr::{rust_entry, rust_get_identity, rust_identity}; use crate::casts::rust_cast_stuff; diff --git a/tests/unit/gotos/src/test_translation_only.rs b/tests/unit/gotos/src/test_translation_only.rs index 139af7c1f4..bfebd02297 100644 --- a/tests/unit/gotos/src/test_translation_only.rs +++ b/tests/unit/gotos/src/test_translation_only.rs @@ -1,5 +1,3 @@ -//! feature_label_break_value - use crate::jump_into_loop::rust_jump_into_loop; use crate::label_break_trigger::rust_triggers_label_break; diff --git a/tests/unit/ints/src/test_arithmetic.rs b/tests/unit/ints/src/test_arithmetic.rs index 15021806a4..155a80ab48 100644 --- a/tests/unit/ints/src/test_arithmetic.rs +++ b/tests/unit/ints/src/test_arithmetic.rs @@ -1,5 +1,3 @@ -//! feature_raw_ref_op - use crate::arithmetic::rust_entry2; use std::ffi::{c_int, c_uint}; diff --git a/tests/unit/macros/src/test_define.rs b/tests/unit/macros/src/test_define.rs index 70f917c05c..2c76c8df1b 100644 --- a/tests/unit/macros/src/test_define.rs +++ b/tests/unit/macros/src/test_define.rs @@ -1,5 +1,3 @@ -//! feature_raw_ref_op - use crate::define::{rust_fns, rust_stmt_expr_inc}; use crate::define::{rust_reference_define, TEST_CONST1, TEST_CONST2, TEST_PARENS}; use crate::define::{rust_test_zstd, ZSTD_WINDOWLOG_MAX_32, ZSTD_WINDOWLOG_MAX_64};