diff --git a/sway-ast/src/intrinsics.rs b/sway-ast/src/intrinsics.rs index 30dea17436d..d14c4e1b85a 100644 --- a/sway-ast/src/intrinsics.rs +++ b/sway-ast/src/intrinsics.rs @@ -1,6 +1,7 @@ +use serde::{Deserialize, Serialize}; use std::fmt; -#[derive(Eq, PartialEq, Debug, Clone, Hash)] +#[derive(Eq, PartialEq, Debug, Clone, Hash, Serialize, Deserialize)] pub enum Intrinsic { IsReferenceType, SizeOfType, diff --git a/sway-ast/src/literal.rs b/sway-ast/src/literal.rs index 1cede566e18..0b175235506 100644 --- a/sway-ast/src/literal.rs +++ b/sway-ast/src/literal.rs @@ -1,25 +1,25 @@ use crate::priv_prelude::*; -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)] pub struct LitString { pub span: Span, pub parsed: String, } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)] pub struct LitChar { pub span: Span, pub parsed: char, } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)] pub struct LitInt { pub span: Span, pub parsed: BigUint, pub ty_opt: Option<(LitIntType, Span)>, } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)] pub enum LitIntType { U8, U16, @@ -32,13 +32,13 @@ pub enum LitIntType { I64, } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)] pub struct LitBool { pub span: Span, pub kind: LitBoolType, } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)] pub enum LitBoolType { True, False, @@ -53,7 +53,7 @@ impl From for bool { } } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)] pub enum Literal { String(LitString), Char(LitChar), diff --git a/sway-ast/src/priv_prelude.rs b/sway-ast/src/priv_prelude.rs index b799d69f37d..9da93d80994 100644 --- a/sway-ast/src/priv_prelude.rs +++ b/sway-ast/src/priv_prelude.rs @@ -35,7 +35,7 @@ pub use { }, extension_trait::extension_trait, num_bigint::BigUint, - serde::Serialize, + serde::{Deserialize, Serialize}, sway_types::{ ast::{Delimiter, PunctKind}, Ident, Span, Spanned, diff --git a/sway-core/Cargo.toml b/sway-core/Cargo.toml index 5e2e5d54536..71ca263d962 100644 --- a/sway-core/Cargo.toml +++ b/sway-core/Cargo.toml @@ -23,7 +23,7 @@ graph-cycles.workspace = true hashbrown.workspace = true hex = { workspace = true, optional = true } im.workspace = true -indexmap.workspace = true +indexmap = { workspace = true, features = ["serde"] } itertools.workspace = true lazy_static.workspace = true object = { workspace = true, features = ["write"] } diff --git a/sway-core/src/decl_engine/id.rs b/sway-core/src/decl_engine/id.rs index 8878b9aaa79..597b716f1c1 100644 --- a/sway-core/src/decl_engine/id.rs +++ b/sway-core/src/decl_engine/id.rs @@ -1,20 +1,20 @@ -use std::collections::hash_map::DefaultHasher; -use std::hash::Hasher; -use std::marker::PhantomData; -use std::{fmt, hash::Hash}; - -use sway_types::{Named, Spanned}; - -use crate::language::ty::{TyDeclParsedType, TyTraitType}; use crate::{ decl_engine::*, engine_threading::*, language::ty::{ - TyEnumDecl, TyFunctionDecl, TyImplSelfOrTrait, TyStructDecl, TyTraitDecl, TyTraitFn, - TyTypeAliasDecl, + TyDeclParsedType, TyEnumDecl, TyFunctionDecl, TyImplSelfOrTrait, TyStructDecl, TyTraitDecl, + TyTraitFn, TyTraitType, TyTypeAliasDecl, }, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::{ + collections::hash_map::DefaultHasher, + fmt, + hash::{Hash, Hasher}, + marker::PhantomData, +}; +use sway_types::{Named, Spanned}; pub type DeclIdIndexType = usize; @@ -27,7 +27,7 @@ impl fmt::Debug for DeclId { } } -#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)] +#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize)] pub struct DeclUniqueId(pub(crate) u64); impl DeclId { @@ -258,3 +258,22 @@ where } } } + +impl Serialize for DeclId { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + self.0.serialize(serializer) + } +} + +impl<'de, T> Deserialize<'de> for DeclId { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let id = DeclIdIndexType::deserialize(deserializer)?; + Ok(DeclId::new(id)) + } +} diff --git a/sway-core/src/decl_engine/interface_decl_id.rs b/sway-core/src/decl_engine/interface_decl_id.rs index 2306622a94a..cba36cfd429 100644 --- a/sway-core/src/decl_engine/interface_decl_id.rs +++ b/sway-core/src/decl_engine/interface_decl_id.rs @@ -1,3 +1,4 @@ +use super::{parsed_engine::ParsedDeclEngineGet, parsed_id::ParsedDeclId}; use crate::{ decl_engine::*, engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext}, @@ -6,8 +7,7 @@ use crate::{ ty, }, }; - -use super::{parsed_engine::ParsedDeclEngineGet, parsed_id::ParsedDeclId}; +use serde::{Deserialize, Serialize}; #[derive(Debug, Eq, PartialEq, Hash, Clone)] pub enum ParsedInterfaceDeclId { @@ -43,7 +43,7 @@ impl From> for ParsedInterfaceDeclId { } } -#[derive(Debug, Eq, PartialEq, Hash, Clone)] +#[derive(Debug, Eq, PartialEq, Hash, Clone, Serialize, Deserialize)] pub enum InterfaceDeclId { Abi(DeclId), Trait(DeclId), diff --git a/sway-core/src/decl_engine/parsed_id.rs b/sway-core/src/decl_engine/parsed_id.rs index 3a97459aa75..1a513a6d5be 100644 --- a/sway-core/src/decl_engine/parsed_id.rs +++ b/sway-core/src/decl_engine/parsed_id.rs @@ -1,16 +1,20 @@ -use std::hash::{DefaultHasher, Hasher}; -use std::marker::PhantomData; -use std::{fmt, hash::Hash}; - -use sway_types::{Named, Spanned}; - -use crate::engine_threading::{ - EqWithEngines, HashWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext, +use super::{ + parsed_engine::{ParsedDeclEngine, ParsedDeclEngineGet, ParsedDeclEngineIndex}, + DeclUniqueId, }; -use crate::Engines; - -use super::parsed_engine::{ParsedDeclEngine, ParsedDeclEngineGet, ParsedDeclEngineIndex}; -use super::DeclUniqueId; +use crate::{ + engine_threading::{ + EqWithEngines, HashWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext, + }, + Engines, +}; +use serde::{Deserialize, Serialize}; +use std::{ + hash::{DefaultHasher, Hasher}, + marker::PhantomData, + {fmt, hash::Hash}, +}; +use sway_types::{Named, Spanned}; pub type ParsedDeclIdIndexType = usize; @@ -91,6 +95,25 @@ impl Ord for ParsedDeclId { } } +impl Serialize for ParsedDeclId { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + self.0.serialize(serializer) + } +} + +impl<'de, T> Deserialize<'de> for ParsedDeclId { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let id = usize::deserialize(deserializer)?; + Ok(ParsedDeclId::new(id)) + } +} + impl ParsedDeclId { pub(crate) fn new(id: usize) -> Self { ParsedDeclId(id, PhantomData) diff --git a/sway-core/src/decl_engine/ref.rs b/sway-core/src/decl_engine/ref.rs index 663fd563a60..4fb8f1bb87f 100644 --- a/sway-core/src/decl_engine/ref.rs +++ b/sway-core/src/decl_engine/ref.rs @@ -20,11 +20,6 @@ //! `fn my_function() { .. }`, and to use [DeclRef] for cases like function //! application `my_function()`. -use std::hash::{Hash, Hasher}; - -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::{Ident, Named, Span, Spanned}; - use crate::{ decl_engine::*, engine_threading::*, @@ -35,6 +30,10 @@ use crate::{ semantic_analysis::TypeCheckContext, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::hash::{Hash, Hasher}; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::{Ident, Named, Span, Spanned}; pub type DeclRefFunction = DeclRef>; pub type DeclRefTrait = DeclRef>; @@ -53,7 +52,7 @@ pub type DeclRefMixedInterface = DeclRef; /// Represents the use of / syntactic reference to a declaration. A /// smart-wrapper around a [DeclId], containing additional information about a /// declaration. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct DeclRef { /// The name of the declaration. // NOTE: In the case of storage, the name is "storage". diff --git a/sway-core/src/language/asm.rs b/sway-core/src/language/asm.rs index c8fcbb429a5..409e343eef6 100644 --- a/sway-core/src/language/asm.rs +++ b/sway-core/src/language/asm.rs @@ -1,8 +1,8 @@ +use serde::{Deserialize, Serialize}; use std::hash::{Hash, Hasher}; - use sway_types::{BaseIdent, Ident, Span}; -#[derive(Debug, Clone, Eq)] +#[derive(Debug, Clone, Eq, Serialize, Deserialize)] pub struct AsmOp { pub(crate) op_name: Ident, pub(crate) op_args: Vec, @@ -43,7 +43,7 @@ impl PartialEq for AsmOp { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct AsmRegister { pub(crate) name: String, } diff --git a/sway-core/src/language/call_path.rs b/sway-core/src/language/call_path.rs index 8036d3ecab8..4ef90706af1 100644 --- a/sway-core/src/language/call_path.rs +++ b/sway-core/src/language/call_path.rs @@ -1,27 +1,25 @@ -use std::{ - cmp::Ordering, - fmt, - hash::{Hash, Hasher}, - sync::Arc, -}; - use crate::{ engine_threading::{ DebugWithEngines, DisplayWithEngines, EqWithEngines, HashWithEngines, OrdWithEngines, OrdWithEnginesContext, PartialEqWithEngines, PartialEqWithEnginesContext, }, + parsed::QualifiedPathType, Engines, Ident, Namespace, }; - +use serde::{Deserialize, Serialize}; +use std::{ + cmp::Ordering, + fmt, + hash::{Hash, Hasher}, + sync::Arc, +}; use sway_error::{ error::CompileError, handler::{ErrorEmitted, Handler}, }; use sway_types::{span::Span, Spanned}; -use super::parsed::QualifiedPathType; - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct CallPathTree { pub qualified_call_path: QualifiedCallPath, pub children: Vec, @@ -75,7 +73,7 @@ impl OrdWithEngines for CallPathTree { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct QualifiedCallPath { pub call_path: CallPath, @@ -179,7 +177,7 @@ impl DebugWithEngines for QualifiedCallPath { /// In the expression `a::b::c()`, `a` and `b` are the prefixes and `c` is the suffix. /// `c` can be any type `T`, but in practice `c` is either an `Ident` or a `TypeInfo`. -#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)] +#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)] pub struct CallPath { pub prefixes: Vec, pub suffix: T, diff --git a/sway-core/src/language/lazy_op.rs b/sway-core/src/language/lazy_op.rs index 73e3daae256..abcb6149d26 100644 --- a/sway-core/src/language/lazy_op.rs +++ b/sway-core/src/language/lazy_op.rs @@ -1,4 +1,6 @@ -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum LazyOp { And, Or, diff --git a/sway-core/src/language/literal.rs b/sway-core/src/language/literal.rs index afedaad12d0..df0bc8735fb 100644 --- a/sway-core/src/language/literal.rs +++ b/sway-core/src/language/literal.rs @@ -1,15 +1,14 @@ use crate::{type_system::*, Engines}; - -use sway_error::error::CompileError; -use sway_types::{integer_bits::IntegerBits, span, u256::U256}; - +use serde::{Deserialize, Serialize}; use std::{ fmt, hash::{Hash, Hasher}, num::{IntErrorKind, ParseIntError}, }; +use sway_error::error::CompileError; +use sway_types::{integer_bits::IntegerBits, span, u256::U256}; -#[derive(Debug, Clone, Eq)] +#[derive(Debug, Clone, Eq, Serialize, Deserialize)] pub enum Literal { U8(u8), U16(u16), diff --git a/sway-core/src/language/parsed/declaration/trait.rs b/sway-core/src/language/parsed/declaration/trait.rs index 12e76e554c6..89bff7b4e94 100644 --- a/sway-core/src/language/parsed/declaration/trait.rs +++ b/sway-core/src/language/parsed/declaration/trait.rs @@ -1,7 +1,4 @@ -use std::hash::{Hash, Hasher}; - use super::{ConstantDeclaration, FunctionDeclaration, FunctionParameter}; - use crate::{ decl_engine::{parsed_id::ParsedDeclId, DeclRefTrait}, engine_threading::*, @@ -9,16 +6,18 @@ use crate::{ transform, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::hash::{Hash, Hasher}; use sway_error::handler::ErrorEmitted; use sway_types::{ident::Ident, span::Span, Named, Spanned}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub enum TraitItem { TraitFn(ParsedDeclId), Constant(ParsedDeclId), Type(ParsedDeclId), // to handle parser recovery: Error represents an incomplete trait item - Error(Box<[Span]>, ErrorEmitted), + Error(Box<[Span]>, #[serde(skip)] ErrorEmitted), } impl EqWithEngines for TraitItem {} @@ -75,7 +74,7 @@ impl Spanned for TraitDeclaration { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Supertrait { pub name: CallPath, pub decl_ref: Option, diff --git a/sway-core/src/language/parsed/expression/mod.rs b/sway-core/src/language/parsed/expression/mod.rs index 7e03c3bf43e..d6f6f2df54b 100644 --- a/sway-core/src/language/parsed/expression/mod.rs +++ b/sway-core/src/language/parsed/expression/mod.rs @@ -1,5 +1,3 @@ -use std::{cmp::Ordering, fmt, hash::Hasher}; - use crate::{ decl_engine::parsed_id::ParsedDeclId, engine_threading::{ @@ -10,6 +8,8 @@ use crate::{ type_system::TypeBinding, Engines, TypeArgument, TypeId, }; +use serde::{Deserialize, Serialize}; +use std::{cmp::Ordering, fmt, hash::Hasher}; use sway_error::handler::ErrorEmitted; use sway_types::{ident::Ident, Span, Spanned}; @@ -198,7 +198,7 @@ impl Spanned for AmbiguousSuffix { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct QualifiedPathType { pub ty: TypeArgument, pub as_trait: TypeId, diff --git a/sway-core/src/language/parsed/use_statement.rs b/sway-core/src/language/parsed/use_statement.rs index ceda688d75f..106fa21e04a 100644 --- a/sway-core/src/language/parsed/use_statement.rs +++ b/sway-core/src/language/parsed/use_statement.rs @@ -1,7 +1,8 @@ use crate::{language::Visibility, parsed::Span}; +use serde::{Deserialize, Serialize}; use sway_types::ident::Ident; -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum ImportType { Star, SelfImport(Span), diff --git a/sway-core/src/language/purity.rs b/sway-core/src/language/purity.rs index 4b498b2fcdd..46961a8d0df 100644 --- a/sway-core/src/language/purity.rs +++ b/sway-core/src/language/purity.rs @@ -1,7 +1,9 @@ +use serde::{Deserialize, Serialize}; + /// The purity of a function is related to its access of contract storage. If a function accesses /// or could potentially access contract storage, it is [Purity::Impure]. If a function does not utilize any /// any accesses (reads _or_ writes) of storage, then it is [Purity::Pure]. -#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, Default)] +#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)] pub enum Purity { #[default] Pure, diff --git a/sway-core/src/language/ty/ast_node.rs b/sway-core/src/language/ty/ast_node.rs index 59d9e942004..34dcdf95572 100644 --- a/sway-core/src/language/ty/ast_node.rs +++ b/sway-core/src/language/ty/ast_node.rs @@ -1,11 +1,3 @@ -use std::{ - fmt::{self, Debug}, - hash::{Hash, Hasher}, -}; - -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::{Ident, Span}; - use crate::{ decl_engine::*, engine_threading::*, @@ -18,12 +10,19 @@ use crate::{ type_system::*, types::*, }; +use serde::{Deserialize, Serialize}; +use std::{ + fmt::{self, Debug}, + hash::{Hash, Hasher}, +}; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::{Ident, Span}; pub trait GetDeclIdent { fn get_decl_ident(&self, engines: &Engines) -> Option; } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyAstNode { pub content: TyAstNodeContent, pub span: Span, @@ -353,13 +352,13 @@ impl TyAstNode { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub enum TyAstNodeContent { Declaration(TyDecl), Expression(TyExpression), // a no-op node used for something that just issues a side effect, like an import statement. SideEffect(TySideEffect), - Error(Box<[Span]>, ErrorEmitted), + Error(Box<[Span]>, #[serde(skip)] ErrorEmitted), } impl EqWithEngines for TyAstNodeContent {} diff --git a/sway-core/src/language/ty/code_block.rs b/sway-core/src/language/ty/code_block.rs index 6ae9b0d0dea..77a841ff4bc 100644 --- a/sway-core/src/language/ty/code_block.rs +++ b/sway-core/src/language/ty/code_block.rs @@ -1,14 +1,13 @@ -use std::hash::Hasher; - -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::Span; - use crate::{ decl_engine::*, engine_threading::*, language::ty::*, semantic_analysis::TypeCheckContext, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::hash::Hasher; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::Span; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyCodeBlock { pub contents: Vec, pub(crate) whole_block_span: Span, diff --git a/sway-core/src/language/ty/declaration/abi.rs b/sway-core/src/language/ty/declaration/abi.rs index 7dae1ef3271..4d3371394ed 100644 --- a/sway-core/src/language/ty/declaration/abi.rs +++ b/sway-core/src/language/ty/declaration/abi.rs @@ -1,18 +1,17 @@ +use super::{TyDeclParsedType, TyTraitInterfaceItem, TyTraitItem}; use crate::{ engine_threading::*, language::parsed::{self, AbiDeclaration}, transform, type_system::*, }; +use serde::{Deserialize, Serialize}; use std::hash::{Hash, Hasher}; - use sway_types::{Ident, Named, Span, Spanned}; -use super::{TyDeclParsedType, TyTraitInterfaceItem, TyTraitItem}; - /// A [TyAbiDecl] contains the type-checked version of the parse tree's /// `AbiDeclaration`. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyAbiDecl { /// The name of the abi trait (also known as a "contract trait") pub name: Ident, diff --git a/sway-core/src/language/ty/declaration/configurable.rs b/sway-core/src/language/ty/declaration/configurable.rs index da7349fc91b..3de69ed1425 100644 --- a/sway-core/src/language/ty/declaration/configurable.rs +++ b/sway-core/src/language/ty/declaration/configurable.rs @@ -1,11 +1,3 @@ -use std::{ - fmt, - hash::{Hash, Hasher}, -}; - -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::{Ident, Named, Span, Spanned}; - use crate::{ decl_engine::{DeclId, DeclMapping, DeclRef, ReplaceDecls}, engine_threading::*, @@ -15,8 +7,15 @@ use crate::{ transform, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::{ + fmt, + hash::{Hash, Hasher}, +}; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::{Ident, Named, Span, Spanned}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyConfigurableDecl { pub call_path: CallPath, pub value: Option, diff --git a/sway-core/src/language/ty/declaration/constant.rs b/sway-core/src/language/ty/declaration/constant.rs index c8acda6b5cf..35b675e6ace 100644 --- a/sway-core/src/language/ty/declaration/constant.rs +++ b/sway-core/src/language/ty/declaration/constant.rs @@ -1,11 +1,3 @@ -use std::{ - fmt, - hash::{Hash, Hasher}, -}; - -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::{Ident, Named, Span, Spanned}; - use crate::{ decl_engine::{DeclMapping, ReplaceDecls}, engine_threading::*, @@ -15,8 +7,15 @@ use crate::{ transform, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::{ + fmt, + hash::{Hash, Hasher}, +}; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::{Ident, Named, Span, Spanned}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyConstantDecl { pub call_path: CallPath, pub value: Option, diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 3c5b6481d09..5a9ba831149 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -1,3 +1,11 @@ +use crate::{ + decl_engine::*, + engine_threading::*, + language::{parsed::Declaration, ty::*, Visibility}, + type_system::*, + types::*, +}; +use serde::{Deserialize, Serialize}; use std::{ fmt, hash::{Hash, Hasher}, @@ -9,15 +17,7 @@ use sway_error::{ }; use sway_types::{Ident, Named, Span, Spanned}; -use crate::{ - decl_engine::*, - engine_threading::*, - language::{parsed::Declaration, ty::*, Visibility}, - type_system::*, - types::*, -}; - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub enum TyDecl { VariableDecl(Box), ConstantDecl(ConstantDecl), @@ -33,7 +33,7 @@ pub enum TyDecl { // If type parameters are defined for a function, they are put in the namespace just for // the body of that function. GenericTypeForFunctionScope(GenericTypeForFunctionScope), - ErrorRecovery(Span, ErrorEmitted), + ErrorRecovery(Span, #[serde(skip)] ErrorEmitted), StorageDecl(StorageDecl), TypeAliasDecl(TypeAliasDecl), } @@ -46,70 +46,70 @@ pub trait TyDeclParsedType { type ParsedType; } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct ConstantDecl { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct ConfigurableDecl { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TraitTypeDecl { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct FunctionDecl { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TraitDecl { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct StructDecl { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct EnumDecl { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct EnumVariantDecl { pub enum_ref: DeclRefEnum, pub variant_name: Ident, pub variant_decl_span: Span, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct ImplSelfOrTrait { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct AbiDecl { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct GenericTypeForFunctionScope { pub name: Ident, pub type_id: TypeId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct StorageDecl { pub decl_id: DeclId, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TypeAliasDecl { pub decl_id: DeclId, } diff --git a/sway-core/src/language/ty/declaration/enum.rs b/sway-core/src/language/ty/declaration/enum.rs index 2ce5e3eae46..126fb4549a5 100644 --- a/sway-core/src/language/ty/declaration/enum.rs +++ b/sway-core/src/language/ty/declaration/enum.rs @@ -1,26 +1,23 @@ +use crate::{ + engine_threading::*, + has_changes, + language::{parsed::EnumDeclaration, ty::TyDeclParsedType, CallPath, Visibility}, + transform, + type_system::*, +}; +use monomorphization::MonomorphizeHelper; +use serde::{Deserialize, Serialize}; use std::{ cmp::Ordering, hash::{Hash, Hasher}, }; - -use monomorphization::MonomorphizeHelper; use sway_error::{ error::CompileError, handler::{ErrorEmitted, Handler}, }; use sway_types::{Ident, Named, Span, Spanned}; -use crate::{ - engine_threading::*, - has_changes, - language::{parsed::EnumDeclaration, CallPath, Visibility}, - transform, - type_system::*, -}; - -use super::TyDeclParsedType; - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyEnumDecl { pub call_path: CallPath, pub type_parameters: Vec, @@ -133,7 +130,7 @@ impl Spanned for TyEnumVariant { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TyEnumVariant { pub name: Ident, pub type_argument: TypeArgument, diff --git a/sway-core/src/language/ty/declaration/function.rs b/sway-core/src/language/ty/declaration/function.rs index 8bb26f1f598..6849ccaa7db 100644 --- a/sway-core/src/language/ty/declaration/function.rs +++ b/sway-core/src/language/ty/declaration/function.rs @@ -1,37 +1,31 @@ -use std::{ - fmt, - hash::{Hash, Hasher}, -}; - -use monomorphization::MonomorphizeHelper; -use sha2::{Digest, Sha256}; -use sway_error::handler::{ErrorEmitted, Handler}; - use crate::{ + decl_engine::*, + engine_threading::*, has_changes, + language::{parsed, ty::*, Inline, Purity, Visibility}, language::{ parsed::{FunctionDeclaration, FunctionDeclarationKind}, CallPath, }, - transform::AttributeKind, -}; - -use crate::{ - decl_engine::*, - engine_threading::*, - language::{parsed, ty::*, Inline, Purity, Visibility}, semantic_analysis::TypeCheckContext, - transform, + transform::{self, AttributeKind}, type_system::*, types::*, }; - +use monomorphization::MonomorphizeHelper; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::{ + fmt, + hash::{Hash, Hasher}, +}; +use sway_error::handler::{ErrorEmitted, Handler}; use sway_types::{ constants::{INLINE_ALWAYS_NAME, INLINE_NEVER_NAME}, Ident, Named, Span, Spanned, }; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub enum TyFunctionDeclKind { Default, Entry, @@ -39,7 +33,7 @@ pub enum TyFunctionDeclKind { Test, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyFunctionDecl { pub name: Ident, pub body: TyCodeBlock, @@ -489,7 +483,7 @@ impl TyFunctionDecl { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TyFunctionParameter { pub name: Ident, pub is_reference: bool, diff --git a/sway-core/src/language/ty/declaration/impl_trait.rs b/sway-core/src/language/ty/declaration/impl_trait.rs index 0c2d25aa5fd..6125eeb9569 100644 --- a/sway-core/src/language/ty/declaration/impl_trait.rs +++ b/sway-core/src/language/ty/declaration/impl_trait.rs @@ -1,7 +1,4 @@ -use std::hash::{Hash, Hasher}; - -use sway_types::{Ident, Named, Span, Spanned}; - +use super::{TyDeclParsedType, TyTraitItem}; use crate::{ decl_engine::DeclRefMixedInterface, engine_threading::*, @@ -9,13 +6,14 @@ use crate::{ language::{parsed::ImplSelfOrTrait, CallPath}, type_system::*, }; - -use super::{TyDeclParsedType, TyTraitItem}; +use serde::{Deserialize, Serialize}; +use std::hash::{Hash, Hasher}; +use sway_types::{Ident, Named, Span, Spanned}; pub type TyImplItem = TyTraitItem; // impl Trait for Type -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyImplSelfOrTrait { pub impl_type_parameters: Vec, pub trait_name: CallPath, diff --git a/sway-core/src/language/ty/declaration/storage.rs b/sway-core/src/language/ty/declaration/storage.rs index abc5a67b20c..1036159a826 100644 --- a/sway-core/src/language/ty/declaration/storage.rs +++ b/sway-core/src/language/ty/declaration/storage.rs @@ -1,11 +1,3 @@ -use std::hash::{Hash, Hasher}; - -use sway_error::{ - error::{CompileError, StructFieldUsageContext}, - handler::{ErrorEmitted, Handler}, -}; -use sway_types::{Ident, Named, Span, Spanned}; - use crate::{ engine_threading::*, ir_generation::storage::get_storage_key_string, @@ -15,8 +7,15 @@ use crate::{ type_system::*, Namespace, }; +use serde::{Deserialize, Serialize}; +use std::hash::{Hash, Hasher}; +use sway_error::{ + error::{CompileError, StructFieldUsageContext}, + handler::{ErrorEmitted, Handler}, +}; +use sway_types::{Ident, Named, Span, Spanned}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyStorageDecl { pub fields: Vec, pub span: Span, @@ -243,7 +242,7 @@ impl Spanned for TyStorageField { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyStorageField { pub name: Ident, pub namespace_names: Vec, diff --git a/sway-core/src/language/ty/declaration/struct.rs b/sway-core/src/language/ty/declaration/struct.rs index c4dc7e3e219..e64281b1ea9 100644 --- a/sway-core/src/language/ty/declaration/struct.rs +++ b/sway-core/src/language/ty/declaration/struct.rs @@ -1,24 +1,21 @@ -use std::{ - cmp::Ordering, - hash::{Hash, Hasher}, -}; - -use monomorphization::MonomorphizeHelper; -use sway_types::{Ident, Named, Span, Spanned}; - use crate::{ engine_threading::*, error::module_can_be_changed, has_changes, - language::{parsed::StructDeclaration, CallPath, Visibility}, + language::{parsed::StructDeclaration, ty::TyDeclParsedType, CallPath, Visibility}, transform, type_system::*, Namespace, }; +use monomorphization::MonomorphizeHelper; +use serde::{Deserialize, Serialize}; +use std::{ + cmp::Ordering, + hash::{Hash, Hasher}, +}; +use sway_types::{Ident, Named, Span, Spanned}; -use super::TyDeclParsedType; - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyStructDecl { pub call_path: CallPath, pub fields: Vec, @@ -182,7 +179,7 @@ impl From for (bool, bool) { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TyStructField { pub visibility: Visibility, pub name: Ident, diff --git a/sway-core/src/language/ty/declaration/trait.rs b/sway-core/src/language/ty/declaration/trait.rs index f0c7ee24ff3..14970814fe3 100644 --- a/sway-core/src/language/ty/declaration/trait.rs +++ b/sway-core/src/language/ty/declaration/trait.rs @@ -1,12 +1,3 @@ -use std::{ - fmt, - hash::{Hash, Hasher}, -}; - -use monomorphization::MonomorphizeHelper; -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::{Ident, Named, Span, Spanned}; - use crate::{ decl_engine::{ DeclEngineReplace, DeclRefConstant, DeclRefFunction, DeclRefTraitFn, DeclRefTraitType, @@ -16,6 +7,7 @@ use crate::{ has_changes, language::{ parsed::{self, TraitDeclaration}, + ty::{TyDecl, TyDeclParsedType}, CallPath, Visibility, }, semantic_analysis::{ @@ -25,10 +17,16 @@ use crate::{ transform, type_system::*, }; +use monomorphization::MonomorphizeHelper; +use serde::{Deserialize, Serialize}; +use std::{ + fmt, + hash::{Hash, Hasher}, +}; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::{Ident, Named, Span, Spanned}; -use super::{TyDecl, TyDeclParsedType}; - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyTraitDecl { pub name: Ident, pub type_parameters: Vec, @@ -46,7 +44,7 @@ impl TyDeclParsedType for TyTraitDecl { type ParsedType = TraitDeclaration; } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub enum TyTraitInterfaceItem { TraitFn(DeclRefTraitFn), Constant(DeclRefConstant), @@ -82,7 +80,7 @@ impl DebugWithEngines for TyTraitInterfaceItem { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub enum TyTraitItem { Fn(DeclRefFunction), Constant(DeclRefConstant), diff --git a/sway-core/src/language/ty/declaration/trait_type.rs b/sway-core/src/language/ty/declaration/trait_type.rs index 24925afa457..9c9cee3e5eb 100644 --- a/sway-core/src/language/ty/declaration/trait_type.rs +++ b/sway-core/src/language/ty/declaration/trait_type.rs @@ -1,18 +1,15 @@ +use crate::{ + engine_threading::*, has_changes, language::parsed::TraitTypeDeclaration, + language::ty::TyDeclParsedType, transform, type_system::*, +}; +use serde::{Deserialize, Serialize}; use std::{ fmt, hash::{Hash, Hasher}, }; - use sway_types::{Ident, Named, Span, Spanned}; -use crate::{ - engine_threading::*, has_changes, language::parsed::TraitTypeDeclaration, transform, - type_system::*, -}; - -use super::TyDeclParsedType; - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyTraitType { pub name: Ident, pub attributes: transform::AttributesMap, diff --git a/sway-core/src/language/ty/declaration/type_alias.rs b/sway-core/src/language/ty/declaration/type_alias.rs index cb33c0d27bd..9c41fae8f50 100644 --- a/sway-core/src/language/ty/declaration/type_alias.rs +++ b/sway-core/src/language/ty/declaration/type_alias.rs @@ -1,17 +1,14 @@ -use std::hash::{Hash, Hasher}; - -use sway_types::{Ident, Named, Span, Spanned}; - use crate::{ engine_threading::*, - language::{parsed::TypeAliasDeclaration, CallPath, Visibility}, + language::{parsed::TypeAliasDeclaration, ty::TyDeclParsedType, CallPath, Visibility}, transform, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::hash::{Hash, Hasher}; +use sway_types::{Ident, Named, Span, Spanned}; -use super::TyDeclParsedType; - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyTypeAliasDecl { pub name: Ident, pub call_path: CallPath, diff --git a/sway-core/src/language/ty/declaration/variable.rs b/sway-core/src/language/ty/declaration/variable.rs index d7eb80ad7c6..5337651b8e5 100644 --- a/sway-core/src/language/ty/declaration/variable.rs +++ b/sway-core/src/language/ty/declaration/variable.rs @@ -1,14 +1,13 @@ -use std::hash::{Hash, Hasher}; - -use sway_types::{Ident, Named, Spanned}; - use crate::{ engine_threading::*, language::{parsed::VariableDeclaration, ty::*}, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::hash::{Hash, Hasher}; +use sway_types::{Ident, Named, Spanned}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyVariableDecl { pub name: Ident, pub body: TyExpression, diff --git a/sway-core/src/language/ty/expression/asm.rs b/sway-core/src/language/ty/expression/asm.rs index 04944125258..6e4ad95af3c 100644 --- a/sway-core/src/language/ty/expression/asm.rs +++ b/sway-core/src/language/ty/expression/asm.rs @@ -1,10 +1,9 @@ +use crate::{engine_threading::*, language::ty::*, type_system::*}; +use serde::{Deserialize, Serialize}; use std::hash::{Hash, Hasher}; - use sway_types::Ident; -use crate::{engine_threading::*, language::ty::*, type_system::*}; - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyAsmRegisterDeclaration { pub initializer: Option, pub(crate) name: Ident, diff --git a/sway-core/src/language/ty/expression/contract.rs b/sway-core/src/language/ty/expression/contract.rs index 2bdec40421e..926aba9923c 100644 --- a/sway-core/src/language/ty/expression/contract.rs +++ b/sway-core/src/language/ty/expression/contract.rs @@ -1,6 +1,7 @@ use crate::language::ty::*; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct ContractCallParams { // This is none in encoding V1 pub(crate) func_selector: Option<[u8; 4]>, diff --git a/sway-core/src/language/ty/expression/expression.rs b/sway-core/src/language/ty/expression/expression.rs index a0c70559b1d..4f369d3c269 100644 --- a/sway-core/src/language/ty/expression/expression.rs +++ b/sway-core/src/language/ty/expression/expression.rs @@ -1,13 +1,3 @@ -use std::{fmt, hash::Hasher}; - -use sway_error::{ - error::CompileError, - handler::{ErrorEmitted, Handler}, - type_error::TypeError, - warning::{CompileWarning, Warning}, -}; -use sway_types::{Span, Spanned}; - use crate::{ decl_engine::*, engine_threading::*, @@ -21,8 +11,17 @@ use crate::{ type_system::*, types::*, }; +use serde::{Deserialize, Serialize}; +use std::{fmt, hash::Hasher}; +use sway_error::{ + error::CompileError, + handler::{ErrorEmitted, Handler}, + type_error::TypeError, + warning::{CompileWarning, Warning}, +}; +use sway_types::{Span, Spanned}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyExpression { pub expression: TyExpressionVariant, pub return_type: TypeId, diff --git a/sway-core/src/language/ty/expression/expression_variant.rs b/sway-core/src/language/ty/expression/expression_variant.rs index 9908bb09f5b..ea221466273 100644 --- a/sway-core/src/language/ty/expression/expression_variant.rs +++ b/sway-core/src/language/ty/expression/expression_variant.rs @@ -1,13 +1,3 @@ -use std::{ - collections::VecDeque, - fmt::{self, Write}, - hash::{Hash, Hasher}, -}; - -use indexmap::IndexMap; -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::{Ident, Named, Span, Spanned}; - use crate::{ decl_engine::*, engine_threading::*, @@ -20,8 +10,17 @@ use crate::{ }, type_system::*, }; +use indexmap::IndexMap; +use serde::{Deserialize, Serialize}; +use std::{ + collections::VecDeque, + fmt::{self, Write}, + hash::{Hash, Hasher}, +}; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::{Ident, Named, Span, Spanned}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub enum TyExpressionVariant { Literal(Literal), FunctionApplication { diff --git a/sway-core/src/language/ty/expression/intrinsic_function.rs b/sway-core/src/language/ty/expression/intrinsic_function.rs index 4fb4e72c3bc..d7646ecf64c 100644 --- a/sway-core/src/language/ty/expression/intrinsic_function.rs +++ b/sway-core/src/language/ty/expression/intrinsic_function.rs @@ -1,18 +1,18 @@ -use std::{ - fmt, - hash::{Hash, Hasher}, -}; - use crate::{ abi_generation::abi_str::AbiStrContext, engine_threading::*, has_changes, language::ty::*, type_system::*, types::*, }; use itertools::Itertools; +use serde::{Deserialize, Serialize}; +use std::{ + fmt, + hash::{Hash, Hasher}, +}; use sway_ast::Intrinsic; use sway_error::handler::{ErrorEmitted, Handler}; use sway_types::Span; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TyIntrinsicFunctionKind { pub kind: Intrinsic, pub arguments: Vec, diff --git a/sway-core/src/language/ty/expression/reassignment.rs b/sway-core/src/language/ty/expression/reassignment.rs index 54a8e740c5c..698af692dda 100644 --- a/sway-core/src/language/ty/expression/reassignment.rs +++ b/sway-core/src/language/ty/expression/reassignment.rs @@ -1,11 +1,3 @@ -use std::{ - borrow::Cow, - hash::{Hash, Hasher}, -}; - -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::{Ident, Span, Spanned}; - use crate::{ decl_engine::*, engine_threading::*, @@ -17,14 +9,21 @@ use crate::{ }, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::{ + borrow::Cow, + hash::{Hash, Hasher}, +}; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::{Ident, Span, Spanned}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyReassignment { pub lhs: TyReassignmentTarget, pub rhs: TyExpression, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub enum TyReassignmentTarget { /// An [TyExpression] representing a single variable or a path /// to a part of an aggregate. @@ -258,7 +257,7 @@ impl UpdateConstantExpression for TyReassignment { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub enum ProjectionKind { StructField { name: Ident, diff --git a/sway-core/src/language/ty/expression/scrutinee.rs b/sway-core/src/language/ty/expression/scrutinee.rs index 68c51af26eb..78fde994732 100644 --- a/sway-core/src/language/ty/expression/scrutinee.rs +++ b/sway-core/src/language/ty/expression/scrutinee.rs @@ -1,19 +1,19 @@ -use sway_types::{Ident, Span}; - use crate::{ decl_engine::{DeclRefEnum, DeclRefStruct}, language::{ty::*, *}, type_system::*, }; +use serde::{Deserialize, Serialize}; +use sway_types::{Ident, Span}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TyScrutinee { pub variant: TyScrutineeVariant, pub type_id: TypeId, pub span: Span, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub enum TyScrutineeVariant { Or(Vec), CatchAll, @@ -36,7 +36,7 @@ pub enum TyScrutineeVariant { Tuple(Vec), } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TyStructScrutineeField { pub field: Ident, pub scrutinee: Option, diff --git a/sway-core/src/language/ty/expression/storage.rs b/sway-core/src/language/ty/expression/storage.rs index 127d64fd20b..adaa6f28730 100644 --- a/sway-core/src/language/ty/expression/storage.rs +++ b/sway-core/src/language/ty/expression/storage.rs @@ -1,13 +1,11 @@ +use super::TyExpression; +use crate::{engine_threading::*, type_system::TypeId}; +use serde::{Deserialize, Serialize}; use std::hash::{Hash, Hasher}; - use sway_types::{Ident, Span, Spanned}; -use crate::{engine_threading::*, type_system::TypeId}; - -use super::TyExpression; - /// Describes the full storage access including all the subfields -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyStorageAccess { pub fields: Vec, pub storage_field_names: Vec, @@ -64,7 +62,7 @@ impl TyStorageAccess { } /// Describes a single subfield access in the sequence when accessing a subfield within storage. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyStorageAccessDescriptor { pub name: Ident, pub type_id: TypeId, diff --git a/sway-core/src/language/ty/expression/struct_exp_field.rs b/sway-core/src/language/ty/expression/struct_exp_field.rs index 5f879640fb3..7262f38a901 100644 --- a/sway-core/src/language/ty/expression/struct_exp_field.rs +++ b/sway-core/src/language/ty/expression/struct_exp_field.rs @@ -1,8 +1,3 @@ -use std::hash::{Hash, Hasher}; - -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::Ident; - use crate::{ decl_engine::*, engine_threading::*, @@ -10,8 +5,12 @@ use crate::{ semantic_analysis::{TypeCheckContext, TypeCheckFinalization, TypeCheckFinalizationContext}, type_system::*, }; +use serde::{Deserialize, Serialize}; +use std::hash::{Hash, Hasher}; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::Ident; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct TyStructExpressionField { pub name: Ident, pub value: TyExpression, diff --git a/sway-core/src/language/ty/side_effect/include_statement.rs b/sway-core/src/language/ty/side_effect/include_statement.rs index 5d35f02b722..666e5b63a58 100644 --- a/sway-core/src/language/ty/side_effect/include_statement.rs +++ b/sway-core/src/language/ty/side_effect/include_statement.rs @@ -1,8 +1,8 @@ use crate::language::Visibility; - +use serde::{Deserialize, Serialize}; use sway_types::{ident::Ident, Span, Spanned}; -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct TyIncludeStatement { pub span: Span, pub visibility: Visibility, diff --git a/sway-core/src/language/ty/side_effect/side_effect.rs b/sway-core/src/language/ty/side_effect/side_effect.rs index 41f2a5b945b..9e8e672c5fa 100644 --- a/sway-core/src/language/ty/side_effect/side_effect.rs +++ b/sway-core/src/language/ty/side_effect/side_effect.rs @@ -1,11 +1,12 @@ use super::{TyIncludeStatement, TyUseStatement}; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct TySideEffect { pub side_effect: TySideEffectVariant, } -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum TySideEffectVariant { IncludeStatement(TyIncludeStatement), UseStatement(TyUseStatement), diff --git a/sway-core/src/language/ty/side_effect/use_statement.rs b/sway-core/src/language/ty/side_effect/use_statement.rs index 5d9c0cf1ca1..476a58907ef 100644 --- a/sway-core/src/language/ty/side_effect/use_statement.rs +++ b/sway-core/src/language/ty/side_effect/use_statement.rs @@ -1,7 +1,8 @@ use crate::language::parsed; +use serde::{Deserialize, Serialize}; use sway_types::{ident::Ident, Span, Spanned}; -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct TyUseStatement { pub call_path: Vec, pub span: Span, diff --git a/sway-core/src/language/ty/variable_mutability.rs b/sway-core/src/language/ty/variable_mutability.rs index ba2532028b6..b1831af1279 100644 --- a/sway-core/src/language/ty/variable_mutability.rs +++ b/sway-core/src/language/ty/variable_mutability.rs @@ -1,6 +1,7 @@ use crate::language::Visibility; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default, Serialize, Deserialize)] pub enum VariableMutability { // mutable Mutable, diff --git a/sway-core/src/language/visibility.rs b/sway-core/src/language/visibility.rs index a3838e06aff..e6b19912e7e 100644 --- a/sway-core/src/language/visibility.rs +++ b/sway-core/src/language/visibility.rs @@ -1,4 +1,6 @@ -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum Visibility { Private, Public, diff --git a/sway-core/src/transform/attribute.rs b/sway-core/src/transform/attribute.rs index d29978205c8..d44538befa2 100644 --- a/sway-core/src/transform/attribute.rs +++ b/sway-core/src/transform/attribute.rs @@ -21,6 +21,8 @@ //! #[foo(bar, bar)] use indexmap::IndexMap; +use serde::{Deserialize, Serialize}; +use std::{hash::Hash, sync::Arc}; use sway_ast::Literal; use sway_types::{ constants::{ @@ -29,9 +31,7 @@ use sway_types::{ Ident, Span, Spanned, }; -use std::{hash::Hash, sync::Arc}; - -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct AttributeArg { pub name: Ident, pub value: Option, @@ -47,7 +47,7 @@ impl Spanned for AttributeArg { /// An attribute has a name (i.e "doc", "storage"), /// a vector of possible arguments and /// a span from its declaration. -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Attribute { pub name: Ident, pub args: Vec, @@ -55,7 +55,7 @@ pub struct Attribute { } /// Valid kinds of attributes supported by the compiler -#[derive(Clone, Debug, Eq, PartialEq, Hash)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] pub enum AttributeKind { Doc, DocComment, @@ -104,7 +104,7 @@ impl AttributeKind { } /// Stores the attributes associated with the type. -#[derive(Default, Clone, Debug, Eq, PartialEq)] +#[derive(Default, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct AttributesMap(Arc>>); impl AttributesMap { diff --git a/sway-core/src/type_system/ast_elements/binding.rs b/sway-core/src/type_system/ast_elements/binding.rs index 3ea45397a51..96d91b24f74 100644 --- a/sway-core/src/type_system/ast_elements/binding.rs +++ b/sway-core/src/type_system/ast_elements/binding.rs @@ -1,7 +1,3 @@ -use sway_ast::Intrinsic; -use sway_error::handler::{ErrorEmitted, Handler}; -use sway_types::{Span, Spanned}; - use crate::{ decl_engine::{ parsed_id::ParsedDeclId, DeclEngineGetParsedDeclId, DeclEngineInsert, DeclId, DeclRef, @@ -18,6 +14,10 @@ use crate::{ type_system::priv_prelude::*, EnforceTypeArguments, Ident, }; +use serde::{Deserialize, Serialize}; +use sway_ast::Intrinsic; +use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::{Span, Spanned}; /// A `TypeBinding` is the result of using turbofish to bind types to /// generic parameters. @@ -78,7 +78,7 @@ use crate::{ /// - `data4` has a type ascription and has type arguments in the `TypeBinding`, /// so, with the type from the value passed to `value`, all three are unified /// together -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TypeBinding { pub inner: T, pub type_arguments: TypeArgs, @@ -103,7 +103,7 @@ pub struct TypeBinding { /// ``` /// So we can have type parameters in the `Prefix` or `Regular` variant but not /// in both. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub enum TypeArgs { /// `Regular` variant indicates the type arguments are located after the suffix. Regular(Vec), diff --git a/sway-core/src/type_system/ast_elements/trait_constraint.rs b/sway-core/src/type_system/ast_elements/trait_constraint.rs index cfe0b41bcb1..f51fe79d691 100644 --- a/sway-core/src/type_system/ast_elements/trait_constraint.rs +++ b/sway-core/src/type_system/ast_elements/trait_constraint.rs @@ -1,15 +1,3 @@ -use std::{ - cmp::Ordering, - fmt, - hash::{Hash, Hasher}, -}; - -use sway_error::{ - error::CompileError, - handler::{ErrorEmitted, Handler}, -}; -use sway_types::Spanned; - use crate::{ engine_threading::*, language::{parsed::Supertrait, ty, CallPath}, @@ -21,8 +9,19 @@ use crate::{ types::{CollectTypesMetadata, CollectTypesMetadataContext, TypeMetadata}, EnforceTypeArguments, }; +use serde::{Deserialize, Serialize}; +use std::{ + cmp::Ordering, + fmt, + hash::{Hash, Hasher}, +}; +use sway_error::{ + error::CompileError, + handler::{ErrorEmitted, Handler}, +}; +use sway_types::Spanned; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TraitConstraint { pub trait_name: CallPath, pub type_arguments: Vec, diff --git a/sway-core/src/type_system/ast_elements/type_argument.rs b/sway-core/src/type_system/ast_elements/type_argument.rs index bdddde93ad4..3ab7e5a0322 100644 --- a/sway-core/src/type_system/ast_elements/type_argument.rs +++ b/sway-core/src/type_system/ast_elements/type_argument.rs @@ -1,4 +1,5 @@ use crate::{engine_threading::*, language::CallPathTree, type_system::priv_prelude::*}; +use serde::{Deserialize, Serialize}; use std::{cmp::Ordering, fmt, hash::Hasher}; use sway_types::{Span, Spanned}; @@ -13,7 +14,7 @@ use sway_types::{Span, Spanned}; /// /// The annotations are ignored when calculating the [TypeArgument]'s hash /// (with engines) and equality (with engines). -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TypeArgument { /// The [TypeId] of the "referenced" [TypeInfo]. pub type_id: TypeId, diff --git a/sway-core/src/type_system/ast_elements/type_parameter.rs b/sway-core/src/type_system/ast_elements/type_parameter.rs index cdd656aaef0..7b6c8b48e5a 100644 --- a/sway-core/src/type_system/ast_elements/type_parameter.rs +++ b/sway-core/src/type_system/ast_elements/type_parameter.rs @@ -7,19 +7,18 @@ use crate::{ semantic_analysis::{GenericShadowingMode, TypeCheckContext}, type_system::priv_prelude::*, }; - -use sway_error::{ - error::CompileError, - handler::{ErrorEmitted, Handler}, -}; -use sway_types::{ident::Ident, span::Span, BaseIdent, Spanned}; - +use serde::{Deserialize, Serialize}; use std::{ cmp::Ordering, collections::BTreeMap, fmt, hash::{Hash, Hasher}, }; +use sway_error::{ + error::CompileError, + handler::{ErrorEmitted, Handler}, +}; +use sway_types::{ident::Ident, span::Span, BaseIdent, Spanned}; /// [TypeParameter] describes a generic type parameter, including its /// monomorphized version. It holds the `name` of the parameter, its @@ -33,7 +32,7 @@ use std::{ /// /// The annotations are ignored when calculating the [TypeParameter]'s hash /// (with engines) and equality (with engines). -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TypeParameter { pub type_id: TypeId, /// Denotes the initial type represented by the [TypeParameter], before diff --git a/sway-core/src/type_system/id.rs b/sway-core/src/type_system/id.rs index 090ed6b370a..5e8f5bba90e 100644 --- a/sway-core/src/type_system/id.rs +++ b/sway-core/src/type_system/id.rs @@ -1,5 +1,6 @@ #![allow(clippy::mutable_key_type)] use indexmap::IndexMap; +use serde::{Deserialize, Serialize}; use sway_error::{ error::CompileError, handler::{ErrorEmitted, Handler}, @@ -34,7 +35,7 @@ pub enum TreatNumericAs { } /// A identifier to uniquely refer to our type terms -#[derive(PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd, Debug)] +#[derive(PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd, Debug, Deserialize, Serialize)] pub struct TypeId(usize); impl DisplayWithEngines for TypeId { diff --git a/sway-core/src/type_system/info.rs b/sway-core/src/type_system/info.rs index dc089c9c25f..e0304d62ed2 100644 --- a/sway-core/src/type_system/info.rs +++ b/sway-core/src/type_system/info.rs @@ -12,19 +12,19 @@ use crate::{ type_system::priv_prelude::*, Ident, }; -use sway_error::{ - error::{CompileError, InvalidImplementingForType}, - handler::{ErrorEmitted, Handler}, -}; -use sway_types::{integer_bits::IntegerBits, span::Span}; - +use serde::{Deserialize, Serialize}; use std::{ cmp::Ordering, fmt, hash::{Hash, Hasher}, }; +use sway_error::{ + error::{CompileError, InvalidImplementingForType}, + handler::{ErrorEmitted, Handler}, +}; +use sway_types::{integer_bits::IntegerBits, span::Span}; -#[derive(Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)] +#[derive(Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] pub enum AbiName { Deferred, Known(CallPath), diff --git a/sway-error/src/handler.rs b/sway-error/src/handler.rs index 24d2222da1b..ddb4d3b24f5 100644 --- a/sway-error/src/handler.rs +++ b/sway-error/src/handler.rs @@ -1,5 +1,4 @@ use crate::{error::CompileError, warning::CompileWarning}; - use core::cell::RefCell; /// A handler with which you can emit diagnostics. @@ -127,7 +126,7 @@ impl Handler { } /// Proof that an error was emitted through a `Handler`. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)] pub struct ErrorEmitted { _priv: (), } diff --git a/sway-parse/src/attribute.rs b/sway-parse/src/attribute.rs index 84a174f3bf2..4b3d545eb8c 100644 --- a/sway-parse/src/attribute.rs +++ b/sway-parse/src/attribute.rs @@ -188,67 +188,121 @@ mod tests { fn main() { () } - "#,), @r###" + "#,), @r#" Annotated( attribute_list: [ AttributeDecl( hash_kind: Outer(HashToken( - span: (82, 108), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 82, + end: 108, + source_id: None, + ), )), attribute: SquareBrackets( inner: Punctuated( value_separator_pairs: [], final_value_opt: Some(Attribute( - name: Ident( - to_string: "doc-comment", - span: (82, 108), + name: BaseIdent( + name_override_opt: Some("doc-comment"), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 82, + end: 108, + source_id: None, + ), + is_raw_ident: false, ), args: Some(Parens( inner: Punctuated( value_separator_pairs: [], final_value_opt: Some(AttributeArg( - name: Ident( - to_string: " This is a doc comment.", - span: (85, 108), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 85, + end: 108, + source_id: None, + ), + is_raw_ident: false, ), value: None, )), ), - span: (85, 108), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 85, + end: 108, + source_id: None, + ), )), )), ), - span: (82, 108), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 82, + end: 108, + source_id: None, + ), ), ), AttributeDecl( hash_kind: Outer(HashToken( - span: (121, 122), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 121, + end: 122, + source_id: None, + ), )), attribute: SquareBrackets( inner: Punctuated( value_separator_pairs: [], final_value_opt: Some(Attribute( - name: Ident( - to_string: "storage", - span: (123, 130), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 123, + end: 130, + source_id: None, + ), + is_raw_ident: false, ), args: Some(Parens( inner: Punctuated( value_separator_pairs: [], final_value_opt: Some(AttributeArg( - name: Ident( - to_string: "read", - span: (131, 135), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 131, + end: 135, + source_id: None, + ), + is_raw_ident: false, ), value: None, )), ), - span: (130, 136), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 130, + end: 136, + source_id: None, + ), )), )), ), - span: (122, 137), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 122, + end: 137, + source_id: None, + ), ), ), ], @@ -256,11 +310,22 @@ mod tests { fn_signature: FnSignature( visibility: None, fn_token: FnToken( - span: (150, 152), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 150, + end: 152, + source_id: None, + ), ), - name: Ident( - to_string: "main", - span: (153, 157), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 153, + end: 157, + source_id: None, + ), + is_raw_ident: false, ), generics: None, arguments: Parens( @@ -268,7 +333,12 @@ mod tests { value_separator_pairs: [], final_value_opt: None, )), - span: (157, 159), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 157, + end: 159, + source_id: None, + ), ), return_type_opt: None, where_clause_opt: None, @@ -278,63 +348,122 @@ mod tests { statements: [], final_expr_opt: Some(Tuple(Parens( inner: Nil, - span: (178, 180), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 178, + end: 180, + source_id: None, + ), ))), - span: (161, 193), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 161, + end: 193, + source_id: None, + ), + ), + span: Span( + src: "\n // I will be ignored.\n //! I will be ignored.\n /// This is a doc comment.\n #[storage(read)]\n fn main() {\n ()\n }\n ", + start: 160, + end: 194, + source_id: None, ), - span: (160, 194), ), ), ) - "###); + "#); } #[test] fn parse_attribute() { assert_ron_snapshot!(parse::(r#" name(arg1, arg2 = "value", arg3) - "#,), @r###" + "#,), @r#" Attribute( - name: Ident( - to_string: "name", - span: (13, 17), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n name(arg1, arg2 = \"value\", arg3)\n ", + start: 13, + end: 17, + source_id: None, + ), + is_raw_ident: false, ), args: Some(Parens( inner: Punctuated( value_separator_pairs: [ (AttributeArg( - name: Ident( - to_string: "arg1", - span: (18, 22), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n name(arg1, arg2 = \"value\", arg3)\n ", + start: 18, + end: 22, + source_id: None, + ), + is_raw_ident: false, ), value: None, ), CommaToken( - span: (22, 23), + span: Span( + src: "\n name(arg1, arg2 = \"value\", arg3)\n ", + start: 22, + end: 23, + source_id: None, + ), )), (AttributeArg( - name: Ident( - to_string: "arg2", - span: (24, 28), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n name(arg1, arg2 = \"value\", arg3)\n ", + start: 24, + end: 28, + source_id: None, + ), + is_raw_ident: false, ), value: Some(String(LitString( - span: (31, 38), + span: Span( + src: "\n name(arg1, arg2 = \"value\", arg3)\n ", + start: 31, + end: 38, + source_id: None, + ), parsed: "value", ))), ), CommaToken( - span: (38, 39), + span: Span( + src: "\n name(arg1, arg2 = \"value\", arg3)\n ", + start: 38, + end: 39, + source_id: None, + ), )), ], final_value_opt: Some(AttributeArg( - name: Ident( - to_string: "arg3", - span: (40, 44), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n name(arg1, arg2 = \"value\", arg3)\n ", + start: 40, + end: 44, + source_id: None, + ), + is_raw_ident: false, ), value: None, )), ), - span: (17, 45), + span: Span( + src: "\n name(arg1, arg2 = \"value\", arg3)\n ", + start: 17, + end: 45, + source_id: None, + ), )), ) - "###); + "#); } } diff --git a/sway-parse/src/module.rs b/sway-parse/src/module.rs index 7936012e96a..05d97d82b42 100644 --- a/sway-parse/src/module.rs +++ b/sway-parse/src/module.rs @@ -87,17 +87,27 @@ mod tests { fn main() { () } - "#,), @r###" + "#,), @r#" Annotated( attribute_list: [], value: Module( kind: Script( script_token: ScriptToken( - span: (13, 19), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 13, + end: 19, + source_id: None, + ), ), ), semicolon_token: SemicolonToken( - span: (19, 20), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 19, + end: 20, + source_id: None, + ), ), items: [ Annotated( @@ -106,11 +116,22 @@ mod tests { fn_signature: FnSignature( visibility: None, fn_token: FnToken( - span: (42, 44), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 42, + end: 44, + source_id: None, + ), ), - name: Ident( - to_string: "main", - span: (45, 49), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 45, + end: 49, + source_id: None, + ), + is_raw_ident: false, ), generics: None, arguments: Parens( @@ -118,7 +139,12 @@ mod tests { value_separator_pairs: [], final_value_opt: None, )), - span: (49, 51), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 49, + end: 51, + source_id: None, + ), ), return_type_opt: None, where_clause_opt: None, @@ -128,17 +154,32 @@ mod tests { statements: [], final_expr_opt: Some(Tuple(Parens( inner: Nil, - span: (70, 72), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 70, + end: 72, + source_id: None, + ), ))), - span: (53, 85), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 53, + end: 85, + source_id: None, + ), + ), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 52, + end: 86, + source_id: None, ), - span: (52, 86), ), )), ), ], ), ) - "###); + "#); } } diff --git a/sway-parse/src/path.rs b/sway-parse/src/path.rs index 657e339747c..4a2a57ac706 100644 --- a/sway-parse/src/path.rs +++ b/sway-parse/src/path.rs @@ -136,37 +136,65 @@ mod tests { fn parse_nested_path() { assert_ron_snapshot!(parse::(r#" std::vec::Vec - "#,), @r###" + "#,), @r#" PathExpr( root_opt: None, prefix: PathExprSegment( - name: Ident( - to_string: "std", - span: (13, 16), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n std::vec::Vec\n ", + start: 13, + end: 16, + source_id: None, + ), + is_raw_ident: false, ), generics_opt: None, ), suffix: [ (DoubleColonToken( - span: (16, 18), + span: Span( + src: "\n std::vec::Vec\n ", + start: 16, + end: 18, + source_id: None, + ), ), PathExprSegment( - name: Ident( - to_string: "vec", - span: (18, 21), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n std::vec::Vec\n ", + start: 18, + end: 21, + source_id: None, + ), + is_raw_ident: false, ), generics_opt: None, )), (DoubleColonToken( - span: (21, 23), + span: Span( + src: "\n std::vec::Vec\n ", + start: 21, + end: 23, + source_id: None, + ), ), PathExprSegment( - name: Ident( - to_string: "Vec", - span: (23, 26), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n std::vec::Vec\n ", + start: 23, + end: 26, + source_id: None, + ), + is_raw_ident: false, ), generics_opt: None, )), ], ) - "###); + "#); } } diff --git a/sway-parse/tests/noop_script.rs b/sway-parse/tests/noop_script.rs index fd3fe83cf0e..709da28de33 100644 --- a/sway-parse/tests/noop_script.rs +++ b/sway-parse/tests/noop_script.rs @@ -12,17 +12,27 @@ fn noop_script_file() { fn main() { () } - "#,), @r###" + "#,), @r#" Some(Annotated( attribute_list: [], value: Module( kind: Script( script_token: ScriptToken( - span: (7, 13), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 7, + end: 13, + source_id: None, + ), ), ), semicolon_token: SemicolonToken( - span: (13, 14), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 13, + end: 14, + source_id: None, + ), ), items: [ Annotated( @@ -31,11 +41,22 @@ fn noop_script_file() { fn_signature: FnSignature( visibility: None, fn_token: FnToken( - span: (28, 30), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 28, + end: 30, + source_id: None, + ), ), - name: Ident( - to_string: "main", - span: (31, 35), + name: BaseIdent( + name_override_opt: None, + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 31, + end: 35, + source_id: None, + ), + is_raw_ident: false, ), generics: None, arguments: Parens( @@ -43,7 +64,12 @@ fn noop_script_file() { value_separator_pairs: [], final_value_opt: None, )), - span: (35, 37), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 35, + end: 37, + source_id: None, + ), ), return_type_opt: None, where_clause_opt: None, @@ -53,16 +79,31 @@ fn noop_script_file() { statements: [], final_expr_opt: Some(Tuple(Parens( inner: Nil, - span: (48, 50), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 48, + end: 50, + source_id: None, + ), ))), - span: (39, 57), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 39, + end: 57, + source_id: None, + ), + ), + span: Span( + src: "\n script;\n \n fn main() {\n ()\n }\n ", + start: 38, + end: 58, + source_id: None, ), - span: (38, 58), ), )), ), ], ), )) - "###); + "#); } diff --git a/sway-types/Cargo.toml b/sway-types/Cargo.toml index aad3c5e52ac..89931ff9ab7 100644 --- a/sway-types/Cargo.toml +++ b/sway-types/Cargo.toml @@ -19,7 +19,7 @@ num-bigint.workspace = true num-traits.workspace = true parking_lot.workspace = true rustc-hash.workspace = true -serde = { workspace = true, features = ["derive"] } +serde = { workspace = true, features = ["derive", "std", "rc"] } sway-utils.workspace = true thiserror.workspace = true diff --git a/sway-types/src/ident.rs b/sway-types/src/ident.rs index f4aa577bc5d..ff1c168e9e6 100644 --- a/sway-types/src/ident.rs +++ b/sway-types/src/ident.rs @@ -1,7 +1,5 @@ -use serde::Serialize; - use crate::{span::Span, Spanned}; - +use serde::{Deserialize, Serialize}; use std::{ cmp::{Ord, Ordering}, fmt, @@ -13,7 +11,7 @@ pub trait Named { fn name(&self) -> &BaseIdent; } -#[derive(Clone)] +#[derive(Clone, Serialize, Deserialize)] pub struct BaseIdent { name_override_opt: Option>, span: Span, @@ -93,18 +91,6 @@ impl BaseIdent { /// often be different. pub type Ident = BaseIdent; -impl Serialize for Ident { - // Serialize an `Ident` struct with two fields: `to_string` and `span`. - fn serialize(&self, serializer: S) -> Result { - use serde::ser::SerializeStruct; - - let mut state = serializer.serialize_struct("Ident", 2)?; - state.serialize_field("to_string", &self.to_string())?; - state.serialize_field("span", &self.span)?; - state.end() - } -} - impl Hash for Ident { fn hash(&self, state: &mut H) { self.as_str().hash(state); diff --git a/sway-types/src/span.rs b/sway-types/src/span.rs index c757fb523da..99536c080ee 100644 --- a/sway-types/src/span.rs +++ b/sway-types/src/span.rs @@ -1,12 +1,11 @@ -use std::fmt::Display; - -use serde::{Deserialize, Serialize}; - use crate::SourceId; - -use { - lazy_static::lazy_static, - std::{cmp, fmt, hash::Hash, sync::Arc}, +use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; +use std::{ + cmp, + fmt::{self, Display}, + hash::Hash, + sync::Arc, }; lazy_static! { @@ -43,7 +42,7 @@ impl<'a> Position<'a> { } /// Represents a span of the source code in a specific file. -#[derive(Clone, Ord, PartialOrd)] +#[derive(Clone, Ord, PartialOrd, Serialize, Deserialize)] pub struct Span { // The original source code. src: Arc, @@ -71,18 +70,6 @@ impl PartialEq for Span { impl Eq for Span {} -impl Serialize for Span { - // Serialize a tuple two fields: `start` and `end`. - fn serialize(&self, serializer: S) -> Result { - use serde::ser::SerializeTuple; - - let mut state = serializer.serialize_tuple(2)?; - state.serialize_element(&self.start)?; - state.serialize_element(&self.end)?; - state.end() - } -} - impl From for std::ops::Range { fn from(value: Span) -> Self { Self { diff --git a/sway-types/src/u256.rs b/sway-types/src/u256.rs index e116308f169..2f42b43b0f3 100644 --- a/sway-types/src/u256.rs +++ b/sway-types/src/u256.rs @@ -1,10 +1,10 @@ -use std::ops::{Not, Shl, Shr}; - use num_bigint::{BigUint, ParseBigIntError, TryFromBigIntError}; use num_traits::Zero; +use serde::{Deserialize, Serialize}; +use std::ops::{Not, Shl, Shr}; use thiserror::Error; -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Serialize, Deserialize)] pub struct U256(BigUint); impl U256 {