Skip to content

Commit

Permalink
interp: improve handling values and comparisons in interfaces
Browse files Browse the repository at this point in the history
Fixes #1347.
  • Loading branch information
mvertes committed May 23, 2022
1 parent 0703926 commit d64563e
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 12 deletions.
33 changes: 32 additions & 1 deletion internal/cmd/genop/genop.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions interp/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ var actions = [...]string{
aDec: "--",
aEqual: "==",
aGreater: ">",
aGreaterEqual: ">=",
aGetFunc: "getFunc",
aGetIndex: "getIndex",
aGetMethod: "getMethod",
Expand All @@ -290,6 +291,7 @@ var actions = [...]string{
aLand: "&&",
aLor: "||",
aLower: "<",
aLowerEqual: "<=",
aMethod: "Method",
aMul: "*",
aMulAssign: "*=",
Expand Down
8 changes: 8 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,14 @@ func TestEvalComparison(t *testing.T) {
},
{src: `1 > _`, err: "1:28: cannot use _ as value"},
{src: `(_) > 1`, err: "1:28: cannot use _ as value"},
{src: `v := interface{}(2); v == 2`, res: "true"},
{src: `v := interface{}(2); v > 1`, err: "1:49: invalid operation: operator > not defined on interface{}"},
{src: `v := interface{}(int64(2)); v == 2`, res: "false"},
{src: `v := interface{}(int64(2)); v != 2`, res: "true"},
{src: `v := interface{}(2.3); v == 2.3`, res: "true"},
{src: `v := interface{}(float32(2.3)); v != 2.3`, res: "true"},
{src: `v := interface{}("hello"); v == "hello"`, res: "true"},
{src: `v := interface{}("hello"); v < "hellp"`, err: "1:55: invalid operation: operator < not defined on interface{}"},
})
}

Expand Down
78 changes: 72 additions & 6 deletions interp/op.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion interp/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (check typecheck) comparison(n *node) error {
if typ.isNil() {
typ = c1.typ
}
return n.cfgErrorf("invalid operation: operator %v not defined on %s", n.action, typ.id(), ".")
return n.cfgErrorf("invalid operation: operator %v not defined on %s", n.action, typ.id())
}
return nil
}
Expand Down
4 changes: 0 additions & 4 deletions interp/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,5 @@ func genComplex(n *node) func(*frame) complex128 {
func genValueString(n *node) func(*frame) (reflect.Value, string) {
value := genValue(n)

if n.typ.TypeOf().Kind() == reflect.Interface {
return func(f *frame) (reflect.Value, string) { v := value(f); return v, v.Elem().String() }
}

return func(f *frame) (reflect.Value, string) { v := value(f); return v, v.String() }
}

0 comments on commit d64563e

Please sign in to comment.