From 9fb52c4c9c496ddd8e7fcea117fed8bdbbc8fb45 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 27 Apr 2023 11:37:29 -0300 Subject: [PATCH] cgen: fix code generated for or-block for void result return function + code generated for indirection comptime checking for logical operators (#18066) --- vlib/v/checker/comptime.v | 13 +--- vlib/v/gen/c/cgen.v | 8 +++ .../slow_tests/inout/or_block_with_rvoid.out | 5 ++ .../v/slow_tests/inout/or_block_with_rvoid.vv | 65 +++++++++++++++++++ 4 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 vlib/v/slow_tests/inout/or_block_with_rvoid.out create mode 100644 vlib/v/slow_tests/inout/or_block_with_rvoid.vv diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index ca67a5e0f8..6e29d93f5d 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -717,18 +717,7 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran if c.is_comptime_selector_field_name(cond.left as ast.SelectorExpr, 'indirections') { - ret := match cond.op { - .gt { c.comptime_fields_default_type.nr_muls() > cond.right.val.i64() } - .lt { c.comptime_fields_default_type.nr_muls() < cond.right.val.i64() } - .ge { c.comptime_fields_default_type.nr_muls() >= cond.right.val.i64() } - .le { c.comptime_fields_default_type.nr_muls() <= cond.right.val.i64() } - else { false } - } - return if ret { - ComptimeBranchSkipState.eval - } else { - ComptimeBranchSkipState.skip - } + return .unknown } } c.error('invalid `\$if` condition', cond.pos) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index e6c574cfe6..1f1b493235 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5823,6 +5823,14 @@ fn (mut g Gen) gen_or_block_stmts(cvar_name string, cast_typ string, stmts []ast g.write('${cvar_name} = ') g.gen_option_error(return_type, expr_stmt.expr) g.writeln(';') + } else if return_type == ast.rvoid_type { + // fn returns !, do not fill var.data + old_inside_opt_data := g.inside_opt_data + g.inside_opt_data = true + g.expr(expr_stmt.expr) + g.inside_opt_data = old_inside_opt_data + g.writeln(';') + g.stmt_path_pos.delete_last() } else { if is_option { g.write('*(${cast_typ}*) ${cvar_name}.data = ') diff --git a/vlib/v/slow_tests/inout/or_block_with_rvoid.out b/vlib/v/slow_tests/inout/or_block_with_rvoid.out new file mode 100644 index 0000000000..199d0303f8 --- /dev/null +++ b/vlib/v/slow_tests/inout/or_block_with_rvoid.out @@ -0,0 +1,5 @@ +aaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaa diff --git a/vlib/v/slow_tests/inout/or_block_with_rvoid.vv b/vlib/v/slow_tests/inout/or_block_with_rvoid.vv new file mode 100644 index 0000000000..931feaea28 --- /dev/null +++ b/vlib/v/slow_tests/inout/or_block_with_rvoid.vv @@ -0,0 +1,65 @@ +struct Encoder {} + +struct Writer {} + +struct StructTypePointer[T] { +mut: + val &T +} + +pub struct Null { + is_null bool = true +} + +pub const null = Null{} + +pub fn (e &Encoder) encode_value[T](val T, mut wr Writer) ! { + e.encode_struct[T](val, 1, mut wr)! +} + +fn (e &Encoder) encode_struct[U](val U, level int, mut wr Writer) ! { + $for field in U.fields { + $if field.indirections > 0 { + println('aaaaaaaaaaaaaaaaaaaa') + } $else { + println('bbbbbbbbbbbbbbbbbbbb') + } + } +} + +fn main() { + e := Encoder{} + + mut sb := Writer{} + mut string_initialized_with_reference := 'ads' + + e.encode_value(StructTypePointer[string]{ val: &string_initialized_with_reference }, mut + sb) or { + println(err) + e.encode_value[Null](null, mut sb) or {} + } + + e.encode_value(StructTypePointer[string]{ val: &string_initialized_with_reference }, mut + sb) or { + println(err) + e.encode_value[Null](null, mut sb) or {} + } + + e.encode_value(StructTypePointer[string]{ val: &string_initialized_with_reference }, mut + sb) or { + println(err) + e.encode_value[Null](null, mut sb) or {} + } + + e.encode_value(StructTypePointer[string]{ val: &string_initialized_with_reference }, mut + sb) or { + dump(err) + e.encode_value[Null](null, mut sb) or {} + } + + e.encode_value(StructTypePointer[string]{ val: &string_initialized_with_reference }, mut + sb) or { + dump(err) + e.encode_value[Null](null, mut sb) or {} + } +}