Skip to content

Commit

Permalink
ast, fmt: fix formatting type decl with anon struct (#19356)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 16, 2023
1 parent 24cb98d commit 09006fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
10 changes: 5 additions & 5 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,11 @@ pub:
generic_types []Type
is_pub bool
// _pos fields for vfmt
mut_pos int // mut:
pub_pos int // pub:
pub_mut_pos int // pub mut:
global_pos int // __global:
module_pos int // module:
mut_pos int = -1 // mut:
pub_pos int = -1 // pub:
pub_mut_pos int = -1 // pub mut:
global_pos int = -1 // __global:
module_pos int = -1 // module:
language Language
is_union bool
attrs []Attr
Expand Down
11 changes: 11 additions & 0 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,17 @@ pub fn (mut f Fmt) alias_type_decl(node ast.AliasTypeDecl) {
if node.is_pub {
f.write('pub ')
}
// aliases of anon struct: `type foo = struct {}`
sym := f.table.sym(node.parent_type)
if sym.info is ast.Struct {
if sym.info.is_anon {
f.write('type ${node.name} = ')
f.write_anon_struct_field_decl(node.parent_type, ast.StructDecl{ fields: sym.info.fields })
f.comments(node.comments, has_nl: false)
f.mark_types_import_as_used(node.parent_type)
return
}
}
ptype := f.table.type_to_str_using_aliases(node.parent_type, f.mod2alias)
f.write('type ${node.name} = ${ptype}')

Expand Down
15 changes: 15 additions & 0 deletions vlib/v/fmt/tests/type_decl_with_anon_struct_keep.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub type Unit1 = struct {}

pub type Unit2 = struct {
name string
age int
}

pub const unit1 = Unit1{}

pub const unit2 = Unit2{'bob', 22}

fn main() {
println(unit1)
println(unit2)
}

0 comments on commit 09006fa

Please sign in to comment.