Skip to content

Commit 1d83ab6

Browse files
authored
parser: check using invalid keyword with none ident (#13743)
1 parent 78b1cbe commit 1d83ab6

12 files changed

+56
-7
lines changed

vlib/v/parser/parser.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,7 @@ pub fn (mut p Parser) parse_ident(language ast.Language) ast.Ident {
19431943
p.register_auto_import('sync')
19441944
}
19451945
mut_pos := p.tok.pos()
1946+
kind_name := '$p.tok.kind'
19461947
is_mut := p.tok.kind == .key_mut || is_shared || is_atomic
19471948
if is_mut {
19481949
p.next()
@@ -1956,7 +1957,11 @@ pub fn (mut p Parser) parse_ident(language ast.Language) ast.Ident {
19561957
p.next()
19571958
}
19581959
if p.tok.kind != .name {
1959-
p.error('unexpected token `$p.tok.lit`')
1960+
if is_mut || is_static || is_volatile {
1961+
p.error_with_pos('the `$kind_name` keyword is invalid here', mut_pos)
1962+
} else {
1963+
p.error('unexpected token `$p.tok.lit`')
1964+
}
19601965
return ast.Ident{
19611966
scope: p.scope
19621967
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/invalid_using_atomic.vv:2:5: error: the `atomic` keyword is invalid here
2+
1 | fn main() {
3+
2 | if atomic true {
4+
| ~~~~~~
5+
3 | println(true)
6+
4 | }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
if atomic true {
3+
println(true)
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/invalid_using_mut.vv:2:5: error: the `mut` keyword is invalid here
2+
1 | fn main() {
3+
2 | if mut true {
4+
| ~~~
5+
3 | println(true)
6+
4 | }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/invalid_using_shared.vv:2:5: error: the `shared` keyword is invalid here
2+
1 | fn main() {
3+
2 | if shared true {
4+
| ~~~~~~
5+
3 | println(true)
6+
4 | }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
if shared true {
3+
println(true)
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/invalid_using_static.vv:2:5: error: the `static` keyword is invalid here
2+
1 | fn main() {
3+
2 | if static true {
4+
| ~~~~~~
5+
3 | println(true)
6+
4 | }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
if static true {
3+
println(true)
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/invalid_using_volatile.vv:2:5: error: the `volatile` keyword is invalid here
2+
1 | fn main() {
3+
2 | if volatile true {
4+
| ~~~~~~~~
5+
3 | println(true)
6+
4 | }

0 commit comments

Comments
 (0)