1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix code generated for or-block for void result return function + code generated for indirection comptime checking for logical operators (#18066)

This commit is contained in:
Felipe Pena 2023-04-27 11:37:29 -03:00 committed by GitHub
parent ee9cfb6df4
commit 9fb52c4c9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 12 deletions

View File

@ -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)

View File

@ -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 = ')

View File

@ -0,0 +1,5 @@
aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaa

View File

@ -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 {}
}
}