From 6682911bc3ce2a39c5b66e9eecf9fb6724d03a9c Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 22 Feb 2023 08:38:08 -0300 Subject: [PATCH] cgen: fix print fn + cycle const error message (#17355) --- vlib/v/checker/checker.v | 2 +- vlib/v/checker/tests/const_cycle_decl_err.out | 3 +++ vlib/v/checker/tests/const_cycle_decl_err.vv | 1 + vlib/v/gen/c/auto_str_methods.v | 3 ++- vlib/v/gen/c/str.v | 2 ++ vlib/v/tests/print_anon_fn_test.v | 16 ++++++++++++++++ 6 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/const_cycle_decl_err.out create mode 100644 vlib/v/checker/tests/const_cycle_decl_err.vv create mode 100644 vlib/v/tests/print_anon_fn_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c3674b2c44..af49a7b6cf 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3118,7 +3118,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type { return c.expected_type } } - c.error('cycle in constant `${c.const_decl}`', node.pos) + c.error('cycle in constant `${c.const_var.name}`', node.pos) return ast.void_type } c.const_deps << name diff --git a/vlib/v/checker/tests/const_cycle_decl_err.out b/vlib/v/checker/tests/const_cycle_decl_err.out new file mode 100644 index 0000000000..9cb350cc54 --- /dev/null +++ b/vlib/v/checker/tests/const_cycle_decl_err.out @@ -0,0 +1,3 @@ +vlib/v/checker/tests/const_cycle_decl_err.vv:1:11: error: cycle in constant `a` + 1 | const a = a + 0 + | ^ diff --git a/vlib/v/checker/tests/const_cycle_decl_err.vv b/vlib/v/checker/tests/const_cycle_decl_err.vv new file mode 100644 index 0000000000..c7e4bf1d4e --- /dev/null +++ b/vlib/v/checker/tests/const_cycle_decl_err.vv @@ -0,0 +1 @@ +const a = a + 0 diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 1158b52eed..f185950a5b 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -989,7 +989,8 @@ fn struct_auto_str_func(sym &ast.TypeSymbol, _field_type ast.Type, fn_name strin } return 'indent_${fn_name}(${obj}, indent_count + 1)', true } else if sym.kind == .function { - return '${fn_name}()', true + obj := '${deref}it.${c_name(field_name)}${sufix}' + return '${fn_name}(${obj})', true } else if sym.kind == .chan { return '${fn_name}(${deref}it.${c_name(field_name)}${sufix})', true } else { diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index 9d9db7f959..7d617808e7 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -153,6 +153,8 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { g.write('*') } g.expr_with_cast(expr, typ, typ) + } else { + g.expr_with_cast(expr, typ, typ) } g.write(')') } diff --git a/vlib/v/tests/print_anon_fn_test.v b/vlib/v/tests/print_anon_fn_test.v new file mode 100644 index 0000000000..65a49ae071 --- /dev/null +++ b/vlib/v/tests/print_anon_fn_test.v @@ -0,0 +1,16 @@ +struct Foo { +mut: + f ?fn (int) + g fn (int) +} + +fn test_main() { + mut foo := ?Foo{} + assert foo == none + + foo = Foo{} + assert foo != none + + println(foo?.f) + println('${foo?.f}') +}