v.checker: allow assigning pointers to fn variables (fix vinix compilation)

pull/11459/head
Delyan Angelov 2021-09-10 11:43:51 +03:00
parent afeb1525a1
commit 7143b8ab37
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 16 additions and 4 deletions

View File

@ -4023,10 +4023,13 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
// Do not allow `a := 0; b := 0; a = &b`
if !is_decl && left is ast.Ident && !is_blank_ident && !left_type.is_real_pointer()
&& right_type.is_real_pointer() {
c.warn(
'cannot assign a reference to a value (this will be an error soon) left=${c.table.type_str(left_type)} $left_type.is_ptr() ' +
'right=${c.table.type_str(right_type)} $right_type.is_real_pointer() ptr=$right_type.is_ptr()',
node.pos)
left_sym := c.table.get_type_symbol(left_type)
if left_sym.kind != .function {
c.warn(
'cannot assign a reference to a value (this will be an error soon) left=${c.table.type_str(left_type)} $left_type.is_ptr() ' +
'right=${c.table.type_str(right_type)} $right_type.is_real_pointer() ptr=$right_type.is_ptr()',
node.pos)
}
}
node.left_types << left_type
match mut left {

View File

@ -51,3 +51,12 @@ fn test_fn_assignment_array() {
assert a[0] == 12
assert a[1] == 10
}
fn test_fn_variables_can_be_assigned_pointers() {
mut fn_ptr := fn (_ voidptr, _ u64) {}
// println(voidptr(fn_ptr))
assert fn_ptr != voidptr(0)
fn_ptr = voidptr(0)
// aprintln(voidptr(fn_ptr))
assert fn_ptr == voidptr(0)
}