Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into fix_option_method_c…
Browse files Browse the repository at this point in the history
…all_selector
  • Loading branch information
felipensp committed Dec 16, 2024
2 parents d822640 + a200c45 commit 901881b
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 21 deletions.
36 changes: 36 additions & 0 deletions doc/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ by using any of the following commands in a terminal:
* [Performance tuning](#performance-tuning)
* [Atomics](#atomics)
* [Global Variables](#global-variables)
* [Static Variables](#static-variables)
* [Cross compilation](#cross-compilation)
* [Debugging](#debugging)
* [C Backend binaries Default](#c-backend-binaries-default)
Expand Down Expand Up @@ -7328,6 +7329,41 @@ to race conditions. There are several approaches to deal with these:
correlated, which is acceptable considering the performance penalty that using
synchronization primitives would represent.
## Static Variables
V also supports *static variables*, which are like *global variables*, but
available only *inside* a single unsafe function (you can look at them as
namespaced globals).
Note: their use is discouraged too, for reasons similar to why globals
are discouraged. The feature is supported to enable translating existing
low level C code into V code, using `v translate`.
Note: the function in which you use a static variable, has to be marked
with @[unsafe]. Also unlike using globals, using static variables, do not
require you to pass the flag `-enable-globals`, because they can only be
read/changed inside a single function, which has full control over the
state stored in them.
Here is a small example of how static variables can be used:
```v
@[unsafe]
fn counter() int {
mut static x := 42
// Note: x is initialised to 42, just _once_.
x++
return x
}
fn f() int {
return unsafe { counter() }
}
println(f()) // prints 43
println(f()) // prints 44
println(f()) // prints 45
```
## Cross compilation
Cross compilation is supported for Windows, Linux and FreeBSD.
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ fn (mut c Checker) check_or_expr(node ast.OrExpr, ret_type ast.Type, expr_return
node.pos)
}
}
if expr !is ast.Ident && !expr_return_type.has_flag(.option) {
if expr !in [ast.Ident, ast.SelectorExpr] && !expr_return_type.has_flag(.option) {
if expr_return_type.has_flag(.result) {
c.error('propagating a Result like an Option is deprecated, use `foo()!` instead of `foo()?`',
node.pos)
Expand Down Expand Up @@ -1791,7 +1791,7 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
}
}
node.typ = field.typ
if node.or_block.kind == .block {
if node.or_block.kind != .absent {
unwrapped_typ := c.unwrap_generic(node.typ)
c.expected_or_type = unwrapped_typ.clear_option_and_result()
c.stmts_ending_with_expression(mut node.or_block.stmts, c.expected_or_type)
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/cast_to_concrete_mut_err.vv
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn (mut m Message) update_text(new_text string) {
m.text = new_text
}

pub fn (mut m Message) update_ext_text(new_text string) {
pub fn (mut m Message) update_ext_text(new_text string) ? {
m.m2?.text = new_text
}

Expand Down
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/option_selector_fn_unwrap_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
vlib/v/checker/tests/option_selector_fn_unwrap_err.vv:7:17: error: to propagate the call, `callback` must return an Option type
5 |
6 | fn callback(foo &Foo) bool {
7 | return foo.func? == callback
| ^
8 | }
9 |
Details: vlib/v/checker/tests/option_selector_fn_unwrap_err.vv:6:23: details: prepend ? before the declaration of the return type of `callback`
4 | }
5 |
6 | fn callback(foo &Foo) bool {
| ~~~~
7 | return foo.func? == callback
8 | }
13 changes: 13 additions & 0 deletions vlib/v/checker/tests/option_selector_fn_unwrap_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
struct Foo {
mut:
func ?fn (voidptr) bool
}

fn callback(foo &Foo) bool {
return foo.func? == callback
}

fn main() {
t := Foo{}
assert callback(&t)
}
12 changes: 8 additions & 4 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,8 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
}
}
if left_sym.kind == .function {
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}',
true)
g.writeln(' = ${anon_ctx}${c_name(left.name)};')
} else if left_is_auto_deref_var {
styp := g.styp(left_typ).trim('*')
Expand Down Expand Up @@ -1043,7 +1044,8 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
if elem_typ.kind == .function {
left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}',
true)
g.write(' = *(voidptr*)array_get(')
} else {
styp := g.styp(info.elem_type)
Expand All @@ -1068,7 +1070,8 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
if elem_typ.kind == .function {
left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}',
true)
g.write(' = *(voidptr*)')
} else {
styp := g.styp(info.elem_type)
Expand All @@ -1095,7 +1098,8 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
if val_typ.kind == .function {
left_type := node.left_types[i]
left_sym := g.table.sym(left_type)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}',
true)
g.write(' = *(voidptr*)map_get(')
} else {
g.write('${styp} _var_${left.pos.pos} = *(${styp}*)map_get(')
Expand Down
30 changes: 23 additions & 7 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ pub fn (mut g Gen) write_typedef_types() {
if elem_sym.info is ast.FnType {
pos := g.out.len
// pos2:=g.out_parallel[g.out_idx].len
g.write_fn_ptr_decl(&elem_sym.info, '')
g.write_fn_ptr_decl(&elem_sym.info, '', true)
fixed = g.out.cut_to(pos)
// g.out_parallel[g.out_idx].cut_to(pos2)
mut def_str := 'typedef ${fixed};'
Expand Down Expand Up @@ -2172,13 +2172,14 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T
stmt_str := g.go_before_last_stmt().trim_space()
mut styp := g.base_type(ret_typ)
g.empty_line = true
final_expr_sym := g.table.final_sym(expr_typ)

if g.table.sym(expr_typ).kind == .none {
if final_expr_sym.kind == .none {
g.write('${g.styp(ret_typ)} ${tmp_var} = ')
g.gen_option_error(ret_typ, expr)
g.writeln(';')
} else if expr is ast.Ident && expr_typ == ast.error_type {
g.writeln('${g.styp(ret_typ)} ${tmp_var} = {.state=2, .err = ${expr.name}};')
g.writeln('${g.styp(ret_typ)} ${tmp_var} = {.state=2, .err=${expr.name}};')
} else {
mut simple_assign := false
if ret_typ.has_flag(.generic) {
Expand Down Expand Up @@ -2225,6 +2226,14 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T
g.write('_option_none(&(${styp}[]) { ')
} else {
g.write('_option_ok(&(${styp}[]) { ')
if final_expr_sym.info is ast.FnType {
final_ret_sym := g.table.final_sym(ret_typ)
if final_ret_sym.info is ast.FnType {
g.write('(')
g.write_fn_ptr_decl(&final_ret_sym.info, '', false)
g.write(')')
}
}
}
if ret_typ.nr_muls() > expr_typ.nr_muls() {
g.write('&'.repeat(ret_typ.nr_muls() - expr_typ.nr_muls()))
Expand Down Expand Up @@ -3192,13 +3201,16 @@ fn cnewlines(s string) string {
return s.replace('\n', r'\n')
}

fn (mut g Gen) write_fn_ptr_decl(func &ast.FnType, ptr_name string) {
fn (mut g Gen) write_fn_ptr_decl(func &ast.FnType, ptr_name string, with_argname bool) {
ret_styp := g.styp(func.func.return_type)
g.write('${ret_styp} (*${ptr_name}) (')
arg_len := func.func.params.len
for i, arg in func.func.params {
arg_styp := g.styp(arg.typ)
g.write('${arg_styp} ${arg.name}')
g.write(arg_styp)
if with_argname {
g.write(' ${arg.name}')
}
if i < arg_len - 1 {
g.write(', ')
}
Expand Down Expand Up @@ -6625,7 +6637,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
if len > 0 {
if elem_sym.info is ast.FnType {
pos := g.out.len
g.write_fn_ptr_decl(&elem_sym.info, '')
g.write_fn_ptr_decl(&elem_sym.info, '', true)
fixed_elem_name = g.out.cut_to(pos)
mut def_str := 'typedef ${fixed_elem_name};'
def_str = def_str.replace_once('(*)', '(*${styp}[${len}])')
Expand Down Expand Up @@ -7029,11 +7041,15 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty
// Now that option types are distinct we need a cast here
if g.fn_decl == unsafe { nil } || g.fn_decl.return_type == ast.void_type {
g.writeln('\treturn;')
} else {
} else if g.fn_decl.return_type.clear_option_and_result() == return_type.clear_option_and_result() {
styp := g.styp(g.fn_decl.return_type).replace('*', '_ptr')
err_obj := g.new_tmp_var()
g.writeln2('\t${styp} ${err_obj};', '\tmemcpy(&${err_obj}, &${cvar_name}, sizeof(_option));')
g.writeln('\treturn ${err_obj};')
} else {
g.write('\treturn ')
g.gen_option_error(g.fn_decl.return_type, ast.None{})
g.writeln(';')
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/dumpexpr.v
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn (mut g Gen) dump_expr_definitions() {
fninfo := dump_sym.info as ast.FnType
str_dumparg_type = 'DumpFNType_${name}'
tdef_pos := g.out.len
g.write_fn_ptr_decl(&fninfo, str_dumparg_type)
g.write_fn_ptr_decl(&fninfo, str_dumparg_type, true)
str_tdef := g.out.after(tdef_pos)
g.go_back(str_tdef.len)
dump_typedefs['typedef ${str_tdef};'] = true
Expand Down
9 changes: 6 additions & 3 deletions vlib/v/gen/c/for.v
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
if node.val_var != '_' {
if val_sym.kind == .function {
g.write('\t')
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var))
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var),
true)
g.writeln(' = ((voidptr*)${cond_var}${op_field}data)[${i}];')
} else if val_sym.kind == .array_fixed && !node.val_is_mut {
right := '((${styp}*)${cond_var}${op_field}data)[${i}]'
Expand Down Expand Up @@ -322,7 +323,8 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
is_fixed_array := val_sym.kind == .array_fixed && !node.val_is_mut
if val_sym.kind == .function {
g.write('\t')
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var))
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var),
true)
} else if is_fixed_array {
styp := g.styp(node.val_type)
g.writeln('\t${styp} ${c_name(node.val_var)};')
Expand Down Expand Up @@ -388,7 +390,8 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
if node.val_var != '_' {
val_sym := g.table.sym(node.val_type)
if val_sym.kind == .function {
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var))
g.write_fn_ptr_decl(val_sym.info as ast.FnType, c_name(node.val_var),
true)
g.write(' = (*(voidptr*)')
g.writeln('DenseArray_value(&${cond_var}${dot_or_ptr}key_values, ${idx}));')
} else if val_sym.kind == .array_fixed && !node.val_is_mut {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
if is_fn_index_call {
if elem_sym.info is ast.FnType {
g.write('((')
g.write_fn_ptr_decl(&elem_sym.info, '')
g.write_fn_ptr_decl(&elem_sym.info, '', true)
if is_direct_array_access {
g.write(')((${elem_type_str}*)')
} else {
Expand Down Expand Up @@ -484,7 +484,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
if g.is_fn_index_call {
if val_sym.info is ast.FnType {
g.write('((')
g.write_fn_ptr_decl(&val_sym.info, '')
g.write_fn_ptr_decl(&val_sym.info, '', true)
g.write(')(*(voidptr*)map_get(')
is_fn_last_index_call = true
g.is_fn_index_call = false
Expand Down
23 changes: 23 additions & 0 deletions vlib/v/tests/options/option_fn_voidptr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
struct Foo {
mut:
func ?fn (voidptr) bool = unsafe { nil }
}

fn callback(foo &Foo) bool {
return foo.func? == callback

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / gcc

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / tcc

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / clang

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / clang (macos-14)

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / msvc

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / gcc

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / ubuntu-docker-musl

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / tcc

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / tests-sanitize-undefined-clang

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / tests-sanitize-address-clang

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / tests-sanitize-undefined-gcc

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / tests-sanitize-memory-clang

to propagate the call, `callback` must return an Option type

Check failure on line 7 in vlib/v/tests/options/option_fn_voidptr_test.v

View workflow job for this annotation

GitHub Actions / tests-sanitize-address-gcc

to propagate the call, `callback` must return an Option type
}

fn test_main() {
t := Foo{
func: callback
}
assert t.func? == callback
call_fn := t.func?
assert call_fn(&t)

mut a := Foo{}
a.func = callback
assert a.func? == callback
call_fn2 := a.func?
assert call_fn2(&a)
}
2 changes: 1 addition & 1 deletion vlib/v/tests/options/option_ptr_unwrap_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn new_linked_list[T]() &LinkedList[T] {
return &LinkedList[T]{}
}

fn (mut li LinkedList[T]) add[T](data T) {
fn (mut li LinkedList[T]) add[T](data T) ? {
mut node := &Node[T]{data, none, none}

if li.head == none {
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/tests/options/option_selector_fn_none_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
struct Foo {
mut:
func ?fn (voidptr) ?bool
}

fn callback(foo &Foo) ?bool {
return foo.func? == callback
}

fn test_main() {
t := Foo{}
assert callback(&t) == none
}

0 comments on commit 901881b

Please sign in to comment.