parser: deprecate `{var |` struct update (#8618)

pull/8637/head^2
Nick Treleaven 2021-02-08 15:03:05 +00:00 committed by GitHub
parent f2100166c7
commit e2ff2a5405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 6 deletions

View File

@ -1054,6 +1054,8 @@ pub:
pos token.Position pos token.Position
} }
*/ */
// deprecated
pub struct Assoc { pub struct Assoc {
pub: pub:
var_name string var_name string

View File

@ -1136,8 +1136,7 @@ pub fn (mut f Fmt) cast_expr(node ast.CastExpr) {
pub fn (mut f Fmt) assoc(node ast.Assoc) { pub fn (mut f Fmt) assoc(node ast.Assoc) {
f.writeln('{') f.writeln('{')
// f.indent++ // f.indent++
f.writeln('\t$node.var_name |') f.writeln('\t...$node.var_name')
// TODO StructInit copy pasta
for i, field in node.fields { for i, field in node.fields {
f.write('\t$field: ') f.write('\t$field: ')
f.expr(node.exprs[i]) f.expr(node.exprs[i])

View File

@ -7,5 +7,8 @@ fn main() {
u := User{ u := User{
age: 54 age: 54
} }
println(u) _ = User{
...u
name: 'hi'
}
} }

View File

@ -6240,9 +6240,9 @@ $staticprefix $interface_name* I_${cctype}_to_Interface_${interface_name}_ptr($c
// //
params_start_pos := g.out.len params_start_pos := g.out.len
mut params := method.params.clone() mut params := method.params.clone()
first_param := params[0] // workaround, { params[0] | ... } doesn't work // hack to mutate typ
params[0] = { params[0] = {
first_param | ...params[0]
typ: params[0].typ.set_nr_muls(1) typ: params[0].typ.set_nr_muls(1)
} }
fargs, _ := g.fn_args(params, false) // second argument is ignored anyway fargs, _ := g.fn_args(params, false) // second argument is ignored anyway

View File

@ -244,7 +244,8 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
node = p.map_init() node = p.map_init()
} else { } else {
// it should be a struct // it should be a struct
if p.peek_tok.kind == .pipe { if p.tok.kind == .name && p.peek_tok.kind == .pipe {
p.warn_with_pos('use e.g. `...struct_var` instead', p.peek_tok.position())
node = p.assoc() node = p.assoc()
} else if (p.tok.kind == .name && p.peek_tok.kind == .colon) } else if (p.tok.kind == .name && p.peek_tok.kind == .colon)
|| p.tok.kind in [.rcbr, .comment, .ellipsis] { || p.tok.kind in [.rcbr, .comment, .ellipsis] {