From 1a279b34f674369c05694bd2d8e493ec2d3bba97 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 7 Nov 2016 11:20:48 -0800 Subject: [PATCH] go/constant: follow-up for https://go-review.googlesource.com/32870 For #17812. Change-Id: I58411aaa0e8b2250a16ddb20c951e39da3d601e8 Reviewed-on: https://go-review.googlesource.com/32872 Reviewed-by: Alan Donovan --- src/go/constant/value.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/go/constant/value.go b/src/go/constant/value.go index 005b999007..7c32473c61 100644 --- a/src/go/constant/value.go +++ b/src/go/constant/value.go @@ -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_)) } -- 2.48.1