cgen: fix match expr in assert

pull/4380/head^2
Enzo Baldisserri 2020-04-13 01:53:26 +02:00 committed by GitHub
parent 5deb86de4f
commit da5a1e458b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 68 deletions

View File

@ -27,7 +27,6 @@ const (
'vlib/v/tests/fixed_array_test.v', 'vlib/v/tests/fixed_array_test.v',
'vlib/v/tests/fn_test.v', 'vlib/v/tests/fn_test.v',
'vlib/v/tests/fn_variadic_test.v', 'vlib/v/tests/fn_variadic_test.v',
'vlib/v/tests/match_test.v',
'vlib/v/tests/msvc_test.v', 'vlib/v/tests/msvc_test.v',
'vlib/v/tests/mut_test.v', 'vlib/v/tests/mut_test.v',
'vlib/v/tests/num_lit_call_method_test.v', 'vlib/v/tests/num_lit_call_method_test.v',

View File

@ -628,9 +628,11 @@ fn (g mut Gen) expr_with_cast(expr ast.Expr, got_type, exp_type table.Type) {
fn (g mut Gen) gen_assert_stmt(a ast.AssertStmt) { fn (g mut Gen) gen_assert_stmt(a ast.AssertStmt) {
g.writeln('// assert') g.writeln('// assert')
g.inside_ternary = true
g.write('if (') g.write('if (')
g.expr(a.expr) g.expr(a.expr)
g.write(')') g.write(')')
g.inside_ternary = false
s_assertion := a.expr.str().replace('"', "\'") s_assertion := a.expr.str().replace('"', "\'")
mut mod_path := g.file.path mut mod_path := g.file.path
$if windows { $if windows {
@ -1495,7 +1497,8 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
g.writeln('// match 0') g.writeln('// match 0')
return return
} }
is_expr := node.is_expr && node.return_type != table.void_type was_inside_ternary := g.inside_ternary
is_expr := (node.is_expr && node.return_type != table.void_type) || was_inside_ternary
if is_expr { if is_expr {
g.inside_ternary = true g.inside_ternary = true
// g.write('/* EM ret type=${g.typ(node.return_type)} expected_type=${g.typ(node.expected_type)} */') // g.write('/* EM ret type=${g.typ(node.return_type)} expected_type=${g.typ(node.expected_type)} */')
@ -1513,12 +1516,14 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
for j, branch in node.branches { for j, branch in node.branches {
if j == node.branches.len - 1 { if j == node.branches.len - 1 {
// last block is an `else{}` // last block is an `else{}`
if node.branches.len > 1 {
if is_expr { if is_expr {
// TODO too many branches. maybe separate ?: matches // TODO too many branches. maybe separate ?: matches
g.write(' : ') g.write(' : ')
} else { } else {
g.writeln('else {') g.writeln('else {')
} }
}
} else { } else {
if j > 0 { if j > 0 {
if is_expr { if is_expr {
@ -1582,11 +1587,11 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
} }
} }
g.stmts(branch.stmts) g.stmts(branch.stmts)
if !g.inside_ternary { if !g.inside_ternary && node.branches.len > 1 {
g.writeln('}') g.writeln('}')
} }
} }
g.inside_ternary = false g.inside_ternary = was_inside_ternary
} }
fn (g mut Gen) ident(node ast.Ident) { fn (g mut Gen) ident(node ast.Ident) {

View File

@ -1,51 +1,78 @@
enum Color { enum Color {
red, green, blue red
green
blue
}
pub fn (c Color) str() string {
return 'tmp'
} }
fn test_match_integers() { fn test_match_integers() {
mut a := 3 mut a := 3
mut b := 0 mut b := 0
match a { match a {
2 { println('two') } 2 {
println('two')
}
3 { 3 {
println('three') println('three')
b = 3 b = 3
} }
4 { println('four') } 4 {
else { println('???') } println('four')
}
else {
println('???')
}
} }
assert b == 3 assert b == 3
assert match 2 { assert match 2 {
1 { 2 } 1 {
2 { 3 } 2
else { 5 } }
2 {
3
}
else {
5
}
} == 3 } == 3
assert match 0 { assert match 0 {
1 { 2 } 1 {
2 { 3 } 2
else { 5 } }
2 {
3
}
else {
5
}
} == 5 } == 5
assert match 1 { assert match 1 {
else { 5 } else {
5
}
} == 5 } == 5
a = 0 a = 0
match 2 { match 2 {
0 { a = 1 } 0 {
1 { a = 2 } a = 1
}
1 {
a = 2
}
else { else {
a = 3 a = 3
println('a is $a') println('a is $a')
} }
} }
assert a == 3 assert a == 3
a = 0 a = 0
match 1 { match 1 {
0 { a = 1 } 0 {
a = 1
}
1 { 1 {
a = 2 a = 2
a = a + 2 a = a + 2
@ -54,7 +81,6 @@ fn test_match_integers() {
else {} else {}
} }
assert a == 6 assert a == 6
a = 0 a = 0
match 1 { match 1 {
else { else {