From 5bdf94a7bcef74dd87185feb2a7ddaffe64713ab Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Mon, 8 May 2023 04:56:36 +0530 Subject: [PATCH] checker: disallow mut for blank idents (#18114) --- vlib/v/parser/parser.v | 4 ++++ vlib/v/parser/tests/atomic_blank_ident_err.out | 4 ++++ vlib/v/parser/tests/atomic_blank_ident_err.vv | 2 ++ vlib/v/parser/tests/mut_blank_ident_err.out | 3 +++ vlib/v/parser/tests/mut_blank_ident_err.vv | 1 + vlib/v/parser/tests/shared_blank_ident_err.out | 3 +++ vlib/v/parser/tests/shared_blank_ident_err.vv | 1 + 7 files changed, 18 insertions(+) create mode 100644 vlib/v/parser/tests/atomic_blank_ident_err.out create mode 100644 vlib/v/parser/tests/atomic_blank_ident_err.vv create mode 100644 vlib/v/parser/tests/mut_blank_ident_err.out create mode 100644 vlib/v/parser/tests/mut_blank_ident_err.vv create mode 100644 vlib/v/parser/tests/shared_blank_ident_err.out create mode 100644 vlib/v/parser/tests/shared_blank_ident_err.vv diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index b67bf4f882..8a079b48fa 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2077,6 +2077,10 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt { p.defer_vars = defer_vars left0 := left[0] + if tok.kind in [.key_mut, .key_shared, .key_atomic] && left0.is_blank_ident() { + return p.error_with_pos('cannot use `${tok.kind}` on `_`', tok.pos()) + } + if tok.kind == .key_mut && p.tok.kind != .decl_assign { return p.error('expecting `:=` (e.g. `mut x :=`)') } diff --git a/vlib/v/parser/tests/atomic_blank_ident_err.out b/vlib/v/parser/tests/atomic_blank_ident_err.out new file mode 100644 index 0000000000..734c367a9d --- /dev/null +++ b/vlib/v/parser/tests/atomic_blank_ident_err.out @@ -0,0 +1,4 @@ +vlib/v/parser/tests/atomic_blank_ident_err.vv:1:1: error: cannot use `atomic` on `_` + 1 | atomic _ = 3 + | ~~~~~~ + 2 | diff --git a/vlib/v/parser/tests/atomic_blank_ident_err.vv b/vlib/v/parser/tests/atomic_blank_ident_err.vv new file mode 100644 index 0000000000..c042387645 --- /dev/null +++ b/vlib/v/parser/tests/atomic_blank_ident_err.vv @@ -0,0 +1,2 @@ +atomic _ = 3 + diff --git a/vlib/v/parser/tests/mut_blank_ident_err.out b/vlib/v/parser/tests/mut_blank_ident_err.out new file mode 100644 index 0000000000..cec4ea3448 --- /dev/null +++ b/vlib/v/parser/tests/mut_blank_ident_err.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/mut_blank_ident_err.vv:1:1: error: cannot use `mut` on `_` + 1 | mut _ = 3 + | ~~~ diff --git a/vlib/v/parser/tests/mut_blank_ident_err.vv b/vlib/v/parser/tests/mut_blank_ident_err.vv new file mode 100644 index 0000000000..96f6217c0a --- /dev/null +++ b/vlib/v/parser/tests/mut_blank_ident_err.vv @@ -0,0 +1 @@ +mut _ = 3 diff --git a/vlib/v/parser/tests/shared_blank_ident_err.out b/vlib/v/parser/tests/shared_blank_ident_err.out new file mode 100644 index 0000000000..6985dbcd94 --- /dev/null +++ b/vlib/v/parser/tests/shared_blank_ident_err.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/shared_blank_ident_err.vv:1:1: error: cannot use `shared` on `_` + 1 | shared _ = 3 + | ~~~~~~ diff --git a/vlib/v/parser/tests/shared_blank_ident_err.vv b/vlib/v/parser/tests/shared_blank_ident_err.vv new file mode 100644 index 0000000000..acdf4ecf04 --- /dev/null +++ b/vlib/v/parser/tests/shared_blank_ident_err.vv @@ -0,0 +1 @@ +shared _ = 3