From 9422cd10097a42a5293d73c0a52640025900f861 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 11 Apr 2020 02:24:00 +0200 Subject: [PATCH] parser: handle mut & receivers; fix checker_error_test --- vlib/v/checker/checker.v | 7 ++++++- vlib/v/parser/fn.v | 4 ++++ vlib/v/pref/default.v | 9 +++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f08353d1e0..87aa2f8cca 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -64,12 +64,17 @@ pub fn (c mut Checker) check_files(ast_files []ast.File) { if c.pref.build_mode == .build_module || c.pref.is_test { return } + if ast_files.len > 1 && ast_files[0].mod.name == 'builtin' { + // TODO hack to fix vv tests + return + } for i, f in c.table.fns { if f.name == 'main' { return } } eprintln('function `main` is undeclared in the main module') + //eprintln(ast_files[0].mod.name) exit(1) } @@ -531,7 +536,7 @@ pub fn (c mut Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.T pub fn (c mut Checker) return_stmt(return_stmt mut ast.Return) { c.expected_type = c.fn_return_type if return_stmt.exprs.len > 0 && c.fn_return_type == table.void_type { - c.error('1too many arguments to return, current function does not return anything', + c.error('too many arguments to return, current function does not return anything', return_stmt.pos) return } diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index c268f5b4e5..62b41f854e 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -107,12 +107,16 @@ fn (p mut Parser) fn_decl() ast.FnDecl { p.next() rec_name = p.check_name() rec_mut = p.tok.kind == .key_mut + is_amp := p.peek_tok.kind == .amp // if rec_mut { // p.check(.key_mut) // } // TODO: talk to alex, should mut be parsed with the type like this? // or should it be a property of the arg, like this ptr/mut becomes indistinguishable rec_type = p.parse_type() + if is_amp && rec_mut { + p.error('use `(f mut Foo)` or `(f &Foo)` instead of `(f mut &Foo)`') + } args << table.Arg{ name: rec_name is_mut: rec_mut diff --git a/vlib/v/pref/default.v b/vlib/v/pref/default.v index 71e82f5e56..7cf86cf165 100644 --- a/vlib/v/pref/default.v +++ b/vlib/v/pref/default.v @@ -3,8 +3,10 @@ // that can be found in the LICENSE file. module pref -import os -import term +import ( + os + term +) pub const ( default_module_path = mpath() @@ -42,7 +44,6 @@ pub fn (p mut Preferences) fill_with_defaults() { } target_dir := if os.is_dir(rpath) { rpath } else { os.dir(rpath) } p.out_name = os.join_path(target_dir, base) - if rpath == '$p.vroot/cmd/v' && os.is_dir('vlib/compiler') { // Building V? Use v2, since we can't overwrite a running // executable on Windows + the precompiled V is more @@ -61,7 +62,7 @@ pub fn (p mut Preferences) fill_with_defaults() { if p.ccompiler == '' { p.ccompiler = default_c_compiler() } - p.is_test = p.path.ends_with('_test.v') + p.is_test = p.path.ends_with('_test.v') || p.path.ends_with('.vv') p.is_script = p.path.ends_with('.v') || p.path.ends_with('.vsh') if p.third_party_option == '' { p.third_party_option = p.cflags