parser: better error on short init struct

pull/4349/head
Daniel Däschle 2020-04-11 13:56:55 +02:00 committed by GitHub
parent 5a1af94452
commit be16c5b21d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -813,12 +813,17 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
if p.tok.kind == .string { if p.tok.kind == .string {
node = p.map_init() node = p.map_init()
} else { } else {
// it should be a struct
if p.peek_tok.kind == .pipe { if p.peek_tok.kind == .pipe {
node = p.assoc() node = p.assoc()
} else if p.peek_tok.kind == .colon || p.tok.kind == .rcbr { } else if p.peek_tok.kind == .colon || p.tok.kind == .rcbr {
node = p.struct_init(true) // short_syntax: true 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 { } else {
p.error('unexpected {') p.error('unexpected $p.tok.lit, expecting struct key')
} }
} }
p.check(.rcbr) p.check(.rcbr)

View 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
}