fmt: do not add braces for trailing struct arg (#7072)

pull/7127/head
Lukas Neubert 2020-12-04 10:22:26 +01:00 committed by GitHub
parent 34049f7135
commit 02ba923ba7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 148 additions and 175 deletions

View File

@ -121,11 +121,7 @@ fn gg_init_sokol_window(user_data voidptr) {
exists := $if !android { os.is_file(g.config.font_path) } $else { true }
if g.config.font_path != '' && exists {
// t := time.ticks()
g.ft = new_ft({
font_path: g.config.font_path
custom_bold_font_path: g.config.custom_bold_font_path
scale: sapp.dpi_scale()
}) or {
g.ft = new_ft(font_path: g.config.font_path, custom_bold_font_path: g.config.custom_bold_font_path, scale: sapp.dpi_scale()) or {
panic(err)
}
// println('FT took ${time.ticks()-t} ms')

View File

@ -1637,9 +1637,7 @@ fn (mut c Checker) type_implements(typ table.Type, inter_typ table.Type, pos tok
for imethod in inter_sym.methods {
if method := typ_sym.find_method(imethod.name) {
if !imethod.is_same_method_as(method) {
sig := c.table.fn_signature(imethod, {
skip_receiver: true
})
sig := c.table.fn_signature(imethod, skip_receiver: true)
c.error('`$styp` incorrectly implements method `$imethod.name` of interface `$inter_sym.source_name`, expected `$sig`',
pos)
return false

View File

@ -312,21 +312,14 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
}
name := node.name.after('.')
f.writeln('enum $name {')
f.comments(node.comments, {
inline: true
level: .indent
})
f.comments(node.comments, inline: true, level: .indent)
for field in node.fields {
f.write('\t$field.name')
if field.has_expr {
f.write(' = ')
f.expr(field.expr)
}
f.comments(field.comments, {
inline: true
has_nl: false
level: .indent
})
f.comments(field.comments, inline: true, has_nl: false, level: .indent)
f.writeln('')
}
f.writeln('}\n')
@ -567,9 +560,7 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
}
if comments.len > 0 {
f.write(' ')
f.comments(comments, CommentsOptions{
has_nl: false
})
f.comments(comments, has_nl: false)
}
f.writeln('\n')
}
@ -669,9 +660,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
} else {
f.write(' ')
}
f.comments(comments[comm_idx..], {
level: .indent
})
f.comments(comments[comm_idx..], level: .indent)
} else {
f.writeln('')
}
@ -685,9 +674,7 @@ pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) {
for comment in comments {
f.indent++
f.empty_line = true
f.comment(comment, {
inline: true
})
f.comment(comment, inline: true)
f.writeln('')
f.indent--
}
@ -703,11 +690,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
for method in node.methods {
f.write('\t')
f.write(method.stringify(f.table, f.cur_mod).after('fn '))
f.comments(method.comments, {
inline: true
has_nl: false
level: .indent
})
f.comments(method.comments, inline: true, has_nl: false, level: .indent)
f.writeln('')
}
f.writeln('}\n')
@ -796,13 +779,9 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
}
ast.Comment {
if f.array_init_depth > 0 {
f.comment(node, {
iembed: true
})
f.comment(node, iembed: true)
} else {
f.comment(node, {
inline: true
})
f.comment(node, inline: true)
}
}
ast.ComptimeCall {
@ -936,9 +915,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.indent++
for branch in node.branches {
if branch.comment.text != '' {
f.comment(branch.comment, {
inline: true
})
f.comment(branch.comment, inline: true)
f.writeln('')
}
if branch.is_else {
@ -961,9 +938,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
}
f.writeln('}')
if branch.post_comments.len > 0 {
f.comments(branch.post_comments, {
inline: true
})
f.comments(branch.post_comments, inline: true)
}
}
f.indent--
@ -1433,9 +1408,7 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
f.single_line_if = false
if it.post_comments.len > 0 {
f.writeln('')
f.comments(it.post_comments, {
has_nl: false
})
f.comments(it.post_comments, has_nl: false)
}
}
@ -1444,15 +1417,14 @@ pub fn (mut f Fmt) at_expr(node ast.AtExpr) {
}
pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
/*
if node.args.len == 1 && node.expected_arg_types.len == 1 && node.args[0].expr is ast.StructInit &&
node.args[0].typ == node.expected_arg_types[0] {
// struct_init := node.args[0].expr as ast.StructInit
// if struct_init.typ == node.args[0].typ {
f.use_short_fn_args = true
// }
old_short_arg_state := f.use_short_fn_args
f.use_short_fn_args = false
if node.args.len > 0 && node.args.last().expr is ast.StructInit {
struct_typ := (node.args.last().expr as ast.StructInit).typ
if struct_typ == table.void_type {
f.use_short_fn_args = true
}
}
*/
for arg in node.args {
f.comments(arg.comments, {})
}
@ -1528,7 +1500,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
f.write(')')
f.or_expr(node.or_block)
}
f.use_short_fn_args = false
f.use_short_fn_args = old_short_arg_state
}
pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
@ -1555,9 +1527,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
}
for branch in it.branches {
for cmnt in branch.comments {
f.comment(cmnt, {
inline: true
})
f.comment(cmnt, inline: true)
f.writeln('')
}
if !branch.is_else {
@ -1568,9 +1538,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
if j < branch.ecmnts.len && branch.ecmnts[j].len > 0 {
f.write(' ')
for cmnt in branch.ecmnts[j] {
f.comment(cmnt, {
iembed: true
})
f.comment(cmnt, iembed: true)
}
}
if j < branch.exprs.len - 1 {
@ -1599,9 +1567,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
}
}
if branch.post_comments.len > 0 {
f.comments(branch.post_comments, {
inline: true
})
f.comments(branch.post_comments, inline: true)
}
}
f.indent--
@ -1778,9 +1744,7 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
if i < it.ecmnts.len && it.ecmnts[i].len > 0 {
f.write(' ')
for cmt in it.ecmnts[i] {
f.comment(cmt, {
iembed: true
})
f.comment(cmt, iembed: true)
}
}
if i == it.exprs.len - 1 {
@ -1844,29 +1808,31 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
}
f.write('}')
} else {
if f.use_short_fn_args {
f.writeln('')
} else {
use_short_args := f.use_short_fn_args
f.use_short_fn_args = false
if !use_short_args {
f.writeln('$name{')
}
f.comments(it.pre_comments, {
inline: true
has_nl: true
level: .indent
})
f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent)
f.indent++
for field in it.fields {
f.write('$field.name: ')
f.prefix_expr_cast_expr(field.expr)
f.comments(field.comments, {
inline: true
has_nl: false
level: .indent
})
single_line_short_args := use_short_args && it.fields.len < 4
if use_short_args && !single_line_short_args {
f.writeln('')
}
for i, field in it.fields {
f.write('$field.name: ')
f.prefix_expr_cast_expr(field.expr)
f.comments(field.comments, inline: true, has_nl: false, level: .indent)
if single_line_short_args {
if i < it.fields.len - 1 {
f.write(', ')
}
} else {
f.writeln('')
}
}
f.indent--
if !f.use_short_fn_args {
if !use_short_args {
f.write('}')
}
}
@ -1888,9 +1854,7 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) {
comments := field.comments
mut j := 0
for j < comments.len && comments[j].pos.pos < field.pos.pos {
f.comment(comments[j], {
inline: true
})
f.comment(comments[j], inline: true)
f.writeln('')
j++
}
@ -1928,9 +1892,7 @@ fn (mut f Fmt) global_decl(it ast.GlobalDecl) {
for field in it.fields {
comments := field.comments
for comment in comments {
f.comment(comment, {
inline: true
})
f.comment(comment, inline: true)
f.writeln('')
}
f.write('$field.name ')

View File

@ -35,9 +35,7 @@ fn test_fmt() {
mut input_files := []string{}
input_files << keep_input_files
input_files << expected_input_files
input_files = vtest.filter_vtest_only(input_files, {
basepath: vroot
})
input_files = vtest.filter_vtest_only(input_files, basepath: vroot)
fmt_bench.set_total_expected_steps(input_files.len)
for istep, ipath in input_files {
fmt_bench.cstep = istep

View File

@ -0,0 +1,32 @@
type Foo = Bar | Baz
struct Bar {
x string
y int
z int
a int
}
struct Baz {
x string
}
fn main() {
bar_func(x: 'bar', y: 13, z: 42)
foo_func(Baz{
x: 'Baz as Foo sumtype'
})
bar_func(
x: 'bar'
y: 2
z: 3
a: 4
)
func_from_other_file(val: 'something')
}
fn bar_func(bar Bar) {
}
fn foo_func(f Foo) {
}

View File

@ -4841,10 +4841,7 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
compare_fn += '_reverse'
}
// Register a new custom `compare_xxx` function for qsort()
g.table.register_fn({
name: compare_fn
return_type: table.int_type
})
g.table.register_fn(name: compare_fn, return_type: table.int_type)
infix_expr := node.args[0].expr as ast.InfixExpr
styp := g.typ(typ)
// Variables `a` and `b` are used in the `.sort(a < b)` syntax, so we can reuse them

View File

@ -377,7 +377,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
p.check(.lcbr)
pre_comments := p.eat_comments()
// Declare the type
reg_idx := p.table.register_type_symbol(table.TypeSymbol{
reg_idx := p.table.register_type_symbol(
kind: .interface_
name: interface_name
source_name: interface_name
@ -386,7 +386,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
info: table.Interface{
types: []
}
})
)
if reg_idx == -1 {
p.error_with_pos('cannot register interface `$interface_name`, another type with this name exists',
name_pos)
@ -434,13 +434,13 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
methods << method
// println('register method $name')
return_type_sym := p.table.get_type_symbol(method.return_type)
ts.register_method(table.Fn{
ts.register_method(
name: name
params: args
return_type: method.return_type
return_type_source_name: return_type_sym.source_name
is_pub: true
})
)
}
p.top_level_statement_end()
p.check(.rcbr)

View File

@ -619,7 +619,7 @@ pub fn (mut t Table) find_or_register_fn_type(mod string, f Fn, is_anon bool, ha
name := if f.name.len == 0 { 'anon_fn_$f.signature()' } else { f.name.clone() }
source_name := if f.name.len == 0 { 'fn $f.source_signature()' } else { f.name.clone() }
anon := f.name.len == 0 || is_anon
return t.register_type_symbol(TypeSymbol{
return t.register_type_symbol(
kind: .function
name: name
source_name: source_name
@ -630,7 +630,7 @@ pub fn (mut t Table) find_or_register_fn_type(mod string, f Fn, is_anon bool, ha
has_decl: has_decl
func: f
}
})
)
}
pub fn (mut t Table) add_placeholder_type(name string, language Language) int {

View File

@ -498,218 +498,215 @@ pub fn (t TypeSymbol) str() string {
pub fn (mut t Table) register_builtin_type_symbols() {
// reserve index 0 so nothing can go there
// save index check, 0 will mean not found
t.register_type_symbol({
kind: .placeholder
name: 'reserved_0'
})
t.register_type_symbol(TypeSymbol{
t.register_type_symbol(kind: .placeholder, name: 'reserved_0')
t.register_type_symbol(
kind: .void
name: 'void'
source_name: 'void'
cname: 'void'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .voidptr
name: 'voidptr'
source_name: 'voidptr'
cname: 'voidptr'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .byteptr
name: 'byteptr'
source_name: 'byteptr'
cname: 'byteptr'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .charptr
name: 'charptr'
source_name: 'charptr'
cname: 'charptr'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .i8
name: 'i8'
source_name: 'i8'
cname: 'i8'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .i16
name: 'i16'
source_name: 'i16'
cname: 'i16'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .int
name: 'int'
source_name: 'int'
cname: 'int'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .i64
name: 'i64'
source_name: 'i64'
cname: 'i64'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .byte
name: 'byte'
source_name: 'byte'
cname: 'byte'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .u16
name: 'u16'
source_name: 'u16'
cname: 'u16'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .u32
name: 'u32'
source_name: 'u32'
cname: 'u32'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .u64
name: 'u64'
source_name: 'u64'
cname: 'u64'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .f32
name: 'f32'
source_name: 'f32'
cname: 'f32'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .f64
name: 'f64'
source_name: 'f64'
cname: 'f64'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .char
name: 'char'
source_name: 'char'
cname: 'char'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .bool
name: 'bool'
cname: 'bool'
source_name: 'bool'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .none_
name: 'none'
source_name: 'none'
cname: 'none'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .string
name: 'string'
source_name: 'string'
cname: 'string'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .ustring
name: 'ustring'
source_name: 'ustring'
cname: 'ustring'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .array
name: 'array'
source_name: 'array'
cname: 'array'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .map
name: 'map'
source_name: 'map'
cname: 'map'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .chan
name: 'chan'
source_name: 'chan'
cname: 'chan'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .size_t
name: 'size_t'
source_name: 'size_t'
cname: 'size_t'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .rune
name: 'rune'
source_name: 'rune'
cname: 'rune'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .any
name: 'any'
source_name: 'any'
cname: 'any'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .any_float
name: 'any_float'
source_name: 'any_float'
cname: 'any_float'
mod: 'builtin'
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .any_int
name: 'any_int'
source_name: 'any_int'
cname: 'any_int'
mod: 'builtin'
})
)
// TODO: remove. for v1 map compatibility
map_string_string_idx := t.find_or_register_map(string_type, string_type)
map_string_int_idx := t.find_or_register_map(string_type, int_type)
t.register_type_symbol({
t.register_type_symbol(
kind: .alias
name: 'map_string'
source_name: 'map_string'
cname: 'map_string'
mod: 'builtin'
parent_idx: map_string_string_idx
})
t.register_type_symbol({
)
t.register_type_symbol(
kind: .alias
name: 'map_int'
source_name: 'map_int'
cname: 'map_int'
mod: 'builtin'
parent_idx: map_string_int_idx
})
)
}
[inline]
@ -929,9 +926,7 @@ pub fn (table &Table) type_to_str(t Type) string {
.function {
if !table.is_fmt {
info := sym.info as FnType
res = table.fn_signature(info.func, {
type_only: true
})
res = table.fn_signature(info.func, type_only: true)
}
}
.map {

View File

@ -23,9 +23,7 @@ fn get_tests_in_dir(dir string) []string {
fn check_path(vexe string, dir string, tests []string) int {
mut nb_fail := 0
paths := vtest.filter_vtest_only(tests, {
basepath: dir
})
paths := vtest.filter_vtest_only(tests, basepath: dir)
for path in paths {
program := path
print(path + ' ')

View File

@ -130,10 +130,7 @@ fn (mut mcache ModFileCacher) mark_folders_as_vmod_free(folders_so_far []string)
// No need to check these folders anymore,
// because their parents do not contain v.mod files
for f in folders_so_far {
mcache.add(f, ModFileAndFolder{
vmod_file: ''
vmod_folder: f
})
mcache.add(f, vmod_file: '', vmod_folder: f)
}
}