Skip to content

Commit

Permalink
Merge branch 'master' into ironcev/4963-error-match-arm-variable-alre…
Browse files Browse the repository at this point in the history
…ady-defined
  • Loading branch information
anton-trunov committed Sep 6, 2023
2 parents 9ab1137 + 7aade9d commit 34c1011
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 76 deletions.
54 changes: 0 additions & 54 deletions sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -537,60 +537,6 @@ impl Bytes {
pub fn is_empty(self) -> bool {
self.len == 0
}

/// Returns the `SHA-2-256` hash of the elements.
///
/// # Returns
///
/// * [b256] - The `SHA-2-256` hash of the elements.
///
/// # Examples
///
/// ```sway
/// use std:bytes::Bytes;
///
/// fn foo() {
/// let bytes = Bytes::new();
/// bytes.push(1);
/// bytes.push(2);
/// bytes.push(3);
/// let sha256_hash = bytes.sha256();
/// }
/// ```
pub fn sha256(self) -> b256 {
let mut result_buffer = b256::min();
asm(hash: result_buffer, ptr: self.buf.ptr, bytes: self.len) {
s256 hash ptr bytes;
hash: b256
}
}

/// Returns the `KECCAK-256` hash of the elements.
///
/// # Returns
///
/// * [b256] - The `KECCAK-256` hash of the elements.
///
/// # Examples
///
/// ```sway
/// use std:bytes::Bytes;
///
/// fn foo() {
/// let bytes = Bytes::new();
/// bytes.push(1);
/// bytes.push(2);
/// bytes.push(3);
/// let keccak256_hash = bytes.keccak256();
/// }
/// ```
pub fn keccak256(self) -> b256 {
let mut result_buffer = b256::min();
asm(hash: result_buffer, ptr: self.buf.ptr, bytes: self.len) {
k256 hash ptr bytes;
hash: b256
}
}
}

// Need to use seperate impl blocks for now: https://github.com/FuelLabs/sway/issues/1548
Expand Down
38 changes: 35 additions & 3 deletions sway-lib-std/src/hash.sw
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ impl Hasher {
}

pub fn sha256(self) -> b256 {
self.bytes.sha256()
let mut result_buffer = b256::min();
asm(hash: result_buffer, ptr: self.bytes.buf.ptr, bytes: self.bytes.len) {
s256 hash ptr bytes;
hash: b256
}
}

pub fn keccak256(self) -> b256 {
self.bytes.keccak256()
let mut result_buffer = b256::min();
asm(hash: result_buffer, ptr: self.bytes.buf.ptr, bytes: self.bytes.len) {
k256 hash ptr bytes;
hash: b256
}
}
}

Expand Down Expand Up @@ -123,6 +131,12 @@ impl Hash for bool {
}
}

impl Hash for Bytes {
fn hash(self, ref mut state: Hasher) {
state.write(self);
}
}

impl<A, B> Hash for (A, B) where A: Hash, B: Hash {
#![inline(never)]
fn hash(self, ref mut state: Hasher) {
Expand Down Expand Up @@ -418,4 +432,22 @@ fn test_hasher_sha256_bool() {
true.hash(hasher);
let sha256 = hasher.sha256();
assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a);
}
}

#[test]
fn test_hasher_sha256_bytes() {
use ::assert::assert;
let mut hasher = Hasher::new();
let mut bytes = Bytes::new();
bytes.push(0u8);
bytes.hash(hasher);
let sha256 = hasher.sha256();
assert(sha256 == 0x6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d);

let mut hasher = Hasher::new();
let mut bytes = Bytes::new();
bytes.push(1u8);
bytes.hash(hasher);
let sha256 = hasher.sha256();
assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a);
}
21 changes: 20 additions & 1 deletion sway-lib-std/src/string.sw
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
library;

use ::assert::assert;
use ::bytes::Bytes;
use ::convert::From;
use ::hash::{Hash, Hasher};
use ::option::Option;
use ::assert::assert;


/// A UTF-8 encoded growable string.
Expand Down Expand Up @@ -265,6 +266,12 @@ impl Eq for String {
}
}

impl Hash for String {
fn hash(self, ref mut state: Hasher) {
state.write(self.bytes);
}
}

// Tests

#[test]
Expand Down Expand Up @@ -495,3 +502,15 @@ fn string_test_equal() {
assert(string1 == string2);
assert(string1 != string3);
}

#[test]
fn string_test_hash() {
use ::hash::sha256;

let mut bytes = Bytes::new();
bytes.push(0u8);

let string = String::from(bytes);

assert(sha256(string) == sha256(bytes));
}
2 changes: 1 addition & 1 deletion sway-lsp/src/capabilities/hover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn hover_data(
}
// The `TypeInfo` of the token does not contain an `Ident`. In this case,
// we use the `Ident` of the token itself.
None => (ident.clone(), token),
None => (ident, token),
};

