v: skip asserts in -prod mode inside non _test.v files

pull/9708/head
Delyan Angelov 2021-04-13 13:07:57 +03:00
parent 84fe2d8c6e
commit 68c3eccec5
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
8 changed files with 27 additions and 5 deletions

View File

@ -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()] {`

View File

@ -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 {

View File

@ -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)

View File

@ -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)

View File

@ -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 {

View File

@ -0,0 +1,4 @@
fn main() {
assert 1 > 2
println('finished')
}

View File

@ -0,0 +1 @@
finished

View File

@ -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"'