Skip to content

Commit

Permalink
Cleanup stdlib warnings (FuelLabs#4648)
Browse files Browse the repository at this point in the history
## Description

This PR cleans up stdlib warnings that have accumulated.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] 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.
  • Loading branch information
tritao authored Jun 13, 2023
1 parent 733d9b5 commit 52ac9b6
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ impl ty::TyIntrinsicFunctionKind {
| Intrinsic::And
| Intrinsic::Or
| Intrinsic::Xor
| Intrinsic::Mod
| Intrinsic::Lsh
| Intrinsic::Rsh => type_check_binary_op(ctx, kind, arguments, type_arguments, span),
| Intrinsic::Mod => type_check_binary_op(ctx, kind, arguments, type_arguments, span),
Intrinsic::Lsh | Intrinsic::Rsh => {
type_check_shift_binary_op(ctx, kind, arguments, type_arguments, span)
}
Intrinsic::Revert => type_check_revert(ctx, kind, arguments, type_arguments, span),
Intrinsic::PtrAdd | Intrinsic::PtrSub => {
type_check_ptr_ops(ctx, kind, arguments, type_arguments, span)
Expand Down Expand Up @@ -1063,6 +1064,124 @@ fn type_check_binary_op(
)
}

/// Signature: `__lsh<T, U>(lhs: T, rhs: U) -> T`
/// Description: Logical left shifts the `lhs` by the `rhs` and returns the result.
/// Constraints: `T` and `U` are an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
///
/// Signature: `__rsh<T, U>(lhs: T, rhs: U) -> T`
/// Description: Logical right shifts the `lhs` by the `rhs` and returns the result.
/// Constraints: `T` and `U` are an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
fn type_check_shift_binary_op(
mut ctx: TypeCheckContext,
kind: sway_ast::Intrinsic,
arguments: Vec<Expression>,
type_arguments: Vec<TypeArgument>,
span: Span,
) -> CompileResult<(ty::TyIntrinsicFunctionKind, TypeId)> {
let type_engine = ctx.engines.te();
let engines = ctx.engines();

let mut warnings = vec![];
let mut errors = vec![];

if arguments.len() != 2 {
errors.push(CompileError::IntrinsicIncorrectNumArgs {
name: kind.to_string(),
expected: 2,
span,
});
return err(warnings, errors);
}
if !type_arguments.is_empty() {
errors.push(CompileError::IntrinsicIncorrectNumTArgs {
name: kind.to_string(),
expected: 0,
span,
});
return err(warnings, errors);
}

let mut ctx = ctx
.by_ref()
.with_type_annotation(type_engine.insert(engines, TypeInfo::Unknown));

let lhs = arguments[0].clone();
let lhs = check!(
ty::TyExpression::type_check(ctx.by_ref(), lhs),
return err(warnings, errors),
warnings,
errors
);

// Check for supported argument types
let arg_ty = check!(
CompileResult::from(
type_engine
.to_typeinfo(lhs.return_type, &lhs.span)
.map_err(CompileError::from)
),
TypeInfo::ErrorRecovery,
warnings,
errors
);
let is_valid_arg_ty = matches!(arg_ty, TypeInfo::UnsignedInteger(_));
if !is_valid_arg_ty {
errors.push(CompileError::IntrinsicUnsupportedArgType {
name: kind.to_string(),
span: lhs.span,
hint: Hint::empty(),
});
return err(warnings, errors);
}

let ctx = ctx
.by_ref()
.with_type_annotation(type_engine.insert(engines, TypeInfo::Unknown));

let rhs = arguments[1].clone();
let rhs = check!(
ty::TyExpression::type_check(ctx, rhs),
return err(warnings, errors),
warnings,
errors
);

// Check for supported argument types
let rhs_ty = check!(
CompileResult::from(
type_engine
.to_typeinfo(rhs.return_type, &rhs.span)
.map_err(CompileError::from)
),
TypeInfo::ErrorRecovery,
warnings,
errors
);
let is_valid_rhs_ty = matches!(rhs_ty, TypeInfo::UnsignedInteger(_));
if !is_valid_rhs_ty {
errors.push(CompileError::IntrinsicUnsupportedArgType {
name: kind.to_string(),
span: lhs.span,
hint: Hint::empty(),
});
return err(warnings, errors);
}

ok(
(
ty::TyIntrinsicFunctionKind {
kind,
arguments: vec![lhs, rhs],
type_arguments: vec![],
span,
},
type_engine.insert(engines, arg_ty),
),
warnings,
errors,
)
}

/// Signature: `__revert(code: u64)`
/// Description: Reverts with error code `code`.
/// Constraints: None.
Expand Down
108 changes: 54 additions & 54 deletions sway-lib-core/src/primitive_conversions.sw
Original file line number Diff line number Diff line change
Expand Up @@ -330,19 +330,19 @@ fn test_u64_to_le_bytes() {
let x: u64 = 578437695752307201;
let result = x.to_le_bytes();

assert(result[0] == 1);
assert(result[1] == 2);
assert(result[2] == 3);
assert(result[3] == 4);
assert(result[4] == 5);
assert(result[5] == 6);
assert(result[6] == 7);
assert(result[7] == 8);
assert(result[0] == 1_u8);
assert(result[1] == 2_u8);
assert(result[2] == 3_u8);
assert(result[3] == 4_u8);
assert(result[4] == 5_u8);
assert(result[5] == 6_u8);
assert(result[6] == 7_u8);
assert(result[7] == 8_u8);
}

#[test]
fn test_u64_from_le_bytes() {
let bytes = [1, 2, 3, 4, 5, 6, 7, 8];
let bytes = [1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8];
let result = u64::from_le_bytes(bytes);

assert(result == 578437695752307201);
Expand All @@ -353,19 +353,19 @@ fn test_u64_to_be_bytes() {
let x: u64 = 578437695752307201;
let result = x.to_be_bytes();

assert(result[0] == 8);
assert(result[1] == 7);
assert(result[2] == 6);
assert(result[3] == 5);
assert(result[4] == 4);
assert(result[5] == 3);
assert(result[6] == 2);
assert(result[7] == 1);
assert(result[0] == 8_u8);
assert(result[1] == 7_u8);
assert(result[2] == 6_u8);
assert(result[3] == 5_u8);
assert(result[4] == 4_u8);
assert(result[5] == 3_u8);
assert(result[6] == 2_u8);
assert(result[7] == 1_u8);
}

#[test]
fn test_u64_from_be_bytes() {
let bytes = [8, 7, 6, 5, 4, 3, 2, 1];
let bytes = [8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8, 2_u8, 1_u8];
let result = u64::from_be_bytes(bytes);

assert(result == 578437695752307201);
Expand All @@ -376,79 +376,79 @@ fn test_u32_to_le_bytes() {
let x: u32 = 67305985;
let result = x.to_le_bytes();

assert(result[0] == 1);
assert(result[1] == 2);
assert(result[2] == 3);
assert(result[3] == 4);
assert(result[0] == 1_u8);
assert(result[1] == 2_u8);
assert(result[2] == 3_u8);
assert(result[3] == 4_u8);
}

#[test]
fn test_u32_from_le_bytes() {
let bytes = [1, 2, 3, 4];
let bytes = [1_u8, 2_u8, 3_u8, 4_u8];
let result = u32::from_le_bytes(bytes);

assert(result == 67305985);
assert(result == 67305985_u32);
}

#[test]
fn test_u32_to_be_bytes() {
let x: u32 = 67305985;
let result = x.to_be_bytes();

assert(result[0] == 4);
assert(result[1] == 3);
assert(result[2] == 2);
assert(result[3] == 1);
assert(result[0] == 4_u8);
assert(result[1] == 3_u8);
assert(result[2] == 2_u8);
assert(result[3] == 1_u8);
}

#[test]
fn test_u32_from_be_bytes() {
let bytes = [4, 3, 2, 1];
let bytes = [4_u8, 3_u8, 2_u8, 1_u8];
let result = u32::from_be_bytes(bytes);

assert(result == 67305985);
assert(result == 67305985_u32);
}

#[test]
fn test_u16_to_le_bytes() {
let x: u16 = 513;
let result = x.to_le_bytes();

assert(result[0] == 1);
assert(result[1] == 2);
assert(result[0] == 1_u8);
assert(result[1] == 2_u8);
}

#[test]
fn test_u16_from_le_bytes() {
let bytes = [1, 2];
let bytes = [1_u8, 2_u8];
let result = u16::from_le_bytes(bytes);

assert(result == 513);
assert(result == 513_u16);
}

#[test]
fn test_u16_to_be_bytes() {
let x: u16 = 513;
let result = x.to_be_bytes();

assert(result[0] == 2);
assert(result[1] == 1);
assert(result[0] == 2_u8);
assert(result[1] == 1_u8);
}

#[test]
fn test_u16_from_be_bytes() {
let bytes = [2, 1];
let bytes = [2_u8, 1_u8];
let result = u16::from_be_bytes(bytes);

assert(result == 513);
assert(result == 513_u16);
}

#[test]
fn test_b256_from_le_bytes() {
let bytes = [32, 31, 30, 29, 28, 27, 26, 25, 24, 23,
22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
12, 11, 10, 9, 8, 7, 6, 5, 4, 3,
2, 1];
let bytes = [32_u8, 31_u8, 30_u8, 29_u8, 28_u8, 27_u8, 26_u8, 25_u8, 24_u8, 23_u8,
22_u8, 21_u8, 20_u8, 19_u8, 18_u8, 17_u8, 16_u8, 15_u8, 14_u8, 13_u8,
12_u8, 11_u8, 10_u8, 9_u8, 8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8,
2_u8, 1_u8];

let x = b256::from_le_bytes(bytes);

Expand All @@ -461,20 +461,20 @@ fn test_b256_to_le_bytes() {

let bytes = x.to_le_bytes();

let mut i = 0;
while i < 32 {
assert(bytes[i] == 32 - i);
i += 1;
let mut i: u8 = 0;
while i < 32_u8 {
assert(bytes[i] == 32_u8 - i);
i += 1_u8;
}

}

#[test]
fn test_b256_from_be_bytes() {
let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32];
let bytes = [1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8, 9_u8, 10_u8,
11_u8, 12_u8, 13_u8, 14_u8, 15_u8, 16_u8, 17_u8, 18_u8, 19_u8, 20_u8,
21_u8, 22_u8, 23_u8, 24_u8, 25_u8, 26_u8, 27_u8, 28_u8, 29_u8, 30_u8,
31_u8, 32_u8];

let x = b256::from_be_bytes(bytes);

Expand All @@ -487,9 +487,9 @@ fn test_b256_to_be_bytes() {

let bytes = x.to_be_bytes();

let mut i = 0;
while i < 32 {
assert(bytes[i] == i + 1);
i += 1;
let mut i: u8 = 0;
while i < 32_u8 {
assert(bytes[i] == i + 1_u8);
i += 1_u8;
}
}

0 comments on commit 52ac9b6

Please sign in to comment.