checker: fix unsafe tests

pull/5983/head
Alexander Medvednikov 2020-07-25 18:58:23 +02:00
parent 34907f7605
commit 7c86156be5
3 changed files with 27 additions and 24 deletions

View File

@ -1,5 +1,5 @@
fn test_ptr_assign() {
v := 5
mut v := 5
mut p := &v
p++
p += 2

View File

@ -1,5 +1,5 @@
fn test_ptr_assign() {
v := 5
mut v := 5
mut p := &v
p++
p += 2

View File

@ -109,7 +109,7 @@ fn (mut g Gen) comp_if(mut it ast.CompIf) {
ret_type_name := g.table.get_type_symbol(comptime_var_type).name
it_type_name := g.table.get_type_symbol(it.tchk_type).name
types_match := comptime_var_type == it.tchk_type
g.writeln('{ // \$if ${it.val} is ${it_type_name}, typecheck start, $comptime_var_type == $it.tchk_type => $ret_type_name == $it_type_name => $types_match ')
g.writeln('{ // \$if $it.val is $it_type_name, typecheck start, $comptime_var_type == $it.tchk_type => $ret_type_name == $it_type_name => $types_match ')
mut stmts := it.stmts
if !types_match {
stmts = []ast.Stmt{}
@ -151,17 +151,17 @@ fn (mut g Gen) comp_if(mut it ast.CompIf) {
fn (mut g Gen) comp_for(node ast.CompFor) {
sym := g.table.get_type_symbol(g.unwrap_generic(node.typ))
g.writeln('{ // 2comptime: \$for $node.val_var in ${sym.name}(${node.kind.str()}) {')
g.writeln('{ // 2comptime: \$for $node.val_var in ${sym.name}($node.kind.str()) {')
// vweb_result_type := table.new_type(g.table.find_type_idx('vweb.Result'))
mut i := 0
// g.writeln('string method = tos_lit("");')
if node.kind == .methods {
mut methods := sym.methods.filter(it.attrs.len == 0) // methods without attrs first
methods_with_attrs := sym.methods.filter(it.attrs.len > 0) // methods without attrs first
methods_with_attrs := sym.methods.filter(it.attrs.len > 0) // methods with attrs second
methods << methods_with_attrs
if methods.len > 0 {
g.writeln('\tFunctionData $node.val_var;')
g.writeln('\tmemset(&${node.val_var}, 0, sizeof(FunctionData));')
g.writeln('\tmemset(&$node.val_var, 0, sizeof(FunctionData));')
}
for method in methods { // sym.methods {
/*
@ -183,8 +183,9 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
attrs.join(', ') + '}));')
}
method_sym := g.table.get_type_symbol(method.return_type)
g.writeln('\t${node.val_var}.ret_type = tos_lit("${method_sym.name}");')
g.writeln('\t${node.val_var}.type = ${int(method.return_type).str()};')
g.writeln('\t${node.val_var}.ret_type = tos_lit("$method_sym.name");')
styp := int(method.return_type).str()
g.writeln('\t${node.val_var}.type = $styp;')
//
g.comptime_var_type_map[node.val_var] = method.return_type
g.stmts(node.stmts)
@ -201,7 +202,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
fields << fields_with_attrs
if fields.len > 0 {
g.writeln('\tFieldData $node.val_var;')
g.writeln('\tmemset(&${node.val_var}, 0, sizeof(FieldData));')
g.writeln('\tmemset(&$node.val_var, 0, sizeof(FieldData));')
}
for field in fields {
g.writeln('\t// field $i')
@ -213,11 +214,13 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
for attrib in field.attrs {
attrs << 'tos_lit("$attrib")'
}
g.writeln('\t${node.val_var}.attrs = new_array_from_c_array($attrs.len, $attrs.len, sizeof(string), _MOV((string[$attrs.len]){' + attrs.join(', ') + '}));')
g.writeln('\t${node.val_var}.attrs = new_array_from_c_array($attrs.len, $attrs.len, sizeof(string), _MOV((string[$attrs.len]){' +
attrs.join(', ') + '}));')
}
field_sym := g.table.get_type_symbol(field.typ)
g.writeln('\t${node.val_var}.typ = tos_lit("$field_sym.name");')
g.writeln('\t${node.val_var}.type = ${int(field.typ).str()};')
styp := int(field.typ).str()
g.writeln('\t${node.val_var}.type = $styp;')
g.writeln('\t${node.val_var}.is_pub = $field.is_pub;')
g.writeln('\t${node.val_var}.is_mut = $field.is_mut;')
g.comptime_var_type_map[node.val_var] = field.typ