fmt: keep generic type in method call (#7874)

pull/7877/head
zakuro 2021-01-05 09:29:58 +09:00 committed by GitHub
parent efb80bdffd
commit a7a8e659f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 6 deletions

View File

@ -1614,6 +1614,14 @@ pub fn (mut f Fmt) at_expr(node ast.AtExpr) {
f.write(node.name) f.write(node.name)
} }
fn (mut f Fmt) write_generic_if_require(node ast.CallExpr) {
if node.generic_type != 0 && node.generic_type != table.void_type {
f.write('<')
f.write(f.table.type_to_str(node.generic_type))
f.write('>')
}
}
pub fn (mut f Fmt) call_expr(node ast.CallExpr) { pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
old_short_arg_state := f.use_short_fn_args old_short_arg_state := f.use_short_fn_args
f.use_short_fn_args = false f.use_short_fn_args = false
@ -1669,7 +1677,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
} }
} }
f.expr(node.left) f.expr(node.left)
f.write('.' + node.name + '(') f.write('.' + node.name)
f.write_generic_if_require(node)
f.write('(')
f.call_args(node.args) f.call_args(node.args)
f.write(')') f.write(')')
// if is_mut { // if is_mut {
@ -1690,11 +1700,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
} }
f.write('$name') f.write('$name')
} }
if node.generic_type != 0 && node.generic_type != table.void_type { f.write_generic_if_require(node)
f.write('<')
f.write(f.table.type_to_str(node.generic_type))
f.write('>')
}
f.write('(') f.write('(')
f.call_args(node.args) f.call_args(node.args)
f.write(')') f.write(')')

View File

@ -0,0 +1,14 @@
fn simple<T>() T {
return T{}
}
struct Foo {}
fn (_ Foo) simple<T>() T {
return T{}
}
fn main() {
simple<int>()
Foo{}.simple<int>()
}