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 .

-- 
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/a8212173-c664-4f48-93dc-528a8d8391bcn%40googlegroups.com.

Reply via email to