Skip to content

Commit

Permalink
fix(gnolang#1966): go2Gno loses type info
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy committed May 1, 2024
1 parent 5429655 commit 0d7f67f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
6 changes: 5 additions & 1 deletion gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 66 additions & 0 deletions gnovm/pkg/gnolang/preprocess_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 0d7f67f

Please sign in to comment.