From 96ee0b86a7f25115902787986185c03acd3a2afb Mon Sep 17 00:00:00 2001 From: AMIR <31338382+amiremohamadi@users.noreply.github.com> Date: Thu, 9 Jan 2025 01:47:37 +0330 Subject: [PATCH 1/6] fix: formatting for loop (#6807) ## Description Fix #6804 ## Checklist - [x] 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) - [x] 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. Co-authored-by: IGI-111 --- swayfmt/src/utils/language/expr/mod.rs | 2 +- swayfmt/src/utils/language/expr/tests.rs | 10 ++++++ swayfmt/tests/mod.rs | 40 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/swayfmt/src/utils/language/expr/mod.rs b/swayfmt/src/utils/language/expr/mod.rs index 9fd5005a24a..95a53c01c30 100644 --- a/swayfmt/src/utils/language/expr/mod.rs +++ b/swayfmt/src/utils/language/expr/mod.rs @@ -279,7 +279,7 @@ impl Format for Expr { |formatter| -> Result<(), FormatterError> { write!(formatted_code, "{} ", ForToken::AS_STR)?; value_pattern.format(formatted_code, formatter)?; - write!(formatted_code, "{} ", InToken::AS_STR)?; + write!(formatted_code, " {} ", InToken::AS_STR)?; iterator.format(formatted_code, formatter)?; IfExpr::open_curly_brace(formatted_code, formatter)?; block.get().format(formatted_code, formatter)?; diff --git a/swayfmt/src/utils/language/expr/tests.rs b/swayfmt/src/utils/language/expr/tests.rs index ef84d8b0c61..6472940e346 100644 --- a/swayfmt/src/utils/language/expr/tests.rs +++ b/swayfmt/src/utils/language/expr/tests.rs @@ -264,3 +264,13 @@ intermediate_whitespace "{ let i = 42; }"); + +fmt_test_expr!(basic_for_loop +"for iter in [0, 1, 7, 8, 15] { + let i = 42; +}", +intermediate_whitespace +"for iter in [0, 1, 7, 8, 15]{ +let i = 42; +}" +); diff --git a/swayfmt/tests/mod.rs b/swayfmt/tests/mod.rs index c3369d150c3..75f46632b23 100644 --- a/swayfmt/tests/mod.rs +++ b/swayfmt/tests/mod.rs @@ -3449,3 +3449,43 @@ fn tuple_field_access() { "#}, ); } + +#[test] +fn contract_for_loop() { + check( + indoc! {r#" + contract; + + abi MyContract { + fn test_function() -> bool; + } + + impl MyContract for Contract { + fn test_function() -> bool { + let mut my_vec: Vec = Vec::new(); + for iter in my_vec.iter() { + + } + + true + } + } + "#}, + indoc! {r#" + contract; + + abi MyContract { + fn test_function() -> bool; + } + + impl MyContract for Contract { + fn test_function() -> bool { + let mut my_vec: Vec = Vec::new(); + for iter in my_vec.iter() { } + + true + } + } + "#}, + ); +} From bb855e8acd0cf29906c86db80e1f84f5d758d53a Mon Sep 17 00:00:00 2001 From: AMIR <31338382+amiremohamadi@users.noreply.github.com> Date: Thu, 9 Jan 2025 04:15:47 +0330 Subject: [PATCH 2/6] Rename forc doc `--manifest-path` to `--path` (#6797) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Closes https://github.com/FuelLabs/sway/issues/5789 ## Checklist - [x] 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. --------- Co-authored-by: Cameron Carstens Co-authored-by: Joshua Batty Co-authored-by: Kaya Gökalp --- .github/workflows/ci.yml | 2 +- .github/workflows/gh-pages.yml | 2 +- forc-plugins/forc-doc/README.md | 6 +++--- forc-plugins/forc-doc/src/cli.rs | 11 ++++++----- forc-plugins/forc-doc/src/lib.rs | 2 +- .../forc-doc/src/tests/expects/impl_trait/mod.rs | 4 ++-- forc-plugins/forc-doc/tests/lib.rs | 4 ++-- forc-plugins/forc-fmt/src/main.rs | 4 +++- 8 files changed, 19 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7438d9fee0f..5f1199a3d8a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -287,7 +287,7 @@ jobs: run: | cargo install --locked --debug --path ./forc-plugins/forc-doc - name: Build sway-lib-std docs - run: forc doc --manifest-path ./sway-lib-std + run: forc doc --path ./sway-lib-std build-forc-test-project: runs-on: buildjet-4vcpu-ubuntu-2204 diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index fa2ac16b866..7e3201451c5 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -50,7 +50,7 @@ jobs: run: mdbook build docs/reference - name: Build Sway std library - run: forc doc --manifest-path ./sway-lib-std + run: forc doc --path ./sway-lib-std - name: Deploy master std uses: peaceiris/actions-gh-pages@v3 diff --git a/forc-plugins/forc-doc/README.md b/forc-plugins/forc-doc/README.md index 90531e248cd..c82602aa225 100644 --- a/forc-plugins/forc-doc/README.md +++ b/forc-plugins/forc-doc/README.md @@ -78,7 +78,7 @@ $ cargo install --path forc-plugins/forc-doc Great! Let's check everything is working as intended. Try running `forc doc` on one of the test directories: ```sh -$ forc doc --manifest-path src/tests/data/impl_traits --open +$ forc doc --path src/tests/data/impl_traits --open ``` If it succeeded, you should be seeing the test docs in your browser. @@ -274,10 +274,10 @@ Now we can call this function anytime we need to generate a searchbar for our we Once you've made some changes, run the `forc doc` binary, passing it a path containing a `Forc.toml`: ```sh -cargo run -- --manifest-path path/to/manifest --open +cargo run -- --path path/to/manifest --open ``` -> **Tip:** VS Code user? Try the Live Server plugin to make viewing changes even easier. It will reload a webpage on updates, so you only need to rebuild the docs (`cargo run -- --manifest-path path/to/manifest`). Just right click the index file of docs produced by `forc doc` which can be found in the `out/doc` directory, and choose the option "open with Live Server". Voila! +> **Tip:** VS Code user? Try the Live Server plugin to make viewing changes even easier. It will reload a webpage on updates, so you only need to rebuild the docs (`cargo run -- --path path/to/manifest`). Just right click the index file of docs produced by `forc doc` which can be found in the `out/doc` directory, and choose the option "open with Live Server". Voila! [forc-reference]: https://fuellabs.github.io/sway/master/book/forc/index.html "forc reference" [manifest-reference]: https://fuellabs.github.io/sway/master/book/forc/manifest_reference.html "manifest reference" diff --git a/forc-plugins/forc-doc/src/cli.rs b/forc-plugins/forc-doc/src/cli.rs index 309bfb880d6..dff10298122 100644 --- a/forc-plugins/forc-doc/src/cli.rs +++ b/forc-plugins/forc-doc/src/cli.rs @@ -6,7 +6,7 @@ forc_util::cli_examples! { crate::cli::Command { [ Build the docs for a project in the current path => "forc doc"] [ Build the docs for a project in the current path and open it in the browser => "forc doc --open" ] - [ Build the docs for a project located in another path => "forc doc --manifest-path {path}" ] + [ Build the docs for a project located in another path => "forc doc --path {path}" ] [ Build the docs for the current project exporting private types => "forc doc --document-private-items" ] [ Build the docs offline without downloading any dependency from the network => "forc doc --offline" ] } @@ -20,10 +20,11 @@ forc_util::cli_examples! { version )] pub struct Command { - /// Path to the Forc.toml file. By default, forc-doc searches for the Forc.toml - /// file in the current directory or any parent directory. - #[clap(long)] - pub manifest_path: Option, + /// Path to the project. + /// + /// If not specified, current working directory will be used. + #[clap(short, long, alias = "manifest-path")] + pub path: Option, /// Include non-public items in the documentation. #[clap(long)] pub document_private_items: bool, diff --git a/forc-plugins/forc-doc/src/lib.rs b/forc-plugins/forc-doc/src/lib.rs index 4043cfde1f2..ffaf5a5dbe8 100644 --- a/forc-plugins/forc-doc/src/lib.rs +++ b/forc-plugins/forc-doc/src/lib.rs @@ -57,7 +57,7 @@ pub fn compile_html( get_doc_dir: &dyn Fn(&Command) -> String, ) -> Result<(PathBuf, Box)> { // get manifest directory - let dir = if let Some(ref path) = build_instructions.manifest_path { + let dir = if let Some(ref path) = build_instructions.path { PathBuf::from(path) } else { std::env::current_dir()? diff --git a/forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs b/forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs index 602b66f49d7..555954ce244 100644 --- a/forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs +++ b/forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs @@ -21,7 +21,7 @@ fn test_impl_traits_default() { let doc_dir_name: &str = "impl_traits_default"; let project_name = "impl_traits"; let command = Command { - manifest_path: Some(format!("{}/{}", DATA_DIR, project_name)), + path: Some(format!("{}/{}", DATA_DIR, project_name)), doc_path: Some(doc_dir_name.into()), ..Default::default() }; @@ -171,7 +171,7 @@ fn test_impl_traits_no_deps() { let doc_dir_name: &str = "impl_traits_no_deps"; let project_name: &str = "impl_traits_clone"; let command = Command { - manifest_path: Some(format!("{}/{}", DATA_DIR, project_name)), + path: Some(format!("{}/{}", DATA_DIR, project_name)), doc_path: Some(doc_dir_name.into()), no_deps: true, ..Default::default() diff --git a/forc-plugins/forc-doc/tests/lib.rs b/forc-plugins/forc-doc/tests/lib.rs index 9624b656a4f..996e095645b 100644 --- a/forc-plugins/forc-doc/tests/lib.rs +++ b/forc-plugins/forc-doc/tests/lib.rs @@ -5,10 +5,10 @@ use std::path::Path; fn builds_lib_std_docs() { let path = Path::new("./../../sway-lib-std"); let build_instructions = Command { - manifest_path: Some(path.to_str().unwrap().to_string()), + path: Some(path.to_str().unwrap().to_string()), ..Default::default() }; - println!("Building docs for {:?}", build_instructions.manifest_path); + println!("Building docs for {:?}", build_instructions.path); let res = compile_html(&build_instructions, &get_doc_dir); assert!(res.is_ok()); } diff --git a/forc-plugins/forc-fmt/src/main.rs b/forc-plugins/forc-fmt/src/main.rs index fffefbbd840..331c3e51373 100644 --- a/forc-plugins/forc-fmt/src/main.rs +++ b/forc-plugins/forc-fmt/src/main.rs @@ -45,7 +45,9 @@ pub struct App { /// - Exits with `1` and prints a diff if formatting is required. #[clap(short, long)] pub check: bool, - /// Path to the project, if not specified, current working directory will be used. + /// Path to the project. + /// + /// If not specified, current working directory will be used. #[clap(short, long)] pub path: Option, #[clap(short, long)] From e1c3fa38ca64c27062ee8a1818d66279841993f0 Mon Sep 17 00:00:00 2001 From: Vaivaswatha N Date: Thu, 9 Jan 2025 15:10:47 +0530 Subject: [PATCH 3/6] fix bug in `memcopyopt` related to killing defined symbols (#6820) --- sway-ir/src/optimize/memcpyopt.rs | 11 ++++++-- .../Forc.lock | 13 +++++++++ .../Forc.toml | 8 ++++++ .../src/lib.sw | 19 +++++++++++++ .../src/main.sw | 27 +++++++++++++++++++ .../test.toml | 3 +++ 6 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/src/lib.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/test.toml diff --git a/sway-ir/src/optimize/memcpyopt.rs b/sway-ir/src/optimize/memcpyopt.rs index 265db81695b..fbce2059c59 100644 --- a/sway-ir/src/optimize/memcpyopt.rs +++ b/sway-ir/src/optimize/memcpyopt.rs @@ -301,7 +301,6 @@ fn local_copy_prop( available_copies.remove(copy); } } - copies.retain(|copy| available_copies.contains(copy)); } if let Some(copies) = dest_to_copies.get_mut(&sym) { for copy in &*copies { @@ -333,9 +332,17 @@ fn local_copy_prop( available_copies.remove(copy); } } - copies.retain(|copy| available_copies.contains(copy)); } } + // Update src_to_copies and dest_to_copies to remove every copy not in available_copies. + src_to_copies.retain(|_, copies| { + copies.retain(|copy| available_copies.contains(copy)); + !copies.is_empty() + }); + dest_to_copies.retain(|_, copies| { + copies.retain(|copy| available_copies.contains(copy)); + !copies.is_empty() + }); } ReferredSymbols::Incomplete(_) => { // The only safe thing we can do is to clear all information. diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/Forc.lock new file mode 100644 index 00000000000..e52d486e352 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = "core" +source = "path+from-root-1F6D5E67DD060824" + +[[package]] +name = "mutability_of_references_memcpy_bug" +source = "member" +dependencies = ["std"] + +[[package]] +name = "std" +source = "path+from-root-1F6D5E67DD060824" +dependencies = ["core"] diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/Forc.toml new file mode 100644 index 00000000000..66e2ad87de3 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "mutability_of_references_memcpy_bug" + +[dependencies] +std = { path = "../../../../../reduced_std_libs/sway-lib-std-assert" } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/src/lib.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/src/lib.sw new file mode 100644 index 00000000000..88c1bfa547f --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/src/lib.sw @@ -0,0 +1,19 @@ +library; + +#[inline(never)] +pub fn unit(b: u256) -> u256 { + b +} + +#[inline(never)] +pub fn weird(_b: u256) { + let mut b = _b; // b = _b = 2 + + log(b); + + let mut b_tmp = b; // b_tmp = b = 2 + + b = 0x03u256; // b = a = 1 + + assert(unit(b_tmp) == 0x02u256); +} diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/src/main.sw new file mode 100644 index 00000000000..91454432859 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/src/main.sw @@ -0,0 +1,27 @@ +contract; + +pub mod lib; + +use ::lib::weird; + +abi MyContract { + fn test_function(b: u256) -> u256; +} + +impl MyContract for Contract { + + fn test_function(_b: u256) -> u256 { + weird(_b); + 0x00u256 + } +} + + + +#[test] +fn test() { + let caller = abi(MyContract, CONTRACT_ID); + let b = 0x02u256; + + let _ = caller.test_function(b); +} diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/test.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/test.toml new file mode 100644 index 00000000000..ad1782559e7 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/mutability_of_references_memcpy_bug/test.toml @@ -0,0 +1,3 @@ +category = "unit_tests_pass" +validate_abi = false +expected_warnings = 0 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 4/6] 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 5/6] 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 6/6] 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();