Skip to content

Commit 2af05b1

Browse files
committed
Type check negation for unsigned integers.
1 parent 7895667 commit 2af05b1

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

compiler/passes/src/type_checking/check_expressions.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -970,11 +970,21 @@ impl ExpressionVisitor for TypeChecker<'_> {
970970
}
971971
UnaryOperation::Negate => {
972972
let type_ = self.visit_expression(&input.receiver, &None);
973-
if !matches!(&type_, Type::Err | Type::Integer(_) | Type::Group | Type::Field) {
973+
if !matches!(
974+
&type_,
975+
Type::Err
976+
| Type::Integer(IntegerType::I8)
977+
| Type::Integer(IntegerType::I16)
978+
| Type::Integer(IntegerType::I32)
979+
| Type::Integer(IntegerType::I64)
980+
| Type::Integer(IntegerType::I128)
981+
| Type::Group
982+
| Type::Field
983+
) {
974984
self.emit_err(TypeCheckerError::type_should_be2(
975985
&type_,
976-
"an integer, group, or field",
977-
input.span(),
986+
"a signed integer, group, or field",
987+
input.receiver.span(),
978988
));
979989
}
980990
type_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace = "Compile"
2+
expectation = "Fail"
3+
outputs = ["""
4+
Error [ETYC0372117]: Expected a signed integer, group, or field but type `u8` was found.
5+
--> compiler-test:5:17
6+
|
7+
5 | return -a;
8+
| ^
9+
"""]

tests/expectations/compiler/statements/compare_invalid_negates_fail.out

+35
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,71 @@
11
namespace = "Compile"
22
expectation = "Fail"
33
outputs = ["""
4+
Error [ETYC0372117]: Expected a signed integer, group, or field but type `u8` was found.
5+
--> compiler-test:5:24
6+
|
7+
5 | let b: bool = -a == -1u8;
8+
| ^
49
Error [ETYC0372008]: The value -1 is not a valid `u8`
510
--> compiler-test:5:29
611
|
712
5 | let b: bool = -a == -1u8;
813
| ^^^^
14+
Error [ETYC0372117]: Expected a signed integer, group, or field but type `u8` was found.
15+
--> compiler-test:6:24
16+
|
17+
6 | let c: bool = -a > -1u8;
18+
| ^
919
Error [ETYC0372008]: The value -1 is not a valid `u8`
1020
--> compiler-test:6:28
1121
|
1222
6 | let c: bool = -a > -1u8;
1323
| ^^^^
24+
Error [ETYC0372117]: Expected a signed integer, group, or field but type `u8` was found.
25+
--> compiler-test:7:24
26+
|
27+
7 | let d: bool = -a < -1u8;
28+
| ^
1429
Error [ETYC0372008]: The value -1 is not a valid `u8`
1530
--> compiler-test:7:28
1631
|
1732
7 | let d: bool = -a < -1u8;
1833
| ^^^^
34+
Error [ETYC0372117]: Expected a signed integer, group, or field but type `u8` was found.
35+
--> compiler-test:8:24
36+
|
37+
8 | let e: bool = -a >= -1u8;
38+
| ^
1939
Error [ETYC0372008]: The value -1 is not a valid `u8`
2040
--> compiler-test:8:29
2141
|
2242
8 | let e: bool = -a >= -1u8;
2343
| ^^^^
44+
Error [ETYC0372117]: Expected a signed integer, group, or field but type `u8` was found.
45+
--> compiler-test:9:24
46+
|
47+
9 | let f: bool = -a <= -1u8;
48+
| ^
2449
Error [ETYC0372008]: The value -1 is not a valid `u8`
2550
--> compiler-test:9:29
2651
|
2752
9 | let f: bool = -a <= -1u8;
2853
| ^^^^
54+
Error [ETYC0372117]: Expected a signed integer, group, or field but type `u8` was found.
55+
--> compiler-test:10:22
56+
|
57+
10 | let g: u8 = -a * -1u8;
58+
| ^
2959
Error [ETYC0372008]: The value -1 is not a valid `u8`
3060
--> compiler-test:10:26
3161
|
3262
10 | let g: u8 = -a * -1u8;
3363
| ^^^^
64+
Error [ETYC0372117]: Expected a signed integer, group, or field but type `u8` was found.
65+
--> compiler-test:11:22
66+
|
67+
11 | let h: u8 = -a ** -1u8;
68+
| ^
3469
Error [ETYC0372008]: The value -1 is not a valid `u8`
3570
--> compiler-test:11:27
3671
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
namespace = "Compile"
3+
expectation = "Fail"
4+
*/
5+
6+
program test.aleo {
7+
transition main(a: u8) -> u8 {
8+
return -a;
9+
}
10+
}

0 commit comments

Comments
 (0)