Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

C: write a function named like a Rust keyword as r#name (#1427)

qenjuquany5d26983698ef
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5139f258..8d859012 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- `bridge` on a C function named like a Rust keyword (`mod`, `match`, `where`) writes `r#mod` in
+ `port.rs`, in the `port::` calls and for the exported function in `lib.rs`, so the crate builds
+ and still exports the symbol `mod`; it used to write `fn mod` and fail to compile.
- The MCP `port_unit` prompt takes `"language": "ts"`, the name `analyze --json` reports and every
tool's `lang` takes, and adds the TypeScript notes; `typescript` still works. Its argument
description and its error list `ts`. Before, `ts` was refused as an invalid parameter.
@@ -29,6 +32,7 @@
the original for the slower functions, instead of `bridge --keep`, which refuses an existing
crate. Its `diff` hints name the crate: `rust-anything diff <crate>` (#1356 by @m3lonxd).
description and its error list `ts`. Before, `ts` was refused as an invalid parameter (#1329 by
+ and still exports the symbol `mod`; it used to write `fn mod` and fail to compile (#1333 by
@nmnhut0208).
- `obligations --evidence` ends with `1 of 3 obligations has no recorded case` for a single
one. With no obligations at all, it and `app record` say that none were listed and point to
diff --git a/src/codegen_c.rs b/src/codegen_c.rs
index 59331b8f..edebdc20 100644
--- a/src/codegen_c.rs
+++ b/src/codegen_c.rs
@@ -197,6 +197,18 @@ fn ident(name: &str) -> String {
crate::bridge::rust_ident(name)
}
+/// A C function's name as a Rust identifier: `r#mod` for a keyword, which still exports the symbol
+/// `mod`. Names `rust_ident` would rename (`self` to `self_`) are kept as written, since a renamed
+/// function would export another symbol.
+fn fn_ident(name: &str) -> String {
+ let id = ident(name);
+ if id.starts_with("r#") {
+ id
+ } else {
+ name.to_string()
+ }
+}
+
fn ret_type(f: &ExportedFn) -> Option<String> {
if let Some(s) = c_struct(&f.returns) {
return Some(format!("crate::{}", s.rust_name()));
@@ -328,7 +340,12 @@ pub fn port_rs(m: &Manifest) -> String {
f.params[i].name
);
}
- let _ = writeln!(out, "pub fn {}({}){ret} {{", f.name, params.join(", "));
+ let _ = writeln!(
+ out,
+ "pub fn {}({}){ret} {{",
+ fn_ident(&f.name),
+ params.join(", ")
+ );
let names: Vec<String> = shown
.iter()
.map(|p| format!("&{}", ident(&p.name)))
@@ -610,7 +627,7 @@ pub fn lib_rs(m: &Manifest, port: &str) -> String {
})
.collect();
let ret = ret_type(f).map(|r| format!(" -> {r}")).unwrap_or_default();
- let call = format!("port::{}({})", f.name, args.join(", "));
+ let call = format!("port::{}({})", fn_ident(&f.name), args.join(", "));
let body = match returns_into(f) {
Some(i) => {
let target = ident(&f.params[i].name);
@@ -677,13 +694,14 @@ pub fn lib_rs(m: &Manifest, port: &str) -> String {
"\n/// `{prototype}` (`{}:{}`).\n\
#[cfg(not(feature = \"shadow\"))]\n\
// SAFETY: exported under the C name it replaces: link it instead of the original object.\n\
- #[unsafe(no_mangle)]\npub extern \"C\" fn {name}({params}){ret} {{\n {body}\n}}\n\n\
+ #[unsafe(no_mangle)]\npub extern \"C\" fn {rust_name}({params}){ret} {{\n {body}\n}}\n\n\
/// `{name}` as `rust_anything_{name}`, for shadow builds beside the original.\n\
// SAFETY: a name no C code defines.\n\
#[unsafe(no_mangle)]\npub extern \"C\" fn rust_anything_{name}({params}){ret} {{\n {body}\n}}",
f.file,
f.line,
name = f.name,
+ rust_name = fn_ident(&f.name),
params = params.join(", "),
);
}
@@ -991,6 +1009,35 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
+ #[test]
+ fn a_function_named_like_a_rust_keyword_gets_a_raw_identifier() {
+ let m = Manifest {
+ schema: String::new(),
+ root: std::path::PathBuf::new(),
+ root_written: None,
+ unit: "m.c".into(),
+ language: crate::lang::Lang::C,
+ kind: crate::bridge::Kind::CAbi,
+ crate_name: "m-rs".into(),
+ exports: crate::lang::c::exports("m.c", "int mod(int a, int b) { return a % b; }"),
+ classes: Vec::new(),
+ js_only: Vec::new(),
+ batches: Vec::new(),
+ allocator: Default::default(),
+ float_tolerance: None,
+ ignore_key_order: false,
+ messages: false,
+ end_state: None,
+ };
+ let port = port_rs(&m);
+ assert!(port.contains("pub fn r#mod("), "{port}");
+ let lib = lib_rs(&m, &port);
+ assert!(lib.contains("pub extern \"C\" fn r#mod("), "{lib}");
+ assert!(lib.contains("port::r#mod("), "{lib}");
+ assert!(!lib.contains("port::mod("), "{lib}");
+ assert!(lib.contains("fn rust_anything_mod("), "{lib}");
+ }
+
#[test]
fn test_pointee() {
// Scalar