diff --git a/Cargo.toml b/Cargo.toml index 75fdd1697fd..500fa3e7632 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,6 +78,7 @@ unused_must_use = "warn" mismatched_lifetime_syntaxes = "allow" [workspace.lints.clippy] +cast_lossless = "warn" semicolon_if_nothing_returned = "warn" result_large_err = "allow" uninlined_format_args = "warn" diff --git a/acvm-repo/acir_field/src/field_element.rs b/acvm-repo/acir_field/src/field_element.rs index 57542248ec5..8efeb63927e 100644 --- a/acvm-repo/acir_field/src/field_element.rs +++ b/acvm-repo/acir_field/src/field_element.rs @@ -220,9 +220,9 @@ impl AcirField for FieldElement { let as_bigint = self.0.into_bigint(); let limbs = as_bigint.as_ref(); - let mut result = limbs[0] as u128; + let mut result = u128::from(limbs[0]); if limbs.len() > 1 { - let high_limb = limbs[1] as u128; + let high_limb = u128::from(limbs[1]); result += high_limb << 64; } diff --git a/acvm-repo/acvm/src/pwg/blackbox/aes128.rs b/acvm-repo/acvm/src/pwg/blackbox/aes128.rs index e241a39f5af..fc46b53f233 100644 --- a/acvm-repo/acvm/src/pwg/blackbox/aes128.rs +++ b/acvm-repo/acvm/src/pwg/blackbox/aes128.rs @@ -25,7 +25,7 @@ pub(super) fn solve_aes128_encryption_opcode( // Write witness assignments for (output_witness, value) in outputs.iter().zip(ciphertext.into_iter()) { - insert_value(output_witness, F::from(value as u128), initial_witness)?; + insert_value(output_witness, F::from(u128::from(value)), initial_witness)?; } Ok(()) diff --git a/acvm-repo/acvm/src/pwg/blackbox/bigint.rs b/acvm-repo/acvm/src/pwg/blackbox/bigint.rs index 1c5a436fa11..5a2540df9d6 100644 --- a/acvm-repo/acvm/src/pwg/blackbox/bigint.rs +++ b/acvm-repo/acvm/src/pwg/blackbox/bigint.rs @@ -51,7 +51,7 @@ impl AcvmBigIntSolver { bytes.push(0); } bytes.iter().zip(outputs.iter()).for_each(|(byte, output)| { - initial_witness.insert(*output, F::from(*byte as u128)); + initial_witness.insert(*output, F::from(u128::from(*byte))); }); Ok(()) } diff --git a/acvm-repo/acvm/src/pwg/blackbox/hash.rs b/acvm-repo/acvm/src/pwg/blackbox/hash.rs index a4af9de55cf..c7a1fc75df2 100644 --- a/acvm-repo/acvm/src/pwg/blackbox/hash.rs +++ b/acvm-repo/acvm/src/pwg/blackbox/hash.rs @@ -101,7 +101,7 @@ pub(crate) fn solve_sha_256_permutation_opcode( sha256_compression(&mut state, &message); for (output_witness, value) in outputs.iter().zip(state.into_iter()) { - insert_value(output_witness, F::from(value as u128), initial_witness)?; + insert_value(output_witness, F::from(u128::from(value)), initial_witness)?; } Ok(()) diff --git a/acvm-repo/acvm/src/pwg/blackbox/mod.rs b/acvm-repo/acvm/src/pwg/blackbox/mod.rs index 663603cf0a2..0c1c2d0484b 100644 --- a/acvm-repo/acvm/src/pwg/blackbox/mod.rs +++ b/acvm-repo/acvm/src/pwg/blackbox/mod.rs @@ -111,7 +111,7 @@ pub(crate) fn solve( } let output_state = keccakf1600(state)?; for (output_witness, value) in outputs.iter().zip(output_state.into_iter()) { - insert_value(output_witness, F::from(value as u128), initial_witness)?; + insert_value(output_witness, F::from(u128::from(value)), initial_witness)?; } Ok(()) } diff --git a/acvm-repo/acvm/src/pwg/memory_op.rs b/acvm-repo/acvm/src/pwg/memory_op.rs index 34747371afa..31e041d5695 100644 --- a/acvm-repo/acvm/src/pwg/memory_op.rs +++ b/acvm-repo/acvm/src/pwg/memory_op.rs @@ -49,7 +49,7 @@ impl MemoryOpSolver { if index >= self.block_len { return Err(OpcodeResolutionError::IndexOutOfBounds { opcode_location: ErrorLocation::Unresolved, - index: F::from(index as u128), + index: F::from(u128::from(index)), array_size: self.block_len, }); } @@ -62,7 +62,7 @@ impl MemoryOpSolver { fn read_memory_index(&self, index: MemoryIndex) -> Result> { self.block_value.get(&index).copied().ok_or(OpcodeResolutionError::IndexOutOfBounds { opcode_location: ErrorLocation::Unresolved, - index: F::from(index as u128), + index: F::from(u128::from(index)), array_size: self.block_len, }) } diff --git a/acvm-repo/acvm/tests/solver.rs b/acvm-repo/acvm/tests/solver.rs index fccabfd1777..6071832e20b 100644 --- a/acvm-repo/acvm/tests/solver.rs +++ b/acvm-repo/acvm/tests/solver.rs @@ -901,7 +901,7 @@ prop_compose! { (inputs in proptest::collection::vec(any::<(u8, bool)>(), modulus.len()), modulus in Just(modulus)) -> (Vec, Vec) { let inputs = inputs.into_iter().zip(modulus.iter()).map(|((input, use_constant), modulus_byte)| { - (FieldElement::from(input.clamp(0, *modulus_byte) as u128), use_constant) + (FieldElement::from(u128::from(input.clamp(0, *modulus_byte))), use_constant) }).collect(); (inputs, modulus) } @@ -913,7 +913,7 @@ prop_compose! { -> (Vec, Vec, Vec) { let (inputs, modulus) = inputs_modulus; let second_inputs = second_inputs.into_iter().zip(modulus.iter()).map(|((input, use_constant), modulus_byte)| { - (FieldElement::from(input.clamp(0, *modulus_byte) as u128), use_constant) + (FieldElement::from(u128::from(input.clamp(0, *modulus_byte))), use_constant) }).collect(); (inputs, second_inputs, modulus) } @@ -925,7 +925,7 @@ prop_compose! { -> (Vec, Vec, Vec, Vec) { let (inputs, second_inputs, modulus) = inputs_pair_modulus; let third_inputs = third_inputs.into_iter().zip(modulus.iter()).map(|((input, use_constant), modulus_byte)| { - (FieldElement::from(input.clamp(0, *modulus_byte) as u128), use_constant) + (FieldElement::from(u128::from(input.clamp(0, *modulus_byte))), use_constant) }).collect(); (inputs, second_inputs, third_inputs, modulus) } @@ -1718,7 +1718,7 @@ proptest! { let mut extra_bytes: Vec<_> = extra_bytes .into_iter() .take(extra_bytes_len as usize) - .map(|(x, use_constant)| (FieldElement::from(x as u128), use_constant)) + .map(|(x, use_constant)| (FieldElement::from(u128::from(x)), use_constant)) .collect(); input.append(&mut extra_bytes); let expected_results: Vec<_> = drop_use_constant(&input); @@ -1733,7 +1733,7 @@ proptest! { let mut extra_bytes: Vec<_> = extra_bytes .into_iter() .take(extra_bytes_len as usize) - .map(|(x, use_constant)| (FieldElement::from(x as u128), use_constant)) + .map(|(x, use_constant)| (FieldElement::from(u128::from(x)), use_constant)) .collect(); input.append(&mut extra_bytes); let expected_results: Vec<_> = drop_use_constant(&input); @@ -1748,7 +1748,7 @@ proptest! { fn bigint_from_to_le_bytes_bigger_than_u8((input, modulus) in bigint_with_modulus(), patch_location: usize, larger_value: u16, use_constant: bool) { let mut input = input; let patch_location = patch_location % input.len(); - let larger_value = FieldElement::from(std::cmp::max((u8::MAX as u16) + 1, larger_value) as u128); + let larger_value = FieldElement::from(u128::from(std::cmp::max(u16::from(u8::MAX) + 1, larger_value))); input[patch_location] = (larger_value, use_constant); let expected_results: Vec<_> = drop_use_constant(&input); let pedantic_solving = true; diff --git a/acvm-repo/bn254_blackbox_solver/src/embedded_curve_ops.rs b/acvm-repo/bn254_blackbox_solver/src/embedded_curve_ops.rs index 74c420d042c..1070a3a9bc1 100644 --- a/acvm-repo/bn254_blackbox_solver/src/embedded_curve_ops.rs +++ b/acvm-repo/bn254_blackbox_solver/src/embedded_curve_ops.rs @@ -70,7 +70,7 @@ pub fn multi_scalar_mul( Ok(( FieldElement::from_repr(out_x), FieldElement::from_repr(out_y), - FieldElement::from(output_point.is_zero() as u128), + FieldElement::from(u128::from(output_point.is_zero())), )) } else { Ok((FieldElement::from(0_u128), FieldElement::from(0_u128), FieldElement::from(1_u128))) @@ -107,7 +107,7 @@ pub fn embedded_curve_add( Ok(( FieldElement::from_repr(res_x), FieldElement::from_repr(res_y), - FieldElement::from(res.is_zero() as u128), + FieldElement::from(u128::from(res.is_zero())), )) } else if res.is_zero() { Ok((FieldElement::from(0_u128), FieldElement::from(0_u128), FieldElement::from(1_u128))) diff --git a/acvm-repo/brillig_vm/src/arithmetic.rs b/acvm-repo/brillig_vm/src/arithmetic.rs index 60bff05c791..c43613105c0 100644 --- a/acvm-repo/brillig_vm/src/arithmetic.rs +++ b/acvm-repo/brillig_vm/src/arithmetic.rs @@ -179,7 +179,7 @@ pub(crate) fn evaluate_binary_int_op( } else { Err(BrilligArithmeticError::BitshiftOverflow { bit_size: 8, - shift_size: rhs as u128, + shift_size: u128::from(rhs), }) } } @@ -189,7 +189,7 @@ pub(crate) fn evaluate_binary_int_op( } else { Err(BrilligArithmeticError::BitshiftOverflow { bit_size: 16, - shift_size: rhs as u128, + shift_size: u128::from(rhs), }) } } @@ -199,7 +199,7 @@ pub(crate) fn evaluate_binary_int_op( } else { Err(BrilligArithmeticError::BitshiftOverflow { bit_size: 32, - shift_size: rhs as u128, + shift_size: u128::from(rhs), }) } } @@ -209,7 +209,7 @@ pub(crate) fn evaluate_binary_int_op( } else { Err(BrilligArithmeticError::BitshiftOverflow { bit_size: 64, - shift_size: rhs as u128, + shift_size: u128::from(rhs), }) } } diff --git a/acvm-repo/brillig_vm/src/cast.rs b/acvm-repo/brillig_vm/src/cast.rs index 66b2a0f00d8..48635ed4df6 100644 --- a/acvm-repo/brillig_vm/src/cast.rs +++ b/acvm-repo/brillig_vm/src/cast.rs @@ -39,28 +39,28 @@ pub(crate) fn cast( (U8(value), BitSize::Integer(IntegerBitSize::U32)) => U32(value.into()), (U8(value), BitSize::Integer(IntegerBitSize::U64)) => U64(value.into()), (U8(value), BitSize::Integer(IntegerBitSize::U128)) => U128(value.into()), - (U8(value), BitSize::Field) => Field((value as u128).into()), + (U8(value), BitSize::Field) => Field(u128::from(value).into()), (U16(value), BitSize::Integer(IntegerBitSize::U1)) => U1(value & 0x01 == 1), (U16(value), BitSize::Integer(IntegerBitSize::U8)) => U8(value as u8), (U16(value), BitSize::Integer(IntegerBitSize::U32)) => U32(value.into()), (U16(value), BitSize::Integer(IntegerBitSize::U64)) => U64(value.into()), (U16(value), BitSize::Integer(IntegerBitSize::U128)) => U128(value.into()), - (U16(value), BitSize::Field) => Field((value as u128).into()), + (U16(value), BitSize::Field) => Field(u128::from(value).into()), (U32(value), BitSize::Integer(IntegerBitSize::U1)) => U1(value & 0x01 == 1), (U32(value), BitSize::Integer(IntegerBitSize::U8)) => U8(value as u8), (U32(value), BitSize::Integer(IntegerBitSize::U16)) => U16(value as u16), (U32(value), BitSize::Integer(IntegerBitSize::U64)) => U64(value.into()), (U32(value), BitSize::Integer(IntegerBitSize::U128)) => U128(value.into()), - (U32(value), BitSize::Field) => Field((value as u128).into()), + (U32(value), BitSize::Field) => Field(u128::from(value).into()), (U64(value), BitSize::Integer(IntegerBitSize::U1)) => U1(value & 0x01 == 1), (U64(value), BitSize::Integer(IntegerBitSize::U8)) => U8(value as u8), (U64(value), BitSize::Integer(IntegerBitSize::U16)) => U16(value as u16), (U64(value), BitSize::Integer(IntegerBitSize::U32)) => U32(value as u32), (U64(value), BitSize::Integer(IntegerBitSize::U128)) => U128(value.into()), - (U64(value), BitSize::Field) => Field((value as u128).into()), + (U64(value), BitSize::Field) => Field(u128::from(value).into()), (U128(value), BitSize::Integer(IntegerBitSize::U1)) => U1(value & 0x01 == 1), (U128(value), BitSize::Integer(IntegerBitSize::U8)) => U8(value as u8), diff --git a/acvm-repo/brillig_vm/src/memory.rs b/acvm-repo/brillig_vm/src/memory.rs index 71fc256c208..2b177473531 100644 --- a/acvm-repo/brillig_vm/src/memory.rs +++ b/acvm-repo/brillig_vm/src/memory.rs @@ -103,10 +103,10 @@ impl MemoryValue { match self { MemoryValue::Field(value) => *value, MemoryValue::U1(value) => F::from(*value), - MemoryValue::U8(value) => F::from(*value as u128), - MemoryValue::U16(value) => F::from(*value as u128), - MemoryValue::U32(value) => F::from(*value as u128), - MemoryValue::U64(value) => F::from(*value as u128), + MemoryValue::U8(value) => F::from(u128::from(*value)), + MemoryValue::U16(value) => F::from(u128::from(*value)), + MemoryValue::U32(value) => F::from(u128::from(*value)), + MemoryValue::U64(value) => F::from(u128::from(*value)), MemoryValue::U128(value) => F::from(*value), } } @@ -115,11 +115,11 @@ impl MemoryValue { pub fn to_u128(&self) -> Result { match self { MemoryValue::Field(..) => Err(MemoryTypeError::NotAnInteger), - MemoryValue::U1(value) => Ok(*value as u8 as u128), - MemoryValue::U8(value) => Ok(*value as u128), - MemoryValue::U16(value) => Ok(*value as u128), - MemoryValue::U32(value) => Ok(*value as u128), - MemoryValue::U64(value) => Ok(*value as u128), + MemoryValue::U1(value) => Ok(u128::from(*value)), + MemoryValue::U8(value) => Ok(u128::from(*value)), + MemoryValue::U16(value) => Ok(u128::from(*value)), + MemoryValue::U32(value) => Ok(u128::from(*value)), + MemoryValue::U64(value) => Ok(u128::from(*value)), MemoryValue::U128(value) => Ok(*value), } } diff --git a/compiler/noirc_errors/src/reporter.rs b/compiler/noirc_errors/src/reporter.rs index d406e897d65..e4e7a2fe1ec 100644 --- a/compiler/noirc_errors/src/reporter.rs +++ b/compiler/noirc_errors/src/reporter.rs @@ -200,7 +200,7 @@ pub fn report_all<'files>( diagnostics.append(&mut errors); let error_count = - diagnostics.iter().map(|error| error.report(files, deny_warnings) as u32).sum(); + diagnostics.iter().map(|error| u32::from(error.report(files, deny_warnings))).sum(); ReportedErrors { error_count } } diff --git a/compiler/noirc_evaluator/src/acir/acir_context/big_int.rs b/compiler/noirc_evaluator/src/acir/acir_context/big_int.rs index 2a707651c57..8448b6efcba 100644 --- a/compiler/noirc_evaluator/src/acir/acir_context/big_int.rs +++ b/compiler/noirc_evaluator/src/acir/acir_context/big_int.rs @@ -12,11 +12,11 @@ pub(super) struct BigIntId { impl BigIntId { pub(super) fn bigint_id>(&self) -> F { - F::from(self.bigint_id as u128) + F::from(u128::from(self.bigint_id)) } pub(super) fn modulus_id>(&self) -> F { - F::from(self.modulus_id as u128) + F::from(u128::from(self.modulus_id)) } } diff --git a/compiler/noirc_evaluator/src/acir/acir_context/black_box.rs b/compiler/noirc_evaluator/src/acir/acir_context/black_box.rs index 7da59e421ca..7fcf4f270b6 100644 --- a/compiler/noirc_evaluator/src/acir/acir_context/black_box.rs +++ b/compiler/noirc_evaluator/src/acir/acir_context/black_box.rs @@ -131,7 +131,7 @@ impl> AcirContext { output_count = 0; let modulus_id = self.big_int_ctx.get_or_insert_modulus(big_modulus); - let result_id = self.big_int_ctx.new_big_int(F::from(modulus_id as u128)); + let result_id = self.big_int_ctx.new_big_int(F::from(u128::from(modulus_id))); (modulus, vec![result_id.bigint_id::(), result_id.modulus_id::()]) } BlackBoxFunc::AES128Encrypt => { diff --git a/compiler/noirc_evaluator/src/acir/acir_context/generated_acir/mod.rs b/compiler/noirc_evaluator/src/acir/acir_context/generated_acir/mod.rs index 5abaed1a1c5..9fbc2f2e805 100644 --- a/compiler/noirc_evaluator/src/acir/acir_context/generated_acir/mod.rs +++ b/compiler/noirc_evaluator/src/acir/acir_context/generated_acir/mod.rs @@ -408,12 +408,12 @@ impl GeneratedAcir { let limbs_nb = Expression { mul_terms: Vec::new(), linear_combinations: Vec::new(), - q_c: F::from(limb_count as u128), + q_c: F::from(u128::from(limb_count)), }; let radix_expr = Expression { mul_terms: Vec::new(), linear_combinations: Vec::new(), - q_c: F::from(radix as u128), + q_c: F::from(u128::from(radix)), }; let inputs = vec![ BrilligInputs::Single(expr.clone()), diff --git a/compiler/noirc_evaluator/src/acir/tests/mod.rs b/compiler/noirc_evaluator/src/acir/tests/mod.rs index ca5473d1b84..862eb81143c 100644 --- a/compiler/noirc_evaluator/src/acir/tests/mod.rs +++ b/compiler/noirc_evaluator/src/acir/tests/mod.rs @@ -1134,10 +1134,10 @@ fn test_u32(lhs in 0u32.., rhs in 0u32..) { //unchecked operations assume no under/over-flow let mut unchecked_operators = vec![]; - if (lhs + rhs).to_u128() <= u32::MAX as u128 { + if (lhs + rhs).to_u128() <= u128::from(u32::MAX) { unchecked_operators.push("unchecked_add"); } - if (lhs * rhs).to_u128() <= u32::MAX as u128 { + if (lhs * rhs).to_u128() <= u128::from(u32::MAX) { unchecked_operators.push("unchecked_mul"); } if lhs >= rhs { @@ -1176,8 +1176,8 @@ fn test_constraint_u64(lhs in 0u64.., rhs in 0u64..) { #[test] fn test_constraint_u16(lhs in 0u16.., rhs in 0u16..) { - let lhs = FieldElement::from(lhs as u128); - let rhs = FieldElement::from(rhs as u128); + let lhs = FieldElement::from(u128::from(lhs)); + let rhs = FieldElement::from(u128::from(rhs)); let operators = ["constrain ==", "constrain !="]; test_operators(&operators, "u16", &[lhs,rhs]); test_operators(&operators, "i16", &[lhs,rhs]); @@ -1185,8 +1185,8 @@ fn test_constraint_u16(lhs in 0u16.., rhs in 0u16..) { #[test] fn test_constraint_u8(lhs in 0u8.., rhs in 0u8..) { - let lhs = FieldElement::from(lhs as u128); - let rhs = FieldElement::from(rhs as u128); + let lhs = FieldElement::from(u128::from(lhs)); + let rhs = FieldElement::from(u128::from(rhs)); let operators = ["constrain ==", "constrain !="]; test_operators(&operators, "u8", &[lhs,rhs]); test_operators(&operators, "i8", &[lhs,rhs]); diff --git a/compiler/noirc_evaluator/src/brillig/brillig_ir/codegen_control_flow.rs b/compiler/noirc_evaluator/src/brillig/brillig_ir/codegen_control_flow.rs index d03c3583856..481c2993725 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_ir/codegen_control_flow.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_ir/codegen_control_flow.rs @@ -208,7 +208,7 @@ impl BrilligContext< ctx.indirect_const_instruction( current_revert_data_pointer, 64, - (error_selector.as_u64() as u128).into(), + u128::from(error_selector.as_u64()).into(), ); ctx.codegen_usize_op_in_place(current_revert_data_pointer, BrilligBinaryOp::Add, 1); @@ -286,7 +286,7 @@ impl BrilligContext< self.indirect_const_instruction( ReservedRegisters::free_memory_pointer(), 64, - (error_selector.as_u64() as u128).into(), + u128::from(error_selector.as_u64()).into(), ); self.trap_instruction(HeapVector { pointer: ReservedRegisters::free_memory_pointer(), diff --git a/compiler/noirc_evaluator/src/brillig/brillig_ir/codegen_intrinsic.rs b/compiler/noirc_evaluator/src/brillig/brillig_ir/codegen_intrinsic.rs index 3daf0486110..1ffd35893ee 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_ir/codegen_intrinsic.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_ir/codegen_intrinsic.rs @@ -48,7 +48,7 @@ impl BrilligContext< // The modulus is guaranteed to fit, since we are truncating down to a bit size that is strictly less than the value_to_truncate.bit_size let modulus_var = self.make_constant_instruction( - F::from(2_usize).pow(&F::from(bit_size as u128)), + F::from(2_usize).pow(&F::from(u128::from(bit_size))), value_to_truncate.bit_size, ); diff --git a/compiler/noirc_evaluator/src/ssa/interpreter/intrinsics.rs b/compiler/noirc_evaluator/src/ssa/interpreter/intrinsics.rs index 6f16ec0f342..8902020f351 100644 --- a/compiler/noirc_evaluator/src/ssa/interpreter/intrinsics.rs +++ b/compiler/noirc_evaluator/src/ssa/interpreter/intrinsics.rs @@ -126,7 +126,7 @@ impl Interpreter<'_, W> { let result = acvm::blackbox_solver::aes128_encrypt(&inputs, iv_array, key_array) .map_err(Self::convert_error)?; - let result = result.iter().map(|v| (*v as u128).into()); + let result = result.iter().map(|v| u128::from(*v).into()); let result = Value::array_from_iter(result, NumericType::unsigned(8))?; Ok(vec![result]) } @@ -150,7 +150,7 @@ impl Interpreter<'_, W> { let inputs = self.lookup_bytes(args[0], "call Blake2s BlackBox")?; let result = acvm::blackbox_solver::blake2s(&inputs).map_err(Self::convert_error)?; - let result = result.iter().map(|e| (*e as u128).into()); + let result = result.iter().map(|e| u128::from(*e).into()); let result = Value::array_from_iter(result, NumericType::unsigned(8))?; Ok(vec![result]) } @@ -159,7 +159,7 @@ impl Interpreter<'_, W> { let inputs = self.lookup_bytes(args[0], "call Blake3 BlackBox")?; let results = acvm::blackbox_solver::blake3(&inputs).map_err(Self::convert_error)?; - let results = results.iter().map(|e| (*e as u128).into()); + let results = results.iter().map(|e| u128::from(*e).into()); let results = Value::array_from_iter(results, NumericType::unsigned(8))?; Ok(vec![results]) } @@ -242,14 +242,14 @@ impl Interpreter<'_, W> { let mut points = Vec::new(); for (i, v) in input_points.elements.borrow().iter().enumerate() { if i % 3 == 2 { - points.push((v.as_bool().ok_or( + points.push(u128::from(v.as_bool().ok_or( InterpreterError::Internal(InternalError::TypeError { value_id: args[0], value: v.to_string(), expected_type: "bool", instruction: "retrieving is_infinite in call to MultiScalarMul blackbox", }) - )? as u128).into()); + )?).into()); } else { points.push( v.as_field().ok_or( @@ -305,7 +305,7 @@ impl Interpreter<'_, W> { })?; let results = acvm::blackbox_solver::keccakf1600(inputs_array) .map_err(Self::convert_error)?; - let results = results.iter().map(|e| (*e as u128).into()); + let results = results.iter().map(|e| u128::from(*e).into()); let results = Value::array_from_iter(results, NumericType::Unsigned { bit_size: 64 })?; Ok(vec![results]) @@ -376,7 +376,7 @@ impl Interpreter<'_, W> { }) })?; acvm::blackbox_solver::sha256_compression(&mut state, &inputs); - let result = state.iter().map(|e| (*e as u128).into()); + let result = state.iter().map(|e| u128::from(*e).into()); let result = Value::array_from_iter(result, NumericType::unsigned(32))?; Ok(vec![result]) } diff --git a/compiler/noirc_evaluator/src/ssa/interpreter/mod.rs b/compiler/noirc_evaluator/src/ssa/interpreter/mod.rs index e81e409f561..6f306808d79 100644 --- a/compiler/noirc_evaluator/src/ssa/interpreter/mod.rs +++ b/compiler/noirc_evaluator/src/ssa/interpreter/mod.rs @@ -1557,8 +1557,8 @@ fn interpret_u1_binary_op( // (1, 0) -> (division by 0) // (1, 1) -> 1 if !rhs { - let lhs = (lhs as u8).to_string(); - let rhs = (rhs as u8).to_string(); + let lhs = u8::from(lhs).to_string(); + let rhs = u8::from(rhs).to_string(); return Err(InterpreterError::DivisionByZero { lhs_id, lhs, rhs_id, rhs }); } lhs @@ -1569,8 +1569,8 @@ fn interpret_u1_binary_op( // (1, 0) -> (division by 0) // (1, 1) -> 0 if !rhs { - let lhs = format!("u1 {}", lhs as u8); - let rhs = format!("u1 {}", rhs as u8); + let lhs = format!("u1 {}", u8::from(lhs)); + let rhs = format!("u1 {}", u8::from(rhs)); return Err(InterpreterError::DivisionByZero { lhs_id, lhs, rhs_id, rhs }); } false diff --git a/compiler/noirc_evaluator/src/ssa/interpreter/tests/instructions.rs b/compiler/noirc_evaluator/src/ssa/interpreter/tests/instructions.rs index 2ed1b10cf9d..dd33ddc3aec 100644 --- a/compiler/noirc_evaluator/src/ssa/interpreter/tests/instructions.rs +++ b/compiler/noirc_evaluator/src/ssa/interpreter/tests/instructions.rs @@ -588,7 +588,7 @@ fn not() { assert_eq!(values[0], Value::bool(true)); assert_eq!(values[1], Value::bool(false)); - let not_constant = !136_u8 as u128; + let not_constant = u128::from(!136_u8); assert_eq!(values[2], from_constant(not_constant.into(), NumericType::unsigned(8))); } @@ -603,7 +603,7 @@ fn truncate() { } ", ); - let constant = 257_u16 as u8 as u128; + let constant = u128::from(257_u16 as u8); assert_eq!(value, from_constant(constant.into(), NumericType::unsigned(32))); } @@ -1082,7 +1082,8 @@ fn make_array() { assert_eq!(values[0], Value::array(one_two.clone(), vec![Type::field()])); assert_eq!(values[1], Value::slice(one_two, Arc::new(vec![Type::field()]))); - let hello = vecmap(b"Hello", |char| from_constant((*char as u32).into(), NumericType::char())); + let hello = + vecmap(b"Hello", |char| from_constant(u32::from(*char).into(), NumericType::char())); assert_eq!(values[2], Value::array(hello.clone(), vec![Type::char()])); assert_eq!(values[3], Value::slice(hello, Arc::new(vec![Type::char()]))); } diff --git a/compiler/noirc_evaluator/src/ssa/interpreter/tests/mod.rs b/compiler/noirc_evaluator/src/ssa/interpreter/tests/mod.rs index d295ba84f60..fc14c2398f6 100644 --- a/compiler/noirc_evaluator/src/ssa/interpreter/tests/mod.rs +++ b/compiler/noirc_evaluator/src/ssa/interpreter/tests/mod.rs @@ -68,7 +68,7 @@ pub(crate) fn from_constant(constant: FieldElement, typ: NumericType) -> Value { } fn from_u32_slice(slice: &[u32], typ: NumericType) -> Value { - let values = slice.iter().map(|v| from_constant((*v as u128).into(), typ)).collect(); + let values = slice.iter().map(|v| from_constant(u128::from(*v).into(), typ)).collect(); Value::array(values, vec![Type::Numeric(typ)]) } diff --git a/compiler/noirc_evaluator/src/ssa/interpreter/value.rs b/compiler/noirc_evaluator/src/ssa/interpreter/value.rs index ddf10acf3bf..8995211a28c 100644 --- a/compiler/noirc_evaluator/src/ssa/interpreter/value.rs +++ b/compiler/noirc_evaluator/src/ssa/interpreter/value.rs @@ -359,17 +359,17 @@ impl NumericValue { NumericValue::Field(field) => *field, NumericValue::U1(boolean) if *boolean => FieldElement::one(), NumericValue::U1(_) => FieldElement::zero(), - NumericValue::U8(value) => FieldElement::from(*value as u32), - NumericValue::U16(value) => FieldElement::from(*value as u32), + NumericValue::U8(value) => FieldElement::from(u32::from(*value)), + NumericValue::U16(value) => FieldElement::from(u32::from(*value)), NumericValue::U32(value) => FieldElement::from(*value), NumericValue::U64(value) => FieldElement::from(*value), NumericValue::U128(value) => FieldElement::from(*value), // Need to cast possibly negative values to the unsigned variants // first to ensure they are zero-extended rather than sign-extended - NumericValue::I8(value) => FieldElement::from(*value as u8 as i128), - NumericValue::I16(value) => FieldElement::from(*value as u16 as i128), - NumericValue::I32(value) => FieldElement::from(*value as u32 as i128), - NumericValue::I64(value) => FieldElement::from(*value as u64 as i128), + NumericValue::I8(value) => FieldElement::from(i128::from(*value as u8)), + NumericValue::I16(value) => FieldElement::from(i128::from(*value as u16)), + NumericValue::I32(value) => FieldElement::from(i128::from(*value as u32)), + NumericValue::I64(value) => FieldElement::from(i128::from(*value as u64)), } } diff --git a/compiler/noirc_evaluator/src/ssa/ir/dfg.rs b/compiler/noirc_evaluator/src/ssa/ir/dfg.rs index 3a4aa9f65c1..376b7894f26 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/dfg.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/dfg.rs @@ -645,7 +645,7 @@ impl DataFlowGraph { #[allow(clippy::match_like_matches_macro)] match (self.type_of_value(array), self.get_numeric_constant(index)) { (Type::Array(elements, len), Some(index)) - if index.to_u128() < (len as u128 * elements.len() as u128) => + if index.to_u128() < (u128::from(len) * elements.len() as u128) => { true } diff --git a/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/call.rs b/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/call.rs index 08dee54f6fd..17c8b076d82 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/call.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/call.rs @@ -95,7 +95,7 @@ pub(super) fn simplify_call( } Intrinsic::ArrayLen => { if let Some(length) = dfg.try_get_array_length(arguments[0]) { - let length = FieldElement::from(length as u128); + let length = FieldElement::from(u128::from(length)); SimplifyResult::SimplifiedTo(dfg.make_constant(length, NumericType::length_type())) } else if matches!(dfg.type_of_value(arguments[1]), Type::Slice(_)) { SimplifyResult::SimplifiedTo(arguments[0]) @@ -633,7 +633,7 @@ fn simplify_black_box_func( const_input.try_into().expect("Keccakf1600 input should have length of 25"), ) .expect("Rust solvable black box function should not fail"); - let state_values = state.iter().map(|x| FieldElement::from(*x as u128)); + let state_values = state.iter().map(|x| FieldElement::from(u128::from(*x))); let result_array = make_constant_array( dfg, state_values, diff --git a/compiler/noirc_evaluator/src/ssa/ir/function.rs b/compiler/noirc_evaluator/src/ssa/ir/function.rs index 22d757a91f2..eb605cb7d6d 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/function.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/function.rs @@ -218,7 +218,7 @@ impl Function { .iter() .map(|block| { let block = &self.dfg[*block]; - block.instructions().len() + block.terminator().is_some() as usize + block.instructions().len() + usize::from(block.terminator().is_some()) }) .sum() } diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs index b9a75a27ee5..bb81cc96d42 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs @@ -146,7 +146,7 @@ pub(crate) fn eval_constant_binary_op( if operator == BinaryOp::Shl || operator == BinaryOp::Shr { let op = "bit-shift"; - if rhs >= bit_size as u128 { + if rhs >= u128::from(bit_size) { return Failure(format!("attempt to {op} with overflow")); } } @@ -204,7 +204,7 @@ pub(crate) fn eval_constant_binary_op( // and the operation should be handled by ACIR generation. return Failure("attempt to divide by zero".to_string()); } - BinaryOp::Shr | BinaryOp::Shl if rhs >= bit_size as i128 => { + BinaryOp::Shr | BinaryOp::Shl if rhs >= i128::from(bit_size) => { let op = binary_op_function_name(operator); return Failure(format!("attempt to {op} with overflow")); } @@ -290,10 +290,10 @@ pub(crate) fn convert_signed_integer_to_field_element(int: i128, bit_size: u32) FieldElement::from(int as u128) } else { let two_complement = match bit_size { - 8 => ((int as i8) as u8) as u128, - 16 => ((int as i16) as u16) as u128, - 32 => ((int as i32) as u32) as u128, - 64 => ((int as i64) as u64) as u128, + 8 => u128::from((int as i8) as u8), + 16 => u128::from((int as i16) as u16), + 32 => u128::from((int as i32) as u32), + 64 => u128::from((int as i64) as u64), _ => unreachable!("ICE - invalid bit size {bit_size} for signed integer"), }; FieldElement::from(two_complement) @@ -365,8 +365,8 @@ impl BinaryOp { BinaryOp::And => |x, y| Some(x & y), BinaryOp::Or => |x, y| Some(x | y), BinaryOp::Xor => |x, y| Some(x ^ y), - BinaryOp::Eq => |x, y| Some((x == y) as u128), - BinaryOp::Lt => |x, y| Some((x < y) as u128), + BinaryOp::Eq => |x, y| Some(u128::from(x == y)), + BinaryOp::Lt => |x, y| Some(u128::from(x < y)), BinaryOp::Shl => |x, y| y.to_u32().and_then(|y| x.checked_shl(y)), BinaryOp::Shr => |x, y| y.to_u32().and_then(|y| x.checked_shr(y)), } @@ -382,8 +382,8 @@ impl BinaryOp { BinaryOp::And => |x, y| Some(x & y), BinaryOp::Or => |x, y| Some(x | y), BinaryOp::Xor => |x, y| Some(x ^ y), - BinaryOp::Eq => |x, y| Some((x == y) as i128), - BinaryOp::Lt => |x, y| Some((x < y) as i128), + BinaryOp::Eq => |x, y| Some(i128::from(x == y)), + BinaryOp::Lt => |x, y| Some(i128::from(x < y)), BinaryOp::Shl => |x, y| y.to_u32().and_then(|y| x.checked_shl(y)), BinaryOp::Shr => |x, y| y.to_u32().and_then(|y| x.checked_shr(y)), } @@ -454,7 +454,7 @@ mod test { for (i, u) in [(-1i64, u64::MAX), (-2i64, u64::MAX - 1), (1i64, 1u64)] { let i: i128 = i.into(); let f = convert_signed_integer_to_field_element(i, 64); - assert_eq!(f.to_u128(), u as u128); + assert_eq!(f.to_u128(), u128::from(u)); assert_eq!(i, try_convert_field_element_to_signed_integer(f, 64).unwrap()); } } diff --git a/compiler/noirc_evaluator/src/ssa/opt/basic_conditional.rs b/compiler/noirc_evaluator/src/ssa/opt/basic_conditional.rs index 016dd16ecb6..a30d025b0ef 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/basic_conditional.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/basic_conditional.rs @@ -207,7 +207,7 @@ fn block_cost(block: BasicBlockId, dfg: &DataFlowGraph) -> u32 { // check if index is in bound if let (Some(index), Some(len)) = (dfg.get_numeric_constant(*index), dfg.try_get_array_length(*array)) { // The index is in-bounds - if index.to_u128() < len as u128 { + if index.to_u128() < u128::from(len) { in_bound = true; } } diff --git a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs index 844ed67b07f..1a234a5093b 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs @@ -514,7 +514,7 @@ fn create_apply_functions( /// Transforms a [FunctionId] into a [FieldElement] fn function_id_to_field(function_id: FunctionId) -> FieldElement { - (function_id.to_u32() as u128).into() + u128::from(function_id.to_u32()).into() } /// Creates a single apply function to enable dispatch across multiple function variants diff --git a/compiler/noirc_evaluator/src/ssa/opt/die.rs b/compiler/noirc_evaluator/src/ssa/opt/die.rs index 57905f199c5..73cffafa28c 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/die.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/die.rs @@ -564,7 +564,7 @@ impl Context { ); let index = index.first(); let array_length = - function.dfg.make_constant((array_length as u128).into(), length_type); + function.dfg.make_constant(u128::from(array_length).into(), length_type); let is_index_out_of_bounds = function.dfg.insert_instruction_and_results( Instruction::binary(BinaryOp::Lt, index, array_length), diff --git a/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs b/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs index f55d4d05b54..740da29dece 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs @@ -1192,7 +1192,7 @@ impl<'f> Context<'f> { self.inserter .function .dfg - .make_constant(FieldElement::from(*elem as u32), NumericType::unsigned(8)) + .make_constant(FieldElement::from(u32::from(*elem)), NumericType::unsigned(8)) }) .collect(); let constant_array = Instruction::MakeArray { elements, typ: expected_array_type }; @@ -1226,7 +1226,7 @@ impl<'f> Context<'f> { condition: ValueId, call_stack: CallStackId, ) -> (ValueId, ValueId) { - let index = !abscissa as usize; + let index = usize::from(!abscissa); if inputs[3] == inputs[0] && inputs[4] == inputs[1] { // Point doubling let predicated_value = diff --git a/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs b/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs index 8467ff823e5..363378d26ce 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs @@ -152,7 +152,7 @@ impl<'a> ValueMerger<'a> { for i in 0..len { for (element_index, element_type) in element_types.iter().enumerate() { let index = - ((i * element_types.len() as u32 + element_index as u32) as u128).into(); + u128::from(i * element_types.len() as u32 + element_index as u32).into(); let index = self.dfg.make_constant(index, NumericType::length_type()); let typevars = Some(vec![element_type.clone()]); @@ -217,7 +217,7 @@ impl<'a> ValueMerger<'a> { for i in 0..len { for (element_index, element_type) in element_types.iter().enumerate() { let index_u32 = i * element_types.len() as u32 + element_index as u32; - let index_value = (index_u32 as u128).into(); + let index_value = u128::from(index_u32).into(); let index = self.dfg.make_constant(index_value, NumericType::length_type()); let typevars = Some(vec![element_type.clone()]); diff --git a/compiler/noirc_evaluator/src/ssa/opt/loop_invariant.rs b/compiler/noirc_evaluator/src/ssa/opt/loop_invariant.rs index 9fbad7ddecb..233fc1f086f 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/loop_invariant.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/loop_invariant.rs @@ -2020,17 +2020,47 @@ mod test { true, "nested loop empty, but add cannot overflow" )] - #[test_case("u32", 0, 10, 10, true, "add", (u32::MAX-5) as i64, true, "add overflows, and loop executes")] - #[test_case("u32", 0, 10, 0, true, "add", (u32::MAX-5) as i64, false, "add overflows, but loop empty")] - #[test_case("u32", 0, 10, 0, true, "unchecked_add", (u32::MAX-5) as i64, true, "loop empty, but add unchecked")] + #[test_case("u32", 0, 10, 10, true, "add", i64::from(u32::MAX - 5), true, "add overflows, and loop executes")] + #[test_case("u32", 0, 10, 0, true, "add", i64::from(u32::MAX - 5), false, "add overflows, but loop empty")] + #[test_case("u32", 0, 10, 0, true, "unchecked_add", i64::from(u32::MAX - 5), true, "loop empty, but add unchecked")] #[test_case("u32", 5, 10, 10, true, "sub", 5, true, "loop executes, sub cannot overflow")] #[test_case("u32", 0, 10, 10, true, "sub", 5, true, "sub overflows, and loop executes")] #[test_case("u32", 0, 10, 0, true, "sub", 5, false, "sub overflows, but loop empty")] #[test_case("u32", 0, 10, 0, true, "unchecked_sub", 5, true, "loop empty, but sub unchecked")] #[test_case("u32", 0, 10, 10, true, "mul", 10, true, "loop executes, mul cannot overflow")] - #[test_case("u32", 0, 10, 10, true, "mul", u32::MAX as i64, true, "mul overflows, and loop executes")] - #[test_case("u32", 0, 10, 0, true, "mul", u32::MAX as i64, false, "mul overflows, but loop empty")] - #[test_case("u32", 0, 10, 0, true, "unchecked_mul", u32::MAX as i64, true, "loop empty, but mul unchecked")] + #[test_case( + "u32", + 0, + 10, + 10, + true, + "mul", + i64::from(u32::MAX), + true, + "mul overflows, and loop executes" + )] + #[test_case( + "u32", + 0, + 10, + 0, + true, + "mul", + i64::from(u32::MAX), + false, + "mul overflows, but loop empty" + )] + #[test_case( + "u32", + 0, + 10, + 0, + true, + "unchecked_mul", + i64::from(u32::MAX), + true, + "loop empty, but mul unchecked" + )] #[test_case("u32", 0, 10, 10, true, "div", 2, true, "loop executes, div ok")] #[test_case("u32", 0, 10, 0, true, "div", 2, true, "loop empty, div ok")] #[test_case("u32", 0, 10, 10, true, "div", 0, true, "div by zero, and loop executes")] diff --git a/compiler/noirc_evaluator/src/ssa/opt/remove_bit_shifts.rs b/compiler/noirc_evaluator/src/ssa/opt/remove_bit_shifts.rs index 35a0c408c59..4c801ff07fb 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/remove_bit_shifts.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/remove_bit_shifts.rs @@ -317,7 +317,7 @@ impl Context<'_, '_, '_> { // All operations are unchecked as we're acting on Field types (which are always unchecked) for i in 1..max_exponent_bits + 1 { let idx = self.numeric_constant( - FieldElement::from((max_exponent_bits - i) as i128), + FieldElement::from(i128::from(max_exponent_bits - i)), NumericType::length_type(), ); let b = self.insert_array_get(exponent_bits, idx, Type::bool()); diff --git a/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs b/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs index 4d184e344a8..f2c15407d9f 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs @@ -770,7 +770,7 @@ impl Loop { fn count_all_instructions(&self, function: &Function) -> usize { let iter = self.blocks.iter().map(|block| { let block = &function.dfg[*block]; - block.instructions().len() + block.terminator().is_some() as usize + block.instructions().len() + usize::from(block.terminator().is_some()) }); iter.sum() } diff --git a/compiler/noirc_evaluator/src/ssa/parser/mod.rs b/compiler/noirc_evaluator/src/ssa/parser/mod.rs index 1e19d45abca..68ca089bc2c 100644 --- a/compiler/noirc_evaluator/src/ssa/parser/mod.rs +++ b/compiler/noirc_evaluator/src/ssa/parser/mod.rs @@ -662,7 +662,7 @@ impl<'a> Parser<'a> { .bytes() .map(|byte| { ParsedValue::NumericConstant(ParsedNumericConstant { - value: FieldElement::from(byte as u128), + value: FieldElement::from(u128::from(byte)), typ: u8.clone(), }) }) @@ -675,7 +675,7 @@ impl<'a> Parser<'a> { .bytes() .map(|byte| { ParsedValue::NumericConstant(ParsedNumericConstant { - value: FieldElement::from(byte as u128), + value: FieldElement::from(u128::from(byte)), typ: u8.clone(), }) }) diff --git a/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs b/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs index 93d76933e2e..d493c623a1a 100644 --- a/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs +++ b/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs @@ -769,8 +769,9 @@ impl<'a> FunctionContext<'a> { // Checks for index Out-of-bounds match array_type { Type::Array(_, len) => { - let len = - self.builder.numeric_constant(*len as u128, NumericType::length_type()); + let len = self + .builder + .numeric_constant(u128::from(*len), NumericType::length_type()); self.codegen_access_check(index, len); } _ => unreachable!("must have array or slice but got {array_type}"), diff --git a/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs b/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs index c9ef052c746..0dde800c226 100644 --- a/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs +++ b/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs @@ -253,7 +253,7 @@ impl FunctionContext<'_> { } ast::Literal::Bool(value) => { // Don't need to call checked_numeric_constant here since `value` can only be true or false - Ok(self.builder.numeric_constant(*value as u128, NumericType::bool()).into()) + Ok(self.builder.numeric_constant(u128::from(*value), NumericType::bool()).into()) } ast::Literal::Str(string) => Ok(self.codegen_string(string)), ast::Literal::FmtStr(fragments, number_of_fields, fields) => { @@ -278,7 +278,7 @@ impl FunctionContext<'_> { let string = self.codegen_string(&string); let field_count = self .builder - .numeric_constant(*number_of_fields as u128, NumericType::NativeField); + .numeric_constant(u128::from(*number_of_fields), NumericType::NativeField); let fields = self.codegen_expression(fields)?; Ok(Tree::Branch(vec![string, field_count.into(), fields])) @@ -296,7 +296,7 @@ impl FunctionContext<'_> { fn codegen_string(&mut self, string: &str) -> Values { let elements = vecmap(string.as_bytes(), |byte| { - self.builder.numeric_constant(*byte as u128, NumericType::char()).into() + self.builder.numeric_constant(u128::from(*byte), NumericType::char()).into() }); let typ = Self::convert_non_tuple_type(&ast::Type::String(elements.len() as u32)); self.codegen_array(elements, typ) @@ -475,7 +475,7 @@ impl FunctionContext<'_> { let runtime = self.builder.current_function.runtime(); if runtime.is_brillig() { let len = - self.builder.numeric_constant(*len as u128, NumericType::length_type()); + self.builder.numeric_constant(u128::from(*len), NumericType::length_type()); self.codegen_access_check(index, len); } } diff --git a/compiler/noirc_frontend/src/debug/mod.rs b/compiler/noirc_frontend/src/debug/mod.rs index c2b7527207f..d4a4e521f27 100644 --- a/compiler/noirc_frontend/src/debug/mod.rs +++ b/compiler/noirc_frontend/src/debug/mod.rs @@ -351,7 +351,7 @@ impl DebugInstrumenter { ast::LValue::MemberAccess { object, field_name, location } => { cursor = object; let field_name_id = self.insert_field_name(field_name.as_str()); - indexes.push(sint_expr(-(field_name_id.0 as i128), *location)); + indexes.push(sint_expr(-i128::from(field_name_id.0), *location)); } ast::LValue::Index { index, array, location: _ } => { cursor = array; @@ -656,7 +656,7 @@ fn build_assign_var_stmt(var_id: SourceVarId, expr: ast::Expression) -> ast::Sta location, }), is_macro_call: false, - arguments: vec![uint_expr(var_id.0 as u128, location), expr], + arguments: vec![uint_expr(u128::from(var_id.0), location), expr], })); ast::Statement { kind: ast::StatementKind::Semi(ast::Expression { kind, location }), location } } @@ -671,7 +671,7 @@ fn build_drop_var_stmt(var_id: SourceVarId, location: Location) -> ast::Statemen location, }), is_macro_call: false, - arguments: vec![uint_expr(var_id.0 as u128, location)], + arguments: vec![uint_expr(u128::from(var_id.0), location)], })); ast::Statement { kind: ast::StatementKind::Semi(ast::Expression { kind, location }), location } } @@ -696,7 +696,7 @@ fn build_assign_member_stmt( }), is_macro_call: false, arguments: [ - vec![uint_expr(var_id.0 as u128, location)], + vec![uint_expr(u128::from(var_id.0), location)], vec![expr.clone()], indexes.iter().rev().cloned().collect(), ] @@ -715,7 +715,7 @@ fn build_debug_call_stmt(fname: &str, fn_id: DebugFnId, location: Location) -> a location, }), is_macro_call: false, - arguments: vec![uint_expr(fn_id.0 as u128, location)], + arguments: vec![uint_expr(u128::from(fn_id.0), location)], })); ast::Statement { kind: ast::StatementKind::Semi(ast::Expression { kind, location }), location } } diff --git a/compiler/noirc_frontend/src/elaborator/enums.rs b/compiler/noirc_frontend/src/elaborator/enums.rs index 515165f6383..419716ca924 100644 --- a/compiler/noirc_frontend/src/elaborator/enums.rs +++ b/compiler/noirc_frontend/src/elaborator/enums.rs @@ -784,7 +784,7 @@ impl Elaborator<'_> { ($value:expr) => {{ let negative = $value < 0; // Widen the value so that SignedType::MIN does not wrap to 0 when negated below - let mut widened = $value as i128; + let mut widened = i128::from($value); if negative { widened = -widened; } @@ -800,8 +800,8 @@ impl Elaborator<'_> { Value::I32(value) => signed_to_signed_field!(value), Value::I64(value) => signed_to_signed_field!(value), Value::U1(value) => SignedField::positive(value), - Value::U8(value) => SignedField::positive(value as u128), - Value::U16(value) => SignedField::positive(value as u128), + Value::U8(value) => SignedField::positive(u128::from(value)), + Value::U16(value) => SignedField::positive(u128::from(value)), Value::U32(value) => SignedField::positive(value), Value::U64(value) => SignedField::positive(value), Value::U128(value) => SignedField::positive(value), diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs index 0059ae9e084..68ab0158cac 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs @@ -1736,11 +1736,11 @@ fn evaluate_prefix_with_value(rhs: Value, operator: UnaryOp, location: Location) fn to_u128(value: Value) -> Option { match value { - Value::U1(value) => Some(if value { 1_u128 } else { 0_u128 }), - Value::U8(value) => Some(value as u128), - Value::U16(value) => Some(value as u128), - Value::U32(value) => Some(value as u128), - Value::U64(value) => Some(value as u128), + Value::U1(value) => Some(u128::from(value)), + Value::U8(value) => Some(u128::from(value)), + Value::U16(value) => Some(u128::from(value)), + Value::U32(value) => Some(u128::from(value)), + Value::U64(value) => Some(u128::from(value)), Value::U128(value) => Some(value), _ => None, } @@ -1748,10 +1748,10 @@ fn to_u128(value: Value) -> Option { fn to_i128(value: Value) -> Option { match value { - Value::I8(value) => Some(value as i128), - Value::I16(value) => Some(value as i128), - Value::I32(value) => Some(value as i128), - Value::I64(value) => Some(value as i128), + Value::I8(value) => Some(i128::from(value)), + Value::I16(value) => Some(i128::from(value)), + Value::I32(value) => Some(i128::from(value)), + Value::I64(value) => Some(i128::from(value)), _ => None, } } diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin/builtin_helpers.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin/builtin_helpers.rs index 0fdb3719f35..1aa1987b955 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin/builtin_helpers.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin/builtin_helpers.rs @@ -640,7 +640,7 @@ pub(super) fn hash_item( let mut hasher = std::collections::hash_map::DefaultHasher::new(); item.hash(&mut hasher); let hash = hasher.finish(); - Ok(Value::Field(SignedField::positive(hash as u128))) + Ok(Value::Field(SignedField::positive(u128::from(hash)))) } pub(super) fn eq_item( diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/cast.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/cast.rs index d863bd0283e..f60b6cff42b 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/cast.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/cast.rs @@ -14,7 +14,7 @@ use crate::{ fn bit_size(typ: &Type) -> u32 { match typ { Type::FieldElement => FieldElement::max_num_bits(), - Type::Integer(_, bit_size) => bit_size.bit_size() as u32, + Type::Integer(_, bit_size) => u32::from(bit_size.bit_size()), Type::Bool => 2, _ => FieldElement::max_num_bits(), } @@ -104,18 +104,18 @@ fn convert_to_field(value: Value, location: Location) -> IResult<(FieldElement, Ok(match value { Value::Field(value) if value.is_negative() => (-value.absolute_value(), true), Value::Field(value) => (value.absolute_value(), false), - Value::U1(value) => ((value as u128).into(), false), - Value::U8(value) => ((value as u128).into(), false), - Value::U16(value) => ((value as u128).into(), false), - Value::U32(value) => ((value as u128).into(), false), - Value::U64(value) => ((value as u128).into(), false), + Value::U1(value) => (u128::from(value).into(), false), + Value::U8(value) => (u128::from(value).into(), false), + Value::U16(value) => (u128::from(value).into(), false), + Value::U32(value) => (u128::from(value).into(), false), + Value::U64(value) => (u128::from(value).into(), false), Value::U128(value) => (value.into(), false), // `is_negative` is only used for conversions to Field in which case // these should always be positive so that `-1 as i8 as Field == 255` - Value::I8(value) => (FieldElement::from(value as u8 as i128), false), - Value::I16(value) => (FieldElement::from(value as u16 as i128), false), - Value::I32(value) => (FieldElement::from(value as u32 as i128), false), - Value::I64(value) => (FieldElement::from(value as u64 as i128), false), + Value::I8(value) => (FieldElement::from(i128::from(value as u8)), false), + Value::I16(value) => (FieldElement::from(i128::from(value as u16)), false), + Value::I32(value) => (FieldElement::from(i128::from(value as u32)), false), + Value::I64(value) => (FieldElement::from(i128::from(value as u64)), false), Value::Bool(value) => (FieldElement::from(value), false), value => { let typ = value.get_type().into_owned(); diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/infix.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/infix.rs index 770026ba923..a45ed2f7359 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/infix.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/infix.rs @@ -195,6 +195,7 @@ pub(super) fn evaluate_infix( (lhs_value as lhs ">>" rhs_value as rhs) => { #[allow(unused_comparisons, clippy::absurd_extreme_comparisons)] if rhs > 127 {return Err(rhs_overflow);} + #[allow(clippy::cast_lossless)] lhs.checked_shr(rhs as u32) } }, @@ -203,9 +204,9 @@ pub(super) fn evaluate_infix( (lhs_value as lhs "<<" rhs_value as rhs) => { #[allow(unused_comparisons, clippy::absurd_extreme_comparisons)] if rhs > 127 {return Err(lhs_overflow);} - lhs.checked_shl( - rhs as u32 - )} + #[allow(clippy::cast_lossless)] + lhs.checked_shl(rhs as u32) + } }, BinaryOpKind::Modulo => match (&lhs_value, &rhs_value) { (Value::I8(i8::MIN), Value::I8(-1)) => Ok(Value::I8(0)), diff --git a/compiler/noirc_frontend/src/hir/comptime/value.rs b/compiler/noirc_frontend/src/hir/comptime/value.rs index 10b4e33e4b7..1551d6d3862 100644 --- a/compiler/noirc_frontend/src/hir/comptime/value.rs +++ b/compiler/noirc_frontend/src/hir/comptime/value.rs @@ -214,11 +214,11 @@ impl Value { Some(IntegerTypeSuffix::U1), )), Value::U8(value) => ExpressionKind::Literal(Literal::Integer( - SignedField::positive(value as u128), + SignedField::positive(u128::from(value)), Some(IntegerTypeSuffix::U8), )), Value::U16(value) => ExpressionKind::Literal(Literal::Integer( - SignedField::positive(value as u128), + SignedField::positive(u128::from(value)), Some(IntegerTypeSuffix::U16), )), Value::U32(value) => ExpressionKind::Literal(Literal::Integer( @@ -387,12 +387,12 @@ impl Value { Value::U1(value) => { HirExpression::Literal(HirLiteral::Integer(SignedField::positive(value))) } - Value::U8(value) => { - HirExpression::Literal(HirLiteral::Integer(SignedField::positive(value as u128))) - } - Value::U16(value) => { - HirExpression::Literal(HirLiteral::Integer(SignedField::positive(value as u128))) - } + Value::U8(value) => HirExpression::Literal(HirLiteral::Integer(SignedField::positive( + u128::from(value), + ))), + Value::U16(value) => HirExpression::Literal(HirLiteral::Integer( + SignedField::positive(u128::from(value)), + )), Value::U32(value) => { HirExpression::Literal(HirLiteral::Integer(SignedField::positive(value))) } @@ -540,18 +540,18 @@ impl Value { } Value::TypedExpr(TypedExpr::ExprId(expr_id)) => vec![Token::UnquoteMarker(expr_id)], Value::Bool(bool) => vec![Token::Bool(bool)], - Value::U1(bool) => vec![Token::Int((bool as u128).into(), None)], + Value::U1(bool) => vec![Token::Int(u128::from(bool).into(), None)], Value::U8(value) => { - vec![Token::Int((value as u128).into(), None)] + vec![Token::Int(u128::from(value).into(), None)] } Value::U16(value) => { - vec![Token::Int((value as u128).into(), None)] + vec![Token::Int(u128::from(value).into(), None)] } Value::U32(value) => { - vec![Token::Int((value as u128).into(), None)] + vec![Token::Int(u128::from(value).into(), None)] } Value::U64(value) => { - vec![Token::Int((value as u128).into(), None)] + vec![Token::Int(u128::from(value).into(), None)] } Value::U128(value) => vec![Token::Int(value.into(), None)], Value::I8(value) => { @@ -694,10 +694,10 @@ impl Value { Self::I32(value) => (*value >= 0).then_some((*value as u128).into()), Self::I64(value) => (*value >= 0).then_some((*value as u128).into()), Self::U1(value) => Some(if *value { SignedField::one() } else { SignedField::zero() }), - Self::U8(value) => Some((*value as u128).into()), - Self::U16(value) => Some((*value as u128).into()), - Self::U32(value) => Some((*value as u128).into()), - Self::U64(value) => Some((*value as u128).into()), + Self::U8(value) => Some(u128::from(*value).into()), + Self::U16(value) => Some(u128::from(*value).into()), + Self::U32(value) => Some(u128::from(*value).into()), + Self::U64(value) => Some(u128::from(*value).into()), Self::U128(value) => Some((*value).into()), _ => None, } diff --git a/compiler/noirc_frontend/src/monomorphization/mod.rs b/compiler/noirc_frontend/src/monomorphization/mod.rs index 1418fc9cc14..7698c9af529 100644 --- a/compiler/noirc_frontend/src/monomorphization/mod.rs +++ b/compiler/noirc_frontend/src/monomorphization/mod.rs @@ -1950,7 +1950,7 @@ impl<'interner> Monomorphizer<'interner> { let int_type = Type::Integer(Signedness::Unsigned, arr_elem_bits); let bytes_as_expr = vecmap(bytes, |byte| { - let value = SignedField::positive(byte as u32); + let value = SignedField::positive(u32::from(byte)); Expression::Literal(Literal::Integer(value, int_type.clone(), location)) }); diff --git a/tooling/ast_fuzzer/src/program/expr.rs b/tooling/ast_fuzzer/src/program/expr.rs index cc2bb62951d..9258d716d6c 100644 --- a/tooling/ast_fuzzer/src/program/expr.rs +++ b/tooling/ast_fuzzer/src/program/expr.rs @@ -39,37 +39,36 @@ pub fn gen_literal( Expression::Literal(Literal::Integer(field, Type::Field, Location::dummy())) } Type::Integer(signedness, integer_bit_size) => { - let (field, is_negative) = - if signedness.is_signed() { - match integer_bit_size { - One => bool::arbitrary(u).map(|n| (Field::from(n), n))?, - Eight => i8::arbitrary(u) - .map(|n| (Field::from(n.unsigned_abs() as u32), n < 0))?, - Sixteen => i16::arbitrary(u) - .map(|n| (Field::from(n.unsigned_abs() as u32), n < 0))?, - ThirtyTwo => { - i32::arbitrary(u).map(|n| (Field::from(n.unsigned_abs()), n < 0))? - } - SixtyFour => { - i64::arbitrary(u).map(|n| (Field::from(n.unsigned_abs()), n < 0))? - } - HundredTwentyEight => { - // `ssa_gen::FunctionContext::checked_numeric_constant` doesn't allow negative - // values with 128 bits, so let's stick to the positive range. - i128::arbitrary(u).map(|n| (Field::from(n.abs()), false))? - } + let (field, is_negative) = if signedness.is_signed() { + match integer_bit_size { + One => bool::arbitrary(u).map(|n| (Field::from(n), n))?, + Eight => i8::arbitrary(u) + .map(|n| (Field::from(u32::from(n.unsigned_abs())), n < 0))?, + Sixteen => i16::arbitrary(u) + .map(|n| (Field::from(u32::from(n.unsigned_abs())), n < 0))?, + ThirtyTwo => { + i32::arbitrary(u).map(|n| (Field::from(n.unsigned_abs()), n < 0))? } - } else { - let f = match integer_bit_size { - One => Field::from(bool::arbitrary(u)?), - Eight => Field::from(u8::arbitrary(u)? as u32), - Sixteen => Field::from(u16::arbitrary(u)? as u32), - ThirtyTwo => Field::from(u32::arbitrary(u)?), - SixtyFour => Field::from(u64::arbitrary(u)?), - HundredTwentyEight => Field::from(u128::arbitrary(u)?), - }; - (f, false) + SixtyFour => { + i64::arbitrary(u).map(|n| (Field::from(n.unsigned_abs()), n < 0))? + } + HundredTwentyEight => { + // `ssa_gen::FunctionContext::checked_numeric_constant` doesn't allow negative + // values with 128 bits, so let's stick to the positive range. + i128::arbitrary(u).map(|n| (Field::from(n.abs()), false))? + } + } + } else { + let f = match integer_bit_size { + One => Field::from(bool::arbitrary(u)?), + Eight => Field::from(u32::from(u8::arbitrary(u)?)), + Sixteen => Field::from(u32::from(u16::arbitrary(u)?)), + ThirtyTwo => Field::from(u32::arbitrary(u)?), + SixtyFour => Field::from(u64::arbitrary(u)?), + HundredTwentyEight => Field::from(u128::arbitrary(u)?), }; + (f, false) + }; Expression::Literal(Literal::Integer( SignedField::new(field, is_negative), @@ -139,15 +138,15 @@ pub fn gen_range( Eight => { let s = i8::arbitrary(u)?; let e = s.saturating_add_unsigned(u.choose_index(max_size)? as u8); - let s = (Field::from(s.unsigned_abs() as u32), s < 0); - let e = (Field::from(e.unsigned_abs() as u32), e < 0); + let s = (Field::from(u32::from(s.unsigned_abs())), s < 0); + let e = (Field::from(u32::from(e.unsigned_abs())), e < 0); (s, e) } Sixteen => { let s = i16::arbitrary(u)?; let e = s.saturating_add_unsigned(u.choose_index(max_size)? as u16); - let s = (Field::from(s.unsigned_abs() as u32), s < 0); - let e = (Field::from(e.unsigned_abs() as u32), e < 0); + let s = (Field::from(u32::from(s.unsigned_abs())), s < 0); + let e = (Field::from(u32::from(e.unsigned_abs())), e < 0); (s, e) } ThirtyTwo => { @@ -171,15 +170,15 @@ pub fn gen_range( Eight => { let s = u8::arbitrary(u)?; let e = s.saturating_add(u.choose_index(max_size)? as u8); - let s = Field::from(s as u32); - let e = Field::from(e as u32); + let s = Field::from(u32::from(s)); + let e = Field::from(u32::from(e)); (s, e) } Sixteen => { let s = u16::arbitrary(u)?; let e = s.saturating_add(u.choose_index(max_size)? as u16); - let s = Field::from(s as u32); - let e = Field::from(e as u32); + let s = Field::from(u32::from(s)); + let e = Field::from(u32::from(e)); (s, e) } ThirtyTwo => { @@ -266,7 +265,7 @@ where /// 8-bit unsigned int literal, used in bit shifts. pub fn u8_literal(value: u8) -> Expression { - int_literal(value as u32, false, types::U8) + int_literal(u32::from(value), false, types::U8) } /// 32-bit unsigned int literal, used in indexing arrays. diff --git a/tooling/greybox_fuzzer/src/mutation/dictionary.rs b/tooling/greybox_fuzzer/src/mutation/dictionary.rs index 1d3ab8acd71..248b2523463 100644 --- a/tooling/greybox_fuzzer/src/mutation/dictionary.rs +++ b/tooling/greybox_fuzzer/src/mutation/dictionary.rs @@ -127,7 +127,7 @@ impl FullDictionary { _ => panic!("Shouldn't be used with other input value types"), }; for character in initial_string.as_bytes().iter() { - full_dictionary.insert(FieldElement::from(*character as i128)); + full_dictionary.insert(FieldElement::from(i128::from(*character))); } } AbiType::Array { length: _, typ } => { diff --git a/tooling/greybox_fuzzer/src/mutation/int.rs b/tooling/greybox_fuzzer/src/mutation/int.rs index 992f07e1977..6670b9a0841 100644 --- a/tooling/greybox_fuzzer/src/mutation/int.rs +++ b/tooling/greybox_fuzzer/src/mutation/int.rs @@ -171,7 +171,7 @@ fn add_small>( update: i8, ) -> FieldElement { let converted: T = T::from(*input).expect("Primitive should convert"); - let update_t: T = T::from(update as i128).expect("Primitive should convert"); + let update_t: T = T::from(i128::from(update)).expect("Primitive should convert"); let (after_update, _) = converted.overflowing_add(&update_t); i128_to_field(after_update.as_(), T::BITS) } @@ -182,7 +182,7 @@ fn wrapping_add_small_unsigned FieldElement { let converted: T = T::from(*input).expect("Primitive should convert"); - let update_t: T = T::from(update as u128).expect("Primitive should convert"); + let update_t: T = T::from(u128::from(update)).expect("Primitive should convert"); let after_update = converted.wrapping_add(&update_t); u128_to_field(after_update.as_()) } @@ -193,7 +193,7 @@ fn wrapping_sub_small_unsigned FieldElement { let converted: T = T::from(*input).expect("Primitive should convert"); - let update_t: T = T::from(update as u128).expect("Primitive should convert"); + let update_t: T = T::from(u128::from(update)).expect("Primitive should convert"); let after_update = converted.wrapping_sub(&update_t); u128_to_field(after_update.as_()) } @@ -393,10 +393,10 @@ impl<'a> IntMutator<'a> { FixedIntSubstitutionOptions::Pow2 => 2i128.pow(self.prng.gen_range(0..(width - 1))), }; let checked_value = match width { - 8 => value.clamp(i8::MIN as i128, i8::MAX as i128), - 16 => value.clamp(i16::MIN as i128, i16::MAX as i128), - 32 => value.clamp(i32::MIN as i128, i32::MAX as i128), - 64 => value.clamp(i64::MIN as i128, i64::MAX as i128), + 8 => value.clamp(i128::from(i8::MIN), i128::from(i8::MAX)), + 16 => value.clamp(i128::from(i16::MIN), i128::from(i16::MAX)), + 32 => value.clamp(i128::from(i32::MIN), i128::from(i32::MAX)), + 64 => value.clamp(i128::from(i64::MIN), i128::from(i64::MAX)), 128 => value, _ => { panic!("Shouldn't be reachable") diff --git a/tooling/greybox_fuzzer/src/mutation/mod.rs b/tooling/greybox_fuzzer/src/mutation/mod.rs index 14463d49448..0e44cbc49da 100644 --- a/tooling/greybox_fuzzer/src/mutation/mod.rs +++ b/tooling/greybox_fuzzer/src/mutation/mod.rs @@ -213,7 +213,7 @@ impl InputMutator { }; // This is an array and can be structurally mutated if the number of elements is more than one // This is an array and can be structurally mutated if the number of elements is more than one - let arrays_hit = arrays_hit + ((length > 1) as usize); + let arrays_hit = arrays_hit + usize::from(length > 1); let mut structural_mutation_directive = None; let mut element_vector_with_value_mutation: Vec = (0..length) .zip(weight_tree_node.subnodes.as_ref().unwrap()) diff --git a/tooling/nargo/src/foreign_calls/rpc.rs b/tooling/nargo/src/foreign_calls/rpc.rs index b139a268317..82f27ace792 100644 --- a/tooling/nargo/src/foreign_calls/rpc.rs +++ b/tooling/nargo/src/foreign_calls/rpc.rs @@ -222,7 +222,7 @@ mod server_tests { let response = match req.function_call.function.as_str() { "sum" => self.sum(req.function_call.inputs[0].clone()), "echo" => self.echo(req.function_call.inputs[0].clone()), - "id" => FieldElement::from(req.session_id as u128).into(), + "id" => FieldElement::from(u128::from(req.session_id)).into(), _ => panic!("unexpected foreign call"), }; Ok(response) diff --git a/tooling/nargo_cli/tests/comptime_correctness.rs b/tooling/nargo_cli/tests/comptime_correctness.rs index 4dab4b01e54..a6913fa7125 100644 --- a/tooling/nargo_cli/tests/comptime_correctness.rs +++ b/tooling/nargo_cli/tests/comptime_correctness.rs @@ -183,7 +183,7 @@ fn comptime_check_field_and() { #[ignore] fn comptime_check_field_shl() { let strategy = any::<(u32, u8)>() - .prop_map(|(a, b)| (format!("{a} << {b}"), "a << b", a, b as u32)) + .prop_map(|(a, b)| (format!("{a} << {b}"), "a << b", a, u32::from(b))) .boxed(); comptime_check_field_expression(strategy, *NUM_CASES, false); @@ -193,7 +193,7 @@ fn comptime_check_field_shl() { #[ignore] fn comptime_check_field_shr() { let strategy = any::<(u32, u8)>() - .prop_map(|(a, b)| (format!("{a} >> {b}"), "a >> b", a, b as u32)) + .prop_map(|(a, b)| (format!("{a} >> {b}"), "a >> b", a, u32::from(b))) .boxed(); comptime_check_field_expression(strategy, *NUM_CASES, false); diff --git a/tooling/nargo_cli/tests/stdlib-props.rs b/tooling/nargo_cli/tests/stdlib-props.rs index d708cc47b12..19b135646f8 100644 --- a/tooling/nargo_cli/tests/stdlib-props.rs +++ b/tooling/nargo_cli/tests/stdlib-props.rs @@ -110,7 +110,7 @@ fn get_unsigned_strategies() -> Vec<(u32, BoxedStrategy)> { let strategy_u8 = any::() .prop_map(|x| { SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], + vec![("x", InputValue::Field(u128::from(x).into()))], InputValue::Field(0_u128.into()), ) }) @@ -118,7 +118,7 @@ fn get_unsigned_strategies() -> Vec<(u32, BoxedStrategy)> { let strategy_u16 = any::() .prop_map(|x| { SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], + vec![("x", InputValue::Field(u128::from(x).into()))], InputValue::Field(0_u128.into()), ) }) @@ -126,7 +126,7 @@ fn get_unsigned_strategies() -> Vec<(u32, BoxedStrategy)> { let strategy_u32 = any::() .prop_map(|x| { SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], + vec![("x", InputValue::Field(u128::from(x).into()))], InputValue::Field(0_u128.into()), ) }) @@ -134,7 +134,7 @@ fn get_unsigned_strategies() -> Vec<(u32, BoxedStrategy)> { let strategy_u64 = any::() .prop_map(|x| { SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], + vec![("x", InputValue::Field(u128::from(x).into()))], InputValue::Field(0_u128.into()), ) }) @@ -162,7 +162,7 @@ fn get_signed_strategies() -> Vec<(u32, BoxedStrategy)> { x = 0; } SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], + vec![("x", InputValue::Field(u128::from(x).into()))], InputValue::Field(0_u128.into()), ) }) @@ -174,7 +174,7 @@ fn get_signed_strategies() -> Vec<(u32, BoxedStrategy)> { x = 0; } SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], + vec![("x", InputValue::Field(u128::from(x).into()))], InputValue::Field(0_u128.into()), ) }) @@ -185,7 +185,7 @@ fn get_signed_strategies() -> Vec<(u32, BoxedStrategy)> { x = 0; } SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], + vec![("x", InputValue::Field(u128::from(x).into()))], InputValue::Field(0_u128.into()), ) }) @@ -197,24 +197,24 @@ fn get_truncate_strategies() -> Vec<(u32, BoxedStrategy)> { let strategy_u16 = any::() .prop_map(|x| { SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], - InputValue::Field((x as u128).into()), + vec![("x", InputValue::Field(u128::from(x).into()))], + InputValue::Field(u128::from(x).into()), ) }) .boxed(); let strategy_u32 = any::() .prop_map(|x| { SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], - InputValue::Field((x as u128).into()), + vec![("x", InputValue::Field(u128::from(x).into()))], + InputValue::Field(u128::from(x).into()), ) }) .boxed(); let strategy_u64 = any::() .prop_map(|x| { SnippetInputOutput::new( - vec![("x", InputValue::Field((x as u128).into()))], - InputValue::Field((x as u128).into()), + vec![("x", InputValue::Field(u128::from(x).into()))], + InputValue::Field(u128::from(x).into()), ) }) .boxed(); diff --git a/tooling/noirc_abi/src/input_parser/json.rs b/tooling/noirc_abi/src/input_parser/json.rs index 7391c5d62a3..72d61a026ef 100644 --- a/tooling/noirc_abi/src/input_parser/json.rs +++ b/tooling/noirc_abi/src/input_parser/json.rs @@ -166,7 +166,7 @@ impl InputValue { JsonTypes::Integer(integer), AbiType::Integer { sign: crate::Sign::Signed, width }, ) => { - let new_value = parse_integer_to_signed(integer as i128, *width, arg_name)?; + let new_value = parse_integer_to_signed(i128::from(integer), *width, arg_name)?; InputValue::Field(new_value) } diff --git a/tooling/noirc_abi/src/input_parser/toml.rs b/tooling/noirc_abi/src/input_parser/toml.rs index 0314fe3de6c..e35470903d8 100644 --- a/tooling/noirc_abi/src/input_parser/toml.rs +++ b/tooling/noirc_abi/src/input_parser/toml.rs @@ -151,7 +151,7 @@ impl InputValue { TomlTypes::Integer(integer), AbiType::Integer { sign: crate::Sign::Signed, width }, ) => { - let new_value = parse_integer_to_signed(integer as i128, *width, arg_name)?; + let new_value = parse_integer_to_signed(i128::from(integer), *width, arg_name)?; InputValue::Field(new_value) } diff --git a/tooling/ssa_fuzzer/fuzzer/src/acir_vs_brillig.rs b/tooling/ssa_fuzzer/fuzzer/src/acir_vs_brillig.rs index e9f26ab40d9..6fb679cc29d 100644 --- a/tooling/ssa_fuzzer/fuzzer/src/acir_vs_brillig.rs +++ b/tooling/ssa_fuzzer/fuzzer/src/acir_vs_brillig.rs @@ -98,7 +98,7 @@ libfuzzer_sys::fuzz_target!(|data: &[u8]| -> Corpus { }); libfuzzer_sys::fuzz_mutator!(|data: &mut [u8], _size: usize, max_size: usize, seed: u32| { - let mut rng = StdRng::seed_from_u64(seed as u64); + let mut rng = StdRng::seed_from_u64(u64::from(seed)); let mut new_fuzzer_data: FuzzerData = borrow_decode_from_slice(data, bincode::config::legacy()) .unwrap_or((FuzzerData::default(), 1337)) .0; diff --git a/tooling/ssa_fuzzer/fuzzer/src/brillig.rs b/tooling/ssa_fuzzer/fuzzer/src/brillig.rs index 8d821d0fa0c..3881f8e1d49 100644 --- a/tooling/ssa_fuzzer/fuzzer/src/brillig.rs +++ b/tooling/ssa_fuzzer/fuzzer/src/brillig.rs @@ -89,7 +89,7 @@ libfuzzer_sys::fuzz_target!(|data: &[u8]| -> Corpus { }); libfuzzer_sys::fuzz_mutator!(|data: &mut [u8], _size: usize, max_size: usize, seed: u32| { - let mut rng = StdRng::seed_from_u64(seed as u64); + let mut rng = StdRng::seed_from_u64(u64::from(seed)); let mut new_fuzzer_data: FuzzerData = borrow_decode_from_slice(data, bincode::config::legacy()) .unwrap_or((FuzzerData::default(), 1337)) .0; diff --git a/tooling/ssa_fuzzer/fuzzer/src/fuzz_lib/initial_witness.rs b/tooling/ssa_fuzzer/fuzzer/src/fuzz_lib/initial_witness.rs index 7f3de345679..eb3b1e672b2 100644 --- a/tooling/ssa_fuzzer/fuzzer/src/fuzz_lib/initial_witness.rs +++ b/tooling/ssa_fuzzer/fuzzer/src/fuzz_lib/initial_witness.rs @@ -65,13 +65,13 @@ impl From for FieldElement { WitnessValueNumeric::U128(u128) => FieldElement::from(u128), WitnessValueNumeric::U64(u64) => FieldElement::from(u64), WitnessValueNumeric::U32(u32) => FieldElement::from(u32), - WitnessValueNumeric::U16(u16) => FieldElement::from(u16 as u64), - WitnessValueNumeric::U8(u8) => FieldElement::from(u8 as u64), + WitnessValueNumeric::U16(u16) => FieldElement::from(u64::from(u16)), + WitnessValueNumeric::U8(u8) => FieldElement::from(u64::from(u8)), WitnessValueNumeric::Boolean(bool) => FieldElement::from(bool), WitnessValueNumeric::I64(i64) => FieldElement::from(i64), - WitnessValueNumeric::I32(i32) => FieldElement::from(i32 as u64), - WitnessValueNumeric::I16(i16) => FieldElement::from(i16 as u64), - WitnessValueNumeric::I8(i8) => FieldElement::from(i8 as u64), + WitnessValueNumeric::I32(i32) => FieldElement::from(u64::from(i32)), + WitnessValueNumeric::I16(i16) => FieldElement::from(u64::from(i16)), + WitnessValueNumeric::I8(i8) => FieldElement::from(u64::from(i8)), } } } diff --git a/tooling/ssa_fuzzer/fuzzer/src/mutations/initial_witness/numeric_witness.rs b/tooling/ssa_fuzzer/fuzzer/src/mutations/initial_witness/numeric_witness.rs index 90f6f769bbe..c3b95697574 100644 --- a/tooling/ssa_fuzzer/fuzzer/src/mutations/initial_witness/numeric_witness.rs +++ b/tooling/ssa_fuzzer/fuzzer/src/mutations/initial_witness/numeric_witness.rs @@ -195,14 +195,14 @@ impl WitnessAddSubPowerOfTwoMutation { WitnessValueNumeric::Field(FieldRepresentation { high: if !sign { - field.high.wrapping_add((is_high as i128 * power_of_two) as u128) + field.high.wrapping_add((i128::from(is_high) * power_of_two) as u128) } else { - field.high.wrapping_sub((is_high as i128 * power_of_two) as u128) + field.high.wrapping_sub((i128::from(is_high) * power_of_two) as u128) }, low: if !sign { - field.low.wrapping_add((!is_high as i128 * power_of_two) as u128) + field.low.wrapping_add((i128::from(!is_high) * power_of_two) as u128) } else { - field.low.wrapping_sub((!is_high as i128 * power_of_two) as u128) + field.low.wrapping_sub((i128::from(!is_high) * power_of_two) as u128) }, }) } diff --git a/tooling/ssa_fuzzer/fuzzer/src/utils/redis.rs b/tooling/ssa_fuzzer/fuzzer/src/utils/redis.rs index 4fb1b7b86e8..60392f51c84 100644 --- a/tooling/ssa_fuzzer/fuzzer/src/utils/redis.rs +++ b/tooling/ssa_fuzzer/fuzzer/src/utils/redis.rs @@ -43,7 +43,9 @@ impl RedisManager { return Err(e); } - std::thread::sleep(Duration::from_millis(100 * self.reconnect_attempts as u64)); + std::thread::sleep(Duration::from_millis( + 100 * u64::from(self.reconnect_attempts), + )); return self.get_connection(); } } diff --git a/tooling/ssa_fuzzer/src/builder.rs b/tooling/ssa_fuzzer/src/builder.rs index 856aed38897..c1e3e4d5701 100644 --- a/tooling/ssa_fuzzer/src/builder.rs +++ b/tooling/ssa_fuzzer/src/builder.rs @@ -430,7 +430,7 @@ impl FuzzerBuilder { .import_intrinsic("to_le_radix") .expect("to_le_radix intrinsic should be available"); let element_type = Type::Numeric(NumericType::U8); - let result_type = Type::Array(Arc::new(vec![element_type.clone()]), limb_count as u32); + let result_type = Type::Array(Arc::new(vec![element_type.clone()]), u32::from(limb_count)); let result = self.builder.insert_call( intrinsic, vec![field_value.value_id, radix], @@ -953,7 +953,7 @@ impl FuzzerBuilder { fn bytes_to_ssa_array(&mut self, vec: Vec) -> TypedValue { let elements: Vec> = vec .into_iter() - .map(|x| self.builder.numeric_constant(x as u32, NumericType::U8.into())) + .map(|x| self.builder.numeric_constant(u32::from(x), NumericType::U8.into())) .collect(); let array_type = Type::Array(Arc::new(vec![Type::Numeric(NumericType::U8)]), elements.len() as u32); @@ -966,7 +966,7 @@ impl FuzzerBuilder { fn bytes_to_ssa_slice(&mut self, vec: Vec) -> TypedValue { let elements: Vec> = vec .into_iter() - .map(|x| self.builder.numeric_constant(x as u32, NumericType::U8.into())) + .map(|x| self.builder.numeric_constant(u32::from(x), NumericType::U8.into())) .collect(); let slice_type = Type::Slice(Arc::new(vec![Type::Numeric(NumericType::U8)])); TypedValue::new(