mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: better error on short init struct
This commit is contained in:
parent
5a1af94452
commit
be16c5b21d
@ -813,12 +813,17 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
|
||||
if p.tok.kind == .string {
|
||||
node = p.map_init()
|
||||
} else {
|
||||
// it should be a struct
|
||||
if p.peek_tok.kind == .pipe {
|
||||
node = p.assoc()
|
||||
} else if p.peek_tok.kind == .colon || p.tok.kind == .rcbr {
|
||||
node = p.struct_init(true) // short_syntax: true
|
||||
} else if p.tok.kind == .name {
|
||||
p.next()
|
||||
lit := if p.tok.lit != '' { p.tok.lit } else { p.tok.kind.str() }
|
||||
p.error('unexpected ‘$lit‘, expecting ‘:‘')
|
||||
} else {
|
||||
p.error('unexpected {')
|
||||
p.error('unexpected ‘$p.tok.lit‘, expecting struct key')
|
||||
}
|
||||
}
|
||||
p.check(.rcbr)
|
||||
|
18
vlib/v/tests/short_struct_param_syntax_test.v
Normal file
18
vlib/v/tests/short_struct_param_syntax_test.v
Normal file
@ -0,0 +1,18 @@
|
||||
struct TOptions {
|
||||
a int
|
||||
}
|
||||
|
||||
fn t(options TOptions) bool {
|
||||
if options.a == 1 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fn test_short_struct_as_parameter(){
|
||||
if t({a: 1}) {
|
||||
assert true
|
||||
return
|
||||
}
|
||||
assert false
|
||||
}
|
Loading…
Reference in New Issue
Block a user