Skip to content

Commit b9d5d1e

Browse files
authored
fix(go, c): escape ret and err (#774)
* fix(go, c): escape ret and err Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> * added the test case Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> * skip go_params test in C# Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> * rustfmt Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> --------- Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
1 parent 0024241 commit b9d5d1e

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

crates/c/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,6 +2833,10 @@ pub fn to_c_ident(name: &str) -> String {
28332833
"xor" => "xor_".into(),
28342834
"xor_eq" => "xor_eq_".into(),
28352835
"_Packed" => "_Packed_".into(),
2836+
// ret and err needs to be escaped because they are used as
2837+
// variable names for option and result flattening.
2838+
"ret" => "ret_".into(),
2839+
"err" => "err_".into(),
28362840
s => s.to_snake_case(),
28372841
}
28382842
}

crates/csharp/tests/codegen.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ macro_rules! codegen_test {
6363
"variants",
6464
"worlds-with-types",
6565
"zero-size-tuple",
66+
"go_params",
6667
]
6768
.contains(&$name)
6869
{

crates/go/src/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl InterfaceGenerator<'_> {
433433
if is_pointer {
434434
prefix.push_str("&");
435435
}
436-
if name != "err" && name != "ret" {
436+
if name != "ret" {
437437
param_name = format!("lower_{name}");
438438
} else {
439439
param_name.push_str(name);

crates/go/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ fn avoid_keyword(s: &str) -> String {
362362
}
363363

364364
// a list of Go keywords
365-
const GOKEYWORDS: [&str; 25] = [
365+
const GOKEYWORDS: [&str; 26] = [
366366
"break",
367367
"default",
368368
"func",
@@ -388,4 +388,7 @@ const GOKEYWORDS: [&str; 25] = [
388388
"import",
389389
"return",
390390
"var",
391+
// not a Go keyword but needs to escape due to
392+
// it's used as a variable name that passes to C
393+
"ret",
391394
];

tests/codegen/go_params.wit

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package foo:foo;
2+
3+
world go-params {
4+
import error-code: func(err: u32) -> option<u32>;
5+
import error-code2: func(ret: u32) -> option<u32>;
6+
import error-code3: func(err: u32, ret: option<u32>) -> result<u32>;
7+
import error-code4: func() -> result<u32>;
8+
}

0 commit comments

Comments
 (0)