diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b581c4b5e9..fecb8ebc28 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -670,8 +670,13 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) { c.expected_type = table.none_type // TODO a hack to make `x := if ... work` // check variablename for beginning with capital letter 'Abc' for ident in assign_stmt.left { - if assign_stmt.op == .decl_assign && scanner.contains_capital(ident.name) { + is_decl := assign_stmt.op == .decl_assign + if is_decl && scanner.contains_capital(ident.name) { c.error('variable names cannot contain uppercase letters, use snake_case instead', ident.pos) + } else if is_decl && ident.kind != .blank_ident { + if ident.name.starts_with('__') { + c.error('variable names cannot start with `__`', ident.pos) + } } } if assign_stmt.left.len > assign_stmt.right.len { @@ -998,6 +1003,9 @@ fn (c mut Checker) stmt(node ast.Stmt) { c.in_for_count-- } ast.GoStmt { + if !is_call_expr(it.call_expr) { + c.error('expression in `go` must be a function call', expr_pos(it.call_expr))1 + } c.expr(it.call_expr) } // ast.HashStmt {} @@ -1020,6 +1028,13 @@ fn (c mut Checker) stmt(node ast.Stmt) { } } +fn is_call_expr(expr ast.Expr) bool { + return match expr { + ast.CallExpr { true } + else { false } + } +} + fn (c mut Checker) stmts(stmts []ast.Stmt) { c.expected_type = table.void_type for stmt in stmts { diff --git a/vlib/v/checker/tests/inout/decl_underscore.out b/vlib/v/checker/tests/inout/decl_underscore.out new file mode 100644 index 0000000000..2ae7fd09fd --- /dev/null +++ b/vlib/v/checker/tests/inout/decl_underscore.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/inout/decl_underscore.v:2:2: error: variable names cannot start with `__` + 1| fn main() { + 2| __abc := 1 + ~~~~~ + 3| } \ No newline at end of file diff --git a/vlib/v/checker/tests/inout/decl_underscore.vv b/vlib/v/checker/tests/inout/decl_underscore.vv new file mode 100644 index 0000000000..6ef91ce0dc --- /dev/null +++ b/vlib/v/checker/tests/inout/decl_underscore.vv @@ -0,0 +1,3 @@ +fn main() { + __abc := 1 +} diff --git a/vlib/v/checker/tests/inout/go_expr.out b/vlib/v/checker/tests/inout/go_expr.out new file mode 100644 index 0000000000..4430db9a06 --- /dev/null +++ b/vlib/v/checker/tests/inout/go_expr.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/inout/go_expr.v:2:5: error: expression in `go` must be a function call + 1| fn main() { + 2| go 1 + ^ + 3| } \ No newline at end of file diff --git a/vlib/v/checker/tests/inout/go_expr.vv b/vlib/v/checker/tests/inout/go_expr.vv new file mode 100644 index 0000000000..969b29dc5e --- /dev/null +++ b/vlib/v/checker/tests/inout/go_expr.vv @@ -0,0 +1,3 @@ +fn main() { + go 1 +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 5d5f369d2a..038803eed7 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -242,8 +242,7 @@ fn (p mut Parser) check(expected token.Kind) { // p.next() // } if p.tok.kind != expected { - s := 'unexpected `${p.tok.kind.str()}`, expecting `${expected.str()}`' - p.error(s) + p.error('unexpected `${p.tok.kind.str()}`, expecting `${expected.str()}`') } p.next() } @@ -420,9 +419,7 @@ pub fn (p mut Parser) stmt() ast.Stmt { ast.CallExpr { // call_expr = it } - else { - p.error('expression in `go` must be a function call') - } + else {} } return ast.GoStmt{ call_expr: expr @@ -1808,9 +1805,6 @@ fn (p mut Parser) assign_stmt() ast.Stmt { p.error('unknown variable `$ident.name`') } if is_decl && ident.kind != .blank_ident { - if ident.name.starts_with('__') { - p.error('variable names cannot start with `__`') - } if p.scope.known_var(ident.name) { p.error('redefinition of `$ident.name`') }