diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index b31ee5e5c6..06d45caa49 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -283,6 +283,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string { global_g.init() global_g.timers.show('cgen init') global_g.tests_inited = false + global_g.file = files.last() if !pref.no_parallel { mut pp := pool.new_pool_processor(callback: cgen_process_one_file_cb) pp.set_shared_context(global_g) // TODO: make global_g shared @@ -2886,7 +2887,7 @@ fn (mut g Gen) expr(node_ ast.Expr) { g.writeln('($shared_styp*)__dup${shared_styp}(&($shared_styp){.mtx = {0}, .val =') } } - last_stmt_pos := g.stmt_path_pos.last() + last_stmt_pos := if g.stmt_path_pos.len > 0 { g.stmt_path_pos.last() } else { 0 } g.call_expr(node) // if g.fileis('1.strings') { // println('before:' + node.autofree_pregen) diff --git a/vlib/v/tests/json_with_struct_having_fields_with_default_values_test.v b/vlib/v/tests/json_with_struct_having_fields_with_default_values_test.v new file mode 100644 index 0000000000..337bb1eb45 --- /dev/null +++ b/vlib/v/tests/json_with_struct_having_fields_with_default_values_test.v @@ -0,0 +1,27 @@ +import json + +struct Window { +pub mut: + width f64 + height f64 +} + +fn make_default_window_settings() Window { + return Window{ + width: 1280 + height: 720 + } +} + +struct Settings { +pub mut: + window Window = make_default_window_settings() +} + +fn test_encoding_works() { + mut settings := Settings{} + dump(settings) + encoded := json.encode(settings) + println(encoded) + assert encoded == '{"window":{"width":1280,"height":720}}' +}