Skip to content

Commit

Permalink
checker: check struct aliased field unsign type assigning negative va…
Browse files Browse the repository at this point in the history
…lue (fix #22868) (#22871)
  • Loading branch information
yuyi98 authored Nov 16, 2024
1 parent 719d9a5 commit 8300a06
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
c.fail_if_stack_struct_action_outside_unsafe(mut init_field.expr,
'assigned')
}
if field_info.typ in ast.unsigned_integer_type_idxs
if c.table.unaliased_type(exp_type) in ast.unsigned_integer_type_idxs
&& mut init_field.expr is ast.IntegerLiteral
&& (init_field.expr as ast.IntegerLiteral).val[0] == `-` {
c.error('cannot assign negative value to unsigned integer type', init_field.expr.pos)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/struct_aliased_field_unsign_type_check_err.vv:9:9: error: cannot assign negative value to unsigned integer type
7 | fn main() {
8 | app := App{
9 | port: -1
| ~~
10 | }
11 | println(app)
12 changes: 12 additions & 0 deletions vlib/v/checker/tests/struct_aliased_field_unsign_type_check_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Port = u16

struct App {
port Port
}

fn main() {
app := App{
port: -1
}
println(app)
}

0 comments on commit 8300a06

Please sign in to comment.