Skip to content

Commit

Permalink
Convert numeric literals to U256 based on inferred expression type
Browse files Browse the repository at this point in the history
Fixes #5453,
Fixes #5454
  • Loading branch information
vaivaswatha committed Jan 17, 2024
1 parent 813ef65 commit af94edc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use sway_types::{
integer_bits::IntegerBits,
span::{Span, Spanned},
state::StateIndex,
u256::U256,
Named,
};

Expand Down Expand Up @@ -365,7 +366,13 @@ impl<'eng> FnCompiler<'eng> {
ty::TyExpressionVariant::Literal(Literal::Numeric(n)) => {
let implied_lit = match &*self.engines.te().get(ast_expr.return_type) {
TypeInfo::UnsignedInteger(IntegerBits::Eight) => Literal::U8(*n as u8),
_ => Literal::U64(*n),
TypeInfo::UnsignedInteger(IntegerBits::V256) => Literal::U256(U256::from(*n)),
_ =>
// Anything more than a byte needs a u64 (except U256 of course).
// (This is how convert_literal_to_value treats it too).
{
Literal::U64(*n)
}
};
Ok(convert_literal_to_value(context, &implied_lit)
.add_metadatum(context, span_md_idx))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,17 @@ fn should_revert_on_div_zero() -> u256 {
let a = 0x0000000000000000000000000000000000000000000000000000000000000000u256;
a / 0x0000000000000000000000000000000000000000000000000000000000000000u256
}

#[test]
fn type_inference_numeric_1() -> u256 {
let a = 1;
let b = 0x2u256;
b * a
}

#[test]
fn type_inference_numeric_2() -> u256 {
let mut result = 0;
result = 3.as_u256();
result
}

0 comments on commit af94edc

Please sign in to comment.