Skip to content

Commit

Permalink
fix: a const, non-const equality check must convert
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma committed Jul 6, 2020
1 parent d229c2a commit a8b1c2a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 302 deletions.
14 changes: 14 additions & 0 deletions _test/comp2.go
@@ -0,0 +1,14 @@
package main

type delta int32

func main() {
a := delta(-1)

println(a != -1)
println(a == -1)
}

// Output:
// false
// true
9 changes: 6 additions & 3 deletions internal/genop/genop.go

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

12 changes: 12 additions & 0 deletions interp/cfg.go
Expand Up @@ -732,6 +732,18 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) {
n.typ = c0.typ
case aEqual, aNotEqual:
n.typ = sc.getType("bool")
if isConstVal(c0) && !isConstVal(c1) || !isConstVal(c0) && isConstVal(c1) {
// if either node is a constant value, but the other is not, the constant
// must be converted into the non-constants type.
switch {
case isConstVal(c0):
convertConstantValue(c0)
c0.rval = c0.rval.Convert(c1.typ.TypeOf())
default:
convertConstantValue(c1)
c1.rval = c1.rval.Convert(c0.typ.TypeOf())
}
}
if n.child[0].sym == nilSym || n.child[1].sym == nilSym {
if n.action == aEqual {
n.gen = isNil
Expand Down

0 comments on commit a8b1c2a

Please sign in to comment.