Skip to content

Sway hex literals have their suffix counted as digits #6727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
camden-smallwood opened this issue Nov 16, 2024 · 3 comments · May be fixed by #7042
Open

Sway hex literals have their suffix counted as digits #6727

camden-smallwood opened this issue Nov 16, 2024 · 3 comments · May be fixed by #7042
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen good first issue Good for newcomers ODHack12 team:compiler Compiler Team

Comments

@camden-smallwood
Copy link
Contributor

camden-smallwood commented Nov 16, 2024

Related Component

compiler

Problem

As the title states, hex literals have their optional suffix counted as part of their digits. This causes the following error, and related errors for types aside from b256:

  --> /Users/camden/Source/charcoal/out/ds_auth/src/main.sw:69:75
   |
67 |     } else if src == storage.owner.read() {
68 |         return true;
69 |     } else if storage.authority.read() == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000000b256)) {
   |                                                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ hex literals must have 1..16 or 64 digits
70 |         return false;
71 |     } else {

If we count the zeroes, logically we can deduce that there is in fact exactly 64 digits in the following:

0x0000000000000000000000000000000000000000000000000000000000000000b256

But there is no attempt made to strip the b256 before counting.

Steps

  1. Create a sway project.
  2. Create a b256 variable using the 0x0000000000000000000000000000000000000000000000000000000000000000b256 constant.
  3. Run forc build

Possible Solution(s)

Maybe strip the suffix before this point:

if let Some(hex_digits) = orig_str.strip_prefix("0x") {
let num_digits = hex_digits.chars().filter(|c| *c != '_').count();
match num_digits {
1..=16 => Literal::Numeric(u64::try_from(parsed).unwrap()),
64 => {
let bytes = parsed.to_bytes_be();
let mut full_bytes = [0u8; 32];
full_bytes[(32 - bytes.len())..].copy_from_slice(&bytes);
Literal::B256(full_bytes)
}
_ => {
let error = ConvertParseTreeError::HexLiteralLength { span };
return Err(handler.emit_err(error.into()));
}
}

Notes

Should be an easy fix, but it should be done ASAP.

Installed components

camden@Camdens-MacBook-Air sway % fuelup check
latest - Up to date
fuelup - Up to date
camden@Camdens-MacBook-Air sway % 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
@camden-smallwood camden-smallwood added bug Something isn't working triage This issue was opened with a template and needs to be triaged by code owners. labels Nov 16, 2024
@camden-smallwood camden-smallwood changed the title Sway hex literals have their suffix counted as a digit Sway hex literals have their suffix counted as digits Nov 16, 2024
@IGI-111 IGI-111 added good first issue Good for newcomers compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen and removed triage This issue was opened with a template and needs to be triaged by code owners. labels Jan 5, 2025
@IGI-111 IGI-111 added the team:compiler Compiler Team label Feb 10, 2025
@krishnabandewar
Copy link

Can I handle this task?

@Samibhr09
Copy link

Can I contribute to this one?

@saimeunt
Copy link
Contributor

Hey I'd like to handle this task, I will carefully study the possible solution hinted at and see how to best implement the fix.

saimeunt added a commit to saimeunt/sway that referenced this issue Mar 25, 2025
@saimeunt saimeunt linked a pull request Mar 26, 2025 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen good first issue Good for newcomers ODHack12 team:compiler Compiler Team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants