checker: fix negative values for unsigned integers (#10976)

pull/10999/head
Louis Schmieder 2021-07-30 02:27:59 +02:00 committed by GitHub
parent 6fa8e4269e
commit 7c504920e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -3949,6 +3949,14 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
left.obj.is_auto_heap = true
}
}
if left_type in ast.unsigned_integer_type_idxs {
if right is ast.IntegerLiteral {
if right.val[0] == `-` {
c.error('Cannot assign negative value to unsigned integer type',
right.pos)
}
}
}
}
ast.GlobalField {
left.obj.typ = left_type

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/negative_assign_to_unsigned.vv:3:9: error: Cannot assign negative value to unsigned integer type
1 | fn main() {
2 | mut u := u32(10)
3 | u = -10
| ~~~
4 | eprintln(u)
5 | }

View File

@ -0,0 +1,5 @@
fn main() {
mut u := u32(10)
u = -10
eprintln(u)
}