Commit graph

58 commits

Author SHA1 Message Date
Daniel Frederico Lins Leite
943c67cb1a
u256 more operations (#4904)
## Description

This PR is part of https://github.com/FuelLabs/sway/pull/4794 and
implements more operators for u256:
- [x] sub
- [x] mul
- [x] div
- [x] comparisions
- [x] mod

## 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: Vaivaswatha N <vaivaswatha@users.noreply.github.com>
2023-08-07 10:35:06 +02:00
Daniel Frederico Lins Leite
390b945e65
u256 add operator (#4896)
## Description

This PR is part of https://github.com/FuelLabs/sway/pull/4794. It
implements support for u256 add operators.

The IR generated is a vanilla `add` operator as any other integer.

```rust
script {
    entry fn main() -> u256, !1 {
        entry():
        v0 = const u256 0x0000000000000000000000000000000000000000000000000000000000000001, !2
        v1 = const u256 0x0000000000000000000000000000000000000000000000000000000000000002, !3
        v2 = call add_0(v0, v1), !4
        ret u256 v2
    }
}
```

This will then be transformed by `miscdemotion` pass to something like:

```rust
script {
    entry fn main() -> u256 {
         local u256 __wide_lhs
         local mut u256 __wide_result
         local u256 __wide_rhs
 
        entry():       
         v0 = get_local ptr u256, __wide_lhs, !0
         v1 = const u256 0x0000000000000000000000000000000000000000000000000000000000000001, !0
         store v1 to v0, !0
         v2 = get_local ptr u256, __wide_rhs, !0
         v3 = const u256 0x0000000000000000000000000000000000000000000000000000000000000002, !0
         store v3 to v2, !0
         v4 = get_local ptr u256, __wide_result, !0
         wide add v0, v2 to v4, !0
         v5 = load v4, !0
         ret u256 v5
    }
}
```

Mind the `wide add` here. It is a Fuel specific operation, giving space
to other targets to implement this differently.


## 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-08-02 21:47:26 +00:00
Cameron Carstens
660abd9acd
Use the Self annotation in implementation blocks (#4871)
## Description

In Rust, when implementing traits the type `Self` is used instead of the
concrete type. This provides a more flexible and generic API design and
it promotes code reusability and simplifies the creation of fluent
interfaces. The same has now been done for Sway.

## 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).
- [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: bitzoic <bitzoic.eth@gmail.com>
2023-07-26 15:02:07 +00:00
IGI-111
303a7a3acf
Numeric type inference (#4829)
## Description

This does two things.

1. Integer types no longer automatically cast to each other. Methods
such as `as_u64` and `try_as_u8` have been introduced to require the
user to explicit the cast.
Warnings about casts losing precision have been removed as they are no
longer possible.

2. Numeric literals (such as `32` without further qualifications) no
longer immediately decay to a `u64` and we instead attempt to type them
as `TypeInfo::Numeric` as far as possible to allow their usage with any
integer type.
Numeric intrinsics unify arguments with the numeric type to enforce
their constraints.
Numeric values are forced to decay to `u64` when resolving methods or
traits.

This change is a prerequisite to move our representation of
sub-byte-sized values to a different memory representation, as numeric
types need to be distinct if we are ever to treat them differently.

Fixes #3470

## 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-07-21 04:37:13 +02:00
SwayStar123
e184eb9737
Fix weird formatting (#4785)
## Description
Looked for the le function for a while being realizing the weird
formatting was hiding it. The newline for the impls are also unnecessary

## 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.
- [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).
- [ ] I have requested a review from the relevant team or maintainers.
2023-07-12 13:18:43 +02:00
Daniel Frederico Lins Leite
bbb89d1dec
Fix: Constant initializer expressions cannot use bitwise not (#4739)
## Description

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

This PR fixes the problem when initializing a constant unsigned integer
using "bitwise not". Main issue was that at `ops.sw` the bitwise
operator was implemented using `AsmFunctions`, which are ignored in
const eval.

I have created a new `__not` intrinsic which now works (almost) as
expected. We still have two issues surrounding this "const eval":

1 - All integers are `u64`, so inside the `const eval`, we do not have
enough context to correct do the bitwise. That is why `Not` for u8, u16
and u32 do a `__and` afterwards;

2 - `bool` is not using the new intrinsic, so it does not support
`bool`.

## 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: IGI-111 <igi-111@protonmail.com>
2023-07-05 18:00:29 +02:00
Anton Trunov
0263c0d638
Handle integer overflow/underflow for non-64-bit integers (#4707)
fixes #4646

## Description

This PR fixes the linked issue by introducing overflow/underflow checks
in the `Add`, `Mul`, `Sub` stdlib traits implementations for the `u8`,
`u16` and `u32` types.

## 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-29 03:49:25 +01:00
João Matos
52ac9b69d3
Cleanup stdlib warnings (#4648)
## Description

This PR cleans up stdlib warnings that have accumulated.

## 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-13 02:09:23 +02:00
SwayStar123
e60f54f106
Byte conversion for primitives (#4630)
## Description
closes #4626 

## 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).
- [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.

---------

Co-authored-by: Marcos Henrich <marcoshenrich@gmail.com>
2023-06-06 16:51:28 +01:00
IGI-111
29b1d0c47b
Use intrinsics for base integer operations (#4553)
## Description
This change introduces `__mod`, `__shl` and `__shr` intrinsics for use
with integer operations to remove the asm block based implementations in
core and enable us to support const evaluation of integer expressions in
most cases, notably when specifying values like `1 << 32`.

## 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-16 19:16:14 +02:00
IGI-111
97f3c5f723
Introduce unique field_id in StorageKey (#4537)
## Description

This adds a field to `StorageKey` that is unique among storage fields.
It aims to alleviate the problem created by having multiple zero sized
fields inside of a storage struct, which are, by necessity, living in
the same slot.

It allows `StorageMap` (which itself is zero-sized) to address its own
values on a unique address map instead of colliding with adjacent
`StorageMap`s.

This is a breaking change because it will generate different IR
for the same code.

Fixes https://github.com/FuelLabs/sway/issues/4524
Fixes https://github.com/FuelLabs/sway/issues/4523


## 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: Sophie Dankel <47993817+sdankel@users.noreply.github.com>
2023-05-05 19:32:41 +01:00
IGI-111
1ecc5e79d6
Implement module privacy rules (#4474)
## Description

This change mainly adds checks to enforce the new module privacy rules
and supporting changes for it.

Changes include updating std and core to use
public modules, updating the parser to allow the use of the `pub mod`
syntax and adding an error type for private modules.

This change is implemented behind a `--experimental-private-modules`
experimental flag and not enabled by default.

It implements part of https://github.com/FuelLabs/sway/issues/4446, the
`pub use` syntax is yet to be implemented.

## 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-28 10:30:53 +01:00
Mohammad Fawaz
1db8385158
Replace old storage API with the new one explained in the StorageKey RFC (#4464)
## Description
Implement https://github.com/FuelLabs/sway-rfcs/pull/23

Closes https://github.com/FuelLabs/sway/issues/3419 (technically via the
RFC)
Closes https://github.com/FuelLabs/sway/issues/3202
Closes https://github.com/FuelLabs/sway/issues/3043
Closes https://github.com/FuelLabs/sway/issues/2639
Closes https://github.com/FuelLabs/sway/issues/2465
Closes https://github.com/FuelLabs/sway/issues/4304

This is a big one but mostly removes stuff. It's hard to review but the
summary of changes below should be sufficient. Most of the changes here
are things that I just had to do to make CI pass.

- Removed the `--experimental-storage` flag and made its
[behavior](https://github.com/FuelLabs/sway/pull/4297) the default. Also
removed everything that was required for the previous storage APIs in
favour of the new ones.
- Break down the `std::storage` into multiple submodules:
 - `storage_key` implements the API for `std::core::StorageKey`
- `storage_api` implements the free functions `read` (previously `get`),
`write` (previously `store`), and `clear`.
- 4 more modules for the dynamic storage types which now use the new
`StorageKey` API.
- `#[storage(write)]` now allows reading from storage as well. This is
needed because the way we pack structs in storage now requires that we
sometimes read from storage first if we were to write a portion of a
slot.
- Removed the "storage only types" checks and the corresponding tests.
- Removed the `__get_storage_key` intrinsic and the `get_storage_key` IR
instruction and all corresponding tests. Also removed the `state_index`
metadata as it is no longer required.
- Added tests and example to showcase nested storage maps and nested
storage vectors.

## 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-21 07:36:40 -04:00
Mohammad Fawaz
9685fab224
Experimental: StorageKey feature (#4297)
## Description
This PR introduces `StorageHandle` to the `core` as an **experimental**
feature. `StorageHandle` is a descriptor of a location in storage
containing two fields:
1. A key pointing to a storage slot `s`.
2. An offset pointing to a particular word in `s` or in its subsequent
slots. This offset is new and is not described in the RFC. I will
rectify that shortly.
The standard library introduces helper methods `read`, `try_read`, and
`write` to `StorageHandle` that allow reading from and writing to the
location pointed to by the handle. `try_read` returns an `Option`
wrapping the data while `read` automatically internally unwraps the data
and could panic.

The summary of this change is as follows:
* New struct `core::experimental::storage::StorageHandle`
* New storage library `std::experimental::storage::*` that should
eventually replace `std::storage::*`. This new library mirrors the old
one to the extent possible and introduces the helper methods for
`StorageHandle`. Other data structures such as `StorageVec` and
`StorageBytes` will be introduced in a separate PR.
* Add an experimental flag `--experimental-storage` to `forc` that
enables all the required code in the compiler to support `StorageHandle`
as described in the https://github.com/FuelLabs/sway-rfcs/pull/23.
* Type checking phases assumes the existence of `StorageHandle` and
monomorphizes it as needed.
* Storage accesses now always return `StorageHandle` and storage
reassignment are no longer relevant.
* IR gen handles storage accesses by "filling" the key and the offset in
an aggregate representing the `StorageHandle`. The key and the offset
are statically determined based on the index of the storage variable in
the `storage` block and the field accessed, if applicable.
  * Storage initializers are now handled based on the new model
* The new approach packs struct fields by default but does not pack
across storage variables. This offers the most amount of flexibility.
This is a deviation from the RFC which I will update shortly.
* To implement `StorageMap` and other dynamic storage data structures,
we now write `impl StorageHandle<StorageMap<K, V>>` instead of `impl
StorageMap<K, V>` directly. Also, note that the `get` method now returns
a `StorageHandle` instead of the actual data. To get the actual data,
`read()` or `try_read()` should be called on the resulting handle. This
is needed for multiple reasons including proper support for nested
dynamic storage types. Rust also does the same, in a way (where `get`
actually returns ` &` which happens to coerce to the real data in
certain places).
* I added various tests but they're not comprehensive. Some tests on my
list:
  * Extensive tests for storage maps in structs (which now work!)
* Extensive tests for storage accesses with various types and struct
layouts

I still need to figure out how to do nested dynamic storage types with
this approach. The stuff I have in `map_in_map_access` in
`test_projects/experimental_storage/src/main.sw` is just an attempt but
not ergonomic at all of course.

## Checklist

- [ ] 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.
- [ ] 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).
- [ ] I have requested a review from the relevant team or maintainers.
2023-04-13 00:24:09 +00:00
Marcos Henrich
dcb7917e9b
Implements DCA warnings for function parameters. (#4406)
## Description

With these changes we will have DCA warnings for function parameters.

This PR also fixes tests that have unused function parameters.

## Checklist

- [ ] 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: Mohammad Fawaz <mohammadfawaz89@gmail.com>
2023-04-12 14:53:21 +00:00
Toby Hutton
d6238aef53
IR explicit pointer refactor. (#4336) 2023-04-02 21:30:34 +00:00
Cameron Carstens
55a4fee462
Introduce the StorableSlice trait to the std library (#4302)
## Description

This PR introduces a way more generalized way to store heap types into
storage as discussed
[here](https://github.com/FuelLabs/sway/issues/4013) with the
`StorableSlice` trait. The current implementation of the `store` and
`get` functions do not support heap types. With the use of `raw_slice`
any heap types may now be stored.

This PR unlocks https://github.com/FuelLabs/sway-libs/issues/40 which is
required for a number of other issues.

With the introduction of the `StorableSlice` trait, the `StorageBytes`
have been refactored to use it.

This trait could be used to convert a `Vec` to a `StorageVec` in an
efficient manner in another PR. While `Vec` already has the ability to
convert to a `raw_slice`, `Storage_Vec` stores each element using a
unique key created by hashing the index of that element i.e.
sha256(index) in a more linked-list like fashion. This would need to be
changed if this functionality is desired.

**NOTE:** A `from_raw_slice` function has been added to the `Bytes` type
to enable the refactor. This should be removed and replaced once
https://github.com/FuelLabs/sway/pull/3882 is resolved and also uses the
`len_bytes` function introduced in that PR.

## 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: bitzoic <cameron.carstens@fuel.sh>
2023-03-20 13:46:52 -04:00
Vaivaswatha N
b48cabd14e
More intrinsics and const_eval (#4260)
## Description
New intrinsics and constant evaluations supported by this PR can be seen
in the additions to
[const_inits](https://github.com/FuelLabs/sway/compare/vaivaswatha/const_eval_407?expand=1#diff-5886ff784ee10cb93be850eba9966c80d539bf6c08eaeb7d286183494690c610)
test.

 Closes #407.

## 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-10 16:12:54 +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
Nick Furfaro
7074632c2c
Rename Shiftable trait (#3901)
Closes #3897
2023-01-27 17:21:29 +00:00
Nick Furfaro
46f92f280b
Format sway-lib-core and sway-lib-std (#3902)
This just applies formatting by `forc fmt`.
2023-01-26 21:54:19 +00:00
Emily Herbert
0ab1d22ca5
Add ops::* to the prelude in core (#3892)
This PR fixes the bug described in #3891 by adding `ops` to the prelude
in `core`. This change is congruent with Rust's approach as well.

This is a _breaking change_---if any users have any traits manually
implemented for which core already has an existing implementation, this
will now create an error. A rightful one, IMO, but a breaking change
nonetheless.

A few of the tests were written in a way that is not compatible with
this change---so this PR rewrites these. The only one of note is that a
old `should_pass/language/trait_override_bug` test was refactored to a
new `should_fail/redefine_method_from_core` test.

This PR also refactors the trait finding algorithm in
`check_if_trait_constraints_are_satisfied_for_type` to reduce it from
O(n^2) to O(n).

Closes #3891

Co-authored-by: emilyaherbert <emily.herbert@fuel.sh>
2023-01-26 21:19:53 +00:00
Marcos Henrich
f14d6ef1c9
Adds docstring to core::never::Never (#3706)
Copies the documentation from
https://doc.rust-lang.org/std/primitive.never.html.
Copies the link above to the possible extend. Rust has more use cases to
the Never type than Sway currently has, so the sections were not
included in the
docstring as they are not applicable to the Sway Never type.

Co-authored-by: João Matos <joao@tritao.eu>
2023-01-10 05:32:56 -05:00
Marcos Henrich
2faff70d3d
Introduces Never enum to lib-std. (#3362)
Re-enables test diverge_in_op_not.

In rust
```
let b = {
    return 5;
}[0];
```
Gives error: `cannot index into a value of type !`
While:
```
let b = {
    return 5;
    [true, false]
}[0];
```
No longer gives an error.
This shows us that rust compiler uses the implicit return of a block
that deterministically aborts to compile the rest of the code.

The behavior of `TyCodeBlock::type_check` was changed to as rust do and
apply the implicit return when possible even when block
deterministically aborts. When a block deterministically aborts and no
implicit return is used then the block return type is assigned to Never
enum.

Now similar to rust the following code:
```
let b = {
    return 5;
}[0];
```
Gives the error: `No method named "index" found for type "Never".`

Closes #2881
Closes #3061

Adds changes done by @emilyaherbert.
Empty match arms are ok for types that have no valid constructor.
[2b52de9](2b52de98fd)

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

Co-authored-by: emilyaherbert <emily.herbert@fuel.sh>
2022-12-27 15:45:34 -05:00
JT
9084c32d86
Jtriley eth/bitwise trait impls (#3589)
Draft PR for bitwise impls.

Progress: Created boilerplate for new impls and tests.

Resolves #3456

Co-authored-by: Toby Hutton <toby@grusly.com>
2022-12-15 13:42:19 +00:00
Nick Furfaro
71a33f95cf
New Bytes type module for the stdlib (#3454)
Adds a new heap type `Bytes` to the stdlib which holds an arbitrary
number of packed bytes.
2022-12-10 07:06:24 -05:00
Nick Furfaro
c471e825e8
Rename core::num to core::primitives (#3488)
The current `core::num` name is not well aligned with types like `b256`
which are not numeric types.
In addition, in Rust, methods like `min(), `max()` and `bits()` are in
the `primitives` module.
Closes #2777
2022-12-02 01:56:58 +00:00
Nick Furfaro
671e9252be
Simplify compose and decompose (#3336)
Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
2022-11-11 06:38:18 +11:00
JC
704b58d0c9
Kinda allow returning Vecs from scripts (#3288)
This PR is a minimal cut of https://github.com/FuelLabs/sway/pull/3251
that just implements the bare minimum required to fix
https://github.com/FuelLabs/fuels-ts/issues/467:
- A new type `raw_slice` and support for returning it from scripts.
- Support for conversion between `raw_slice` and `Vec`.

```rs
script;

fn main() -> raw_slice {
    // Create a vec
    let vec = Vec::new();
    vec.push(42);
    vec.push(1337);

    // Return it
    vec.as_slice()
}
```

Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
2022-11-07 22:47:53 +11:00
Mohammad Fawaz
b83ad9bd85
Disallow assigning to initialized registers in asm blocks (#3239)
Closes https://github.com/FuelLabs/sway/issues/3155

This is needed due to `mem2reg` and some unclear semantics around
initialized registers being passed as ref/mut ref. Besides, it's a good
idea to restrict `asm` blocks as much as possible to avoid weird
unwanted behavior.

<img width="883" alt="image"
src="https://user-images.githubusercontent.com/59666792/199332120-974d1821-680e-43a1-922d-1a32d491b3cd.png">

* I also removed the `disallow_opcodes` check because the parser takes
care of that now. The parser only allows legal opcodes in `asm` blocks.
* Finally, I update the parser to accept the correct storage opcodes
with `fuel-core 0.13` and updates all the tests.

Co-authored-by: Vaivaswatha N <vaivaswatha@users.noreply.github.com>
2022-11-02 16:23:39 +00:00
JC
81f7f571b7
Add intrinsics for operating on raw_ptrs (#3157)
This PR:
- Adds `__ptr_add<T>(ptr: raw_ptr, count: u64)` (which basically does
`ptr + __size_of::<T>() * count`) and its sub counterpart to be used in
pointer offset calculations.
- Makes `std::alloc::*` and `raw_ptr::*` have type arguments and work in
increments of `size_of::<T>()` instead of bytes.
- Removes test `fixing_generic_type` which was broken instead of
maintaining it. This test was created to reproduce an issue I was having
a few months ago. Fixing this allowed other things to be built so there
are other tests that touch this case.
2022-11-02 20:31:38 +05:30
Mazdak Farrokhzad
cd100147ab
Eliminate need for ~ in ~Foo::bar() (#3218)
Fixes https://github.com/FuelLabs/sway/issues/1436.

This PR first removes the need for `~` in `~Foo::bar` by first delaying
the interpretation of `Foo::Bar` (associated call, free function call,
or enum variant construction?) until type checking. This is achieved
with `ExprKind::AmbiguousPathExpression`. During type checking, we ask
whether `Foo` (and any prefixes) is a module, or an enum. If it's
neither, we attempt type checking as an associated function call.
Otherwise, we continue as if we had a `ExpressionKind::DelineatedPath`.

Once `~` is made redundant, it's then also removed from the language and
so also from tests, examples, and the standard library.

Co-authored-by: Alex Hansen <alex@alex-hansen.com>
2022-10-31 19:45:38 +00:00
Emily Herbert
a970054f02
Detect and disallow multiple methods with the same name. (#3207) 2022-10-30 20:00:29 -04:00
JC
467e22c9c0
Remove raw_ptr::addr() (#3132)
With this change, `core` and `std` should stop providing a convenient
way to access pointers as `u64`s.

While this might seem limiting, with `alloc()` returning `raw_ptr`s that
have `add()` and `sub()`, all usages of `.addr()` could be refactored.
(If not there's still `asm(r1: ptr) { r1: u64 }` of course.)

Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
Co-authored-by: Toby Hutton <toby@grusly.com>
2022-10-27 13:17:46 +00:00
JC
13ffbc3328
Add raw_ptr type (#2828)
This PR:
- Introduces the `raw_ptr` type.
- Changes RawVec's `ptr` field from `u64` to `raw_ptr`.
- Disallows returning `raw_ptr`s (and aggregate types that contain it)
from being returned from `main()` fns.


![image](https://user-images.githubusercontent.com/412180/195128463-1809a7f4-3964-419c-9eac-03df799dedc3.png)

---

Note to @sezna:

On our meeting we discussed `checks.rs`, which led me to this
implementation:
090a4d1a4d

That didn't work because `raw_ptr`s are converted to
`sway_ir::irtype::Type::Uint(64)`s so there wasn't a way to
differentiate them. I've reverted that and went with this instead:
fc6bf34084

Co-authored-by: Andrew Cann <shum@canndrew.org>
2022-10-25 12:34:11 +11:00
rostyslavtyshko
2503164e79
Bitwise not for all unsigned integers (#2909)
Co-authored-by: tyshkor <tyshko1@gmail.com>
Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
Co-authored-by: Simon <46566889+simonr0204@users.noreply.github.com>
2022-10-21 18:44:49 +00:00
Vaivaswatha N
c3d3273a7c
Add intrinsics for add, sub, mul and div (#2771) 2022-09-14 16:43:43 +05:30
Vaivaswatha N
8f5d88c5fd
Fold CMP and CBR instructions (#2615) 2022-08-24 12:05:20 -07:00
Kaya Gökalp
8673f73a20
Additional checks for unnecessary visibility qualifier (ABI definitions, ABI impls and trait impls) (#2432) 2022-08-01 02:11:16 +00:00
Nick Furfaro
8011c65362
Bugfix in rsh method for b256 (#2316)
* fix: fix bug in rsh implementation for b256

* test: add test to prevent regression
2022-07-14 16:40:38 +00:00
Mohammad Fawaz
4ba140a4fb
Correctly display DCA warnings and fix some incorrect warnings that showed up (#2230) 2022-07-06 10:45:59 +00:00
Vaivaswatha N
753639c1a6
Introduce __eq intrinsic (#2100)
* Introduce `__eq` intrinsic

* Lower to `Instruction::Cmp` instead of to assembly in IRGen

* Refactor intrinsics to all have arg and type arg vectors
2022-06-29 16:04:39 +00:00
Emily Herbert
cb04fa624c
Prevent unconstrained type parameters in impl blocks (#1942)
* Do not rely on TypeMapping when type checking declarations.

* Prevent leaking types in impls.

* Prevent unconstrained type parameters.

* clippy
2022-06-15 15:20:40 +00:00
Emily Herbert
0ecf1ec3c1
Prevent leaking types in impl blocks (#1941)
* Do not rely on TypeMapping when type checking declarations.

* Prevent leaking types in impls.

* clippy
2022-06-14 14:58:27 +00:00
Nick Furfaro
f1632e5437
Furnic/revert b256 add (#1961)
* feat: Implement Add for b256

* modify: make U128 functions public

* feat: add b256_ops module to stdlib

* docs: add comments

* reorganize: move b256_ops tests to e2e harness

* test: stuff

* refactor: cleanup unneeded mut variables

* feat: add core-utils lib

* fixup

* refactor: use new lib for compostion

* chore: fix manifest project name

* refactor: rename module to compose

* cleanup

* refactor: rename to core_utils

* fixup

* chore: rename new lib and update manifests

* test: fix last assert

* docs: add module level docs

* fixup

* chore: remove and rebuild for lock files

* fixup

* test: cleanup

* chore: rebuild basic_predicate

* chore: rebuild

* fix: restore test project

* fix: add oracle file

* chore: rebuild sdk-harness tests

* Revert "Implement Add for b256 (#1707)"

This reverts commit 560ca4b4c6.

* chore: rebuild lockfiles
2022-06-13 16:05:16 -05:00
Nick Furfaro
560ca4b4c6
Implement Add for b256 (#1707)
* feat: Implement Add for b256

* modify: make U128 functions public

* feat: add b256_ops module to stdlib

* docs: add comments

* reorganize: move b256_ops tests to e2e harness

* test: stuff

* refactor: cleanup unneeded mut variables

* feat: add core-utils lib

* fixup

* refactor: use new lib for compostion

* chore: fix manifest project name

* refactor: rename module to compose

* cleanup

* refactor: rename to core_utils

* fixup

* chore: rename new lib and update manifests

* test: fix last assert

* docs: add module level docs

* fixup

* chore: remove and rebuild for lock files

* fixup

* test: cleanup

* chore: rebuild basic_predicate

* chore: rebuild

* fix: restore test project

* fix: add oracle file

* chore: rebuild sdk-harness tests
2022-06-10 18:24:27 -07:00
Nick Furfaro
fe5753db99
Implement left & right shift for b256 (#1549)
* feat: add left and right shift for b256

* style: fmt

* fix: rename helper functions

* merge cleanup

* fix: update type of other to u64

* test: add first shifting assertions

* fix: cleanup & refactor shifting functions

* test: get initial tests passing

* feat: draft impl add for b256

* test: add first test for impl add for b256

* rename functions

* test: cleanup

* bugfix: WIP

* progress

* refactor: add loop version of lsh

* fix: use ifs for now until vec lands

* test: fix tests

* cleanup

* cleanup: rsh_with_carry

* feat: impl right shift

* test: add tests for right shift

* cleanup

* fmt

* cleanup: remove unused const FLAG

* refactor: remove asm from shifting funcs
2022-05-26 11:14:36 -07:00
John Adler
9cb14b5996
Fix Shiftable trait to shift by u64. (#1604) 2022-05-19 07:19:25 -04:00
Nick Furfaro
c000ccf915
Cleanup constants.sw & add impl b256 to core/num (#1599)
* cleanup: remove some consts from constants.sw

* feat: add impl b256 to num module

* test: fix all usage of ZERO const in tests

* fix: update example project to use new b256::min()

* style: fmt

* fix: fix comments

* style: fmt examples

* Revert "fix: fix comments"

This reverts commit 65e7176a4e.

* docs: fix comment for b256::max()

* fix: update intrinsics

* fmt
2022-05-18 20:01:27 -07:00
Nick Furfaro
e89bb84b12
Remove compiler warnings (#1576)
* fix: rename functions in snake_case

* test: add tests for numeric constants

* style: fmt
2022-05-17 15:17:52 -07:00