Skip to content

Commit

Permalink
fix(tinygo): syntax error when field name is 'type' in record and a f…
Browse files Browse the repository at this point in the history
…unction exists that uses that record (#912)

* add world-has-go-keyword testcase

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* add world-has-go-keyword testcase

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* fix(tinygo): fix world-has-go-keyword testcase

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* add enum-has-go-keyword testcase

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* add interface-has-go-keyword testcase

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* add interface-has-go-keyword testcase

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* add record-has-go-keyword-and-used-in-fn testcase

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* fix(tinygo): fix record-has-go-keyword-and-used-in-fn testcase

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* update list of unsupported testcases for csharp

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* feat(go): add understore as prefix to keyword in go

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>

---------

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>
Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
Co-authored-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
  • Loading branch information
rajatjindal and Mossaka authored Apr 2, 2024
1 parent 2339d53 commit 174c584
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 5 deletions.
3 changes: 3 additions & 0 deletions crates/csharp/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ macro_rules! codegen_test {
"import-and-export-resource",
"import-and-export-resource-alias",
"import-func",
"interface-has-golang-keyword",
"issue544",
"issue551",
"issue569",
"issue573",
"issue607",
"issue668",
"enum-has-golang-keyword",
"just-export",
"lift-lower-foreign",
"lists",
"many-arguments",
"option-result",
"record-has-keyword-used-in-func",
"rename-interface",
"resource-alias",
"resource-borrow-in-record",
Expand Down
11 changes: 8 additions & 3 deletions crates/go/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
uwriteln!(self.lower_src, "var {lower_name} {c_typedef_target}");
for field in r.fields.iter() {
let c_field_name = &self.get_c_field_name(field);
let field_name = &self.interface.field_name(field);
let field_name = &self.get_go_field_name(field);

self.lower_value(
&format!("{param}.{field_name}"),
Expand Down Expand Up @@ -439,7 +439,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
ty_name = self.interface.get_ty(&Type::Id(*id)),
);
for field in r.fields.iter() {
let field_name = &self.interface.field_name(field);
let field_name = &self.get_go_field_name(field);
let c_field_name = &self.get_c_field_name(field);
self.lift_value(
&format!("{param}.{c_field_name}"),
Expand Down Expand Up @@ -690,6 +690,11 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
}

pub(crate) fn get_c_field_name(&mut self, field: &Field) -> String {
field.name.to_snake_case()
avoid_keyword(field.name.to_snake_case().as_str())
}

pub(crate) fn get_go_field_name(&mut self, field: &Field) -> String {
let name = &self.interface.field_name(field);
avoid_keyword(name)
}
}
4 changes: 2 additions & 2 deletions crates/go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl WorldGenerator for TinyGo {
// prepend package and imports header
let src = mem::take(&mut self.src);
wit_bindgen_core::generated_preamble(&mut self.src, env!("CARGO_PKG_VERSION"));
let snake = self.world.to_snake_case();
let snake = avoid_keyword(self.world.to_snake_case().as_str()).to_owned();
// add package
self.src.push_str("package ");
self.src.push_str(&snake);
Expand Down Expand Up @@ -365,7 +365,7 @@ impl WorldGenerator for TinyGo {

fn avoid_keyword(s: &str) -> String {
if GOKEYWORDS.contains(&s) {
format!("{s}_")
format!("_{s}")
} else {
s.into()
}
Expand Down
19 changes: 19 additions & 0 deletions tests/codegen/enum-has-go-keyword.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package foo:foo;

interface foo-bar {
record foo{
something: string,
anything: string
}

enum bar{
anything,
%type
}

fetch: func(x: foo, e: bar) -> string;
}

world foo-world {
import foo-bar;
}
18 changes: 18 additions & 0 deletions tests/codegen/interface-has-go-keyword.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package foo:foo;

interface %type {
record foo{
something: string,
anything: string
}

record bar{
anything: u16
}

fetch: func(x: foo) -> result<bar>;
}

world foo-world {
import %type;
}
18 changes: 18 additions & 0 deletions tests/codegen/record-has-go-keyword-and-used-in-fn.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package foo:foo;

interface foo-bar {
record foo{
%type: string,
anything: string
}

record bar{
anything: u16
}

fetch: func(x: foo) -> result<bar>;
}

world foo-world {
import foo-bar;
}
9 changes: 9 additions & 0 deletions tests/codegen/world-has-go-keyword.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package foo:foo;

interface foo-bar {
hello: func() -> string;
}

world %type {
import foo-bar;
}

0 comments on commit 174c584

Please sign in to comment.