Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Include struct names in ABIs #2266

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn create_input_toml_template(
.collect();
toml::Value::Array(default_value_vec)
}
AbiType::Struct { fields } => {
AbiType::Struct { fields, .. } => {
let default_value_map = toml::map::Map::from_iter(
fields.into_iter().map(|(name, typ)| (name, default_value(typ))),
);
Expand Down Expand Up @@ -128,6 +128,7 @@ mod tests {
typed_param(
"d",
AbiType::Struct {
name: String::from("MyStruct"),
fields: vec![
(String::from("d1"), AbiType::Field),
(
Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_abi/src/input_parser/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl JsonTypes {

(InputValue::String(s), AbiType::String { .. }) => JsonTypes::String(s.to_string()),

(InputValue::Struct(map), AbiType::Struct { fields }) => {
(InputValue::Struct(map), AbiType::Struct { fields, .. }) => {
let map_with_json_types = try_btree_map(fields, |(key, field_type)| {
JsonTypes::try_from_input_value(&map[key], field_type)
.map(|json_value| (key.to_owned(), json_value))
Expand Down Expand Up @@ -155,7 +155,7 @@ impl InputValue {
InputValue::Vec(array_elements)
}

(JsonTypes::Table(table), AbiType::Struct { fields }) => {
(JsonTypes::Table(table), AbiType::Struct { fields, .. }) => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
// Check that json contains a value for each field of the struct.
let field_id = format!("{arg_name}.{field_name}");
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_abi/src/input_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ mod serialization_tests {
AbiParameter {
name: "bar".into(),
typ: AbiType::Struct {
name: "MyStruct".into(),
fields: vec![
("field1".into(), AbiType::Integer { sign: Sign::Unsigned, width: 8 }),
(
Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_abi/src/input_parser/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl TomlTypes {

(InputValue::String(s), AbiType::String { .. }) => TomlTypes::String(s.to_string()),

(InputValue::Struct(map), AbiType::Struct { fields }) => {
(InputValue::Struct(map), AbiType::Struct { fields, .. }) => {
let map_with_toml_types = try_btree_map(fields, |(key, field_type)| {
TomlTypes::try_from_input_value(&map[key], field_type)
.map(|toml_value| (key.to_owned(), toml_value))
Expand Down Expand Up @@ -138,7 +138,7 @@ impl InputValue {
InputValue::Vec(array_elements)
}

(TomlTypes::Table(table), AbiType::Struct { fields }) => {
(TomlTypes::Table(table), AbiType::Struct { fields, .. }) => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
// Check that json contains a value for each field of the struct.
let field_id = format!("{arg_name}.{field_name}");
Expand Down
3 changes: 2 additions & 1 deletion crates/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub enum AbiType {
},
Boolean,
Struct {
name: String,
#[serde(
serialize_with = "serialization::serialize_struct_fields",
deserialize_with = "serialization::deserialize_struct_fields"
Expand Down Expand Up @@ -308,7 +309,7 @@ impl Abi {
encoded_value.extend(str_as_fields);
}

(InputValue::Struct(object), AbiType::Struct { fields }) => {
(InputValue::Struct(object), AbiType::Struct { fields, .. }) => {
for (field, typ) in fields {
encoded_value.extend(Self::encode_value(object[field].clone(), typ)?);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/noirc_abi/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ mod tests {
\"name\":\"thing3\",
\"type\": {
\"kind\":\"struct\",
\"name\": \"MyStruct\",
\"fields\": [
{
\"name\": \"field1\",
Expand Down Expand Up @@ -119,6 +120,7 @@ mod tests {
let expected_struct = AbiParameter {
name: "thing3".to_string(),
typ: AbiType::Struct {
name: "MyStruct".to_string(),
fields: vec![
("field1".to_string(), AbiType::Integer { sign: Sign::Unsigned, width: 3 }),
(
Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@
Type::Bool => write!(f, "bool"),
Type::String(len) => write!(f, "str<{len}>"),
Type::FmtString(len, elements) => {
write!(f, "fmtstr<{len}, {elements}>")

Check warning on line 559 in crates/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (fmtstr)
}
Type::Unit => write!(f, "()"),
Type::Error => write!(f, "error"),
Expand Down Expand Up @@ -986,7 +986,7 @@
let struct_type = def.borrow();
let fields = struct_type.get_fields(args);
let fields = vecmap(fields, |(name, typ)| (name, typ.as_abi_type()));
AbiType::Struct { fields }
AbiType::Struct { fields, name: struct_type.name.to_string() }
}
Type::Tuple(_) => todo!("as_abi_type not yet implemented for tuple types"),
Type::TypeVariable(_, _) => unreachable!(),
Expand Down