Commit graph

88 commits

Author SHA1 Message Date
Daniel Frederico Lins Leite
f88bbf42d9
String slices (#4996)
## Description

This PR introduces `string slices`.

The basic usage is at `test/src/ir_generation/tests/str_slice.sw`:

```sway
let a: str = "ABC";
```

Before this PR `a` would be of type `str[3]`, a string array.

Now both `string slices` and `string arrays` exist. This PR contains a
new intrinsic that converts from string literals to arrays.

```sway
let a: str = "ABC";
let b: str[3] = __to_str_array("ABC");
```

Runtime conversions can be done using 

```sway
let a = "abcd";
let b: str[4] = a.try_as_str_array().unwrap();
let c = from_str_array(b);
```

string slices to string arrays can fail, so they return
`Option<str[N]>`; and because of this `try_as_str_array` lives in `std`.
The inverse, `from_str_array` only fails if `alloc` fails and lives in
`core`.

At this PR `string slices` are forbidden at `configurable`, `storage`,
`const`, and main arguments and returns. The reason for these
limitations is the internal structure of `string slices` having a `ptr`.

The optimized IR for initializing the slice is:

```
v0 = const string<3> "abc"
v1 = ptr_to_int v0 to u64, !2
v2 = get_local ptr { u64, u64 }, __anon_0, !2
v3 = const u64 0
v4 = get_elem_ptr v2, ptr u64, v3
store v1 to v4, !2

v5 = const u64 1
v6 = get_elem_ptr v2, ptr u64, v5
v7 = const u64 3
store v7 to v6, !2

v8 = get_local ptr slice, __anon_1, !2
mem_copy_bytes v8, v2, 16
```

## 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).
- [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.
2023-09-11 13:24:41 +00:00
SwayStar123
159a2402ab
Introduce the AssetId type (#4955)
## Description
Change AssetId from alias to struct. Allows implementing of methods.
Also moved the helper functions into the struct as methods.

## 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).
- [ ] 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 <54727135+bitzoic@users.noreply.github.com>
Co-authored-by: Braqzen <103777923+Braqzen@users.noreply.github.com>
2023-09-06 12:57:09 +00:00
Kaya Gökalp
92dc9f361a
Bump to v0.45.0 (#5026)
Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
2023-08-25 12:13:44 +03:00
Marcos Henrich
4f760014e7
Adds Hash trait and Hasher struct to std lib. (#4701)
## Description

Implements Hash trait for builtin types and for some std lib types.

This is required to have a proper hashing that does not change in case
the internal memory representation of type changes.

Current hashing functions are hashing the types in memory directly, as
this representation might change in future versions of the VM and
compiler we want to have a more reliable way of getting hashes that
won't change in future versions of the compiler and std lib.

Replaces use of sha256 function with Hasher in storage_map and
storage_vec.

Adds intrinsic __check_str_type.

__check_str_type returns an error at compile time in case the provided
generic type is not a str.

This is used to guarantee that `Hasher` `write_str` is not called with
non str types.

Closes #3835.

## 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).
- [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 <joshpbatty@gmail.com>
2023-08-21 10:15:29 +01:00
Kaya Gökalp
04a597093e
Bump to v0.44.1 (#4969)
## Description
Bumps version to v0.44.1, to get some of tooling hot-fixes released.
2023-08-16 22:23:39 +03:00
IGI-111
241d30f7cd
Bump to v0.44.0 (#4945) 2023-08-14 15:33:58 +02:00
Kaya Gökalp
254e50edb1
docs-ci: temporarily disable check-links for exact version usages in docs (#4948)
## Description
related to #4946.

Directly using `/latest` instead of exact versioning does not work for
some reason. To unblock bump PRs (one waiting, #4945) this PR disables
link checks at links which uses exact latest versions. We should find a
way to point to `latest` without using an exact version.
2023-08-14 12:10:08 +00:00
Green Baneling
e6f8925f98
An upgrade to fuel-core v0.20.3 (#4821)
This pull request is an upgrade to a `fuel-core 0.20.1` release.

The biggest changes coming from this new release is a rework of how
AssetId is structured to support [granular asset
minting](https://github.com/FuelLabs/fuel-specs/pull/491). This means a
contract can mint and burn multiple AssetId's, and the ContractId !=
AssetId.

---------

Co-authored-by: bitzoic <bitzoic.eth@gmail.com>
Co-authored-by: Cameron Carstens <54727135+bitzoic@users.noreply.github.com>
Co-authored-by: Sophie Dankel <47993817+sdankel@users.noreply.github.com>
Co-authored-by: Brandon Kite <brandonkite92@gmail.com>
Co-authored-by: Sophie <sophiedankel@gmail.com>
2023-08-14 11:19:40 +02:00
Sarah Schwartz
d9d9192875
add docs CI, rename docs README to index (#4874)
This PR includes a docs CI to prevent the docs-hub from breaking

You can find a README for the CI
[here](https://github.com/FuelLabs/github-actions/blob/master/docs-hub/README.md).

This replaces the previous markdown lint CI.
2023-08-09 10:51:10 -06:00
Mitchell Mackert
2bb072496d
chore: remove unused keys from examples under sway repo reference (#4854)
## Description

This PR removes unused keys in sway reference so that warnings do not
appear while searching for std after creating a new project with `forc
init`.

Closes #4709 
Co-authored-by: Kaya Gökalp <kaya.gokalp@fuel.sh>
2023-07-26 16:11:36 +00:00
Igor Rončević
cdf825a595
Give more helpful error when shadowing constants (#4819)
## Description

- Introduce three distinguished new error messages on const shadowing as
defined in #4587:
  - Variables shadowing constants.
  - Constants shadowing variables.
  - Constants shadowing constants, _item-style_ and _sequentially_.
- _Item-style_ and _sequential shadowing_ are added as an explicit
concept to the `TypeCheckContext`. This follows the same pattern that we
have in passing _ABI mode_ and _disallow functions_ contextual
information. Basically, the parts of the semantic analysis lower in the
chain need contextual information available and deducible in the upper
parts of the chain. In this case, the deduction is simple - as soon as
we are within a function body, we apply _sequential shadowing_. (Note
that this simple approach will not work if we ever introduce types local
to functions. But this is not an issues. In that case we will need to
redefine the const shadowing within functions in general, and also by
that time I assume we will have the graph collections which will allow
us different approach :-))
- Clean up of redundant error messages as defined in  #4799.

Closes #4587, #4799

## 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).
- [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.
- [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.
2023-07-19 12:10:57 +02:00
Braqzen
7d774907c7
Sway Ref: Style guide unused variables (#4660) 2023-07-17 22:41:38 +00:00
Braqzen
c54b5d0d5f
Sway Ref: Style Guide single use variables (#4664) 2023-07-13 12:15:31 +00:00
IGI-111
0d45b890c9
Fix style guide example not using local std (#4721)
## Description

Style guide example is blocking new releases because it tries to use an
unpublished version of std

## 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).
- [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.
2023-06-30 13:27:22 +10:00
Braqzen
91ce0f2fb5
Sway Ref: Assertions assert_eq (#4688) 2023-06-21 23:57:28 +02:00
Braqzen
a413feb5e5
Sway Ref: Style guide pattern matching (#4655) 2023-06-16 06:35:44 +00:00
Braqzen
4081e70073
Sway Ref: Style guide enum intro (#4653) 2023-06-15 08:34:31 +10:00
Braqzen
ac4ff14482
Sway Reference: Style Guide name convention (#4638) 2023-06-07 13:29:48 +00:00
Braqzen
6e7b971ba2
Sway Reference: Section 9 annotations: allow dead code (#4624) 2023-06-06 16:40:48 +00:00
Braqzen
336720aa16
Sway Reference: Section 9 Annotations inline (#4636) 2023-06-06 15:57:48 +01:00
Braqzen
6921e71390
Sway Reference: Section 9 Annotations: Testing (#4622) 2023-06-03 17:09:41 +01:00
Braqzen
6a9ba1e880
Sway Reference: Section 9 Annotations. Document the payable annotation (#4619) 2023-06-02 13:37:00 +00:00
Kaya Gökalp
8844b4f60e
ci-fix: use latest (sourced via path) version of std while testing sway reference examples (#4597)
## Description

closes #4595.
unblocks #4525.

Once #4596 is fixed we can remove std declarations in each manifest file
with a single patch statement in the workspace level manifest file. That
will also open the way to re-enable the disabled member (reentrancy) as
the example depends on sway-lib and sway-lib depends on latest released
std. So without the patch table it is not useable.

follow-up: #4596.
2023-05-30 07:40:13 +10:00
Braqzen
be7e73a790
Test reference examples under CI (#4583) 2023-05-25 12:36:42 +01:00
IGI-111
ab679d905d
Allow loops in predicates (#4526)
## Description
Since predicates are now gas-metered and allow backwards jumps, this
lifts the restriction so loops can be used in predicates. Fix #4521


## 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).
- [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.
2023-05-03 15:20:23 +02:00
IGI-111
6a3fba19d4
Add Option and Result variants in prelude (#4504)
## Description

This also handles ambiguities in cases where a lone ident is either a
variable or a variant; or a variable declaration or an enum scrutinee.

Fix #4480

## 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).
- [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.
2023-04-28 09:58:54 +00:00
Call Delegation
af5f536101
Added to msg_sender to prelude and removed from swayfiles (#4483)
## Description
Fixes #4478

## 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).
- [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: Kin Chan <calldelegation@calldelegation.local>
2023-04-27 17:05:37 -04:00
IGI-111
30f869a3a8
Implement OR match pattern (#4348)
## Description

Allow users to specify match patters such as:

```sway
let x = 1;
match x {
  0 | 1 => true,
  _ => false
}
```

We also check that all patterns in a disjunction declare the same set of
variables (not doing so is an error).

Fix https://github.com/FuelLabs/sway/issues/769

This requires a change in the pattern matching analysis to remove the
assumption that a specialized matrix of a vector pattern is always a
vector, which is now no longer true because or patterns can generate
multiple branches and therefore multiple rows.

## 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).
- [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 <joshpbatty@gmail.com>
2023-04-18 22:08:17 +00:00
Mohammad Yasir
800089eb62
Updated the sway.js file by adding mod and type in keyword (#4427)
## Description
fixes #4424 
closes #4424 


## 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).
- [ ] 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: Joshua Batty <joshpbatty@gmail.com>
2023-04-12 22:39:45 +00:00
João Matos
92ecf7f4a3
Implement associated consts for traits (#4301)
## Description

This is the last step to getting associated consts to work.

First it refactors the existing const support to use a newly-introduced
`ConstantExpression` instead of relying on `VariableExpression` like
we've been doing previously.

Then it adds IR generation for associated constants and enables the
relevant tests.

[Add ConstantExpression and IR generation
support.](3d2e62598f)

[Update ConstantExpression constant before IR
generation.](4ec7ebe807)

[Add IR generation for associated
consts.](67386a874e)

Then there also some commits adding some new documentation:

[Add traits documentation to the
reference.](afda9026a0)

[Add some documentation about associated consts to the
book.](6845ddc166)

Closes https://github.com/FuelLabs/sway/issues/3797.

## 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).
- [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.
2023-04-05 13:20:17 +00:00
AndrésF
63382365bd
doc: Adding annotations support on highlight.js (#4186)
## Description

resolves #2740
closes #4157

> In order to give a better highlighting I picked the "meta" scope as
seen in [highlight.js
docs](https://highlightjs.readthedocs.io/en/latest/css-classes-reference.html)
> 
> The ["Mode"
](https://highlightjs.readthedocs.io/en/latest/language-guide.html) I've
added provides the html class: _hljs-meta_ however, the result is a
white color for annotation (like plain text) and not a blue color as
requested in the issue.
> 
> The solution I've found for this is to add an additional css file to
specify custom behavior for the desired class. I haven't added this file
because I think it's out of scope.


## 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).
- [ ?] 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: Mohammad Fawaz <mohammadfawaz89@gmail.com>
2023-03-10 15:24:50 +00:00
IGI-111
0eaa3a6da9
Standardized module structure (#4232)
## Description

Implement RFC 0006, fix #4191.

Remove the `dep` reseved keyword in favor of `mod`.

Change path resolution for submodules to use an adjascent file at the
root and a folder named after the current module in other cases.

Remove the need for an argument to `library`, the LSP now points to
empty spans at the top of module files.

The library names are now determined by the file name, or the package
name at the top level.

## 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).
- [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.
2023-03-09 19:14:52 +11:00
Riley
b80283f131
Remove reentrancy library (#3698)
## Overview

Closes #3217, migrates the reentrancy library to Sway-libs.

> Notice: Awaiting merge and version bump from Sway-libs ([relevant pull
request](https://github.com/FuelLabs/sway-libs/pull/70)).

## Changes

- remove reentrancy library
- remove reentrancy tests
- modifies documentation to point to new reentrancy guard url
- modifies imports of `std::reentrancy::` to `sway_libs::reentrancy`

---------

Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
2023-02-07 09:15:29 -05:00
Braqzen
4131a16927
Add logging::log & assert::assert_eq to prelude.sw (#3916) 2023-01-29 14:28:06 -07:00
Mohammad Fawaz
0a3beae92e
Update std::storage::get and std::storage::StorageMap::get to return an Option (#3862)
API change:
Previous:
```rust
pub fn get<T>(key: b256) -> T;
pub fn get(self, key: K) -> V;
```
Now:
```rust
pub fn get<T>(key: b256) -> Option<T>;
pub fn get(self, key: K) -> Option<V>;
```
`Option::None` indicates that the storage slot requested in not
available.

- Updated all storage intrinsics to return a `bool` except for
`__state_load_word` because it already returns the value loaded. The
`bool` returned represents the previous state of the storage slots as
described in the specs. I was not able to update `__state_load_word` to
return both the value loaded and the `bool` unfortunately because I hit
various hiccups along the way, particularly in IR/codegen.
- Updated `get` and `StorageMap::get` in the standard library to return
an `Option` based on the Boolean mentioned above. In the copy type case,
I had to use `asm` blocks instead of `__state_load_word` until we're
able to return a struct from an intrinsic.
- I did not update `store` and `StorageMap::insert` to return an
`Option`, for now, because that would involve an extra storage read to
return the previous value, if available. We can think about whether this
is worth it at some point.
- I added `unwrap` and `unwrap_or` where it makes sense in `StoargeVec`.
- Update various docs, examples, and tests.

Closes https://github.com/FuelLabs/sway/issues/3318

I will open separate issues for updating `__state_load_word` and
thinking about making `store` and `insert` also return an `Option`.
2023-01-24 14:00:17 +00:00
Ali Jafri
41af03e888
Update in "Example 2" (#3652) 2022-12-24 22:50:48 +00:00
Alex Hansen
8101a646a6
Add introductory sentence to the sway reference (#3548) 2022-12-09 00:43:06 +00:00
Braqzen
ec90af2cfb
Introduce the Sway reference, a more technical Sway source of documentation. (#2562)
## Summary

More information can be found in #2443 .

Notes for PR:

- The review is meant to take place using `mdbook` so check out the main
`README.md` to see how to run it in the browser
- This book is incomplete however there are a lot of pages so the idea
is to PR this into the repo soon so that subsequent reviews are smaller
- No CI added until the book is ready to replace the current book
- Ideally reviews are split between multiple people because there is a
lot of content

Closes #2443 

## Pages Reviewed
@matt-user : Program Types -> Functions, Misc

Co-authored-by: Bikem <bengisuozaydin@gmail.com>
Co-authored-by: Matt <54373384+matt-user@users.noreply.github.com>
Co-authored-by: Cameron Carstens <54727135+bitzoic@users.noreply.github.com>
Co-authored-by: bing <bingcicle@proton.me>
Co-authored-by: Mitch Martin <111294749+mitch-fuel@users.noreply.github.com>
Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Co-authored-by: Luiz Felipe Bolsoni Gomes <8636507+LuizAsFight@users.noreply.github.com>
Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
Co-authored-by: Alex Hansen <alex@alex-hansen.com>
2022-12-08 21:53:09 +00:00