FuelLabs / sway

🌴 Empowering everyone to build reliable and efficient smart contracts.
https://docs.fuel.network/docs/sway/
Apache License 2.0
62.58k stars 5.37k forks source link

Functions implemented through traits cannot be used to instantiate constants #6746

Open camden-smallwood opened 4 days ago

camden-smallwood commented 4 days ago

Related Component

compiler

Problem

When instantiating a constant, if the value expression contains any function calls that are not implemented directly on a type, but through a trait instead, then the code will fail to compile. This is a big road block for translating Solidity code using Charcoal.


Working:

contract;

use sway_libs::signed_integers::common::*;
use sway_libs::signed_integers::i256::*;

const VALUE: I256 = I256::from_uint(12345);
image

Working:

contract;

use sway_libs::signed_integers::common::*;
use sway_libs::signed_integers::i256::*;

const VALUE: u256 = I256::from_uint(12345).underlying();
image

NOT Working:

contract;

use sway_libs::signed_integers::common::*;
use sway_libs::signed_integers::i256::*;

const VALUE: I256 = I256::from_uint(12345).wrapping_neg();
image

Steps

The non-working example above uses the I256 struct and WrappingNeg trait from the sway_libs::signed_integers library.

  1. Create a Forc project.
  2. Create a constant using a function that is implemented through a trait.
  3. Compile the project.

Possible Solution(s)

I'm not sure if trait calls are viewed differently than regular function calls, but they probably shouldn't be.

Notes

No response

Installed components

camden@Camdens-MacBook-Air sway-test % fuelup check
latest - Up to date
fuelup - Up to date
camden@Camdens-MacBook-Air sway-test % fuelup show
Default host: aarch64-apple-darwin
fuelup home: /Users/camden/.fuelup

Installed toolchains
--------------------
latest-aarch64-apple-darwin (default)

active toolchain
----------------
latest-aarch64-apple-darwin (default)
  forc : 0.66.4
    - forc-client
      - forc-deploy : 0.66.4
      - forc-run : 0.66.4
    - forc-crypto : 0.66.4
    - forc-debug : 0.66.4
    - forc-doc : 0.66.4
    - forc-fmt : 0.66.4
    - forc-lsp : 0.66.4
    - forc-tx : 0.66.4
    - forc-wallet : 0.11.1
  fuel-core : 0.40.0
  fuel-core-keygen : 0.40.0

fuels versions
--------------
forc : 0.66.9
forc-wallet : 0.66.9