Skip to content

Fix #6727 #7042

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sway-ast/src/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum LitIntType {
I16,
I32,
I64,
B256,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Serialize, Deserialize)]
Expand Down Expand Up @@ -97,6 +98,7 @@ impl Literal {
LitIntType::I16 => "i16",
LitIntType::I32 => "i32",
LitIntType::I64 => "i64",
LitIntType::B256 => "b256",
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3620,6 +3620,12 @@ fn literal_to_literal(
let error = ConvertParseTreeError::SignedIntegersNotSupported { span };
return Err(handler.emit_err(error.into()));
}
LitIntType::B256 => {
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)
}
},
}
}
Expand Down
6 changes: 6 additions & 0 deletions sway-parse/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,13 @@ pub fn parse_int_suffix(suffix: &str) -> Option<LitIntType> {
"i16" => LitIntType::I16,
"i32" => LitIntType::I32,
"i64" => LitIntType::I64,
"b256" => LitIntType::B256,
_ => return None,
})
}

fn parse_digits(big_uint: &mut BigUint, l: &mut Lexer<'_>, radix: u32) -> Option<usize> {
let mut num_digits = 1;
loop {
match l.stream.peek() {
None => break None,
Expand All @@ -773,9 +775,13 @@ fn parse_digits(big_uint: &mut BigUint, l: &mut Lexer<'_>, radix: u32) -> Option
Some(&(index, character)) => match character.to_digit(radix) {
None => break Some(index),
Some(digit) => {
if (radix == 16 && num_digits == 64) || (radix == 2 && num_digits == 256) {
break Some(index);
}
let _ = l.stream.next();
*big_uint *= radix;
*big_uint += digit;
num_digits += 1;
}
},
};
Expand Down
1 change: 1 addition & 0 deletions swayfmt/src/utils/language/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Format for Literal {
LitIntType::I16 => "_i16",
LitIntType::I32 => "_i32",
LitIntType::I64 => "_i64",
LitIntType::B256 => "_b256",
};
write!(formatted_code, "{}", int_type)?;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "b256_literals"
source = "member"
dependencies = ["std"]

[[package]]
name = "std"
source = "path+from-root-CB876055E4BB3A9C"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "b256_literals"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"configurables": [],
"functions": [
{
"attributes": null,
"inputs": [],
"name": "main",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
],
"loggedTypes": [],
"messagesTypes": [],
"types": [
{
"components": null,
"type": "bool",
"typeId": 0,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"concreteTypes": [
{
"concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903",
"type": "bool"
}
],
"configurables": [],
"encodingVersion": "1",
"functions": [
{
"attributes": null,
"inputs": [],
"name": "main",
"output": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903"
}
],
"loggedTypes": [],
"messagesTypes": [],
"metadataTypes": [],
"programType": "script",
"specVersion": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
script;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing is that this test should have more diverse sets of values to make sure that things like endianess works correctly.

fn main() -> bool {
// Hex
let _x = 0x0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000b256;
assert(_x == b256::zero());

let _x = 0x0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_b256;
assert(_x.as_u256() == 0xb256);

// Binary
let _y = 0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000b256;
assert(_y == b256::zero());

let _y = 0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_1011_0010_0101_0110;
assert(_y.as_u256() == 0xb256);

true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "run"
expected_result = { action = "return", value = 1 }
expected_result_new_encoding = { action = "return_data", value = "01" }
validate_abi = true
Loading