parser: fix struct init and assign using `sql` variable (#10968)

pull/10973/head weekly.2021.30
yuyi 2021-07-27 02:14:31 +08:00 committed by GitHub
parent 79c7aed3c2
commit cb7be87d4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View File

@ -674,7 +674,7 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
return p.for_stmt() return p.for_stmt()
} }
.name { .name {
if p.tok.lit == 'sql' { if p.tok.lit == 'sql' && p.peek_tok.kind == .name {
return p.sql_stmt() return p.sql_stmt()
} }
if p.peek_tok.kind == .colon { if p.peek_tok.kind == .colon {

View File

@ -1,6 +1,5 @@
vlib/v/parser/tests/sql_no_db_expr_a.vv:3:6: error: invalid expression: unexpected token `:=`, expecting database vlib/v/parser/tests/sql_no_db_expr_a.vv:4:1: error: invalid expression: unexpected token `}`
1 | fn x() {
2 | // SqlStmt 2 | // SqlStmt
3 | sql := 3 | sql :=
| ~~
4 | } 4 | }
| ^

View File

@ -0,0 +1,26 @@
struct FooFoo {
pub mut:
conn Foo
}
struct Foo {
host string = '127.0.0.1'
port u32 = 3306
username string
password string
dbname string
}
fn test_struct_init_and_assign() {
mut sql := FooFoo{}
sql.conn = Foo{
username: 'username'
password: 'abc'
dbname: 'test'
}
assert sql.conn.host == '127.0.0.1'
assert sql.conn.port == 3306
assert sql.conn.username == 'username'
assert sql.conn.password == 'abc'
assert sql.conn.dbname == 'test'
}