checker: fix negative values for unsigned integers (#10976)
parent
6fa8e4269e
commit
7c504920e1
|
@ -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
|
||||
|
|
|
@ -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 | }
|
|
@ -0,0 +1,5 @@
|
|||
fn main() {
|
||||
mut u := u32(10)
|
||||
u = -10
|
||||
eprintln(u)
|
||||
}
|
Loading…
Reference in New Issue