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

Type arguments in JSON ABI and the function selector #2218

Merged
merged 10 commits into from
Jul 6, 2022
33 changes: 1 addition & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion forc-pkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ description = "Building, locking, fetching and updating Sway projects as Forc pa
anyhow = "1"
forc-util = { version = "0.17.0", path = "../forc-util" }
fuel-tx = "0.13"
fuels-types = "0.12"
git2 = { version = "0.14", features = ["vendored-libgit2", "vendored-openssl"] }
petgraph = { version = "0.6", features = ["serde-1"] }
semver = { version = "1.0", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_ignored = "0.1"
sway-core = { version = "0.17.0", path = "../sway-core" }
sway-types = { version = "0.17.0", path = "../sway-types" }
sway-utils = { version = "0.17.0", path = "../sway-utils" }
toml = "0.5"
tracing = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use forc_util::{
print_on_success, print_on_success_library, println_yellow_err,
};
use fuel_tx::StorageSlot;
use fuels_types::JsonABI;
use petgraph::{
self,
visit::{EdgeRef, IntoNodeReferences},
Expand All @@ -27,6 +26,7 @@ use sway_core::{
semantic_analysis::namespace, source_map::SourceMap, types::*, BytecodeCompilationResult,
CompileAstResult, CompileError, TreeType,
};
use sway_types::JsonABI;
use sway_utils::constants;
use tracing::info;
use url::Url;
Expand Down
1 change: 0 additions & 1 deletion sway-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fuel-crypto = "0.5"
fuel-tx = "0.13"
fuel-types = "0.5"
fuel-vm = "0.12"
fuels-types = "0.12"
hex = { version = "0.4", optional = true }
im = "15.0"
itertools = "0.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use crate::{
types::{JsonAbiString, ToJsonAbi},
TypeInfo,
};
use fuels_types::Property;
use std::hash::{Hash, Hasher};
use sway_types::{Ident, Span, Spanned};
use sway_types::{Ident, Property, Span, Spanned};

#[derive(Clone, Debug, Eq)]
pub struct TypedEnumDeclaration {
Expand Down Expand Up @@ -190,6 +189,10 @@ impl ToJsonAbi for TypedEnumVariant {
name: self.name.to_string(),
type_field: self.type_id.json_abi_str(),
components: self.type_id.generate_json_abi(),
type_arguments: self
.type_id
.get_type_parameters()
.map(|v| v.iter().map(|param| param.generate_json_abi()).collect()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code golf: .map(TypeParameter::generate_json_abi)

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ mod function_parameter;
pub use function_parameter::*;

use crate::{error::*, parse_tree::*, semantic_analysis::*, style::*, type_engine::*, types::*};
use fuels_types::{Function, Property};
use sha2::{Digest, Sha256};
use sway_types::{Ident, Span, Spanned};
use sway_types::{Function, Ident, Property, Span, Spanned};

#[derive(Clone, Debug, Eq)]
pub struct TypedFunctionDeclaration {
Expand Down Expand Up @@ -95,12 +94,20 @@ impl ToJsonAbi for TypedFunctionDeclaration {
name: x.name.as_str().to_string(),
type_field: x.type_id.json_abi_str(),
components: x.type_id.generate_json_abi(),
type_arguments: x
.type_id
.get_type_parameters()
.map(|v| v.iter().map(|param| param.generate_json_abi()).collect()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar golf can be played here

})
.collect(),
outputs: vec![Property {
name: "".to_string(),
type_field: self.return_type.json_abi_str(),
components: self.return_type.generate_json_abi(),
type_arguments: self
.return_type
.get_type_parameters()
.map(|v| v.iter().map(|param| param.generate_json_abi()).collect()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why go for par when you can go for a birdie?

}],
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{error::*, parse_tree::*, semantic_analysis::*, type_engine::*, types::*};
use fuels_types::Property;
use std::hash::{Hash, Hasher};
use sway_types::{Ident, Span, Spanned};
use sway_types::{Ident, Property, Span, Spanned};

#[derive(Clone, Debug, Eq)]
pub struct TypedStructDeclaration {
Expand Down Expand Up @@ -182,6 +181,10 @@ impl ToJsonAbi for TypedStructField {
name: self.name.to_string(),
type_field: self.type_id.json_abi_str(),
components: self.type_id.generate_json_abi(),
type_arguments: self
.type_id
.get_type_parameters()
.map(|v| v.iter().map(|param| param.generate_json_abi()).collect()),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions sway-core/src/semantic_analysis/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use crate::{
types::ToJsonAbi,
};
use fuel_tx::StorageSlot;
use fuels_types::JsonABI;
use sway_types::{span::Span, Ident, Spanned};
use sway_types::{span::Span, Ident, JsonABI, Spanned};

#[derive(Clone, Debug)]
pub struct TypedProgram {
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/type_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub(crate) use type_parameter::*;
pub(crate) use unresolved_type_check::*;

use crate::error::*;
use fuels_types::Property;
use std::fmt::Debug;
use sway_types::Property;

#[test]
fn generic_enum_resolution() {
Expand Down
7 changes: 5 additions & 2 deletions sway-core/src/type_engine/type_argument.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::{type_engine::*, types::*};
use fuels_types::Property;
use std::{
fmt,
hash::{Hash, Hasher},
};
use sway_types::Span;
use sway_types::{Property, Span};

#[derive(Debug, Clone)]
pub struct TypeArgument {
Expand Down Expand Up @@ -59,6 +58,10 @@ impl ToJsonAbi for TypeArgument {
name: "__tuple_element".to_string(),
type_field: self.type_id.json_abi_str(),
components: self.type_id.generate_json_abi(),
type_arguments: self
.type_id
.get_type_parameters()
.map(|v| v.iter().map(|param| param.generate_json_abi()).collect()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm running out of creativity but the same code golf could apply here

}
}
}
Expand Down
13 changes: 13 additions & 0 deletions sway-core/src/type_engine/type_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl ToJsonAbi for TypeId {
name: "__array_element".to_string(),
type_field: type_id.json_abi_str(),
components: type_id.generate_json_abi(),
type_arguments: None,
}]),
TypeInfo::Enum { variant_types, .. } => Some(
variant_types
Expand Down Expand Up @@ -161,4 +162,16 @@ impl TypeId {
}
};
}

pub(crate) fn get_type_parameters(&self) -> Option<Vec<TypeParameter>> {
match look_up_type_id(*self) {
TypeInfo::Enum {
type_parameters, ..
} => (!type_parameters.is_empty()).then_some(type_parameters),
TypeInfo::Struct {
type_parameters, ..
} => (!type_parameters.is_empty()).then_some(type_parameters),
_ => None,
}
}
}
101 changes: 84 additions & 17 deletions sway-core/src/type_engine/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,25 +421,64 @@ impl TypeInfo {
}
Byte => "byte".into(),
B256 => "b256".into(),
Struct { fields, .. } => {
let names = fields
.iter()
.map(|field| {
resolve_type(field.type_id, error_msg_span)
.expect("unreachable?")
.to_selector_name(error_msg_span)
})
.collect::<Vec<CompileResult<String>>>();
let mut buf = vec![];
for name in names {
match name.value {
Some(value) => buf.push(value),
None => return name,
Struct {
fields,
type_parameters,
..
} => {
let field_names = {
let names = fields
.iter()
.map(|ty| {
let ty = match resolve_type(ty.type_id, error_msg_span) {
Err(e) => return err(vec![], vec![e.into()]),
Ok(ty) => ty,
};
ty.to_selector_name(error_msg_span)
})
.collect::<Vec<CompileResult<String>>>();
let mut buf = vec![];
for name in names {
match name.value {
Some(value) => buf.push(value),
None => return name,
}
}
buf
};

let type_arguments = {
let type_arguments = type_parameters
.iter()
.map(|ty| {
let ty = match resolve_type(ty.type_id, error_msg_span) {
Err(e) => return err(vec![], vec![e.into()]),
Ok(ty) => ty,
};
ty.to_selector_name(error_msg_span)
})
.collect::<Vec<CompileResult<String>>>();
let mut buf = vec![];
for arg in type_arguments {
match arg.value {
Some(value) => buf.push(value),
None => return arg,
}
}
buf
};

if type_arguments.is_empty() {
format!("s({})", field_names.join(","))
} else {
format!("s<{}>({})", type_arguments.join(","), field_names.join(","))
}
format!("s({})", buf.join(","))
}
Enum { variant_types, .. } => {
Enum {
variant_types,
type_parameters,
..
} => {
let variant_names = {
let names = variant_types
.iter()
Expand All @@ -461,7 +500,35 @@ impl TypeInfo {
buf
};

format!("e({})", variant_names.join(","))
let type_arguments = {
let type_arguments = type_parameters
.iter()
.map(|ty| {
let ty = match resolve_type(ty.type_id, error_msg_span) {
Err(e) => return err(vec![], vec![e.into()]),
Ok(ty) => ty,
};
ty.to_selector_name(error_msg_span)
})
.collect::<Vec<CompileResult<String>>>();
let mut buf = vec![];
for arg in type_arguments {
match arg.value {
Some(value) => buf.push(value),
None => return arg,
}
}
buf
};
if type_arguments.is_empty() {
format!("e({})", variant_names.join(","))
} else {
format!(
"e<{}>({})",
type_arguments.join(","),
variant_names.join(",")
)
}
}
Array(type_id, size) => {
let name = look_up_type_id(*type_id).to_selector_name(error_msg_span);
Expand Down
23 changes: 22 additions & 1 deletion sway-core/src/type_engine/type_parameter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::{error::*, semantic_analysis::*, type_engine::*};
use crate::{
error::*,
semantic_analysis::*,
type_engine::*,
types::{JsonAbiString, ToJsonAbi},
};

use sway_types::{ident::Ident, span::Span, Spanned};

Expand Down Expand Up @@ -66,6 +71,22 @@ impl fmt::Display for TypeParameter {
}
}

impl ToJsonAbi for TypeParameter {
type Output = Property;

fn generate_json_abi(&self) -> Self::Output {
Property {
name: self.name_ident.to_string(),
type_field: self.type_id.json_abi_str(),
components: self.type_id.generate_json_abi(),
type_arguments: self
.type_id
.get_type_parameters()
.map(|v| v.iter().map(|param| param.generate_json_abi()).collect()),
}
}
}

impl TypeParameter {
pub(crate) fn type_check(
ctx: TypeCheckContext,
Expand Down
Loading