Skip to content

Commit

Permalink
bypass type resolution with const operations
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed May 31, 2024
1 parent 808e011 commit f8e03d3
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/xgo/runtime_gen/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.38"
const REVISION = "d939407b345d1697a3a32cf48c5b659d017c4cff+1"
const NUMBER = 248
const REVISION = "808e011ba0498b1aaff762095543c880301dc26b+1"
const NUMBER = 249

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.38"
const REVISION = "d939407b345d1697a3a32cf48c5b659d017c4cff+1"
const NUMBER = 248
const REVISION = "808e011ba0498b1aaff762095543c880301dc26b+1"
const NUMBER = 249

func getRevision() string {
revSuffix := ""
Expand Down
31 changes: 27 additions & 4 deletions patch/syntax/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,35 @@ func getConstDeclValueType(expr syntax.Expr) string {
if isBoolOp(expr.Op) {
return "bool"
}
if expr.X != nil {
return getConstDeclValueType(expr.X)
xIsNil := expr.X == nil
yIsNil := expr.Y == nil
if xIsNil && yIsNil {
return ""
}
// see https://github.com/xhd2015/xgo/issues/172
// var x = 5*y, y's type is not known
var xType string
if !xIsNil {
xType = getConstDeclValueType(expr.X)
}
var yType string
if !yIsNil {
yType = getConstDeclValueType(expr.Y)
}
// either is nil
if xIsNil != yIsNil {
if xType != "" {
return xType
}
return yType
}
if expr.Y != nil {
return getConstDeclValueType(expr.Y)
// 5*5 -> int
// 5*X -> unknown
if xType == yType {
return xType
}
return ""
// TODO: handle SelectorExpr and iota
case *syntax.ParenExpr:
return getConstDeclValueType(expr.X)
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.38"
const REVISION = "d939407b345d1697a3a32cf48c5b659d017c4cff+1"
const NUMBER = 248
const REVISION = "808e011ba0498b1aaff762095543c880301dc26b+1"
const NUMBER = 249

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
17 changes: 15 additions & 2 deletions runtime/test/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,21 @@

package debug

import "testing"
import (
"testing"
"time"

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

const A = 20 * time.Second

func TestHello(t *testing.T) {
t.Logf("hello world!")
mock.PatchByName("github.com/xhd2015/xgo/runtime/test/debug", "A", func() time.Duration {
return 10 * time.Second
})
a := A
if a != 20*time.Second {
t.Fatalf("expect patch A failed because current xgo does not resolve operation type, actual: a=%v, want: %v", a, 20*time.Second)
}
}
20 changes: 20 additions & 0 deletions runtime/test/patch_const/patch_const_op_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package patch_const

import (
"testing"
"time"

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

const A = 20 * time.Second

func TestPatchConstOp(t *testing.T) {
mock.PatchByName("github.com/xhd2015/xgo/runtime/test/patch_const", "A", func() time.Duration {
return 10 * time.Second
})
a := A
if a != 20*time.Second {
t.Fatalf("expect patch A failed because current xgo does not resolve operation type, actual: a=%v, want: %v", a, 20*time.Second)
}
}

0 comments on commit f8e03d3

Please sign in to comment.