Skip to content

Commit

Permalink
Merge branch 'master' into add/predicate-script-templates
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel authored Jan 4, 2024
2 parents 4b432be + bb1a627 commit fa0bc92
Show file tree
Hide file tree
Showing 252 changed files with 7,080 additions and 1,547 deletions.
28 changes: 26 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ concurrency:
env:
CARGO_TERM_COLOR: always
REGISTRY: ghcr.io
RUST_VERSION: 1.73.0
NIGHTLY_RUST_VERSION: nightly-2023-08-27
RUST_VERSION: 1.75.0
NIGHTLY_RUST_VERSION: nightly-2023-12-27

jobs:
build-sway-lib-core:
Expand Down Expand Up @@ -71,6 +71,30 @@ jobs:
- name: Build Sway examples workspace
run: cargo run --locked -p forc -- build --locked --path ./docs/reference/src/code/Forc.toml

forc-fmt-check-sway-lib-core:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.RUST_VERSION }}
- uses: Swatinem/rust-cache@v2
- name: Check Sway sway-lib-core formatting
run: cargo run --locked -p forc-fmt -- --check --path ./sway-lib-core

forc-fmt-check-sway-lib-std:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.RUST_VERSION }}
- uses: Swatinem/rust-cache@v2
- name: Check Sway sway-lib-std formatting
run: cargo run --locked -p forc-fmt -- --check --path ./sway-lib-std

