Skip to content

Commit a0e1f96

Browse files
authored
Merge branch 'master' into ironcev/marker-traits
2 parents d8a6f87 + 1194375 commit a0e1f96

File tree

28 files changed

+751
-178
lines changed

28 files changed

+751
-178
lines changed

sway-core/src/semantic_analysis/namespace/trait_map.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -917,11 +917,6 @@ impl TraitMap {
917917
*map_type_id,
918918
*type_id,
919919
);
920-
type_id.subst(&SubstTypesContext::new(
921-
engines,
922-
&type_mapping,
923-
matches!(code_block_first_pass, CodeBlockFirstPass::No),
924-
));
925920
let trait_items: TraitItems = map_trait_items
926921
.clone()
927922
.into_iter()

sway-core/src/type_system/unify/unify_check.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ impl<'a> UnifyCheck<'a> {
510510
// any type can be coerced into a generic,
511511
// except if the type already contains the generic
512512
(_e, _g @ UnknownGeneric { .. }) => {
513-
!OccursCheck::new(self.engines).check(right, left)
513+
matches!(self.mode, ConstraintSubset)
514+
|| !OccursCheck::new(self.engines).check(right, left)
514515
}
515516

516517
(Alias { ty: l_ty, .. }, Alias { ty: r_ty, .. }) => {

sway-lib-std/src/bytes.sw

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,61 @@ impl Bytes {
816816

817817
spliced
818818
}
819+
820+
/// Resizes the `Bytes` in-place so that `len` is equal to `new_len`.
821+
///
822+
/// # Additional Information
823+
///
824+
/// If `new_len` is greater than `len`, the `Bytes` is extended by the difference, with each additional slot filled with `value`. If `new_len` is less than `len`, the `Bytes` is simply truncated.
825+
///
826+
/// # Arguments
827+
///
828+
/// * `new_len`: [u64] - The new length of the `Bytes`.
829+
/// * `value`: [u8] - The value to fill the new length.
830+
///
831+
/// # Examples
832+
///
833+
/// ```sway
834+
/// fn foo() {
835+
/// let bytes = Bytes::new();
836+
/// bytes.resize(1, 7u8);
837+
/// assert(bytes.len() == 1);
838+
/// assert(bytes.get(0).unwrap() == 7u8);
839+
///
840+
/// bytes.resize(2, 9u8);
841+
/// assert(bytes.len() == 2);
842+
/// assert(bytes.get(0).unwrap() == 7u8);
843+
/// assert(bytes.get(1).unwrap() == 9u8);
844+
///
845+
/// bytes.resize(1, 0);
846+
/// assert(bytes.len() == 1);
847+
/// assert(bytes.get(0).unwrap() == 7u8);
848+
/// assert(bytes.get(1) == None);
849+
/// }
850+
/// ```
851+
pub fn resize(ref mut self, new_len: u64, value: u8) {
852+
// If the `new_len` is less then truncate
853+
if self.len >= new_len {
854+
self.len = new_len;
855+
return;
856+
}
857+
858+
// If we don't have enough capacity, alloc more
859+
if self.buf.cap < new_len {
860+
self.buf.ptr = realloc_bytes(self.buf.ptr, self.buf.cap, new_len);
861+
self.buf.cap = new_len;
862+
}
863+
864+
// Fill the new length with value
865+
let mut i = 0;
866+
let start_ptr = self.buf.ptr.add_uint_offset(self.len);
867+
while i + self.len < new_len {
868+
start_ptr.add_uint_offset(i).write_byte(value);
869+
i += 1;
870+
}
871+
872+
self.len = new_len;
873+
}
819874
}
820875

821876
impl core::ops::Eq for Bytes {
@@ -1020,57 +1075,3 @@ impl AbiDecode for Bytes {
10201075
raw_slice::abi_decode(buffer).into()
10211076
}
10221077
}
1023-
1024-
#[test]
1025-
fn ok_bytes_buffer_ownership() {
1026-
let mut original_array = [1u8, 2u8, 3u8, 4u8];
1027-
let slice = raw_slice::from_parts::<u8>(__addr_of(original_array), 4);
1028-
1029-
// Check Bytes duplicates the original slice
1030-
let mut bytes = Bytes::from(slice);
1031-
bytes.set(0, 5);
1032-
assert(original_array[0] == 1);
1033-
1034-
// At this point, slice equals [5, 2, 3, 4]
1035-
let encoded_slice = encode(bytes);
1036-
1037-
// `Bytes` should duplicate the underlying buffer,
1038-
// so when we write to it, it should not change
1039-
// `encoded_slice`
1040-
let mut bytes = abi_decode::<Bytes>(encoded_slice);
1041-
bytes.set(0, 6);
1042-
assert(bytes.get(0) == Some(6));
1043-
1044-
let mut bytes = abi_decode::<Bytes>(encoded_slice);
1045-
assert(bytes.get(0) == Some(5));
1046-
}
1047-
1048-
#[test]
1049-
fn ok_bytes_bigger_than_3064() {
1050-
let mut v: Bytes = Bytes::new();
1051-
1052-
// We allocate 1024 bytes initially, this is throw away because
1053-
// it is not big enough for the buffer.
1054-
// Then we used to double the buffer to 2048.
1055-
// Then we write an `u64` with the length of the buffer.
1056-
// Then we write the buffer itself.
1057-
// (1024 + 2048) - 8 = 3064
1058-
// Thus, we need a buffer with 3065 bytes to write into the red zone
1059-
let mut a = 3065;
1060-
while a > 0 {
1061-
v.push(1u8);
1062-
a -= 1;
1063-
}
1064-
1065-
// This red zone should not be overwritten
1066-
let red_zone = asm(size: 1024) {
1067-
aloc size;
1068-
hp: raw_ptr
1069-
};
1070-
red_zone.write(0xFFFFFFFFFFFFFFFF);
1071-
assert(red_zone.read::<u64>() == 0xFFFFFFFFFFFFFFFF);
1072-
1073-
let _ = encode(v);
1074-
1075-
assert(red_zone.read::<u64>() == 0xFFFFFFFFFFFFFFFF);
1076-
}

sway-lib-std/src/prelude.sw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub use ::revert::{require, revert, revert_with_log};
2525

2626
// Convert
2727
pub use ::convert::From;
28+
pub use ::clone::Clone;
2829

2930
// Primitive conversions
3031
pub use ::primitive_conversions::{b256::*, str::*, u16::*, u256::*, u32::*, u64::*, u8::*};

sway-lib-std/src/string.sw

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ::bytes::*;
66
use ::convert::*;
77
use ::hash::{Hash, Hasher};
88
use ::option::Option;
9+
use ::clone::Clone;
910

1011
/// A UTF-8 encoded growable string. It has ownership over its buffer.
1112
///
@@ -341,3 +342,11 @@ impl AbiDecode for String {
341342
}
342343
}
343344
}
345+
346+
impl Clone for String {
347+
fn clone(self) -> Self {
348+
Self {
349+
bytes: self.bytes.clone(),
350+
}
351+
}
352+
}

