Skip to content

Commit

Permalink
Merge branch 'master' into standard-documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
bitzoic authored Aug 21, 2023
2 parents 5108a4e + 8991a25 commit 124c184
Show file tree
Hide file tree
Showing 106 changed files with 1,622 additions and 829 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ jobs:

# TODO: Remove this upon merging std tests with the rest of the E2E tests.
cargo-test-lib-std:
runs-on: buildjet-4vcpu-ubuntu-2204
runs-on: buildjet-8vcpu-ubuntu-2204
steps:
- uses: actions/checkout@v3
- name: Install toolchain
Expand Down
2 changes: 0 additions & 2 deletions docs/book/src/advanced/generic_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ purely shorthand for the sake of ergonomics.

## Trait Constraints

> **Note** Trait constraints [have not yet been implemented](https://github.com/FuelLabs/sway/issues/970)
Important background to know before diving into trait constraints is that the `where` clause can be used to specify the required traits for the generic argument. So, when writing something like a `HashMap` you may
want to specify that the generic argument implements a `Hash` trait.

Expand Down
10 changes: 10 additions & 0 deletions docs/book/src/reference/compiler_intrinsics.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ __size_of_str<T>() -> u64

___

```sway
__check_str_type<T>() -> u64
```

**Description:** Throws a compile error if type `T` is not a string.

**Constraints:** None.

___

```sway
__is_reference_type<T>() -> bool
```
Expand Down
21 changes: 13 additions & 8 deletions docs/reference/src/code/operations/hashing/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
library;

// ANCHOR: import_sha256
use std::hash::sha256;
// ANCHOR_END: import_sha256
// ANCHOR: import_keccak256
use std::hash::keccak256;
// ANCHOR_END: import_keccak256
// ANCHOR: import
use std::hash::*;
// ANCHOR_END: import
// ANCHOR: sha256
fn sha256_hashing(age: u64, name: str[5], status: bool) -> b256 {
sha256((age, name, status))
let mut hasher = Hasher::new();
age.hash(hasher);
hasher.write_str(name);
status.hash(hasher);
hasher.sha256()
}
// ANCHOR_END: sha256
// ANCHOR: keccak256
fn keccak256_hashing(age: u64, name: str[5], status: bool) -> b256 {
keccak256((age, name, status))
let mut hasher = Hasher::new();
age.hash(hasher);
hasher.write_str(name);
status.hash(hasher);
hasher.keccak256()
}
// ANCHOR_END: keccak256
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
contract;

use std::hash::*;

// ANCHOR: initialization

storage {
// k = Identity, v = u64
balance: StorageMap<Identity, u64> = StorageMap {},
balance: StorageMap<Identity, u64> = StorageMap::<Identity, u64> {},
// k = (Identity, u64), v = bool
user: StorageMap<(Identity, u64), bool> = StorageMap {},
user: StorageMap<(Identity, u64), bool> = StorageMap::<(Identity, u64), bool> {},
}
// ANCHOR_END: initialization
// ANCHOR: reading_from_storage
Expand Down
3 changes: 2 additions & 1 deletion examples/cei_analysis/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ contract;
mod other_contract;

use other_contract::*;
use std::hash::*;

abi MyContract {
#[storage(read, write)]
fn withdraw(external_contract_id: ContractId);
}

storage {
balances: StorageMap<Identity, u64> = StorageMap {},
balances: StorageMap<Identity, u64> = StorageMap::<Identity, u64> {},
}

impl MyContract for Contract {
Expand Down
47 changes: 46 additions & 1 deletion examples/hashing/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
script;

use std::hash::{keccak256, sha256};
use std::hash::*;

impl Hash for str[4] {
fn hash(self, ref mut state: Hasher) {
state.write_str(self);
}
}

impl Hash for str[32] {
fn hash(self, ref mut state: Hasher) {
state.write_str(self);
}
}

impl Hash for Location {
fn hash(self, ref mut state: Hasher) {
match self {
Location::Earth => {
0_u8.hash(state);
}
Location::Mars => {
1_u8.hash(state);
}
}
}
}

impl Hash for Stats {
fn hash(self, ref mut state: Hasher) {
self.strength.hash(state);
self.agility.hash(state);
}
}

impl Hash for Person {
fn hash(self, ref mut state: Hasher) {
self.name.hash(state);
self.age.hash(state);
self.alive.hash(state);
self.location.hash(state);
self.stats.hash(state);
self.some_tuple.hash(state);
self.some_array.hash(state);
self.some_b256.hash(state);
}
}

const VALUE_A = 0x9280359a3b96819889d30614068715d634ad0cf9bba70c0f430a8c201138f79f;

Expand Down
2 changes: 1 addition & 1 deletion examples/liquidity_pool/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
},
constants::ZERO_B256,
context::msg_amount,
hash::sha256,
hash::*,
token::{
mint_to_address,
transfer_to_address,
Expand Down
8 changes: 5 additions & 3 deletions examples/storage_map/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
contract;

use std::hash::*;

storage {
// ANCHOR: storage_map_decl
map: StorageMap<Address, u64> = StorageMap {},
map: StorageMap<Address, u64> = StorageMap::<Address, u64> {},
// ANCHOR_END: storage_map_decl
// ANCHOR: storage_map_tuple_key
map_two_keys: StorageMap<(b256, bool), b256> = StorageMap {},
map_two_keys: StorageMap<(b256, bool), b256> = StorageMap::<(b256, bool), b256> {},
// ANCHOR_END: storage_map_tuple_key
// ANCHOR: storage_map_nested
nested_map: StorageMap<u64, StorageMap<u64, u64>> = StorageMap {},
nested_map: StorageMap<u64, StorageMap<u64, u64>> = StorageMap::<u64, StorageMap<u64, u64>> {},
// ANCHOR_END: storage_map_nested
}

Expand Down
4 changes: 2 additions & 2 deletions examples/subcurrency/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ANCHOR: body
contract;

use std::hash::sha256;
use std::hash::*;

////////////////////////////////////////
// Event declarations
Expand Down Expand Up @@ -44,7 +44,7 @@ const MINTER = Address::from(0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d
////////////////////////////////////////
// Contract storage persists across transactions.
storage {
balances: StorageMap<Address, u64> = StorageMap {},
balances: StorageMap<Address, u64> = StorageMap::<Address, u64> {},
}

////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions sway-ast/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub enum Intrinsic {
SizeOfType,
SizeOfVal,
SizeOfStr,
CheckStrType,
Eq,
Gt,
Lt,
Expand Down Expand Up @@ -43,6 +44,7 @@ impl fmt::Display for Intrinsic {
Intrinsic::SizeOfType => "size_of",
Intrinsic::SizeOfVal => "size_of_val",
Intrinsic::SizeOfStr => "size_of_str",
Intrinsic::CheckStrType => "check_str_type",
Intrinsic::Eq => "eq",
Intrinsic::Gt => "gt",
Intrinsic::Lt => "lt",
Expand Down Expand Up @@ -83,6 +85,7 @@ impl Intrinsic {
"__size_of" => SizeOfType,
"__size_of_val" => SizeOfVal,
"__size_of_str" => SizeOfStr,
"__check_str_type" => CheckStrType,
"__eq" => Eq,
"__gt" => Gt,
"__lt" => Lt,
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/decl_engine/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ where
&self,
decl_mapping: &DeclMapping,
handler: &Handler,
ctx: &TypeCheckContext,
ctx: &mut TypeCheckContext,
) -> Result<Self, ErrorEmitted> {
let decl_engine = ctx.engines().de();
let mut decl = decl_engine.get(&self.id);
Expand Down Expand Up @@ -341,7 +341,7 @@ impl ReplaceDecls for DeclRefFunction {
&mut self,
decl_mapping: &DeclMapping,
_handler: &Handler,
ctx: &TypeCheckContext,
ctx: &mut TypeCheckContext,
) -> Result<(), ErrorEmitted> {
let engines = ctx.engines();
let decl_engine = engines.de();
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/decl_engine/replace_decls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ pub trait ReplaceDecls {
&mut self,
decl_mapping: &DeclMapping,
handler: &Handler,
ctx: &TypeCheckContext,
ctx: &mut TypeCheckContext,
) -> Result<(), ErrorEmitted>;

fn replace_decls(
&mut self,
decl_mapping: &DeclMapping,
handler: &Handler,
ctx: &TypeCheckContext,
ctx: &mut TypeCheckContext,
) -> Result<(), ErrorEmitted> {
if !decl_mapping.is_empty() {
self.replace_decls_inner(decl_mapping, handler, ctx)?;
Expand Down
24 changes: 23 additions & 1 deletion sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use sway_ir::{
metadata::combine as md_combine,
module::Module,
value::Value,
Instruction, Type,
Instruction, Type, TypeContent,
};
use sway_types::{ident::Ident, span::Spanned, Span};
use sway_utils::mapped_stack::MappedStack;
Expand Down Expand Up @@ -851,6 +851,28 @@ fn const_eval_intrinsic(
value: ConstantValue::Uint(ir_type_str_size_in_bytes(lookup.context, &ir_type)),
}))
}
sway_ast::Intrinsic::CheckStrType => {
let targ = &intrinsic.type_arguments[0];
let ir_type = convert_resolved_typeid(
lookup.engines.te(),
lookup.engines.de(),
lookup.context,
&targ.type_id,
&targ.span,
)
.map_err(ConstEvalError::CompileError)?;
match ir_type.get_content(lookup.context) {
TypeContent::String(_n) => Ok(Some(Constant {
ty: Type::get_unit(lookup.context),
value: ConstantValue::Unit,
})),
_ => Err(ConstEvalError::CompileError(
CompileError::NonStrGenericType {
span: targ.span.clone(),
},
)),
}
}
sway_ast::Intrinsic::Eq => {
assert!(args.len() == 2);
Ok(Some(Constant {
Expand Down
16 changes: 16 additions & 0 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,22 @@ impl<'eng> FnCompiler<'eng> {
let val = matches!(engines.te().get_unaliased(targ.type_id), TypeInfo::Str(_));
Ok(Constant::get_bool(context, val))
}
Intrinsic::CheckStrType => {
let targ = type_arguments[0].clone();
let ir_type = convert_resolved_typeid(
engines.te(),
engines.de(),
context,
&targ.type_id,
&targ.span,
)?;
match ir_type.get_content(context) {
TypeContent::String(_n) => Ok(Constant::get_unit(context)),
_ => Err(CompileError::NonStrGenericType {
span: targ.span.clone(),
}),
}
}
Intrinsic::Eq | Intrinsic::Gt | Intrinsic::Lt => {
let lhs = &arguments[0];
let rhs = &arguments[1];
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/language/ty/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl ReplaceDecls for TyAstNode {
&mut self,
decl_mapping: &DeclMapping,
handler: &Handler,
ctx: &TypeCheckContext,
ctx: &mut TypeCheckContext,
) -> Result<(), ErrorEmitted> {
match self.content {
TyAstNodeContent::ImplicitReturnExpression(ref mut exp) => {
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/language/ty/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl ReplaceDecls for TyCodeBlock {
&mut self,
decl_mapping: &DeclMapping,
handler: &Handler,
ctx: &TypeCheckContext,
ctx: &mut TypeCheckContext,
) -> Result<(), ErrorEmitted> {
handler.scope(|handler| {
for x in self.contents.iter_mut() {
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/language/ty/declaration/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl ReplaceDecls for TyConstantDecl {
&mut self,
decl_mapping: &DeclMapping,
handler: &Handler,
ctx: &TypeCheckContext,
ctx: &mut TypeCheckContext,
) -> Result<(), ErrorEmitted> {
if let Some(expr) = &mut self.value {
expr.replace_decls(decl_mapping, handler, ctx)
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/language/ty/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl ReplaceDecls for TyFunctionDecl {
&mut self,
decl_mapping: &DeclMapping,
handler: &Handler,
ctx: &TypeCheckContext,
ctx: &mut TypeCheckContext,
) -> Result<(), ErrorEmitted> {
self.body.replace_decls(decl_mapping, handler, ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/language/ty/expression/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl ReplaceDecls for TyExpression {
&mut self,
decl_mapping: &DeclMapping,
handler: &Handler,
ctx: &TypeCheckContext,
ctx: &mut TypeCheckContext,
) -> Result<(), ErrorEmitted> {
self.expression.replace_decls(decl_mapping, handler, ctx)
}
Expand Down
Loading

0 comments on commit 124c184

Please sign in to comment.