cgen: fix assert `sumtype is` (#5739)

pull/5700/head
yuyi 2020-07-08 14:05:01 +08:00 committed by GitHub
parent d77a78d945
commit bd16dd9fdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 27 deletions

View File

@ -142,7 +142,7 @@ fn find_vs(vswhere_dir, host_arch string) ?VsInstallation {
res_output := res.output.trim_right('\r\n')
// println('res: "$res"')
version := os.read_file('$res_output\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt') or {
println('Unable to find msvc version')
//println('Unable to find msvc version')
return error('Unable to find vs installation')
}
version2 := version // TODO remove. cgen option bug if expr
@ -313,7 +313,7 @@ pub fn (mut v Builder) cc_msvc() {
fn (mut v Builder) build_thirdparty_obj_file_with_msvc(path string, moduleflags []cflag.CFlag) {
msvc := v.cached_msvc
if msvc.valid == false {
verror('Cannot find MSVC on this OS')
return
@ -408,4 +408,3 @@ pub fn msvc_string_flags(cflags []cflag.CFlag) MsvcStringFlags {
other_flags: other_flags
}
}

View File

@ -1005,24 +1005,24 @@ fn (mut g Gen) gen_assert_stmt(a ast.AssertStmt) {
g.decrement_inside_ternary()
if g.is_test {
g.writeln('{')
g.writeln(' g_test_oks++;')
g.writeln('\tg_test_oks++;')
metaname_ok := g.gen_assert_metainfo(a)
g.writeln(' main__cb_assertion_ok(&$metaname_ok);')
g.writeln('\tmain__cb_assertion_ok(&$metaname_ok);')
g.writeln('} else {')
g.writeln(' g_test_fails++;')
g.writeln('\tg_test_fails++;')
metaname_fail := g.gen_assert_metainfo(a)
g.writeln(' main__cb_assertion_failed(&$metaname_fail);')
g.writeln(' longjmp(g_jump_buffer, 1);')
g.writeln(' // TODO')
g.writeln(' // Maybe print all vars in a test function if it fails?')
g.writeln('\tmain__cb_assertion_failed(&$metaname_fail);')
g.writeln('\tlongjmp(g_jump_buffer, 1);')
g.writeln('\t// TODO')
g.writeln('\t// Maybe print all vars in a test function if it fails?')
g.writeln('}')
return
}
g.writeln('{} else {')
g.writeln(' {} else {')
metaname_panic := g.gen_assert_metainfo(a)
g.writeln(' __print_assert_failure(&$metaname_panic);')
g.writeln(' v_panic(tos_lit("Assertion failed..."));')
g.writeln(' exit(1);')
g.writeln('\t__print_assert_failure(&$metaname_panic);')
g.writeln('\tv_panic(tos_lit("Assertion failed..."));')
g.writeln('\texit(1);')
g.writeln('}')
}
@ -1032,27 +1032,27 @@ fn (mut g Gen) gen_assert_metainfo(a ast.AssertStmt) string {
line_nr := a.pos.line_nr
src := cestring(a.expr.str())
metaname := 'v_assert_meta_info_$g.new_tmp_var()'
g.writeln(' VAssertMetaInfo $metaname;')
g.writeln(' memset(&$metaname, 0, sizeof(VAssertMetaInfo));')
g.writeln(' ${metaname}.fpath = ${ctoslit(mod_path)};')
g.writeln(' ${metaname}.line_nr = $line_nr;')
g.writeln(' ${metaname}.fn_name = ${ctoslit(fn_name)};')
g.writeln(' ${metaname}.src = ${ctoslit(src)};')
g.writeln('\tVAssertMetaInfo $metaname;')
g.writeln('\tmemset(&$metaname, 0, sizeof(VAssertMetaInfo));')
g.writeln('\t${metaname}.fpath = ${ctoslit(mod_path)};')
g.writeln('\t${metaname}.line_nr = $line_nr;')
g.writeln('\t${metaname}.fn_name = ${ctoslit(fn_name)};')
g.writeln('\t${metaname}.src = ${ctoslit(src)};')
match a.expr {
ast.InfixExpr {
g.writeln(' ${metaname}.op = ${ctoslit(it.op.str())};')
g.writeln(' ${metaname}.llabel = ${ctoslit(it.left.str())};')
g.writeln(' ${metaname}.rlabel = ${ctoslit(it.right.str())};')
g.write(' ${metaname}.lvalue = ')
g.writeln('\t${metaname}.op = ${ctoslit(it.op.str())};')
g.writeln('\t${metaname}.llabel = ${ctoslit(it.left.str())};')
g.writeln('\t${metaname}.rlabel = ${ctoslit(it.right.str())};')
g.write('\t${metaname}.lvalue = ')
g.gen_assert_single_expr(it.left, it.left_type)
g.writeln(';')
//
g.write(' ${metaname}.rvalue = ')
g.write('\t${metaname}.rvalue = ')
g.gen_assert_single_expr(it.right, it.right_type)
g.writeln(';')
}
ast.CallExpr {
g.writeln(' ${metaname}.op = tos_lit("call");')
g.writeln('\t${metaname}.op = tos_lit("call");')
}
else {}
}
@ -1067,7 +1067,11 @@ fn (mut g Gen) gen_assert_single_expr(e ast.Expr, t table.Type) {
ast.IndexExpr { g.write(ctoslit(unknown_value)) }
ast.PrefixExpr { g.write(ctoslit(unknown_value)) }
ast.MatchExpr { g.write(ctoslit(unknown_value)) }
else { g.gen_expr_to_string(e, t) }
ast.Type {
sym := g.table.get_type_symbol(t)
g.write(ctoslit('$sym.name'))
}
else { g.gen_expr_to_string(e, t) or { g.write(ctoslit('[$err]')) } }
}
g.write(' /* typeof: ' + typeof(e) + ' type: ' + t.str() + ' */ ')
}

View File

@ -0,0 +1,10 @@
struct Moon {}
struct Mars {}
type World = Moon | Mars
fn test_assert_sumtype() {
w := World(Moon{})
assert w is Moon
assert w !is Mars
}