From 68cbf272fc1798b6f75789dcea9ff73c95551127 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Fri, 8 Sep 2023 20:01:47 +0530 Subject: [PATCH] parser: disallow using `sql` as name (#19298) --- vlib/db/mssql/stmt_handle.v | 4 ++-- vlib/v/checker/tests/match_alias_type_err.vv | 2 +- vlib/v/parser/parser.v | 9 +++++++++ .../parser/tests/keyword_sql_used_as_name_err.out | 5 +++++ .../v/parser/tests/keyword_sql_used_as_name_err.vv | 6 ++++++ vlib/v/parser/tests/sql_no_db_expr_a.out | 2 +- vlib/v/parser/tests/sql_no_db_expr_a.vv | 2 +- vlib/v/parser/tests/sql_no_db_expr_b.out | 6 +++--- vlib/v/parser/tests/sql_no_db_expr_b.vv | 2 +- vlib/v/tests/struct_init_and_assign_test.v | 14 +++++++------- 10 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 vlib/v/parser/tests/keyword_sql_used_as_name_err.out create mode 100644 vlib/v/parser/tests/keyword_sql_used_as_name_err.vv diff --git a/vlib/db/mssql/stmt_handle.v b/vlib/db/mssql/stmt_handle.v index e229c8aca1e50c..ea738b2ad0ab3c 100644 --- a/vlib/db/mssql/stmt_handle.v +++ b/vlib/db/mssql/stmt_handle.v @@ -40,8 +40,8 @@ fn (mut h HStmt) close() { } // exec executes a Sql statement. Result is stored in odbc driver, and not yet read. -fn (h HStmt) exec(sql string) ! { - retcode := C.SQLExecDirect(h.hstmt, sql.str, C.SQLINTEGER(C.SQL_NTS)) +fn (h HStmt) exec(sql_ string) ! { + retcode := C.SQLExecDirect(h.hstmt, sql_.str, C.SQLINTEGER(C.SQL_NTS)) check_error(retcode, 'SQLExecDirect()', C.SQLHANDLE(h.hstmt), C.SQLSMALLINT(C.SQL_HANDLE_STMT))! } diff --git a/vlib/v/checker/tests/match_alias_type_err.vv b/vlib/v/checker/tests/match_alias_type_err.vv index 2673231b19cb33..687be883937e50 100644 --- a/vlib/v/checker/tests/match_alias_type_err.vv +++ b/vlib/v/checker/tests/match_alias_type_err.vv @@ -2,7 +2,7 @@ type Stmt = SelectStmt struct SelectStmt {} -fn parse(sql string) Stmt { +fn parse(sql_ string) Stmt { return SelectStmt{} } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 00dc2783efb62c..31e374aff3100c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -65,6 +65,7 @@ mut: inside_map_init bool inside_orm bool inside_chan_decl bool + inside_attr_decl bool fixed_array_dim int // fixed array dim parsing level or_is_handled bool // ignore `or` in this expression builtin_mod bool // are we in the `builtin` module? @@ -676,6 +677,7 @@ fn (mut p Parser) check_js_name() string { } fn (mut p Parser) check_name() string { + pos := p.tok.pos() name := p.tok.lit if p.peek_tok.kind == .dot && name in p.imports { p.register_used_import(name) @@ -686,6 +688,9 @@ fn (mut p Parser) check_name() string { .key_interface { p.check(.key_interface) } else { p.check(.name) } } + if !p.inside_orm && !p.inside_attr_decl && name == 'sql' { + p.error_with_pos('unexpected keyword `sql`, expecting name', pos) + } return name } @@ -1826,6 +1831,10 @@ fn (mut p Parser) attributes() { fn (mut p Parser) parse_attr(is_at bool) ast.Attr { mut kind := ast.AttrKind.plain + p.inside_attr_decl = true + defer { + p.inside_attr_decl = false + } apos := p.prev_tok.pos() if p.tok.kind == .key_unsafe { p.next() diff --git a/vlib/v/parser/tests/keyword_sql_used_as_name_err.out b/vlib/v/parser/tests/keyword_sql_used_as_name_err.out new file mode 100644 index 00000000000000..ba466639c4cbaf --- /dev/null +++ b/vlib/v/parser/tests/keyword_sql_used_as_name_err.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/keyword_sql_used_as_name_err.vv:1:8: error: unexpected keyword `sql`, expecting name + 1 | fn foo(sql string) { + | ~~~ + 2 | println(sql) + 3 | } diff --git a/vlib/v/parser/tests/keyword_sql_used_as_name_err.vv b/vlib/v/parser/tests/keyword_sql_used_as_name_err.vv new file mode 100644 index 00000000000000..1448059c99355a --- /dev/null +++ b/vlib/v/parser/tests/keyword_sql_used_as_name_err.vv @@ -0,0 +1,6 @@ +fn foo(sql string) { + println(sql) +} +fn main() { + foo("hi") +} diff --git a/vlib/v/parser/tests/sql_no_db_expr_a.out b/vlib/v/parser/tests/sql_no_db_expr_a.out index 41417aeba9013c..4577bb9c890fe1 100644 --- a/vlib/v/parser/tests/sql_no_db_expr_a.out +++ b/vlib/v/parser/tests/sql_no_db_expr_a.out @@ -1,5 +1,5 @@ vlib/v/parser/tests/sql_no_db_expr_a.vv:4:1: error: invalid expression: unexpected token `}` 2 | // SqlStmt - 3 | sql := + 3 | sql_ := 4 | } | ^ diff --git a/vlib/v/parser/tests/sql_no_db_expr_a.vv b/vlib/v/parser/tests/sql_no_db_expr_a.vv index 5181f2dff7ad9b..b2be6b6cf85476 100644 --- a/vlib/v/parser/tests/sql_no_db_expr_a.vv +++ b/vlib/v/parser/tests/sql_no_db_expr_a.vv @@ -1,4 +1,4 @@ fn x() { // SqlStmt - sql := + sql_ := } diff --git a/vlib/v/parser/tests/sql_no_db_expr_b.out b/vlib/v/parser/tests/sql_no_db_expr_b.out index da86e9c192806b..026b7cc21815fc 100644 --- a/vlib/v/parser/tests/sql_no_db_expr_b.out +++ b/vlib/v/parser/tests/sql_no_db_expr_b.out @@ -1,6 +1,6 @@ -vlib/v/parser/tests/sql_no_db_expr_b.vv:3:11: error: invalid expression: unexpected token `:=` +vlib/v/parser/tests/sql_no_db_expr_b.vv:3:12: error: invalid expression: unexpected token `:=` 1 | fn x() { 2 | // SqlExpr - 3 | x := sql := - | ~~ + 3 | x := sql_ := + | ~~ 4 | } diff --git a/vlib/v/parser/tests/sql_no_db_expr_b.vv b/vlib/v/parser/tests/sql_no_db_expr_b.vv index fa36435391da84..31eb7ac00ca833 100644 --- a/vlib/v/parser/tests/sql_no_db_expr_b.vv +++ b/vlib/v/parser/tests/sql_no_db_expr_b.vv @@ -1,4 +1,4 @@ fn x() { // SqlExpr - x := sql := + x := sql_ := } diff --git a/vlib/v/tests/struct_init_and_assign_test.v b/vlib/v/tests/struct_init_and_assign_test.v index 5391c240337f1e..2d4a5dd3f4a549 100644 --- a/vlib/v/tests/struct_init_and_assign_test.v +++ b/vlib/v/tests/struct_init_and_assign_test.v @@ -12,15 +12,15 @@ struct Foo { } fn test_struct_init_and_assign() { - mut sql := FooFoo{} - sql.conn = Foo{ + 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' + 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' }