From 561007df9345e3879ec60f7ca633bf000b6052af Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Thu, 9 Jan 2025 18:11:25 +0700 Subject: [PATCH 1/3] Remove instances of multiple impl blocks (#6822) ## Description Previously due to issue #1548 it was not possible to use a method defined in an impl block in another method defined in the same impl block, that issue is now fixed. ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers. --- sway-lib-core/src/primitive_conversions.sw | 9 --------- sway-lib-std/src/bytes.sw | 3 --- 2 files changed, 12 deletions(-) diff --git a/sway-lib-core/src/primitive_conversions.sw b/sway-lib-core/src/primitive_conversions.sw index 3160a5eca02..f0a1a14de81 100644 --- a/sway-lib-core/src/primitive_conversions.sw +++ b/sway-lib-core/src/primitive_conversions.sw @@ -45,10 +45,7 @@ impl u32 { input: u64 } } -} -// TODO: This must be in a separate impl block until https://github.com/FuelLabs/sway/issues/1548 is resolved -impl u32 { /// Extends a `u32` to a `u256`. /// /// # Returns @@ -114,10 +111,7 @@ impl u16 { input: u64 } } -} -// TODO: This must be in a separate impl block until https://github.com/FuelLabs/sway/issues/1548 is resolved -impl u16 { /// Extends a `u16` to a `u256`. /// /// # Returns @@ -204,10 +198,7 @@ impl u8 { input: u64 } } -} -// TODO: This must be in a separate impl block until https://github.com/FuelLabs/sway/issues/1548 is resolved -impl u8 { /// Extends a `u8` to a `u256`. /// /// # Returns diff --git a/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index cb6a8edb521..5b5c817e914 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -584,10 +584,7 @@ impl Bytes { pub fn ptr(self) -> raw_ptr { self.buf.ptr() } -} -// Need to use separate impl blocks for now: https://github.com/FuelLabs/sway/issues/1548 -impl Bytes { /// Divides one Bytes into two at an index. /// /// # Additional Information From faf399e094c60898d379660f151ae14a8929e983 Mon Sep 17 00:00:00 2001 From: Marcos Henrich Date: Thu, 9 Jan 2025 13:26:18 +0000 Subject: [PATCH 2/3] Fixes array oob on reassignment. (#6819) ## Description When using literal on array reasignement we were not checking the array length. We now throw an array oob error in this case. Fixes #6393 ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. Co-authored-by: Joshua Batty --- .../ast_node/expression/typed_expression.rs | 19 +++++++++++++++++-- .../array_oob_reassignment/Forc.lock | 8 ++++++++ .../array_oob_reassignment/Forc.toml | 9 +++++++++ .../array_oob_reassignment/src/main.sw | 16 ++++++++++++++++ .../array_oob_reassignment/test.toml | 19 +++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/test.toml diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index 06220dcac74..62f88759358 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -2536,12 +2536,27 @@ impl ty::TyExpression { full_span_for_error = Span::join(full_span_for_error, index_span); } ( - TypeInfo::Array(elem_ty, _), - ty::ProjectionKind::ArrayIndex { index_span, .. }, + TypeInfo::Array(elem_ty, array_length), + ty::ProjectionKind::ArrayIndex { index, index_span }, ) => { parent_rover = symbol; symbol = elem_ty.type_id; symbol_span = index_span.clone(); + + if let Some(index_literal) = index + .expression + .as_literal() + .and_then(|x| x.cast_value_to_u64()) + { + if index_literal >= array_length.val() as u64 { + return Err(handler.emit_err(CompileError::ArrayOutOfBounds { + index: index_literal, + count: array_length.val() as u64, + span: index.span.clone(), + })); + } + } + // `index_span` does not contain the enclosing square brackets. // Which means, if this array index access is the last one before the // erroneous expression, the `full_span_for_error` will be missing the diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/Forc.lock new file mode 100644 index 00000000000..7f8a3338563 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/Forc.lock @@ -0,0 +1,8 @@ +[[package]] +name = "array_oob_reassignment" +source = "member" +dependencies = ["core"] + +[[package]] +name = "core" +source = "path+from-root-CC73096846C1E083" diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/Forc.toml new file mode 100644 index 00000000000..cc38134eea8 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/Forc.toml @@ -0,0 +1,9 @@ +[project] +authors = ["Fuel Labs "] +license = "Apache-2.0" +name = "array_oob_reassignment" +entry = "main.sw" +implicit-std = false + +[dependencies] +core = { path = "../../../../../../sway-lib-core" } diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/src/main.sw new file mode 100644 index 00000000000..cd5d7979225 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/src/main.sw @@ -0,0 +1,16 @@ +script; + +fn main() { + let mut a = [u64; 0]; + a[0] = 1; + + + let mut b = [[u64; 1]; 1]; + b[0][1] = 1; + + + b[1][0] = 1; + + + a[0] = return; +} diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/test.toml new file mode 100644 index 00000000000..e3c8288e736 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_reassignment/test.toml @@ -0,0 +1,19 @@ +category = "fail" + +# check: $()let mut a = [u64; 0]; +# nextln: $()This declaration is never used. + +# check: $()let mut b = [[u64; 1]; 1]; +# nextln: $()This declaration is never used. + +# check: $()a[0] = 1; +# nextln: $()Index out of bounds; the length is 0 but the index is 0. + +# check: $()b[0][1] = 1; +# nextln: $()Index out of bounds; the length is 1 but the index is 1. + +# check: $()b[1][0] = 1; +# nextln: $()Index out of bounds; the length is 1 but the index is 1. + +# check: $()a[0] = return; +# nextln: $()Index out of bounds; the length is 0 but the index is 0. \ No newline at end of file From 1192b3f134547d7f0a31ffcc061553d0ca7ab4b6 Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Fri, 10 Jan 2025 17:17:06 +0700 Subject: [PATCH 3/3] add erroneously missing #[test()] attribute (#6824) ## Description A few bytes tests are erroneously missing the test attribute, meaning they are not actually run. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --- .../test_programs/bytes_inline_tests/src/main.sw | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/src/in_language_tests/test_programs/bytes_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/bytes_inline_tests/src/main.sw index 65597943ef7..e32d25da40b 100644 --- a/test/src/in_language_tests/test_programs/bytes_inline_tests/src/main.sw +++ b/test/src/in_language_tests/test_programs/bytes_inline_tests/src/main.sw @@ -779,6 +779,7 @@ fn bytes_append_to_empty() { }; } +#[test()] fn bytes_append_self() { let (mut bytes, a, b, c) = setup(); assert(bytes.len() == 3); @@ -797,6 +798,7 @@ fn bytes_append_self() { assert(bytes.get(5).unwrap() == c); } +#[test()] fn bytes_append_empty_self() { let mut empty_bytes = Bytes::new();