mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
parent
a987f84b15
commit
d632e84090
@ -434,8 +434,9 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
|
||||
*/
|
||||
}
|
||||
if !cloned {
|
||||
if (var_type.has_flag(.optional) && !val_type.has_flag(.optional))
|
||||
|| (var_type.has_flag(.result) && !val_type.has_flag(.result)) {
|
||||
if !g.inside_comptime_for_field
|
||||
&& ((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()
|
||||
g.expr_with_tmp_var(val, val_type, var_type, tmp_var)
|
||||
} else if is_fixed_array_var {
|
||||
|
@ -121,6 +121,10 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) {
|
||||
}
|
||||
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
|
||||
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(')')
|
||||
if !g.inside_call && (m.return_type.has_flag(.optional) || m.return_type.has_flag(.result)) {
|
||||
g.write('.data)')
|
||||
}
|
||||
return
|
||||
}
|
||||
mut j := 0
|
||||
|
10
vlib/v/gen/c/testdata/comptime_optional_call.out
vendored
Normal file
10
vlib/v/gen/c/testdata/comptime_optional_call.out
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
0
|
||||
0
|
||||
Option(0)
|
||||
Option(0)
|
||||
Option(error: none)
|
||||
Option(error: none)
|
||||
1
|
||||
1
|
||||
println(NIL)
|
||||
Option(error: none)
|
28
vlib/v/gen/c/testdata/comptime_optional_call.vv
vendored
Normal file
28
vlib/v/gen/c/testdata/comptime_optional_call.vv
vendored
Normal 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())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user