forc-fmt-check-sway-examples:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions docs/book/src/introduction/standard_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The current version of the prelude lives in [`std::prelude`](https://github.com/
- [`std::result::Result`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/result.sw), an enum for functions that may succeed or fail.
- [`std::assert::assert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw), a function that reverts the VM if the condition provided to it is `false`.
- [`std::assert::assert_eq`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw), a function that reverts the VM and logs its two inputs `v1` and `v2` if the condition `v1` == `v2` is `false`.
- [`std::assert::assert_ne`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw), a function that reverts the VM and logs its two inputs `v1` and `v2` if the condition `v1` != `v2` is `false`.
- [`std::revert::require`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw), a function that reverts the VM and logs a given value if the condition provided to it is `false`.
- [`std::revert::revert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw), a function that reverts the VM.
- [`std::logging::log`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/logging.sw), a function that logs arbitrary stack types.
Expand Down
1 change: 1 addition & 0 deletions docs/reference/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
- [require](./documentation/operations/assertions/require.md)
- [revert](./documentation/operations/assertions/revert.md)
- [assert_eq](./documentation/operations/assertions/assert-eq.md)
- [assert_ne](./documentation/operations/assertions/assert-ne.md)
- [Address Namespace](./documentation/operations/namespace/index.md)
- [Address](./documentation/operations/namespace/address.md)
- [ContractId](./documentation/operations/namespace/contract-id.md)
Expand Down
10 changes: 9 additions & 1 deletion docs/reference/src/code/operations/assertions/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ fn reverts() {

#[allow(dead_code)]
// ANCHOR: assert_eq
fn compare(a: u64, b: u64) {
fn compare_eq(a: u64, b: u64) {
assert_eq(a, b);
// code
}
// ANCHOR_END: assert_eq

#[allow(dead_code)]
// ANCHOR: assert_ne
fn compare_ne(a: u64, b: u64) {
assert_ne(a, b);
// code
}
// ANCHOR_END: assert_ne
1 change: 1 addition & 0 deletions docs/reference/src/documentation/misc/prelude.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The prelude contains the following:
- [`assert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw): A module containing
- `assert`: A function that reverts the VM if the condition provided to it is false
- `assert_eq`: A function that reverts the VM and logs its two inputs v1 and v2 if the condition v1 == v2 is false
- `assert_ne`: A function that reverts the VM and logs its two inputs v1 and v2 if the condition v1 != v2 is false
- [`revert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw): A module containing
- `require`: A function that reverts and logs a given value if the condition is `false`
- `revert`: A function that reverts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# assert_ne

The `assert_ne` function is automatically imported into every program from the [prelude](../../misc/prelude.md). It takes two expressions which are compared and the result is a [Boolean](../../language/built-ins/boolean.md). If the value is `false` then the virtual machine will revert.

## Example

Here is a function which asserts that `a` and `b` must not be equal.

```sway
{{#include ../../../code/operations/assertions/src/lib.sw:assert_ne}}
```
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Handling exceptions may be done through [if expressions](../../language/control-
- [`require`](require.md): Checks if a `condition` is `true` otherwise logs a `value` and reverts
- [`revert`](revert.md): Reverts the virtual machine with the provided exit code
- [`assert_eq`](assert-eq.md): Checks if `a` and `b` are equal otherwise reverts
- [`assert_ne`](assert-ne.md): Checks if `a` and `b` are not equal otherwise reverts
4 changes: 1 addition & 3 deletions examples/msg_sender/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ abi MyOwnedContract {
fn receive(field_1: u64) -> bool;
}

const OWNER = Address::from(
0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c,
);
const OWNER = Address::from(0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c);

impl MyOwnedContract for Contract {
fn receive(field_1: u64) -> bool {
Expand Down
16 changes: 4 additions & 12 deletions examples/storage_map/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ impl StorageMapExample for Contract {
// ANCHOR: storage_map_insert
#[storage(write)]
fn insert_into_storage_map() {
let addr1 = Address::from(
0x0101010101010101010101010101010101010101010101010101010101010101,
);
let addr2 = Address::from(
0x0202020202020202020202020202020202020202020202020202020202020202,
);
let addr1 = Address::from(0x0101010101010101010101010101010101010101010101010101010101010101);
let addr2 = Address::from(0x0202020202020202020202020202020202020202020202020202020202020202);

storage.map.insert(addr1, 42);
storage.map.insert(addr2, 77);
Expand All @@ -43,12 +39,8 @@ impl StorageMapExample for Contract {
// ANCHOR: storage_map_get
#[storage(read, write)]
fn get_from_storage_map() {
let addr1 = Address::from(
0x0101010101010101010101010101010101010101010101010101010101010101,
);
let addr2 = Address::from(
0x0202020202020202020202020202020202020202020202020202020202020202,
);
let addr1 = Address::from(0x0101010101010101010101010101010101010101010101010101010101010101);
let addr2 = Address::from(0x0202020202020202020202020202020202020202020202020202020202020202);

storage.map.insert(addr1, 42);
storage.map.insert(addr2, 77);
Expand Down
8 changes: 2 additions & 6 deletions examples/storage_variables/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ impl StorageExample for Contract {
storage
.var2
.w
.write(
0x1111111111111111111111111111111111111111111111111111111111111111,
);
.write(0x1111111111111111111111111111111111111111111111111111111111111111);
storage.var2.z.write(true);
}
// ANCHOR_END: storage_write
Expand All @@ -49,9 +47,7 @@ impl StorageExample for Contract {
(
storage.var1.x.try_read().unwrap_or(0),
storage.var1.y.try_read().unwrap_or(0),
storage.var2.w.try_read().unwrap_or(
0x0000000000000000000000000000000000000000000000000000000000000000,
),
storage.var2.w.try_read().unwrap_or(0x0000000000000000000000000000000000000000000000000000000000000000),
storage.var2.z.try_read().unwrap_or(false),
)
}
Expand Down
4 changes: 1 addition & 3 deletions examples/storage_vec/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ impl StorageVecContract for Contract {
storage.row.push(TableCell::Int(3));
storage
.row
.push(TableCell::B256(
0x0101010101010101010101010101010101010101010101010101010101010101,
));
.push(TableCell::B256(0x0101010101010101010101010101010101010101010101010101010101010101));
storage.row.push(TableCell::Boolean(true));
}
// ANCHOR_END: storage_vec_multiple_types_fn
Expand Down
4 changes: 1 addition & 3 deletions examples/subcurrency/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ abi Token {
// Constants
////////////////////////////////////////
/// Address of contract creator.
const MINTER = Address::from(
0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b,
);
const MINTER = Address::from(0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b);

////////////////////////////////////////
// Contract storage
Expand Down
4 changes: 1 addition & 3 deletions examples/vec/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ fn main() {

let mut row = Vec::new();
row.push(TableCell::Int(3));
row.push(TableCell::B256(
0x0101010101010101010101010101010101010101010101010101010101010101,
));
row.push(TableCell::B256(0x0101010101010101010101010101010101010101010101010101010101010101));
row.push(TableCell::Boolean(true));
// ANCHOR_END: vec_multiple_data_types
}
4 changes: 1 addition & 3 deletions examples/wallet_contract_caller_script/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ fn main() {
let contract_address = 0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b;
let caller = abi(Wallet, contract_address);
let amount_to_send = 200;
let recipient_address = Address::from(
0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b,
);
let recipient_address = Address::from(0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b);
caller
.send_funds {
gas: 10000,
Expand Down
4 changes: 1 addition & 3 deletions examples/wallet_smart_contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use std::{
// ANCHOR: abi_import
use wallet_abi::Wallet;
// ANCHOR_END: abi_import
const OWNER_ADDRESS = Address::from(
0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861,
);
const OWNER_ADDRESS = Address::from(0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861);

storage {
balance: u64 = 0,
Expand Down
55 changes: 46 additions & 9 deletions forc/src/cli/commands/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::cli::PluginsCommand;
use anyhow::anyhow;
use clap::Parser;
use forc_tracing::println_warning;
use forc_util::ForcResult;
use std::path::{Path, PathBuf};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use tracing::info;

/// Find all forc plugins available via `PATH`.
Expand All @@ -18,15 +22,52 @@ pub struct Command {
describe: bool,
}

fn get_file_name(path: &Path) -> String {
if let Some(path_str) = path.file_name().and_then(|path_str| path_str.to_str()) {
path_str.to_owned()
} else {
path.display().to_string()
}
}

pub(crate) fn exec(command: PluginsCommand) -> ForcResult<()> {
let PluginsCommand {
print_full_path,
describe,
} = command;

let mut plugins = crate::cli::plugin::find_all()
.map(|path| {
get_plugin_info(path.clone(), print_full_path, describe).map(|info| (path, info))
})
.collect::<Result<Vec<(_, _)>, _>>()?
.into_iter()
.fold(HashMap::new(), |mut acc, (path, content)| {
let bin_name = get_file_name(&path);
acc.entry(bin_name.clone())
.or_insert_with(|| (bin_name, vec![], content.clone()))
.1
.push(path);
acc
})
.into_values()
.map(|(bin_name, mut paths, content)| {
paths.sort();
paths.dedup();
(bin_name, paths, content)
})
.collect::<Vec<_>>();
plugins.sort_by(|a, b| a.0.cmp(&b.0));

info!("Installed Plugins:");
for path in crate::cli::plugin::find_all() {
info!("{}", print_plugin(path, print_full_path, describe)?);
for plugin in plugins {
info!("{}", plugin.2);
if plugin.1.len() > 1 {
println_warning(&format!("Multiple paths found for {}", plugin.0));
for path in plugin.1 {
println_warning(&format!(" {}", path.display()));
}
}
}
Ok(())
}
Expand Down Expand Up @@ -72,11 +113,7 @@ fn format_print_description(
let display = if print_full_path {
path.display().to_string()
} else {
path.file_name()
.expect("Failed to read file name")
.to_str()
.expect("Failed to print file name")
.to_string()
get_file_name(&path)
};

let description = parse_description_for_plugin(&path);
Expand All @@ -94,7 +131,7 @@ fn format_print_description(
/// paths yielded from plugin::find_all(), as well as that the file names are in valid
/// unicode format since file names should be prefixed with `forc-`. Should one of these 2
/// assumptions fail, this function panics.
fn print_plugin(path: PathBuf, print_full_path: bool, describe: bool) -> ForcResult<String> {
fn get_plugin_info(path: PathBuf, print_full_path: bool, describe: bool) -> ForcResult<String> {
format_print_description(path, print_full_path, describe)
.map_err(|e| anyhow!("Could not get plugin info: {}", e.as_ref()).into())
}
2 changes: 1 addition & 1 deletion forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub async fn run_cli() -> ForcResult<()> {
Forc::ContractId(command) => contract_id::exec(command),
Forc::PredicateRoot(command) => predicate_root::exec(command),
Forc::Plugin(args) => {
let output = plugin::execute_external_subcommand(args)?;
let output = plugin::execute_external_subcommand(args, opt.silent)?;
let code = output
.status
.code()
Expand Down
20 changes: 18 additions & 2 deletions forc/src/cli/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Items related to plugin support for `forc`.
use anyhow::{bail, Result};
use forc_tracing::println_warning;
use std::{
env, fs,
path::{Path, PathBuf},
Expand All @@ -14,14 +15,29 @@ use std::{
///
/// E.g. given `foo bar baz` where `foo` is an unrecognized subcommand to `forc`, tries to execute
/// `forc-foo bar baz`.
pub(crate) fn execute_external_subcommand(args: Vec<String>) -> Result<process::Output> {
let cmd = args.get(0).expect("`args` must not be empty");
pub(crate) fn execute_external_subcommand(
args: Vec<String>,
silent: bool,
) -> Result<process::Output> {
let cmd = args.first().expect("`args` must not be empty");
let args = &args[1..];
let path = find_external_subcommand(cmd);
let command = match path {
Some(command) => command,
None => bail!("no such subcommand: `{}`", cmd),
};

if let Ok(forc_path) = std::env::current_exe() {
if !silent && command.parent() != forc_path.parent() {
println_warning(&format!(
"The {} ({}) plugin is in a different directory than forc ({})\n",
cmd,
command.display(),
forc_path.display(),
));
}
}

let output = process::Command::new(command)
.stdin(process::Stdio::inherit())
.stdout(process::Stdio::inherit())
Expand Down
11 changes: 7 additions & 4 deletions sway-ast/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ pub enum Expr {
field_span: Span,
},
Ref {
ref_token: RefToken,
ampersand_token: AmpersandToken,
expr: Box<Expr>,
},
Deref {
deref_token: DerefToken,
star_token: StarToken,
expr: Box<Expr>,
},
Not {
Expand Down Expand Up @@ -227,8 +227,11 @@ impl Spanned for Expr {
Expr::TupleFieldProjection {
target, field_span, ..
} => Span::join(target.span(), field_span.clone()),
Expr::Ref { ref_token, expr } => Span::join(ref_token.span(), expr.span()),
Expr::Deref { deref_token, expr } => Span::join(deref_token.span(), expr.span()),
Expr::Ref {
ampersand_token,
expr,
} => Span::join(ampersand_token.span(), expr.span()),
Expr::Deref { star_token, expr } => Span::join(star_token.span(), expr.span()),
Expr::Not { bang_token, expr } => Span::join(bang_token.span(), expr.span()),
Expr::Pow { lhs, rhs, .. } => Span::join(lhs.span(), rhs.span()),
Expr::Mul { lhs, rhs, .. } => Span::join(lhs.span(), rhs.span()),
Expand Down
Loading

0 comments on commit fa0bc92

Please sign in to comment.