sway-lib-std/src/vec.sw

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ::assert::assert;
66
use ::option::Option::{self, *};
77
use ::convert::From;
88
use ::iterator::*;
9+
use ::clone::Clone;
910

1011
struct RawVec<T> {
1112
ptr: raw_ptr,
@@ -697,6 +698,61 @@ impl<T> Vec<T> {
697698
pub fn ptr(self) -> raw_ptr {
698699
self.buf.ptr()
699700
}
701+
702+
/// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
703+
///
704+
/// # Additional Information
705+
///
706+
/// If `new_len` is greater than `len`, the `Vec` is extended by the difference, with each additional slot filled with `value`. If `new_len` is less than `len`, the `Vec` is simply truncated.
707+
///
708+
/// # Arguments
709+
///
710+
/// * `new_len`: [u64] - The new length of the `Vec`.
711+
/// * `value`: [T] - The value to fill the new length.
712+
///
713+
/// # Examples
714+
///
715+
/// ```sway
716+
/// fn foo() {
717+
/// let vec: Vec<u64> = Vec::new();
718+
/// vec.resize(1, 7);
719+
/// assert(vec.len() == 1);
720+
/// assert(vec.get(0).unwrap() == 7);
721+
///
722+
/// vec.resize(2, 9);
723+
/// assert(vec.len() == 2);
724+
/// assert(vec.get(0).unwrap() == 7);
725+
/// assert(vec.get(1).unwrap() == 9);
726+
///
727+
/// vec.resize(1, 0);
728+
/// assert(vec.len() == 1);
729+
/// assert(vec.get(0).unwrap() == 7);
730+
/// assert(vec.get(1) == None);
731+
/// }
732+
/// ```
733+
pub fn resize(ref mut self, new_len: u64, value: T) {
734+
// If the `new_len` is less then truncate
735+
if self.len >= new_len {
736+
self.len = new_len;
737+
return;
738+
}
739+
740+
// If we don't have enough capacity, alloc more
741+
if self.buf.cap < new_len {
742+
self.buf.ptr = realloc::<T>(self.buf.ptr, self.buf.cap, new_len);
743+
self.buf.cap = new_len;
744+
}
745+
746+
// Fill the new length with `value`
747+
let mut i = 0;
748+
let start_ptr = self.buf.ptr.add::<T>(self.len);
749+
while i + self.len < new_len {
750+
start_ptr.add::<T>(i).write::<T>(value);
751+
i += 1;
752+
}
753+
754+
self.len = new_len;
755+
}
700756
}
701757

702758
impl<T> AsRawSlice for Vec<T> {
@@ -791,26 +847,13 @@ impl<T> Iterator for VecIter<T> {
791847
}
792848
}
793849

794-
#[test]
795-
fn ok_vec_buffer_ownership() {
796-
let mut original_array = [1u8, 2u8, 3u8, 4u8];
797-
let slice = raw_slice::from_parts::<u8>(__addr_of(original_array), 4);
798-
799-
// Check Vec duplicates the original slice
800-
let mut bytes = Vec::<u8>::from(slice);
801-
bytes.set(0, 5);
802-
assert(original_array[0] == 1);
803-
804-
// At this point, slice equals [5, 2, 3, 4]
805-
let encoded_slice = encode(bytes);
806-
807-
// `Vec<u8>` should duplicate the underlying buffer,
808-
// so when we write to it, it should not change
809-
// `encoded_slice`
810-
let mut bytes = abi_decode::<Vec<u8>>(encoded_slice);
811-
bytes.set(0, 6);
812-
assert(bytes.get(0) == Some(6));
813-
814-
let mut bytes = abi_decode::<Vec<u8>>(encoded_slice);
815-
assert(bytes.get(0) == Some(5));
850+
impl<T> Clone for Vec<T> {
851+
fn clone(self) -> Self {
852+
let len = self.len();
853+
let buf = RawVec::with_capacity(len);
854+
if len > 0 {
855+
self.ptr().copy_to::<T>(buf.ptr(), len);
856+
}
857+
Self { buf, len }
858+
}
816859
}

test/src/e2e_vm_tests/reduced_std_libs/sway-lib-std-vec/reduced_lib.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ revert.sw
88
vec.sw
99
iterator.sw
1010
convert.sw
11+
clone.sw

test/src/e2e_vm_tests/reduced_std_libs/sway-lib-std-vec/src/lib.sw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod assert;
99
pub mod convert;
1010
pub mod alloc;
1111
pub mod iterator;
12+
pub mod clone;
1213
pub mod vec;
1314

1415
pub mod prelude;

test/src/e2e_vm_tests/reduced_std_libs/sway-lib-std-vec/src/prelude.sw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub use ::revert::{require, revert};
1414

1515
// Convert
1616
pub use ::convert::From;
17+
pub use ::clone::Clone;
1718

1819
// Logging
1920
pub use ::logging::log;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[package]]
2+
name = "core"
3+
source = "path+from-root-9C31758901D851EE"
4+
5+
[[package]]
6+
name = "nested_generics"
7+
source = "member"
8+
dependencies = ["std"]
9+
10+
[[package]]
11+
name = "std"
12+
source = "path+from-root-9C31758901D851EE"
13+
dependencies = ["core"]

0 commit comments

Comments
 (0)