mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parent
360457e021
commit
74efd2621b
@ -321,6 +321,14 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
|
|||||||
left.expr.is_setter = true
|
left.expr.is_setter = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if left_type in ast.unsigned_integer_type_idxs {
|
||||||
|
if mut right is ast.IntegerLiteral {
|
||||||
|
if right.val[0] == `-` {
|
||||||
|
c.error('Cannot assign negative value to unsigned integer type',
|
||||||
|
right.pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if mut left is ast.IndexExpr {
|
if mut left is ast.IndexExpr {
|
||||||
|
@ -124,6 +124,14 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if field.typ in ast.unsigned_integer_type_idxs {
|
||||||
|
if field.default_expr is ast.IntegerLiteral {
|
||||||
|
if field.default_expr.val[0] == `-` {
|
||||||
|
c.error('Cannot assign negative value to unsigned integer type',
|
||||||
|
field.default_expr.pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if field.default_expr is ast.UnsafeExpr {
|
if field.default_expr is ast.UnsafeExpr {
|
||||||
if field.default_expr.expr is ast.Nil && !field.typ.is_ptr()
|
if field.default_expr.expr is ast.Nil && !field.typ.is_ptr()
|
||||||
&& c.table.sym(field.typ).kind != .function && !field.typ.is_pointer() {
|
&& c.table.sym(field.typ).kind != .function && !field.typ.is_pointer() {
|
||||||
@ -482,6 +490,14 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if field_info.typ in ast.unsigned_integer_type_idxs {
|
||||||
|
if mut field.expr is ast.IntegerLiteral {
|
||||||
|
if field.expr.val[0] == `-` {
|
||||||
|
c.error('Cannot assign negative value to unsigned integer type',
|
||||||
|
field.expr.pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Check uninitialized refs/sum types
|
// Check uninitialized refs/sum types
|
||||||
// The variable `fields` contains two parts, the first part is the same as info.fields,
|
// The variable `fields` contains two parts, the first part is the same as info.fields,
|
||||||
|
20
vlib/v/checker/tests/struct_field_unsign_type_check_err.out
Normal file
20
vlib/v/checker/tests/struct_field_unsign_type_check_err.out
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
vlib/v/checker/tests/struct_field_unsign_type_check_err.vv:3:10: error: Cannot assign negative value to unsigned integer type
|
||||||
|
1 | struct Foo {
|
||||||
|
2 | mut:
|
||||||
|
3 | n u32 = -1
|
||||||
|
| ~~
|
||||||
|
4 | }
|
||||||
|
5 |
|
||||||
|
vlib/v/checker/tests/struct_field_unsign_type_check_err.vv:8:6: error: Cannot assign negative value to unsigned integer type
|
||||||
|
6 | fn main() {
|
||||||
|
7 | mut foo := Foo{
|
||||||
|
8 | n: -1
|
||||||
|
| ~~
|
||||||
|
9 | }
|
||||||
|
10 |
|
||||||
|
vlib/v/checker/tests/struct_field_unsign_type_check_err.vv:11:10: error: Cannot assign negative value to unsigned integer type
|
||||||
|
9 | }
|
||||||
|
10 |
|
||||||
|
11 | foo.n = -1
|
||||||
|
| ~~
|
||||||
|
12 | }
|
12
vlib/v/checker/tests/struct_field_unsign_type_check_err.vv
Normal file
12
vlib/v/checker/tests/struct_field_unsign_type_check_err.vv
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
struct Foo {
|
||||||
|
mut:
|
||||||
|
n u32 = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
mut foo := Foo{
|
||||||
|
n: -1
|
||||||
|
}
|
||||||
|
|
||||||
|
foo.n = -1
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user