let contents = hover_format(session.clone(), &engines, &decl_token, &decl_ident.name);
Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/src/core/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl TokenIdent {
.source_id()
.map(|source_id| se.get_path(source_id));
Self {
name: ident.span().clone().str(),
name: ident.span().str(),
range: get_range_from_span(&ident.span()),
path,
is_raw_ident: ident.is_raw_ident(),
Expand Down
5 changes: 3 additions & 2 deletions sway-lsp/src/core/token_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ impl TokenMap {
Some(TypedAstToken::TypedFunctionDeclaration(decl))
if functions_only == Some(true) =>
{
TokenIdent::new(&Ident::new(decl.span.clone()), source_engine)
TokenIdent::new(&Ident::new(decl.span), source_engine)
}
Some(TypedAstToken::TypedDeclaration(decl)) => {
TokenIdent::new(&Ident::new(decl.span().clone()), source_engine)
TokenIdent::new(&Ident::new(decl.span()), source_engine)
}
#[allow(clippy::redundant_clone)]
_ => ident.clone(),
};
if position >= token_ident.range.start && position <= token_ident.range.end {
Expand Down
8 changes: 4 additions & 4 deletions sway-lsp/src/traverse/typed_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ fn collect_type_id(
let decl = ctx.engines.de().get_enum(decl_ref);
if let Some(token) = ctx
.tokens
.try_get_mut(&ctx.ident(&Ident::new(type_span.clone())))
.try_get_mut(&ctx.ident(&Ident::new(type_span)))
.try_unwrap()
{
assign_type_to_token(token, symbol_kind, typed_token.clone(), type_id);
Expand All @@ -1231,7 +1231,7 @@ fn collect_type_id(
let decl = ctx.engines.de().get_struct(decl_ref);
if let Some(token) = ctx
.tokens
.try_get_mut(&ctx.ident(&Ident::new(type_span.clone())))
.try_get_mut(&ctx.ident(&Ident::new(type_span)))
.try_unwrap()
{
assign_type_to_token(token, symbol_kind, typed_token.clone(), type_id);
Expand All @@ -1254,7 +1254,7 @@ fn collect_type_id(
} => {
if let Some(token) = ctx
.tokens
.try_get_mut(&ctx.ident(&Ident::new(name.span().clone())))
.try_get_mut(&ctx.ident(&Ident::new(name.span())))
.try_unwrap()
{
assign_type_to_token(token, symbol_kind, typed_token.clone(), type_id);
Expand All @@ -1273,7 +1273,7 @@ fn collect_type_id(
_ => {
if let Some(token) = ctx
.tokens
.try_get_mut(&ctx.ident(&Ident::new(type_span.clone())))
.try_get_mut(&ctx.ident(&Ident::new(type_span)))
.try_unwrap()
{
assign_type_to_token(token, symbol_kind, typed_token.clone(), type_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"typeArguments": null
},
"name": "C0",
"offset": 7564
"offset": 7372
},
{
"configurableType": {
Expand All @@ -16,7 +16,7 @@
"typeArguments": null
},
"name": "C1",
"offset": 7572
"offset": 7380
},
{
"configurableType": {
Expand All @@ -25,7 +25,7 @@
"typeArguments": null
},
"name": "C2",
"offset": 7588
"offset": 7396
},
{
"configurableType": {
Expand All @@ -34,7 +34,7 @@
"typeArguments": []
},
"name": "C3",
"offset": 7620
"offset": 7428
},
{
"configurableType": {
Expand All @@ -43,7 +43,7 @@
"typeArguments": []
},
"name": "C4",
"offset": 7636
"offset": 7444
},
{
"configurableType": {
Expand All @@ -52,7 +52,7 @@
"typeArguments": []
},
"name": "C5",
"offset": 7652
"offset": 7460
},
{
"configurableType": {
Expand All @@ -61,7 +61,7 @@
"typeArguments": null
},
"name": "C6",
"offset": 7668
"offset": 7476
},
{
"configurableType": {
Expand All @@ -70,7 +70,7 @@
"typeArguments": null
},
"name": "C7",
"offset": 7684
"offset": 7492
},
{
"configurableType": {
Expand All @@ -79,7 +79,7 @@
"typeArguments": null
},
"name": "C9",
"offset": 7732
"offset": 7540
}
],
"functions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ script;
use basic_storage_abi::{BasicStorage, Quad};

fn main() -> u64 {
let addr = abi(BasicStorage, 0x576da79735d7fe54047bef8df46195e2c0978726f68f3c840902b75e77d99c96);
let addr = abi(BasicStorage, 0x21d0b9983dced02ae881b2936f20f69026963f6cc4eb0bfb4991e16b7f8c580f);
let key = 0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let value = 4242;

Expand Down

0 comments on commit 34c1011

Please sign in to comment.