parser: check using invalid keyword with none ident (#13743)

pull/13748/head
yuyi 2022-03-15 18:58:03 +08:00 committed by GitHub
parent 78b1cbefff
commit 1d83ab6be1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 56 additions and 7 deletions

View File

@ -1943,6 +1943,7 @@ pub fn (mut p Parser) parse_ident(language ast.Language) ast.Ident {
p.register_auto_import('sync') p.register_auto_import('sync')
} }
mut_pos := p.tok.pos() mut_pos := p.tok.pos()
kind_name := '$p.tok.kind'
is_mut := p.tok.kind == .key_mut || is_shared || is_atomic is_mut := p.tok.kind == .key_mut || is_shared || is_atomic
if is_mut { if is_mut {
p.next() p.next()
@ -1956,7 +1957,11 @@ pub fn (mut p Parser) parse_ident(language ast.Language) ast.Ident {
p.next() p.next()
} }
if p.tok.kind != .name { if p.tok.kind != .name {
if is_mut || is_static || is_volatile {
p.error_with_pos('the `$kind_name` keyword is invalid here', mut_pos)
} else {
p.error('unexpected token `$p.tok.lit`') p.error('unexpected token `$p.tok.lit`')
}
return ast.Ident{ return ast.Ident{
scope: p.scope scope: p.scope
} }

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/invalid_using_atomic.vv:2:5: error: the `atomic` keyword is invalid here
1 | fn main() {
2 | if atomic true {
| ~~~~~~
3 | println(true)
4 | }

View File

@ -0,0 +1,5 @@
fn main() {
if atomic true {
println(true)
}
}

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/invalid_using_mut.vv:2:5: error: the `mut` keyword is invalid here
1 | fn main() {
2 | if mut true {
| ~~~
3 | println(true)
4 | }

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/invalid_using_shared.vv:2:5: error: the `shared` keyword is invalid here
1 | fn main() {
2 | if shared true {
| ~~~~~~
3 | println(true)
4 | }

View File

@ -0,0 +1,5 @@
fn main() {
if shared true {
println(true)
}
}

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/invalid_using_static.vv:2:5: error: the `static` keyword is invalid here
1 | fn main() {
2 | if static true {
| ~~~~~~
3 | println(true)
4 | }

View File

@ -0,0 +1,5 @@
fn main() {
if static true {
println(true)
}
}

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/invalid_using_volatile.vv:2:5: error: the `volatile` keyword is invalid here
1 | fn main() {
2 | if volatile true {
| ~~~~~~~~
3 | println(true)
4 | }

View File

@ -0,0 +1,5 @@
fn main() {
if volatile true {
println(true)
}
}

View File

@ -1,6 +0,0 @@
vlib/v/parser/tests/unnecessary_mut_2.vv:2:9: error: unexpected token `true`
1 | fn main() {
2 | if mut true {
| ~~~~
3 | println(true)
4 | }