diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index fe42fe7533..5d9405fcb8 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -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] { diff --git a/vlib/v/checker/tests/assign_const_ptr_int_literal_err.out b/vlib/v/checker/tests/assign_const_ptr_int_literal_err.out new file mode 100644 index 0000000000..1454b0797a --- /dev/null +++ b/vlib/v/checker/tests/assign_const_ptr_int_literal_err.out @@ -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)` \ No newline at end of file diff --git a/vlib/v/checker/tests/assign_const_ptr_int_literal_err.vv b/vlib/v/checker/tests/assign_const_ptr_int_literal_err.vv new file mode 100644 index 0000000000..a2b8d97aed --- /dev/null +++ b/vlib/v/checker/tests/assign_const_ptr_int_literal_err.vv @@ -0,0 +1,6 @@ +const a = 3 + +fn main() { + b := &a + dump(b) +} diff --git a/vlib/v/checker/tests/blank_ident_const_ptr_int_literal_err.out b/vlib/v/checker/tests/blank_ident_const_ptr_int_literal_err.out new file mode 100644 index 0000000000..a0b35d08df --- /dev/null +++ b/vlib/v/checker/tests/blank_ident_const_ptr_int_literal_err.out @@ -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)` diff --git a/vlib/v/checker/tests/blank_ident_const_ptr_int_literal_err.vv b/vlib/v/checker/tests/blank_ident_const_ptr_int_literal_err.vv new file mode 100644 index 0000000000..2543082fa1 --- /dev/null +++ b/vlib/v/checker/tests/blank_ident_const_ptr_int_literal_err.vv @@ -0,0 +1,5 @@ +const a = 3 + +fn main() { + _ := &a +}