Skip to content

Commit

Permalink
Merge branch 'master' into dento/idiomatic-opcode-optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentosal authored Jan 11, 2025
2 parents 4c3cfa4 + 1192b3f commit 4fb9bcc
Show file tree
Hide file tree
Showing 25 changed files with 220 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions forc-plugins/forc-doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions forc-plugins/forc-doc/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
}
Expand All @@ -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<String>,
/// Path to the project.
///
/// If not specified, current working directory will be used.
#[clap(short, long, alias = "manifest-path")]
pub path: Option<String>,
/// Include non-public items in the documentation.
#[clap(long)]
pub document_private_items: bool,
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn compile_html(
get_doc_dir: &dyn Fn(&Command) -> String,
) -> Result<(PathBuf, Box<PackageManifestFile>)> {
// 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()?
Expand Down
4 changes: 2 additions & 2 deletions forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions forc-plugins/forc-doc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
4 changes: 3 additions & 1 deletion forc-plugins/forc-fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
#[clap(short, long)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions sway-ir/src/optimize/memcpyopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions sway-lib-core/src/primitive_conversions.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion swayfmt/src/utils/language/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
10 changes: 10 additions & 0 deletions swayfmt/src/utils/language/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}"
);
40 changes: 40 additions & 0 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> = 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<u64> = Vec::new();
for iter in my_vec.iter() { }
true
}
}
"#},
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "array_oob_reassignment"
source = "member"
dependencies = ["core"]

[[package]]
name = "core"
source = "path+from-root-CC73096846C1E083"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "array_oob_reassignment"
entry = "main.sw"
implicit-std = false

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "mutability_of_references_memcpy_bug"

[dependencies]
std = { path = "../../../../../reduced_std_libs/sway-lib-std-assert" }
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit 4fb9bcc

Please sign in to comment.