From 2716a3791648c754673d19c2bb3dbf8fd7c9c8e1 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 1 Jul 2020 20:07:33 +0200 Subject: [PATCH] ci: fix v vet call; fmt: fix SizeOf --- .github/workflows/ci.yml | 2 +- vlib/v/ast/ast.v | 2 ++ vlib/v/fmt/fmt.v | 38 +++++++++++++++---------------- vlib/v/fmt/tests/comments_keep.vv | 4 ++++ vlib/v/parser/struct.v | 9 ++++---- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 091e6b5cd8..03e0747b88 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,7 +42,7 @@ jobs: run: ./v test-fixed - name: Test building v tools run: ./v build-tools - - name: v vet + - name: ./v vet run: | v vet vlib/v/parser v vet vlib/v/ast diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 272ea22c75..efea2f550a 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -186,6 +186,7 @@ pub struct StructInitField { pub: expr Expr pos token.Position + comment Comment pub mut: name string typ table.Type @@ -1011,6 +1012,7 @@ pub fn (stmt Stmt) position() token.Position { // UnsafeStmt { // } */ + // else { return token.Position{} } } } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index b543965e85..1366514f8d 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -145,13 +145,14 @@ pub fn (mut f Fmt) writeln(s string) { fn (mut f Fmt) adjust_complete_line() { for i, buf in f.expr_bufs { // search for low penalties - if i == 0 || f.penalties[i-1] <= 1 { - precedence := if i == 0 { -1 } else { f.precedences[i-1] } + if i == 0 || f.penalties[i - 1] <= 1 { + precedence := if i == 0 { -1 } else { f.precedences[i - 1] } mut len_sub_expr := if i == 0 { buf.len + f.line_len } else { buf.len } mut sub_expr_end_idx := f.penalties.len // search for next position with low penalty and same precedence to form subexpression - for j in i..f.penalties.len { - if f.penalties[j] <= 1 && f.precedences[j] == precedence && len_sub_expr >= max_len[1] { + for j in i .. f.penalties.len { + if f.penalties[j] <= 1 && + f.precedences[j] == precedence && len_sub_expr >= max_len[1] { sub_expr_end_idx = j break } else if f.precedences[j] < precedence { @@ -159,25 +160,25 @@ fn (mut f Fmt) adjust_complete_line() { len_sub_expr = C.INT32_MAX break } else { - len_sub_expr += f.expr_bufs[j+1].len + len_sub_expr += f.expr_bufs[j + 1].len } } // if subexpression would fit in single line adjust penalties to actually do so - if len_sub_expr <= max_len[max_len.len-1] { - for j in i..sub_expr_end_idx { - f.penalties[j] = max_len.len-1 + if len_sub_expr <= max_len[max_len.len - 1] { + for j in i .. sub_expr_end_idx { + f.penalties[j] = max_len.len - 1 } if i > 0 { - f.penalties[i-1] = 0 + f.penalties[i - 1] = 0 } if sub_expr_end_idx < f.penalties.len { f.penalties[sub_expr_end_idx] = 0 } } } - // emergency fallback: decrease penalty in front of long unbreakable parts - if i > 0 && buf.len > max_len[3] - max_len[1] && f.penalties[i-1] > 0 { - f.penalties[i-1] = if buf.len >= max_len[2] { 0 } else { 1 } + // emergency fallback: decrease penalty in front of long unbreakable parts + if i > 0 && buf.len > max_len[3] - max_len[1] && f.penalties[i - 1] > 0 { + f.penalties[i - 1] = if buf.len >= max_len[2] { 0 } else { 1 } } } } @@ -889,10 +890,10 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.write(node.field_name) } ast.SizeOf { - if node.is_type { + if node.is_type { f.write('sizeof(') if node.type_name != '' { - f.write(node.type_name) + f.write(f.short_module(node.type_name)) } else { f.write(f.type_to_str(node.typ)) } @@ -1136,8 +1137,7 @@ pub fn (mut f Fmt) short_module(name string) string { pub fn (mut f Fmt) if_expr(it ast.IfExpr) { single_line := it.branches.len == 2 && it.has_else && - it.branches[0].stmts.len == 1 && - it.branches[1].stmts.len == 1 && + it.branches[0].stmts.len == 1 && it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign) f.single_line_if = single_line for i, branch in it.branches { @@ -1435,10 +1435,10 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) { if last_line_nr < line_nr { penalty-- } - if i == 0 || it.exprs[i - 1] is ast.ArrayInit || + if i == 0 || + it.exprs[i - 1] is ast.ArrayInit || it.exprs[i - 1] is ast.StructInit || - it.exprs[i - 1] is ast.MapInit || - it.exprs[i - 1] is ast.CallExpr { + it.exprs[i - 1] is ast.MapInit || it.exprs[i - 1] is ast.CallExpr { penalty-- } if expr is ast.ArrayInit || diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index c7d06e56a0..95eb19de0b 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -5,8 +5,12 @@ struct User { } fn main() { + u := User{ + name: 'Peter' + } if true { } + n := sizeof(User) // else // else { // } diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index e142a8dceb..904c946b96 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -32,12 +32,12 @@ fn (mut p Parser) struct_decl() ast.StructDecl { p.next() // C || JS p.next() // . } - is_typedef := 'typedef' in p.attrs end_pos := p.tok.position() mut name := p.check_name() if name.len == 1 && name[0].is_capital() { - p.error_with_pos('single letter capital names are reserved for generic template types.', end_pos) + p.error_with_pos('single letter capital names are reserved for generic template types.', + end_pos) } mut generic_types := []table.Type{} if p.tok.kind == .lt { @@ -51,12 +51,10 @@ fn (mut p Parser) struct_decl() ast.StructDecl { } p.check(.gt) } - no_body := p.tok.kind != .lcbr if language == .v && no_body { p.error('`$p.tok.lit` lacks body') } - if language == .v && p.mod != 'builtin' && name.len > 0 && !name[0].is_capital() { p.error_with_pos('struct name `$name` must begin with capital letter', end_pos) } @@ -282,7 +280,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { saved_is_amp := p.is_amp p.is_amp = false for p.tok.kind != .rcbr && p.tok.kind != .rpar { - p.check_comment() + comment := p.check_comment() mut field_name := '' if no_keys { expr := p.expr(0) @@ -290,6 +288,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { fields << ast.StructInitField{ expr: expr pos: expr.position() + comment: comment } } else { first_field_pos := p.tok.position()