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 .