diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 343b13a7b8..2ea6877cb9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 diff --git a/vlib/v/checker/tests/negative_assign_to_unsigned.out b/vlib/v/checker/tests/negative_assign_to_unsigned.out new file mode 100644 index 0000000000..19274475f0 --- /dev/null +++ b/vlib/v/checker/tests/negative_assign_to_unsigned.out @@ -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 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/negative_assign_to_unsigned.vv b/vlib/v/checker/tests/negative_assign_to_unsigned.vv new file mode 100644 index 0000000000..da7646d527 --- /dev/null +++ b/vlib/v/checker/tests/negative_assign_to_unsigned.vv @@ -0,0 +1,5 @@ +fn main() { + mut u := u32(10) + u = -10 + eprintln(u) +} \ No newline at end of file