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.write('if( ') g.inside_ternary = true
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,11 +1516,13 @@ 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 is_expr { if node.branches.len > 1 {
// TODO too many branches. maybe separate ?: matches if is_expr {
g.write(' : ') // TODO too many branches. maybe separate ?: matches
} else { g.write(' : ')
g.writeln('else {') } else {
g.writeln('else {')
}
} }
} else { } else {
if j > 0 { if j > 0 {
@ -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,67 +1,93 @@
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 {
3 { println('two')
println('three') }
b = 3 3 {
} println('three')
4 { println('four') } b = 3
else { println('???') } }
} 4 {
println('four')
}
else {
println('???')
}
}
assert b == 3 assert b == 3
assert match 2 {
assert match 2 { 1 {
1 { 2 } 2
2 { 3 } }
else { 5 } 2 {
} == 3 3
}
assert match 0 { else {
1 { 2 } 5
2 { 3 } }
else { 5 } } == 3
} == 5 assert match 0 {
1 {
assert match 1 { 2
else { 5 } }
} == 5 2 {
3
a = 0 }
match 2 { else {
0 { a = 1 } 5
1 { a = 2 } }
else { } == 5
a = 3 assert match 1 {
println('a is $a') else {
} 5
} }
assert a == 3 } == 5
a = 0
a = 0 match 2 {
match 1 { 0 {
0 { a = 1 } a = 1
1 { }
a = 2 1 {
a = a + 2 a = 2
a = a + 2 }
} else {
else {} a = 3
} println('a is $a')
assert a == 6 }
}
a = 0 assert a == 3
match 1 { a = 0
else { match 1 {
a = -2 0 {
} a = 1
} }
assert a == -2 1 {
a = 2
a = a + 2
a = a + 2
}
else {}
}
assert a == 6
a = 0
match 1 {
else {
a = -2
}
}
assert a == -2
} }
fn test_match_enums() { fn test_match_enums() {