Commit 1a279b34 authored by Robert Griesemer's avatar Robert Griesemer

go/constant: follow-up for https://go-review.googlesource.com/32870

For #17812.

Change-Id: I58411aaa0e8b2250a16ddb20c951e39da3d601e8
Reviewed-on: https://go-review.googlesource.com/32872Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 231aa9d6
...@@ -849,7 +849,9 @@ Error: ...@@ -849,7 +849,9 @@ Error:
func ord(x Value) int { func ord(x Value) int {
switch x.(type) { switch x.(type) {
default: 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: case unknownVal:
return 0 return 0
case boolVal, stringVal: case boolVal, stringVal:
...@@ -869,8 +871,8 @@ func ord(x Value) int { ...@@ -869,8 +871,8 @@ func ord(x Value) int {
// match returns the matching representation (same type) with the // match returns the matching representation (same type) with the
// smallest complexity for two values x and y. If one of them is // 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, // numeric, both of them must be numeric. If one of them is Unknown
// both results are Unknown. // or invalid (say, nil) both results are that value.
// //
func match(x, y Value) (_, _ Value) { func match(x, y Value) (_, _ Value) {
if ord(x) > ord(y) { if ord(x) > ord(y) {
...@@ -928,7 +930,9 @@ func match(x, y Value) (_, _ Value) { ...@@ -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. // BinaryOp returns the result of the binary expression x op y.
...@@ -941,8 +945,8 @@ func match(x, y Value) (_, _ Value) { ...@@ -941,8 +945,8 @@ func match(x, y Value) (_, _ Value) {
// instead of token.QUO; the result is guaranteed to be Int in this case. // instead of token.QUO; the result is guaranteed to be Int in this case.
// Division by zero leads to a run-time panic. // Division by zero leads to a run-time panic.
// //
func BinaryOp(x Value, op token.Token, y Value) Value { func BinaryOp(x_ Value, op token.Token, y_ Value) Value {
x, y = match(x, y) x, y := match(x_, y_)
switch x := x.(type) { switch x := x.(type) {
case unknownVal: case unknownVal:
...@@ -1109,7 +1113,7 @@ func BinaryOp(x Value, op token.Token, y Value) Value { ...@@ -1109,7 +1113,7 @@ func BinaryOp(x Value, op token.Token, y Value) Value {
} }
Error: 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) } func add(x, y Value) Value { return BinaryOp(x, token.ADD, y) }
...@@ -1177,8 +1181,8 @@ func cmpZero(x int, op token.Token) bool { ...@@ -1177,8 +1181,8 @@ func cmpZero(x int, op token.Token) bool {
// If one of the operands is Unknown, the result is // If one of the operands is Unknown, the result is
// false. // false.
// //
func Compare(x Value, op token.Token, y Value) bool { func Compare(x_ Value, op token.Token, y_ Value) bool {
x, y = match(x, y) x, y := match(x_, y_)
switch x := x.(type) { switch x := x.(type) {
case unknownVal: case unknownVal:
...@@ -1248,5 +1252,5 @@ func Compare(x Value, op token.Token, y Value) bool { ...@@ -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_))
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment