Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix constant comparison compile issue #79

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ jobs:
with:
files: ./

- name: Binary Install
continue-on-error: false
run: curl -fsSL https://github.com/xhd2015/xgo/raw/master/install.sh | env INSTALL_TO_BIN=true bash -x

- name: Check Binary Install
continue-on-error: false
run: xgo revision

- name: Build
run: go build -o /dev/null -v ./cmd/xgo

Expand Down
25 changes: 15 additions & 10 deletions cmd/xgo/runtime_gen/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
)

const VERSION = "1.0.25"
const REVISION = "4e6a5615d778b8909e3315a2ead323822581dd0e+1"
const NUMBER = 198
const VERSION = "1.0.26"
const REVISION = "d19c85ac922d26f26038dc99ae3049de0d91d2f6+1"
const NUMBER = 199

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand All @@ -27,27 +27,32 @@ func init() {
fmt.Fprintf(os.Stderr, "WARNING: xgo toolchain: %v\nnote: this message can be turned off by setting %s=false\n", err, XGO_CHECK_TOOLCHAIN_VERSION)
}
}

func checkVersion() error {
// xgoVersion, xgoRevision, xgoNumber := XGO_VERSION, XGO_REVISION, XGO_NUMBER
// _, _, _ = xgoVersion, xgoRevision, xgoNumber
if XGO_VERSION == "" {
return errors.New("failed to detect xgo version, requires xgo >= v1.0.9, consider run 'xgo upgrade'")
return errors.New("failed to detect xgo version, consider install xgo: go install github.com/xhd2015/xgo/cmd/xgo@latest")
}
if XGO_VERSION == VERSION {
// if runtime version is larger, that means
if XGO_NUMBER < NUMBER {
return errors.New("newer xgo available, consider run 'xgo upgrade'")
return errors.New("newer xgo available, consider run: xgo upgrade")
}
// only one case is feasible: XGO_NUMBER >= runtime NUMBER
// because xgo can be compatible with older sdk
return nil
}
var updateCmd string
if XGO_NUMBER == NUMBER {
// good to go
return nil
}
var msg string
if XGO_NUMBER < NUMBER {
updateCmd = "xgo upgrade"
updateCmd := "xgo upgrade"
msg = fmt.Sprintf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run: %s", XGO_VERSION, VERSION, updateCmd)
} else {
updateCmd = "go get github.com/xhd2015/xgo/runtime@latest"
updateCmd := "go get github.com/xhd2015/xgo/runtime@latest"
msg = fmt.Sprintf("xgo/runtime v%s can be upgraded to v%s, consider run: %s", VERSION, XGO_VERSION, updateCmd)
}
return fmt.Errorf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run '%s'", XGO_VERSION, VERSION, updateCmd)
return errors.New(msg)
}
6 changes: 3 additions & 3 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import "fmt"

const VERSION = "1.0.25"
const REVISION = "4e6a5615d778b8909e3315a2ead323822581dd0e+1"
const NUMBER = 198
const VERSION = "1.0.26"
const REVISION = "d19c85ac922d26f26038dc99ae3049de0d91d2f6+1"
const NUMBER = 199

