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

cgen: fix comptime optional methods call and optional field (fix #16499 #16465) (#16503)

This commit is contained in:
shove 2022-11-24 00:08:49 +08:00 committed by GitHub
parent a987f84b15
commit d632e84090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 2 deletions

View File

@ -434,8 +434,9 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
*/ */
} }
if !cloned { if !cloned {
if (var_type.has_flag(.optional) && !val_type.has_flag(.optional)) if !g.inside_comptime_for_field
|| (var_type.has_flag(.result) && !val_type.has_flag(.result)) { && ((var_type.has_flag(.optional) && !val_type.has_flag(.optional))
|| (var_type.has_flag(.result) && !val_type.has_flag(.result))) {
tmp_var := g.new_tmp_var() tmp_var := g.new_tmp_var()
g.expr_with_tmp_var(val, val_type, var_type, tmp_var) g.expr_with_tmp_var(val, val_type, var_type, tmp_var)
} else if is_fixed_array_var { } else if is_fixed_array_var {

View File

@ -121,6 +121,10 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) {
} }
return return
} }
if !g.inside_call && (m.return_type.has_flag(.optional) || m.return_type.has_flag(.result)) {
g.write('(*(${g.base_type(m.return_type)}*)')
}
// TODO: check argument types // TODO: check argument types
g.write('${util.no_dots(sym.name)}_${g.comptime_for_method}(') g.write('${util.no_dots(sym.name)}_${g.comptime_for_method}(')
@ -165,6 +169,9 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) {
} }
} }
g.write(')') g.write(')')
if !g.inside_call && (m.return_type.has_flag(.optional) || m.return_type.has_flag(.result)) {
g.write('.data)')
}
return return
} }
mut j := 0 mut j := 0

View File

@ -0,0 +1,10 @@
0
0
Option(0)
Option(0)
Option(error: none)
Option(error: none)
1
1
println(NIL)
Option(error: none)

View File

@ -0,0 +1,28 @@
struct Foo {
foobar int
bar ?int
baz ?int = none
}
fn (f Foo) bar() int {
return 1
}
fn (f Foo) baz() ?string {
return none
}
fn main() {
foo := Foo{}
$for field in Foo.fields {
var := foo.$(field.name)
println(var)
println(foo.$(field.name))
}
$for method in Foo.methods {
var := foo.$method()
println(var)
println(foo.$method())
}
}