]> Cypherpunks repositories - gostls13.git/commitdiff
go/constant: follow-up for https://go-review.googlesource.com/32870
authorRobert Griesemer <gri@golang.org>
Mon, 7 Nov 2016 19:20:48 +0000 (11:20 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 7 Nov 2016 20:59:49 +0000 (20:59 +0000)
For #17812.

Change-Id: I58411aaa0e8b2250a16ddb20c951e39da3d601e8
Reviewed-on: https://go-review.googlesource.com/32872
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/constant/value.go

index 005b9990071e24dcdff33e117b14250e083ed8e5..7c32473c616503848557440ac46427e03286e244 100644 (file)
@@ -849,7 +849,9 @@ Error:
 func ord(x Value) int {
        switch x.(type) {
        default:
-               return -1 // force invalid value into "x position" in match
+               // force invalid value into "x position" in match
+               // (don't panic here so that callers can provide a better error message)
+               return -1
        case unknownVal:
                return 0
        case boolVal, stringVal:
@@ -869,8 +871,8 @@ func ord(x Value) int {
 
 // match returns the matching representation (same type) with the
 // smallest complexity for two values x and y. If one of them is
-// numeric, both of them must be numeric. If one of them is Unknown,
-// both results are Unknown.
+// numeric, both of them must be numeric. If one of them is Unknown
+// or invalid (say, nil) both results are that value.
 //
 func match(x, y Value) (_, _ Value) {
        if ord(x) > ord(y) {
@@ -928,7 +930,9 @@ func match(x, y Value) (_, _ Value) {
                }
        }
 
-       return x, x // force unknown and invalid values into "x position" in callers of match
+       // force unknown and invalid values into "x position" in callers of match
+       // (don't panic here so that callers can provide a better error message)
+       return x, x
 }
 
 // BinaryOp returns the result of the binary expression x op y.
@@ -941,8 +945,8 @@ func match(x, y Value) (_, _ Value) {
 // instead of token.QUO; the result is guaranteed to be Int in this case.
 // Division by zero leads to a run-time panic.
 //
-func BinaryOp(x Value, op token.Token, y Value) Value {
-       x, y = match(x, y)
+func BinaryOp(x_ Value, op token.Token, y_ Value) Value {
+       x, y := match(x_, y_)
 
        switch x := x.(type) {
        case unknownVal:
@@ -1109,7 +1113,7 @@ func BinaryOp(x Value, op token.Token, y Value) Value {
        }
 
 Error:
-       panic(fmt.Sprintf("invalid binary operation %v %s %v", x, op, y))
+       panic(fmt.Sprintf("invalid binary operation %v %s %v", x_, op, y_))
 }
 
 func add(x, y Value) Value { return BinaryOp(x, token.ADD, y) }
@@ -1177,8 +1181,8 @@ func cmpZero(x int, op token.Token) bool {
 // If one of the operands is Unknown, the result is
 // false.
 //
-func Compare(x Value, op token.Token, y Value) bool {
-       x, y = match(x, y)
+func Compare(x_ Value, op token.Token, y_ Value) bool {
+       x, y := match(x_, y_)
 
        switch x := x.(type) {
        case unknownVal:
@@ -1248,5 +1252,5 @@ func Compare(x Value, op token.Token, y Value) bool {
                }
        }
 
-       panic(fmt.Sprintf("invalid comparison %v %s %v", x, op, y))
+       panic(fmt.Sprintf("invalid comparison %v %s %v", x_, op, y_))
 }