FAQ
Reviewers: gri,

Message:
Hello gri@golang.org (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go.tools


Description:
go.tools/ssa/interp: fix bug: recovered panic without NRPs should return
zero values.

+ test.

Please review this at https://codereview.appspot.com/38040043/

Affected files (+38, -4 lines):
    M ssa/interp/interp.go
    M ssa/interp/ops.go
    M ssa/interp/testdata/recover.go


Index: ssa/interp/interp.go
===================================================================
--- a/ssa/interp/interp.go
+++ b/ssa/interp/interp.go
@@ -532,9 +532,13 @@
   // After a normal return, fr.result contains the result of the call
   // and fr.block is nil.
   //
-// After a recovered panic, fr.result is undefined and fr.block
-// contains the block at which to resume control, which may be
-// nil for a normal return.
+// A recovered panic in a function without named return parameters
+// (NRPs) becomes a normal return of the zero value of the function's
+// result type.
+//
+// After a recovered panic in a function with NRPs, fr.result is
+// undefined and fr.block contains the block at which to resume
+// control.
   //
   func runFrame(fr *frame) {
    defer func() {
@@ -550,7 +554,10 @@
      fmt.Fprintf(os.Stderr, "Panicking: %T %v.\n", fr.panic, fr.panic)
     }
     fr.runDefers()
- fr.block = fr.fn.Recover // recovered panic
+ fr.block = fr.fn.Recover
+ if fr.block == nil {
+ fr.result = zero(fr.fn.Signature.Results()) // return zero
+ }
    }()

    for {
Index: ssa/interp/ops.go
===================================================================
--- a/ssa/interp/ops.go
+++ b/ssa/interp/ops.go
@@ -235,6 +235,15 @@
      s[i] = zero(t.Field(i).Type())
     }
     return s
+ case *types.Tuple:
+ if t.Len() == 1 {
+ return zero(t.At(0).Type())
+ }
+ s := make(tuple, t.Len())
+ for i := range s {
+ s[i] = zero(t.At(i).Type())
+ }
+ return s
    case *types.Chan:
     return chan value(nil)
    case *types.Map:
Index: ssa/interp/testdata/recover.go
===================================================================
--- a/ssa/interp/testdata/recover.go
+++ b/ssa/interp/testdata/recover.go
@@ -2,6 +2,8 @@

   // Tests of panic/recover.

+import "fmt"
+
   func fortyTwo() (r int) {
    r = 42
    // The next two statements simulate a 'return' statement.
@@ -9,8 +11,24 @@
    panic(nil)
   }

+func zero() int {
+ defer func() { recover() }()
+ panic(1)
+}
+
+func zeroEmpty() (int, string) {
+ defer func() { recover() }()
+ panic(1)
+}
+
   func main() {
    if r := fortyTwo(); r != 42 {
     panic(r)
    }
+ if r := zero(); r != 0 {
+ panic(r)
+ }
+ if r, s := zeroEmpty(); r != 0 || s != "" {
+ panic(fmt.Sprint(r, s))
+ }
   }


--

---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Gri at Dec 5, 2013 at 11:14 pm
    LGTM

    https://codereview.appspot.com/38040043/

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 5, '13 at 11:11p
activeDec 5, '13 at 11:14p
posts2
users2
websitegolang.org

2 users in discussion

Gri: 1 post Adonovan: 1 post

People

Translate

site design / logo © 2021 Grokbase