From 68c3eccec5850eeb611c138d834199c572c3def5 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 13 Apr 2021 13:07:57 +0300 Subject: [PATCH] v: skip asserts in -prod mode inside non _test.v files --- vlib/v/ast/ast.v | 4 +++- vlib/v/gen/c/assert.v | 3 +++ vlib/v/gen/js/js.v | 3 +++ vlib/v/markused/walker.v | 6 ++++-- vlib/v/parser/parser.v | 7 +++++++ vlib/v/tests/prod/asserts_should_be_skipped.prod.v | 4 ++++ .../prod/asserts_should_be_skipped.prod.v.expected.txt | 1 + vlib/v/tests/repl/runner/runner.v | 4 ++-- 8 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 vlib/v/tests/prod/asserts_should_be_skipped.prod.v create mode 100644 vlib/v/tests/prod/asserts_should_be_skipped.prod.v.expected.txt diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index b00fbde031..1f1636249f 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -548,6 +548,7 @@ pub: bytes int // number of processed source code bytes mod Module // the module of the source file (from `module xyz` at the top) global_scope &Scope + is_test bool // true for _test.v files pub mut: scope &Scope stmts []Stmt // all the statements in the source file @@ -1245,7 +1246,8 @@ pub struct AssertStmt { pub: pos token.Position pub mut: - expr Expr + expr Expr + is_used bool // asserts are used in _test.v files, as well as in non -prod builds of all files } // `if [x := opt()] {` diff --git a/vlib/v/gen/c/assert.v b/vlib/v/gen/c/assert.v index b555b09f01..fafb76eade 100644 --- a/vlib/v/gen/c/assert.v +++ b/vlib/v/gen/c/assert.v @@ -6,6 +6,9 @@ module c import v.ast fn (mut g Gen) gen_assert_stmt(original_assert_statement ast.AssertStmt) { + if !original_assert_statement.is_used { + return + } mut node := original_assert_statement g.writeln('// assert') if mut node.expr is ast.InfixExpr { diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 08765fc011..427a46bb6e 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -613,6 +613,9 @@ fn (mut g JsGen) expr(node ast.Expr) { // TODO fn (mut g JsGen) gen_assert_stmt(a ast.AssertStmt) { + if !a.is_used { + return + } g.writeln('// assert') g.write('if( ') g.expr(a.expr) diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index 2b2057693d..ae5023c9a2 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -54,8 +54,10 @@ pub fn (mut w Walker) stmt(node ast.Stmt) { w.asm_io(node.input) } ast.AssertStmt { - w.expr(node.expr) - w.n_asserts++ + if node.is_used { + w.expr(node.expr) + w.n_asserts++ + } } ast.AssignStmt { w.exprs(node.left) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 642a5586db..ac2bd19953 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -31,6 +31,7 @@ mut: peek_tok token.Token table &ast.Table language ast.Language + inside_test_file bool // when inside _test.v or _test.vv file inside_if bool inside_if_expr bool inside_ct_if_expr bool @@ -79,6 +80,7 @@ mut: pub fn parse_stmt(text string, table &ast.Table, scope &ast.Scope) ast.Stmt { mut p := Parser{ scanner: scanner.new_scanner(text, .skip_comments, &pref.Preferences{}) + inside_test_file: true table: table pref: &pref.Preferences{} scope: scope @@ -138,6 +140,9 @@ pub fn (mut p Parser) set_path(path string) { p.file_name = path p.file_base = os.base(path) p.file_name_dir = os.dir(path) + if p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv') { + p.inside_test_file = true + } before_dot_v := path.before('.v') // also works for .vv and .vsh language := before_dot_v.all_after_last('.') langauge_with_underscore := before_dot_v.all_after_last('_') @@ -276,6 +281,7 @@ pub fn (mut p Parser) parse() ast.File { return ast.File{ path: p.file_name path_base: p.file_base + is_test: p.inside_test_file lines: p.scanner.line_nr bytes: p.scanner.text.len mod: module_decl @@ -686,6 +692,7 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt { return ast.AssertStmt{ expr: expr pos: pos + is_used: p.inside_test_file || !p.pref.is_prod } } .key_for { diff --git a/vlib/v/tests/prod/asserts_should_be_skipped.prod.v b/vlib/v/tests/prod/asserts_should_be_skipped.prod.v new file mode 100644 index 0000000000..b7b7990b8c --- /dev/null +++ b/vlib/v/tests/prod/asserts_should_be_skipped.prod.v @@ -0,0 +1,4 @@ +fn main() { + assert 1 > 2 + println('finished') +} diff --git a/vlib/v/tests/prod/asserts_should_be_skipped.prod.v.expected.txt b/vlib/v/tests/prod/asserts_should_be_skipped.prod.v.expected.txt new file mode 100644 index 0000000000..f6563985ac --- /dev/null +++ b/vlib/v/tests/prod/asserts_should_be_skipped.prod.v.expected.txt @@ -0,0 +1 @@ +finished diff --git a/vlib/v/tests/repl/runner/runner.v b/vlib/v/tests/repl/runner/runner.v index 7072c80b8a..9259e28790 100644 --- a/vlib/v/tests/repl/runner/runner.v +++ b/vlib/v/tests/repl/runner/runner.v @@ -41,7 +41,7 @@ fn diff_files(file_result string, file_expected string) string { pub fn run_repl_file(wd string, vexec string, file string) ?string { vexec_folder := os.dir(vexec) + os.path_separator - fcontent := os.read_file(file) or { return error('Could not read file $file') } + fcontent := os.read_file(file) or { return error('Could not read repl file $file') } content := fcontent.replace('\r', '') input := content.all_before('===output===\n') output := content.all_after('===output===\n').trim_right('\n\r') @@ -81,7 +81,7 @@ $diff pub fn run_prod_file(wd string, vexec string, file string) ?string { file_expected := '${file}.expected.txt' f_expected_content := os.read_file(file_expected) or { - return error('Could not read file $file') + return error('Could not read expected prod file $file_expected') } expected_content := f_expected_content.replace('\r', '') cmd := '"$vexec" -prod run "$file"'