On Thu, Apr 7, 2022 at 11:03 AM ワタナベハルキ <gt695348...@gmail.com> wrote:
>
> GOARCH="amd64"
> GOHOSTOS="linux"
> GOVERSION="go1.17.2"
>
> Here is my test code
>
> var sink *int
>
> func main(){
>     escape()
> }
>
> // named return value r
> func escape() (r int) {
>     defer func(){
>         recover()
>     }()
>     sink = &r // escape r
>     panic("qOeOp")
>     return
> }
>
> jokoi@ubuntu:~/GoProJ/test$ go build -gcflags "-d defer" main.go
> ./main.go:11:2: stack-allocated defer
>
> and i find something comment may be useful in package 
> cmd/compile/internel/ssagen
>
> if s.hasOpenDefers {
>     ......
>                 // Similarly, skip if there are any heap-allocated result
>                 // parameters that need to be copied back to their stack 
> slots.
>                 for _, f := range s.curfn.Type().Results().FieldSlice() {
>                         if !f.Nname.(*ir.Name).OnStack() {
>                                 s.hasOpenDefers = false
>                                 break
>                         }
>                 }
>         }
>     ......
> }
>
> but i cannot understand why ? As far as i know , coping heap-allocated result 
> parameter back just need a small piece of code like mov rax [rax] at the exit 
> point of one function , why compiler cannot generate  it .

It was probably just never implemented.  An escaping result parameter
is not a common case.

I expect the compiler developers would be happy to review a patch that
makes it work.  See https://go.dev/doc/contribute.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUOyUaA0ZyYOpPsDdPg%2B0Y8o9Jk0a9HSvwxA_RxoCU%2BpQ%40mail.gmail.com.

Reply via email to