summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCasper <casperneo@uchicago.edu>2022-12-15 01:04:57 -0500
committerGitHub <noreply@github.com>2022-12-15 06:04:57 +0000
commita078130c878b0f997af24d532c6ade903ea7f65b (patch)
tree50bf3cb1d9896c21ce9e88b3c4404904acc18373 /src
parent9ed76559dfd266e79e2770c69d5236a211a52bee (diff)
downloadflatbuffers-a078130c878b0f997af24d532c6ade903ea7f65b.tar.gz
flatbuffers-a078130c878b0f997af24d532c6ade903ea7f65b.tar.bz2
flatbuffers-a078130c878b0f997af24d532c6ade903ea7f65b.zip
Fix Rust codegen escaping field in tables. (#7659)
* Fix Rust codegen escaping field in tables. * other gencode * gencode * removed a debug print * regen code Co-authored-by: Casper Neo <cneo@google.com> Co-authored-by: Derek Bailey <derekbailey@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/idl_gen_rust.cpp20
-rw-r--r--src/idl_namer.h9
2 files changed, 23 insertions, 6 deletions
diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp
index c01410a6..4ea9122e 100644
--- a/src/idl_gen_rust.cpp
+++ b/src/idl_gen_rust.cpp
@@ -1629,7 +1629,7 @@ class RustGenerator : public BaseGenerator {
code_.SetValue("OFFSET_VALUE", NumToString(field.value.offset));
code_.SetValue("FIELD", namer_.Field(field));
code_.SetValue("BLDR_DEF_VAL", GetDefaultValue(field, kBuilder));
- code_.SetValue("DISCRIMINANT", namer_.Field(field) + "_type");
+ code_.SetValue("DISCRIMINANT", namer_.LegacyRustUnionTypeMethod(field));
code_.IncrementIdentLevel();
cb(field);
code_.DecrementIdentLevel();
@@ -1747,7 +1747,10 @@ class RustGenerator : public BaseGenerator {
const auto &enum_def = *type.enum_def;
code_.SetValue("ENUM_TY", WrapInNameSpace(enum_def));
code_.SetValue("NATIVE_ENUM_NAME", NamespacedNativeName(enum_def));
- code_ += " let {{FIELD}} = match self.{{FIELD}}_type() {";
+ code_.SetValue("UNION_TYPE_METHOD",
+ namer_.LegacyRustUnionTypeMethod(field));
+
+ code_ += " let {{FIELD}} = match self.{{UNION_TYPE_METHOD}}() {";
code_ += " {{ENUM_TY}}::NONE => {{NATIVE_ENUM_NAME}}::NONE,";
ForAllUnionObjectVariantsBesidesNone(enum_def, [&] {
code_ +=
@@ -1973,10 +1976,12 @@ class RustGenerator : public BaseGenerator {
const EnumDef &union_def = *field.value.type.enum_def;
code_.SetValue("UNION_TYPE", WrapInNameSpace(union_def));
code_.SetValue("UNION_TYPE_OFFSET_NAME",
- namer_.LegacyRustFieldOffsetName(field) + "_TYPE");
+ namer_.LegacyRustUnionTypeOffsetName(field));
+ code_.SetValue("UNION_TYPE_METHOD",
+ namer_.LegacyRustUnionTypeMethod(field));
code_ +=
"\n .visit_union::<{{UNION_TYPE}}, _>("
- "\"{{FIELD}}_type\", Self::{{UNION_TYPE_OFFSET_NAME}}, "
+ "\"{{UNION_TYPE_METHOD}}\", Self::{{UNION_TYPE_OFFSET_NAME}}, "
"\"{{FIELD}}\", Self::{{OFFSET_NAME}}, {{IS_REQ}}, "
"|key, v, pos| {";
code_ += " match key {";
@@ -2045,8 +2050,10 @@ class RustGenerator : public BaseGenerator {
const auto &enum_def = *type.enum_def;
code_.SetValue("ENUM_TY", WrapInNameSpace(enum_def));
code_.SetValue("FIELD", namer_.Field(field));
+ code_.SetValue("UNION_TYPE_METHOD",
+ namer_.LegacyRustUnionTypeMethod(field));
- code_ += " match self.{{FIELD}}_type() {";
+ code_ += " match self.{{UNION_TYPE_METHOD}}() {";
code_ += " {{ENUM_TY}}::NONE => (),";
ForAllUnionObjectVariantsBesidesNone(enum_def, [&] {
code_.SetValue("FIELD", namer_.Field(field));
@@ -2255,8 +2262,9 @@ class RustGenerator : public BaseGenerator {
case ftUnionValue: {
code_.SetValue("ENUM_METHOD",
namer_.Method(*field.value.type.enum_def));
+ code_.SetValue("DISCRIMINANT", namer_.LegacyRustUnionTypeMethod(field));
code_ +=
- " let {{FIELD}}_type = "
+ " let {{DISCRIMINANT}} = "
"self.{{FIELD}}.{{ENUM_METHOD}}_type();";
code_ += " let {{FIELD}} = self.{{FIELD}}.pack(_fbb);";
return;
diff --git a/src/idl_namer.h b/src/idl_namer.h
index f60e30c3..7f89433d 100644
--- a/src/idl_namer.h
+++ b/src/idl_namer.h
@@ -102,6 +102,10 @@ class IdlNamer : public Namer {
std::string LegacyRustFieldOffsetName(const FieldDef &field) const {
return "VT_" + ConvertCase(EscapeKeyword(field.name), Case::kAllUpper);
}
+ std::string LegacyRustUnionTypeOffsetName(const FieldDef &field) const {
+ return "VT_" + ConvertCase(EscapeKeyword(field.name + "_type"), Case::kAllUpper);
+ }
+
std::string LegacySwiftVariant(const EnumVal &ev) const {
auto name = ev.name;
@@ -140,6 +144,11 @@ class IdlNamer : public Namer {
return "mutate_" + d.name;
}
+ std::string LegacyRustUnionTypeMethod(const FieldDef &d) {
+ // assert d is a union
+ return Method(d.name + "_type");
+ }
+
private:
std::string NamespacedString(const struct Namespace *ns,
const std::string &str) const {