mirror of
https://github.com/vlang/v.git
synced 2025-09-15 02:18:47 -04:00
cgen: fix shared optional (#15462)
This commit is contained in:
parent
6d399c5116
commit
ea163197c7
@ -2948,7 +2948,7 @@ fn (mut g Gen) expr(node_ ast.Expr) {
|
|||||||
node.return_type.clear_flag(.optional)
|
node.return_type.clear_flag(.optional)
|
||||||
}
|
}
|
||||||
mut shared_styp := ''
|
mut shared_styp := ''
|
||||||
if g.is_shared && !ret_type.has_flag(.shared_f) {
|
if g.is_shared && !ret_type.has_flag(.shared_f) && !g.inside_or_block {
|
||||||
ret_sym := g.table.sym(ret_type)
|
ret_sym := g.table.sym(ret_type)
|
||||||
shared_typ := ret_type.set_flag(.shared_f)
|
shared_typ := ret_type.set_flag(.shared_f)
|
||||||
shared_styp = g.typ(shared_typ)
|
shared_styp = g.typ(shared_typ)
|
||||||
@ -2977,7 +2977,7 @@ fn (mut g Gen) expr(node_ ast.Expr) {
|
|||||||
g.strs_to_free0 = []
|
g.strs_to_free0 = []
|
||||||
// println('pos=$node.pos.pos')
|
// println('pos=$node.pos.pos')
|
||||||
}
|
}
|
||||||
if g.is_shared && !ret_type.has_flag(.shared_f) {
|
if g.is_shared && !ret_type.has_flag(.shared_f) && !g.inside_or_block {
|
||||||
g.writeln('}, sizeof($shared_styp))')
|
g.writeln('}, sizeof($shared_styp))')
|
||||||
}
|
}
|
||||||
// if g.autofree && node.autofree_pregen != '' { // g.strs_to_free0.len != 0 {
|
// if g.autofree && node.autofree_pregen != '' { // g.strs_to_free0.len != 0 {
|
||||||
|
25
vlib/v/tests/shared_optional_test.v
Normal file
25
vlib/v/tests/shared_optional_test.v
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
struct ABC {
|
||||||
|
mut:
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_shared_optional() {
|
||||||
|
shared abc := foo() or { panic('scared') }
|
||||||
|
rlock abc {
|
||||||
|
println(abc)
|
||||||
|
assert abc.s == 'hello'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() ?ABC {
|
||||||
|
mut a := ABC{}
|
||||||
|
a.bar()?
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut a ABC) bar() ? {
|
||||||
|
a.s = 'hello'
|
||||||
|
println(a.s)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user