Skip to content

Commit 174c584

Browse files
rajatjindalMossaka
andauthored
fix(tinygo): syntax error when field name is 'type' in record and a function exists that uses that record (bytecodealliance#912)
* add world-has-go-keyword testcase Signed-off-by: Rajat Jindal <[email protected]> * add world-has-go-keyword testcase Signed-off-by: Rajat Jindal <[email protected]> * fix(tinygo): fix world-has-go-keyword testcase Signed-off-by: Rajat Jindal <[email protected]> * add enum-has-go-keyword testcase Signed-off-by: Rajat Jindal <[email protected]> * add interface-has-go-keyword testcase Signed-off-by: Rajat Jindal <[email protected]> * add interface-has-go-keyword testcase Signed-off-by: Rajat Jindal <[email protected]> * add record-has-go-keyword-and-used-in-fn testcase Signed-off-by: Rajat Jindal <[email protected]> * fix(tinygo): fix record-has-go-keyword-and-used-in-fn testcase Signed-off-by: Rajat Jindal <[email protected]> * update list of unsupported testcases for csharp Signed-off-by: Rajat Jindal <[email protected]> * feat(go): add understore as prefix to keyword in go Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> --------- Signed-off-by: Rajat Jindal <[email protected]> Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> Co-authored-by: Jiaxiao Zhou (Mossaka) <[email protected]>
1 parent 2339d53 commit 174c584

File tree

7 files changed

+77
-5
lines changed

7 files changed

+77
-5
lines changed

crates/csharp/tests/codegen.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@ macro_rules! codegen_test {
2121
"import-and-export-resource",
2222
"import-and-export-resource-alias",
2323
"import-func",
24+
"interface-has-golang-keyword",
2425
"issue544",
2526
"issue551",
2627
"issue569",
2728
"issue573",
2829
"issue607",
2930
"issue668",
31+
"enum-has-golang-keyword",
3032
"just-export",
3133
"lift-lower-foreign",
3234
"lists",
3335
"many-arguments",
3436
"option-result",
37+
"record-has-keyword-used-in-func",
3538
"rename-interface",
3639
"resource-alias",
3740
"resource-borrow-in-record",

crates/go/src/bindgen.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
198198
uwriteln!(self.lower_src, "var {lower_name} {c_typedef_target}");
199199
for field in r.fields.iter() {
200200
let c_field_name = &self.get_c_field_name(field);
201-
let field_name = &self.interface.field_name(field);
201+
let field_name = &self.get_go_field_name(field);
202202

203203
self.lower_value(
204204
&format!("{param}.{field_name}"),
@@ -439,7 +439,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
439439
ty_name = self.interface.get_ty(&Type::Id(*id)),
440440
);
441441
for field in r.fields.iter() {
442-
let field_name = &self.interface.field_name(field);
442+
let field_name = &self.get_go_field_name(field);
443443
let c_field_name = &self.get_c_field_name(field);
444444
self.lift_value(
445445
&format!("{param}.{c_field_name}"),
@@ -690,6 +690,11 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
690690
}
691691

692692
pub(crate) fn get_c_field_name(&mut self, field: &Field) -> String {
693-
field.name.to_snake_case()
693+
avoid_keyword(field.name.to_snake_case().as_str())
694+
}
695+
696+
pub(crate) fn get_go_field_name(&mut self, field: &Field) -> String {
697+
let name = &self.interface.field_name(field);
698+
avoid_keyword(name)
694699
}
695700
}

crates/go/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl WorldGenerator for TinyGo {
304304
// prepend package and imports header
305305
let src = mem::take(&mut self.src);
306306
wit_bindgen_core::generated_preamble(&mut self.src, env!("CARGO_PKG_VERSION"));
307-
let snake = self.world.to_snake_case();
307+
let snake = avoid_keyword(self.world.to_snake_case().as_str()).to_owned();
308308
// add package
309309
self.src.push_str("package ");
310310
self.src.push_str(&snake);
@@ -365,7 +365,7 @@ impl WorldGenerator for TinyGo {
365365

366366
fn avoid_keyword(s: &str) -> String {
367367
if GOKEYWORDS.contains(&s) {
368-
format!("{s}_")
368+
format!("_{s}")
369369
} else {
370370
s.into()
371371
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package foo:foo;
2+
3+
interface foo-bar {
4+
record foo{
5+
something: string,
6+
anything: string
7+
}
8+
9+
enum bar{
10+
anything,
11+
%type
12+
}
13+
14+
fetch: func(x: foo, e: bar) -> string;
15+
}
16+
17+
world foo-world {
18+
import foo-bar;
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package foo:foo;
2+
3+
interface %type {
4+
record foo{
5+
something: string,
6+
anything: string
7+
}
8+
9+
record bar{
10+
anything: u16
11+
}
12+
13+
fetch: func(x: foo) -> result<bar>;
14+
}
15+
16+
world foo-world {
17+
import %type;
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package foo:foo;
2+
3+
interface foo-bar {
4+
record foo{
5+
%type: string,
6+
anything: string
7+
}
8+
9+
record bar{
10+
anything: u16
11+
}
12+
13+
fetch: func(x: foo) -> result<bar>;
14+
}
15+
16+
world foo-world {
17+
import foo-bar;
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package foo:foo;
2+
3+
interface foo-bar {
4+
hello: func() -> string;
5+
}
6+
7+
world %type {
8+
import foo-bar;
9+
}

0 commit comments

Comments
 (0)