func getRevision() string {
revSuffix := ""
Expand Down
11 changes: 9 additions & 2 deletions patch/syntax/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ func (ctx *BlockContext) traverseExpr(node syntax.Expr, globaleNames map[string]
node.X = ctx.traverseExpr(node.X, globaleNames, imports)
node.Y = ctx.traverseExpr(node.Y, globaleNames, imports)
// if both side are const, then the operation should also
// be wrapped in a const
if node.X != nil && node.Y != nil {
// be wrapped in a const if the operation is not ==
if node.X != nil && node.Y != nil && !isBoolOp(node.Op) {
xConst := ctx.ConstInfo[node.X]
yConst := ctx.ConstInfo[node.Y]
isXgoConv := func(node syntax.Expr) bool {
Expand Down Expand Up @@ -545,6 +545,13 @@ func (ctx *BlockContext) traverseExpr(node syntax.Expr, globaleNames map[string]
}
return node
}
func isBoolOp(op syntax.Operator) bool {
switch op {
case syntax.Eql, syntax.Neq, syntax.Lss, syntax.Leq, syntax.Gtr, syntax.Geq:
return true
}
return false
}

func getConstType(xgoConv *syntax.XgoSimpleConvert) string {
return xgoConv.X.(*syntax.CallExpr).Fun.(*syntax.Name).Value
Expand Down
25 changes: 15 additions & 10 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
)

const VERSION = "1.0.25"
const REVISION = "4e6a5615d778b8909e3315a2ead323822581dd0e+1"
const NUMBER = 198
const VERSION = "1.0.26"
const REVISION = "d19c85ac922d26f26038dc99ae3049de0d91d2f6+1"
const NUMBER = 199

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand All @@ -27,27 +27,32 @@ func init() {
fmt.Fprintf(os.Stderr, "WARNING: xgo toolchain: %v\nnote: this message can be turned off by setting %s=false\n", err, XGO_CHECK_TOOLCHAIN_VERSION)
}
}

func checkVersion() error {
// xgoVersion, xgoRevision, xgoNumber := XGO_VERSION, XGO_REVISION, XGO_NUMBER
// _, _, _ = xgoVersion, xgoRevision, xgoNumber
if XGO_VERSION == "" {
return errors.New("failed to detect xgo version, requires xgo >= v1.0.9, consider run 'xgo upgrade'")
return errors.New("failed to detect xgo version, consider install xgo: go install github.com/xhd2015/xgo/cmd/xgo@latest")
}
if XGO_VERSION == VERSION {
// if runtime version is larger, that means
if XGO_NUMBER < NUMBER {
return errors.New("newer xgo available, consider run 'xgo upgrade'")
return errors.New("newer xgo available, consider run: xgo upgrade")
}
// only one case is feasible: XGO_NUMBER >= runtime NUMBER
// because xgo can be compatible with older sdk
return nil
}
var updateCmd string
if XGO_NUMBER == NUMBER {
// good to go
return nil
}
var msg string
if XGO_NUMBER < NUMBER {
updateCmd = "xgo upgrade"
updateCmd := "xgo upgrade"
msg = fmt.Sprintf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run: %s", XGO_VERSION, VERSION, updateCmd)
} else {
updateCmd = "go get github.com/xhd2015/xgo/runtime@latest"
updateCmd := "go get github.com/xhd2015/xgo/runtime@latest"
msg = fmt.Sprintf("xgo/runtime v%s can be upgraded to v%s, consider run: %s", VERSION, XGO_VERSION, updateCmd)
}
return fmt.Errorf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run '%s'", XGO_VERSION, VERSION, updateCmd)
return errors.New(msg)
}
49 changes: 49 additions & 0 deletions runtime/test/core/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package core

import (
"errors"
"fmt"
)

// copied from core/version.go

const VERSION = "1.0.26"
const REVISION = "4e6a5615d778b8909e3315a2ead323822581dd0e+1"
const NUMBER = 198

// these fields will be filled by compiler
const XGO_VERSION = ""
const XGO_REVISION = ""
const XGO_NUMBER = 0

// copy from core

func checkVersion() error {
// xgoVersion, xgoRevision, xgoNumber := XGO_VERSION, XGO_REVISION, XGO_NUMBER
// _, _, _ = xgoVersion, xgoRevision, xgoNumber
if XGO_VERSION == "" {
return errors.New("failed to detect xgo version, consider install xgo: go install github.com/xhd2015/xgo/cmd/xgo@latest")
}
if XGO_VERSION == VERSION {
// if runtime version is larger, that means
if XGO_NUMBER < NUMBER {
return errors.New("newer xgo available, consider run: xgo upgrade")
}
// only one case is feasible: XGO_NUMBER >= runtime NUMBER
// because xgo can be compatible with older sdk
return nil
}
if XGO_NUMBER == NUMBER {
// good to go
return nil
}
var msg string
if XGO_NUMBER < NUMBER {
updateCmd := "xgo upgrade"
msg = fmt.Sprintf("xgo v%s maybe incompatible with xgo/runtime v%s, consider run: %s", XGO_VERSION, VERSION, updateCmd)
} else {
updateCmd := "go get github.com/xhd2015/xgo/runtime@latest"
msg = fmt.Sprintf("xgo/runtime v%s can be upgraded to v%s, consider run: %s", VERSION, XGO_VERSION, updateCmd)
}
return errors.New(msg)
}
82 changes: 82 additions & 0 deletions runtime/test/core/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package core

import (
"fmt"
"strings"
"testing"

"github.com/xhd2015/xgo/runtime/mock"
)

const corePkg = "github.com/xhd2015/xgo/runtime/test/core"

func TestCheckVersion(t *testing.T) {
tests := []struct {
xgo VersionInfo
runtime VersionInfo
err string
}{
{
VersionInfo{},
VersionInfo{},
"failed to detect xgo version",
},
{
// good to go if xgo > runtime
VersionInfo{Version: "1.0.1", Revision: "B", Number: 101},
VersionInfo{Version: "1.0.0", Revision: "A", Number: 100},
"xgo/runtime v1.0.0 can be upgraded to v1.0.1",
},
{
// not good to go if xgo < runtime
VersionInfo{Version: "1.0.1", Revision: "B", Number: 101},
VersionInfo{Version: "1.0.2", Revision: "C", Number: 102},
"xgo v1.0.1 maybe incompatible with xgo/runtime v1.0.2",
},
}
for i, tt := range tests {
tt := tt
name := fmt.Sprintf("case_%d", i)
t.Run(name, func(t *testing.T) {
err := testVersion(tt.xgo, tt.runtime)
var errMsg string
if err != nil {
errMsg = err.Error()
}
if (errMsg == "") != (tt.err == "") {
t.Fatalf("expect err msg: %q, actual: %q", tt.err, errMsg)
}
if !strings.Contains(errMsg, tt.err) {
t.Fatalf("expect err: %q, actual: %q", tt.err, errMsg)
}
})
}
}

type VersionInfo struct {
Version string
Revision string
Number int
}

func testVersion(xgo VersionInfo, runtime VersionInfo) error {
mock.PatchByName(corePkg, "VERSION", func() string {
return runtime.Version
})
mock.PatchByName(corePkg, "REVISION", func() string {
return runtime.Revision
})
mock.PatchByName(corePkg, "NUMBER", func() int {
return runtime.Number
})
mock.PatchByName(corePkg, "XGO_VERSION", func() string {
return xgo.Version
})
mock.PatchByName(corePkg, "XGO_REVISION", func() string {
return xgo.Revision
})
mock.PatchByName(corePkg, "XGO_NUMBER", func() int {
return xgo.Number
})
return checkVersion()
}
56 changes: 4 additions & 52 deletions runtime/test/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,12 @@

package debug

import (
_ "encoding/json"
_ "io"
_ "io/ioutil"
_ "net"
_ "net/http"
_ "os/exec"
"testing"
_ "time"
import "testing"

"github.com/xhd2015/xgo/runtime/core"
"github.com/xhd2015/xgo/runtime/functab"
)
const XGO_VERSION = ""

func TestListStdlib(t *testing.T) {
funcs := functab.GetFuncs()

stdPkgs := map[string]bool{
"net/http.Get": true,
}
found, missing := getMissing(funcs, stdPkgs, false)
if len(missing) > 0 {
t.Fatalf("expect func list contains: %v, actual %v", missing, found)
}
}

func getMissing(funcs []*core.FuncInfo, missingPkgs map[string]bool, intf bool) (found []string, missing []string) {
for _, fn := range funcs {
pkg := fn.Pkg
if intf {
if fn.Interface {
// t.Logf("found interface: %v", fn)
key := pkg + "." + fn.RecvType
if _, ok := missingPkgs[key]; ok {
missingPkgs[key] = false
}
}
continue
}
displayName := fn.DisplayName()
// fmt.Printf("found: %s %s\n", pkg, displayName)
// debug
key := pkg + "." + displayName
_, ok := missingPkgs[key]
if ok {
missingPkgs[key] = false
}
}
for k, ok := range missingPkgs {
if ok {
missing = append(missing, k)
} else {
found = append(found, k)
}
if XGO_VERSION == "" {
t.Fatalf("fail")
}
return
}
37 changes: 37 additions & 0 deletions runtime/test/patch_const/const_compile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package patch_const

import "testing"

const XGO_VERSION = ""

// see https://github.com/xhd2015/xgo/issues/78
func TestConstCompile(t *testing.T) {
// all operators
if XGO_VERSION != "" {
t.Fatalf("fail")
}
if XGO_VERSION < "" {
t.Fatalf("fail")
}

if XGO_VERSION > "" {
t.Fatalf("fail")
}
if XGO_VERSION <= "" {
} else {
t.Fatalf("fail")
}

if XGO_VERSION >= "" {
} else {
t.Fatalf("fail")
}

if XGO_VERSION == "" {
} else {
t.Fatalf("fail")
}
if "" != XGO_VERSION {
t.Fatalf("fail")
}
}
Loading