parser: fix optional expr with array value (#13599)

pull/13606/head
yuyi 2022-02-25 20:54:12 +08:00 committed by GitHub
parent 73f931b52e
commit 83ea97b1a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 9 deletions

View File

@ -376,9 +376,7 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden
return node
}
p.is_stmt_ident = is_stmt_ident
} else if p.tok.kind in [.lsbr, .nilsbr]
&& (p.inside_fn || p.tok.line_nr == p.prev_tok.line_nr) {
// node = p.index_expr(node)
} else if p.tok.kind in [.lsbr, .nilsbr] && p.tok.line_nr == p.prev_tok.line_nr {
if p.tok.kind == .nilsbr {
node = p.index_expr(node, true)
} else {

View File

@ -3,9 +3,7 @@ const x = 4
[deprecated]
fn g() {
a := [3]
// indexing is currently allowed on next line
_ = a
[0]
_ = a[0]
}
const y = 5
@ -16,7 +14,5 @@ const z = 6
[typedef]
struct C.Foo{}
// test implicit main allows indexing on next line
a := [3]
_ := a
[0]
_ := a[0]

View File

@ -0,0 +1,23 @@
struct Empty {
empty string
}
fn print_error() ?[]Empty {
mut test := []Empty{}
test << Empty{
empty: 'Test'
}
if test[0].empty != '' {
return error('Not empty')
}
return test
}
fn test_option_expr_with_array_value() {
test_error := print_error() or {
eprintln(err)
[]Empty{}
}
println(test_error)
assert '$test_error' == '[]'
}