Skip to content

Commit 68cbf27

Browse files
authored
parser: disallow using sql as name (#19298)
1 parent 9a4fbc8 commit 68cbf27

10 files changed

+36
-16
lines changed

vlib/db/mssql/stmt_handle.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ fn (mut h HStmt) close() {
4040
}
4141

4242
// exec executes a Sql statement. Result is stored in odbc driver, and not yet read.
43-
fn (h HStmt) exec(sql string) ! {
44-
retcode := C.SQLExecDirect(h.hstmt, sql.str, C.SQLINTEGER(C.SQL_NTS))
43+
fn (h HStmt) exec(sql_ string) ! {
44+
retcode := C.SQLExecDirect(h.hstmt, sql_.str, C.SQLINTEGER(C.SQL_NTS))
4545
check_error(retcode, 'SQLExecDirect()', C.SQLHANDLE(h.hstmt), C.SQLSMALLINT(C.SQL_HANDLE_STMT))!
4646
}
4747

vlib/v/checker/tests/match_alias_type_err.vv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ type Stmt = SelectStmt
22

33
struct SelectStmt {}
44

5-
fn parse(sql string) Stmt {
5+
fn parse(sql_ string) Stmt {
66
return SelectStmt{}
77
}
88

vlib/v/parser/parser.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ mut:
6565
inside_map_init bool
6666
inside_orm bool
6767
inside_chan_decl bool
68+
inside_attr_decl bool
6869
fixed_array_dim int // fixed array dim parsing level
6970
or_is_handled bool // ignore `or` in this expression
7071
builtin_mod bool // are we in the `builtin` module?
@@ -676,6 +677,7 @@ fn (mut p Parser) check_js_name() string {
676677
}
677678

678679
fn (mut p Parser) check_name() string {
680+
pos := p.tok.pos()
679681
name := p.tok.lit
680682
if p.peek_tok.kind == .dot && name in p.imports {
681683
p.register_used_import(name)
@@ -686,6 +688,9 @@ fn (mut p Parser) check_name() string {
686688
.key_interface { p.check(.key_interface) }
687689
else { p.check(.name) }
688690
}
691+
if !p.inside_orm && !p.inside_attr_decl && name == 'sql' {
692+
p.error_with_pos('unexpected keyword `sql`, expecting name', pos)
693+
}
689694
return name
690695
}
691696

@@ -1826,6 +1831,10 @@ fn (mut p Parser) attributes() {
18261831

18271832
fn (mut p Parser) parse_attr(is_at bool) ast.Attr {
18281833
mut kind := ast.AttrKind.plain
1834+
p.inside_attr_decl = true
1835+
defer {
1836+
p.inside_attr_decl = false
1837+
}
18291838
apos := p.prev_tok.pos()
18301839
if p.tok.kind == .key_unsafe {
18311840
p.next()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib/v/parser/tests/keyword_sql_used_as_name_err.vv:1:8: error: unexpected keyword `sql`, expecting name
2+
1 | fn foo(sql string) {
3+
| ~~~
4+
2 | println(sql)
5+
3 | }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn foo(sql string) {
2+
println(sql)
3+
}
4+
fn main() {
5+
foo("hi")
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
vlib/v/parser/tests/sql_no_db_expr_a.vv:4:1: error: invalid expression: unexpected token `}`
22
2 | // SqlStmt
3-
3 | sql :=
3+
3 | sql_ :=
44
4 | }
55
| ^
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn x() {
22
// SqlStmt
3-
sql :=
3+
sql_ :=
44
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
vlib/v/parser/tests/sql_no_db_expr_b.vv:3:11: error: invalid expression: unexpected token `:=`
1+
vlib/v/parser/tests/sql_no_db_expr_b.vv:3:12: error: invalid expression: unexpected token `:=`
22
1 | fn x() {
33
2 | // SqlExpr
4-
3 | x := sql :=
5-
| ~~
4+
3 | x := sql_ :=
5+
| ~~
66
4 | }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn x() {
22
// SqlExpr
3-
x := sql :=
3+
x := sql_ :=
44
}

vlib/v/tests/struct_init_and_assign_test.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ struct Foo {
1212
}
1313

1414
fn test_struct_init_and_assign() {
15-
mut sql := FooFoo{}
16-
sql.conn = Foo{
15+
mut sql_ := FooFoo{}
16+
sql_.conn = Foo{
1717
username: 'username'
1818
password: 'abc'
1919
dbname: 'test'
2020
}
21-
assert sql.conn.host == '127.0.0.1'
22-
assert sql.conn.port == 3306
23-
assert sql.conn.username == 'username'
24-
assert sql.conn.password == 'abc'
25-
assert sql.conn.dbname == 'test'
21+
assert sql_.conn.host == '127.0.0.1'
22+
assert sql_.conn.port == 3306
23+
assert sql_.conn.username == 'username'
24+
assert sql_.conn.password == 'abc'
25+
assert sql_.conn.dbname == 'test'
2626
}

0 commit comments

Comments
 (0)