diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index c59b9463ef..6c4f55243d 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -151,7 +151,9 @@ fn (mut g Gen) final_gen_str(typ StrType) { g.gen_str_for_thread(sym.info, styp, str_fn_name) } else { - verror('could not generate string method `$str_fn_name` for type `$styp`') + if sym.name != 'nil' { + verror('could not generate string method `$str_fn_name` for type `$styp`') + } } } } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9c254e53f6..1b703cd073 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -4709,10 +4709,21 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) { continue } if field.has_expr || cinit { + // `__global x = unsafe { nil }` should still use the simple direct initialisation, `g_main_argv` needs it. + mut is_simple_unsafe_expr := false + if field.expr is ast.UnsafeExpr { + if field.expr.expr is ast.Nil { + is_simple_unsafe_expr = true + } + if field.expr.expr.is_literal() { + is_simple_unsafe_expr = true + } + } if g.pref.translated { def_builder.write_string(' = ${g.expr_string(field.expr)}') } else if (field.expr.is_literal() && should_init) || cinit - || (field.expr is ast.ArrayInit && (field.expr as ast.ArrayInit).is_fixed) { + || (field.expr is ast.ArrayInit && (field.expr as ast.ArrayInit).is_fixed) + || is_simple_unsafe_expr { // Simple literals can be initialized right away in global scope in C. // e.g. `int myglobal = 10;` def_builder.write_string(' = ${g.expr_string(field.expr)}')