From 8cd74df2f3f9ae23e6c4267a54762bb2f6c29d3f Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Fri, 25 Dec 2020 15:52:16 +0100 Subject: [PATCH] vfmt: keep UnsafeExpr linebreacks as set by developer (#7558) --- vlib/time/parse.v | 18 ++++++++++++------ vlib/v/fmt/fmt.v | 18 ++++++++++++++++-- vlib/v/fmt/tests/unsafe_keep.vv | 17 +++++++++++++++++ vlib/v/parser/parser.v | 2 +- vlib/v/parser/pratt.v | 8 +++++--- 5 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 vlib/v/fmt/tests/unsafe_keep.vv diff --git a/vlib/time/parse.v b/vlib/time/parse.v index 4ae4326802..49400fcf17 100644 --- a/vlib/time/parse.v +++ b/vlib/time/parse.v @@ -39,8 +39,10 @@ pub fn parse_rfc2822(s string) ?Time { unsafe { tmstr = malloc(s.len * 2) } - count := unsafe { C.snprintf(charptr(tmstr), (s.len * 2), '%s-%02d-%s %s', fields[3].str, - mm, fields[1].str, fields[4].str) } + count := unsafe { + C.snprintf(charptr(tmstr), (s.len * 2), '%s-%02d-%s %s', fields[3].str, mm, fields[1].str, + fields[4].str) + } return parse(tos(tmstr, count)) } @@ -66,12 +68,16 @@ fn parse_iso8601_time(s string) ?(int, int, int, int, i64, bool) { plus_min_z := `a` offset_hour := 0 offset_minute := 0 - mut count := unsafe { C.sscanf(charptr(s.str), '%2d:%2d:%2d.%6d%c%2d:%2d', &hour, - &minute, &second, µsecond, charptr(&plus_min_z), &offset_hour, &offset_minute) } + mut count := unsafe { + C.sscanf(charptr(s.str), '%2d:%2d:%2d.%6d%c%2d:%2d', &hour, &minute, &second, + µsecond, charptr(&plus_min_z), &offset_hour, &offset_minute) + } // Missread microsecond ([Sec Hour Minute].len == 3 < 4) if count < 4 { - count = unsafe { C.sscanf(charptr(s.str), '%2d:%2d:%2d%c%2d:%2d', &hour, &minute, - &second, charptr(&plus_min_z), &offset_hour, &offset_minute) } + count = unsafe { + C.sscanf(charptr(s.str), '%2d:%2d:%2d%c%2d:%2d', &hour, &minute, &second, + charptr(&plus_min_z), &offset_hour, &offset_minute) + } count++ // Increment count because skipped microsecond } if count < 4 { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 4730184c4e..a8b59423ba 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1111,9 +1111,23 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.write(')') } ast.UnsafeExpr { - f.write('unsafe { ') + single_line := node.pos.line_nr >= node.pos.last_line + f.write('unsafe {') + if single_line { + f.write(' ') + } else { + f.writeln('') + f.indent++ + f.empty_line = true + } f.expr(node.expr) - f.write(' }') + if single_line { + f.write(' ') + } else { + f.writeln('') + f.indent-- + } + f.write('}') } } } diff --git a/vlib/v/fmt/tests/unsafe_keep.vv b/vlib/v/fmt/tests/unsafe_keep.vv new file mode 100644 index 0000000000..d1d670207d --- /dev/null +++ b/vlib/v/fmt/tests/unsafe_keep.vv @@ -0,0 +1,17 @@ +fn main() { + unsafe { + println('hi') + println('hi2') + } + unsafe { + println('qwer') + } + unsafe { 6 } + x := unsafe { + 5 + } + y := unsafe { 7 } + unsafe {} + unsafe { + } +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index db12909b54..e303762af0 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2275,6 +2275,7 @@ fn (mut p Parser) unsafe_stmt() ast.Stmt { // `unsafe {expr}` if stmt.expr.is_expr() { p.next() + pos.last_line = p.prev_tok.line_nr - 1 ue := ast.UnsafeExpr{ expr: stmt.expr pos: pos @@ -2293,7 +2294,6 @@ fn (mut p Parser) unsafe_stmt() ast.Stmt { for p.tok.kind != .rcbr { stmts << p.stmt(false) } - pos.last_line = p.tok.line_nr p.next() return ast.Block{ stmts: stmts diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index ae817cf8b9..530fd86d42 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -103,7 +103,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { } .key_unsafe { // unsafe { - pos := p.tok.position() + mut pos := p.tok.position() p.next() if p.inside_unsafe { p.error_with_pos('already inside `unsafe` block', pos) @@ -111,11 +111,13 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { } p.inside_unsafe = true p.check(.lcbr) + e := p.expr(0) + p.check(.rcbr) + pos.last_line = p.prev_tok.line_nr - 1 node = ast.UnsafeExpr{ - expr: p.expr(0) + expr: e pos: pos } - p.check(.rcbr) p.inside_unsafe = false } .key_lock, .key_rlock {