Skip to content

Commit df50ca8

Browse files
saimeuntIGI-111
andauthored
Allow decimal/binary/octal u256 literals (#7043)
## Description This PR removes the constraint on `u256` literals being only expressed in hexadecimal. The constraint has been removed and the code slightly refactored to remove the unnecessary `radix` variable in the `lex_int_lit` function in `token.rs`. The corresponding error `LexErrorKind::U256NotInHex` has been removed from the codebasen as well as the `should_fail` test looking to raise this error. The u256 testing suite has been augmented with additional tests to make sure we can express u256 literals in decimal, binary and octal. Closes #5456 ## 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). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [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: IGI-111 <igi-111@protonmail.com>
1 parent 34aa19c commit df50ca8

File tree

7 files changed

+32
-51
lines changed

7 files changed

+32
-51
lines changed

sway-error/src/lex_error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ pub enum LexErrorKind {
5959
UnicodeTextDirInLiteral { position: usize, character: char },
6060
#[error("invalid escape code")]
6161
InvalidEscapeCode { position: usize },
62-
#[error("invalid u256. Only hex literals are supported")]
63-
U256NotInHex,
6462
}
6563

6664
impl Spanned for LexError {

sway-parse/src/token.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ fn lex_int_lit(
655655
let end_opt = parse_digits(&mut big_uint, l, 10);
656656
(big_uint, end_opt)
657657
};
658-
let (radix, (big_uint, end_opt)) = if digit == 0 {
658+
let (big_uint, end_opt) = if digit == 0 {
659659
let prefixed_int_lit = |l: &mut Lexer<'_>, radix| {
660660
let _ = l.stream.next();
661661
let d = l.stream.next();
@@ -679,32 +679,19 @@ fn lex_int_lit(
679679
};
680680

681681
match l.stream.peek() {
682-
Some((_, 'x')) => (16, prefixed_int_lit(l, 16)?),
683-
Some((_, 'o')) => (8, prefixed_int_lit(l, 8)?),
684-
Some((_, 'b')) => (2, prefixed_int_lit(l, 2)?),
685-
Some((_, '_' | '0'..='9')) => (10, decimal_int_lit(l, 0)),
686-
Some(&(next_index, _)) => (10, (BigUint::from(0u32), Some(next_index))),
687-
None => (10, (BigUint::from(0u32), None)),
682+
Some((_, 'x')) => prefixed_int_lit(l, 16)?,
683+
Some((_, 'o')) => prefixed_int_lit(l, 8)?,
684+
Some((_, 'b')) => prefixed_int_lit(l, 2)?,
685+
Some((_, '_' | '0'..='9')) => decimal_int_lit(l, 0),
686+
Some(&(next_index, _)) => (BigUint::from(0u32), Some(next_index)),
687+
None => (BigUint::from(0u32), None),
688688
}
689689
} else {
690-
(10, decimal_int_lit(l, digit))
690+
decimal_int_lit(l, digit)
691691
};
692692

693693
let ty_opt = lex_int_ty_opt(l)?;
694694

695-
// Only accepts u256 literals in hex form
696-
if let Some((LitIntType::U256, span)) = &ty_opt {
697-
if radix != 16 {
698-
return Err(error(
699-
l.handler,
700-
LexError {
701-
kind: LexErrorKind::U256NotInHex,
702-
span: span.clone(),
703-
},
704-
));
705-
}
706-
}
707-
708695
let literal = Literal::Int(LitInt {
709696
span: span(l, index, end_opt.unwrap_or(l.src.len())),
710697
parsed: big_uint,

test/src/e2e_vm_tests/test_programs/should_fail/u256/u256_only_hex_literal/Forc.lock

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/src/e2e_vm_tests/test_programs/should_fail/u256/u256_only_hex_literal/Forc.toml

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/src/e2e_vm_tests/test_programs/should_fail/u256/u256_only_hex_literal/src/main.sw

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/src/e2e_vm_tests/test_programs/should_fail/u256/u256_only_hex_literal/test.toml

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_operators/src/main.sw

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
library;
22

3-
// returns 3
3+
// returns 32
4+
fn hex_literals() -> u256 {
5+
0x10u256 + 0x10u256
6+
}
7+
8+
// returns 32
9+
fn oct_literals() -> u256 {
10+
0o20u256 + 0o20u256
11+
}
12+
13+
// returns 32
14+
fn bin_literals() -> u256 {
15+
0b0001_0000u256 + 0b0001_0000u256
16+
}
17+
18+
// returns 32
419
fn literals() -> u256 {
5-
0x0000000000000000000000000000000000000000000000000000000000000001u256 + 0x0000000000000000000000000000000000000000000000000000000000000002u256
20+
16u256 + 16u256
621
}
722

823
// returns 1
@@ -117,7 +132,13 @@ fn comparison_operators() -> u256 {
117132

118133
#[test]
119134
fn should_be_able_to_use_literals() {
120-
let result = 0x0000000000000000000000000000000000000000000000000000000000000003u256;
135+
let hex_result = 0x20u256;
136+
assert_eq(hex_literals(), hex_result);
137+
let oct_result = 0o40u256;
138+
assert_eq(oct_literals(), oct_result);
139+
let bin_result = 0b0010_0000u256;
140+
assert_eq(bin_literals(), bin_result);
141+
let result = 32u256;
121142
assert_eq(literals(), result);
122143
}
123144

0 commit comments

Comments
 (0)