diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index c4468d242b..2c93a41cc8 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1943,6 +1943,7 @@ pub fn (mut p Parser) parse_ident(language ast.Language) ast.Ident { p.register_auto_import('sync') } mut_pos := p.tok.pos() + kind_name := '$p.tok.kind' is_mut := p.tok.kind == .key_mut || is_shared || is_atomic if is_mut { p.next() @@ -1956,7 +1957,11 @@ pub fn (mut p Parser) parse_ident(language ast.Language) ast.Ident { p.next() } if p.tok.kind != .name { - p.error('unexpected token `$p.tok.lit`') + 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`') + } return ast.Ident{ scope: p.scope } diff --git a/vlib/v/parser/tests/invalid_using_atomic.out b/vlib/v/parser/tests/invalid_using_atomic.out new file mode 100644 index 0000000000..37b5c030f4 --- /dev/null +++ b/vlib/v/parser/tests/invalid_using_atomic.out @@ -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 | } diff --git a/vlib/v/parser/tests/invalid_using_atomic.vv b/vlib/v/parser/tests/invalid_using_atomic.vv new file mode 100644 index 0000000000..5d9b58aaad --- /dev/null +++ b/vlib/v/parser/tests/invalid_using_atomic.vv @@ -0,0 +1,5 @@ +fn main() { + if atomic true { + println(true) + } +} diff --git a/vlib/v/parser/tests/invalid_using_mut.out b/vlib/v/parser/tests/invalid_using_mut.out new file mode 100644 index 0000000000..fff45d7a9d --- /dev/null +++ b/vlib/v/parser/tests/invalid_using_mut.out @@ -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 | } diff --git a/vlib/v/parser/tests/unnecessary_mut_2.vv b/vlib/v/parser/tests/invalid_using_mut.vv similarity index 100% rename from vlib/v/parser/tests/unnecessary_mut_2.vv rename to vlib/v/parser/tests/invalid_using_mut.vv diff --git a/vlib/v/parser/tests/invalid_using_shared.out b/vlib/v/parser/tests/invalid_using_shared.out new file mode 100644 index 0000000000..318569239c --- /dev/null +++ b/vlib/v/parser/tests/invalid_using_shared.out @@ -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 | } diff --git a/vlib/v/parser/tests/invalid_using_shared.vv b/vlib/v/parser/tests/invalid_using_shared.vv new file mode 100644 index 0000000000..3c30c4e471 --- /dev/null +++ b/vlib/v/parser/tests/invalid_using_shared.vv @@ -0,0 +1,5 @@ +fn main() { + if shared true { + println(true) + } +} diff --git a/vlib/v/parser/tests/invalid_using_static.out b/vlib/v/parser/tests/invalid_using_static.out new file mode 100644 index 0000000000..75bae06cfe --- /dev/null +++ b/vlib/v/parser/tests/invalid_using_static.out @@ -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 | } diff --git a/vlib/v/parser/tests/invalid_using_static.vv b/vlib/v/parser/tests/invalid_using_static.vv new file mode 100644 index 0000000000..c6a1ebc677 --- /dev/null +++ b/vlib/v/parser/tests/invalid_using_static.vv @@ -0,0 +1,5 @@ +fn main() { + if static true { + println(true) + } +} diff --git a/vlib/v/parser/tests/invalid_using_volatile.out b/vlib/v/parser/tests/invalid_using_volatile.out new file mode 100644 index 0000000000..6b5f37bd80 --- /dev/null +++ b/vlib/v/parser/tests/invalid_using_volatile.out @@ -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 | } diff --git a/vlib/v/parser/tests/invalid_using_volatile.vv b/vlib/v/parser/tests/invalid_using_volatile.vv new file mode 100644 index 0000000000..e5dfdece79 --- /dev/null +++ b/vlib/v/parser/tests/invalid_using_volatile.vv @@ -0,0 +1,5 @@ +fn main() { + if volatile true { + println(true) + } +} diff --git a/vlib/v/parser/tests/unnecessary_mut_2.out b/vlib/v/parser/tests/unnecessary_mut_2.out deleted file mode 100644 index 5f7dfee46b..0000000000 --- a/vlib/v/parser/tests/unnecessary_mut_2.out +++ /dev/null @@ -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 | } \ No newline at end of file