mirror of
https://github.com/vlang/v.git
synced 2025-09-08 23:07:19 -04:00
checker: disallow taking the address of consts with int literal values (#19160)
This commit is contained in:
parent
757d26cd89
commit
4908ec57e2
@ -264,7 +264,17 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
|
||||
node.left_types << left_type
|
||||
match mut left {
|
||||
ast.Ident {
|
||||
if left.kind == .blank_ident {
|
||||
if (is_decl || left.kind == .blank_ident) && left_type.is_ptr()
|
||||
&& mut right is ast.PrefixExpr && right.right_type == ast.int_literal_type_idx {
|
||||
if mut right.right is ast.Ident && right.right.obj is ast.ConstField {
|
||||
const_name := right.right.name.all_after_last('.')
|
||||
const_val := (right.right.obj as ast.ConstField).expr
|
||||
c.add_error_detail('Specify the type for the constant value. Example:')
|
||||
c.add_error_detail(' `const ${const_name} = int(${const_val})`')
|
||||
c.error('cannot assign a pointer to a constant with an integer literal value',
|
||||
right.right.pos)
|
||||
}
|
||||
} else if left.kind == .blank_ident {
|
||||
left_type = right_type
|
||||
node.left_types[i] = right_type
|
||||
if node.op !in [.assign, .decl_assign] {
|
||||
|
@ -0,0 +1,9 @@
|
||||
vlib/v/checker/tests/assign_const_ptr_int_literal_err.vv:4:8: error: cannot assign a pointer to a constant with an integer literal value
|
||||
2 |
|
||||
3 | fn main() {
|
||||
4 | b := &a
|
||||
| ^
|
||||
5 | dump(b)
|
||||
6 | }
|
||||
Details: Specify the type for the constant value. Example:
|
||||
`const a = int(3)`
|
6
vlib/v/checker/tests/assign_const_ptr_int_literal_err.vv
Normal file
6
vlib/v/checker/tests/assign_const_ptr_int_literal_err.vv
Normal file
@ -0,0 +1,6 @@
|
||||
const a = 3
|
||||
|
||||
fn main() {
|
||||
b := &a
|
||||
dump(b)
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
vlib/v/checker/tests/blank_ident_const_ptr_int_literal_err.vv:4:8: error: cannot assign a pointer to a constant with an integer literal value
|
||||
2 |
|
||||
3 | fn main() {
|
||||
4 | _ := &a
|
||||
| ^
|
||||
5 | }
|
||||
Details: Specify the type for the constant value. Example:
|
||||
`const a = int(3)`
|
@ -0,0 +1,5 @@
|
||||
const a = 3
|
||||
|
||||
fn main() {
|
||||
_ := &a
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user