mirror of
https://github.com/golang/go
synced 2024-09-05 16:04:50 +00:00
![Cherry Zhang](/assets/img/avatar_default.png)
When a function panics then recovers, it needs to return to the caller with named results having the correct values. For in-register results, we need to load them into registers at the defer return path. For non-open-coded defers, we already generate correct code, as the defer return path is part of the SSA CFG and contains the instructions that are the same as an ordinary return statement, including putting the results to the right places. For open-coded defers, we have a special code generation that emits a disconnected block that currently contains only the deferreturn call and a RET instruction. It leaves the result registers unset. This CL adds instructions that load the result registers on that path. Updates #40724. Change-Id: I1f60514da644fd5fb4b4871a1153c62f42927282 Reviewed-on: https://go-review.googlesource.com/c/go/+/307231 Trust: Cherry Zhang <cherryyz@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Austin Clements <austin@google.com>
37 lines
800 B
Go
37 lines
800 B
Go
// run
|
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Test that when a function recovers from a panic, it
|
|
// returns the correct results to the caller (in particular,
|
|
// setting the result registers correctly).
|
|
|
|
package main
|
|
|
|
type S struct {
|
|
x uint8
|
|
y uint16
|
|
z uint32
|
|
w float64
|
|
}
|
|
|
|
var a0, b0, c0, d0 = 10, "hello", S{1, 2, 3, 4}, [2]int{111, 222}
|
|
|
|
//go:noinline
|
|
//go:registerparams
|
|
func F() (a int, b string, _ int, c S, d [2]int) {
|
|
a, b, c, d = a0, b0, c0, d0
|
|
defer func() { recover() }()
|
|
panic("XXX")
|
|
return
|
|
}
|
|
|
|
func main() {
|
|
a1, b1, zero, c1, d1 := F()
|
|
if a1 != a0 || b1 != b0 || c1 != c0 || d1 != d0 || zero != 0 { // unnamed result gets zero value
|
|
panic("FAIL")
|
|
}
|
|
}
|