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

cgen: fix shared optional (#15462)

This commit is contained in:
yuyi 2022-08-19 01:18:03 +08:00 committed by GitHub
parent 6d399c5116
commit ea163197c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -2948,7 +2948,7 @@ fn (mut g Gen) expr(node_ ast.Expr) {
node.return_type.clear_flag(.optional)
}
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)
shared_typ := ret_type.set_flag(.shared_f)
shared_styp = g.typ(shared_typ)
@ -2977,7 +2977,7 @@ fn (mut g Gen) expr(node_ ast.Expr) {
g.strs_to_free0 = []
// 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))')
}
// if g.autofree && node.autofree_pregen != '' { // g.strs_to_free0.len != 0 {

View 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)
}