Skip to content

Commit 8f77847

Browse files
committed
fix almost all idioms
1 parent c87cae8 commit 8f77847

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

fn_call.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ fn (mut app App) call_expr(call CallExpr) {
9595
} else if fun.name == 'new' {
9696
// new(Foo) => &Foo{}
9797
app.gen('&')
98+
saved_force_upper := app.force_upper
99+
app.force_upper = true
98100
app.expr(call.args[0])
101+
app.force_upper = saved_force_upper
99102
app.gen('{}')
100103
return
101104
} else if fun.name == 'delete' {

main.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mut:
3636
pending_structs []string // inline struct definitions to output
3737
enum_types map[string]bool // types that will become enums (detected by pre-scan)
3838
enum_values map[string]bool // enum constant values (for adding . prefix in match)
39+
array_type_aliases map[string]bool // type aliases to array types (for correct composite lit handling)
3940
fmt_needs_closing_paren bool // for wrapping printf in print(strconv.v_sprintf(...))
4041
at_stmt_level bool // true when we can safely emit temp var declarations
4142
error_vars map[string]bool // variables that hold error/option types (for nil -> none translation)

struct.v

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ fn (mut app App) type_decl(spec TypeSpec) {
7070
// Store alias info
7171
app.struct_or_alias << name
7272
app.struct_or_alias << v_name
73+
// Track if this is an array type alias
74+
if spec.typ is ArrayType {
75+
app.array_type_aliases[name] = true
76+
app.array_type_aliases[v_name] = true
77+
}
7378
// If this type will become an enum (detected by pre-scan), skip the type alias
7479
if name in app.enum_types {
7580
app.type_decl_name = name
@@ -362,6 +367,7 @@ fn (mut app App) struct_decl(struct_name string, spec StructType) {
362367
app.genln('${pub_prefix}struct ${v_struct_name} {')
363368

364369
// First output embedded structs (fields without names)
370+
mut has_pub_mut := false
365371
for field in spec.fields.list {
366372
if field.names.len == 0 {
367373
// Embedded struct - skip if it's a pointer type (V doesn't support embedded pointers)
@@ -375,7 +381,10 @@ fn (mut app App) struct_decl(struct_name string, spec StructType) {
375381
conversion := go2v_type_checked(ident.name)
376382
if conversion.is_basic {
377383
// Primitive type - generate as a named field
378-
app.genln('pub mut:')
384+
if !has_pub_mut {
385+
app.genln('pub mut:')
386+
has_pub_mut = true
387+
}
379388
app.genln('\t${ident.name.camel_to_snake()} ${conversion.v_type}')
380389
continue
381390
}
@@ -396,7 +405,7 @@ fn (mut app App) struct_decl(struct_name string, spec StructType) {
396405
break
397406
}
398407
}
399-
if has_named_fields {
408+
if has_named_fields && !has_pub_mut {
400409
app.genln('pub mut:')
401410
}
402411
for field in spec.fields.list {
@@ -466,7 +475,20 @@ fn (mut app App) composite_lit(c CompositeLit) {
466475
app.composite_lit(c)
467476
}
468477
Ident {
469-
app.struct_init(c)
478+
// Check if this is an array type alias
479+
if c.typ.name in app.array_type_aliases {
480+
// Generate array literal: [elem1, elem2, ...]
481+
app.gen('[')
482+
for i, elt in c.elts {
483+
if i > 0 {
484+
app.gen(', ')
485+
}
486+
app.expr(elt)
487+
}
488+
app.gen(']')
489+
} else {
490+
app.struct_init(c)
491+
}
470492
}
471493
InvalidExpr {
472494
if c.elts.len > 0 {

tests/struct_fields_fn/struct_fields_fn.vv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module main
33
pub struct Ok {
44
pub mut:
55
aaa string
6-
bbb fn () = unsafe { nil }
7-
ccc fn (_ isize) = unsafe { nil }
8-
ddd fn (_ Ok) u16 = unsafe { nil }
6+
bbb fn () = unsafe { nil }
7+
ccc fn (isize) = unsafe { nil }
8+
ddd fn (Ok) u16 = unsafe { nil }
99
}

0 commit comments

Comments
 (0)