diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 64fe1fbff45..eb60926b817 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -882,7 +882,11 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { // Left not const, Right not const ------------------ if n.Op == EQL || n.Op == NEQ { // If == or !=, no conversions. - } else if lnt, ok := lt.(*NativeType); ok { + } else if ok, lnt := func() (bool, *NativeType) { + lnt, okl := lt.(*NativeType) + _, okr := rt.(*NativeType) + return okl && okr, lnt + }(); ok { if debug { if !isShift { assertSameTypes(lt, rt) diff --git a/gnovm/pkg/gnolang/preprocess_test.go b/gnovm/pkg/gnolang/preprocess_test.go new file mode 100644 index 00000000000..f577ce0d817 --- /dev/null +++ b/gnovm/pkg/gnolang/preprocess_test.go @@ -0,0 +1,66 @@ +package gnolang + +import ( + "bytes" + "reflect" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestPrepocessBinaryExpressionPrimaryAndNative(t *testing.T) { + t.Parallel() + + out := new(bytes.Buffer) + pkg := NewPackageNode("time", "time", nil) + pkg.DefineGoNativeValue("Millisecond", time.Millisecond) + pkg.DefineGoNativeValue("Second", time.Second) + pkg.DefineGoNativeType(reflect.TypeOf(time.Duration(0))) + pv := pkg.NewPackage() + store := gonativeTestStore(pkg, pv) + + m := NewMachineWithOptions(MachineOptions{ + PkgPath: "main", + Output: out, + Store: store, + }) + + c := `package main + import "time" +func main() { + var a int64 = 2 + println(a * time.Second) + +}` + n := MustParseFile("main.go", c) + assert.Panics(t, func() { m.RunFiles(n) }, "should panic: invalid operation: int64 * time.Duration") +} + +func TestPrepocessBinaryExpressionNativeAndNative(t *testing.T) { + t.Parallel() + + out := new(bytes.Buffer) + pkg := NewPackageNode("time", "time", nil) + pkg.DefineGoNativeValue("March", time.March) + pkg.DefineGoNativeValue("Wednesday", time.Wednesday) + pkg.DefineGoNativeType(reflect.TypeOf(time.Month(0))) + pkg.DefineGoNativeType(reflect.TypeOf(time.Weekday(0))) + pv := pkg.NewPackage() + store := gonativeTestStore(pkg, pv) + + m := NewMachineWithOptions(MachineOptions{ + PkgPath: "main", + Output: out, + Store: store, + }) + + c := `package main + import "time" +func main() { + println(time.March * time.Wednesday) + +}` + n := MustParseFile("main.go", c) + assert.Panics(t, func() { m.RunFiles(n) }, "should panic: invalid operation: time.Month * time.Weekday") +}