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 consts type recognition with exprs #145

Merged
merged 1 commit into from
May 24, 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
30 changes: 25 additions & 5 deletions .github/workflows/nightly-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- uses: actions/checkout@v4
with:
path: xgo
ref: master
ref: fix-const-expr-type-recognition

- name: Set up Go
uses: actions/setup-go@v4
Expand All @@ -39,6 +39,7 @@ jobs:
run: |
cd xgo
go install ./cmd/xgo
xgo exec --reset-instrument --log-debug go version
xgo revision

- uses: actions/checkout@v4
Expand All @@ -48,7 +49,11 @@ jobs:

- name: Test pocketbase/pocketbase
continue-on-error: true
run: cd pocketbase/pocketbase && xgo test -v ./...
run: |
cd pocketbase/pocketbase
xgo test -v ./...
echo $? > test-exit-code.txt
echo "exit code is $(cat test-exit-code.txt)"

- uses: actions/checkout@v4
with:
Expand All @@ -57,7 +62,12 @@ jobs:

- name: Test gohugoio/hugo
continue-on-error: true
run: cd gohugoio/hugo && xgo test -v ./...
run: |
cd gohugoio/hugo
xgo test -v ./...
echo $? > test-exit-code.txt
echo "exit code is $?"
echo "exit code is $(cat test-exit-code.txt)"

- uses: actions/checkout@v4
with:
Expand All @@ -66,5 +76,15 @@ jobs:

- name: Test kubernetes/kubernetes
continue-on-error: true
run: cd kubernetes/kubernetes && xgo test -v ./...

run: |
cd kubernetes/kubernetes
xgo test -v ./...
echo $? > test-exit-code.txt
echo "exit code is $(cat test-exit-code.txt)"

- name: Summary
run: |
echo "exit code:"
echo " pocketbase/pocketbase: $(cat pocketbase/pocketbase/test-exit-code.txt)"
echo " gohugoio/hugo: $(cat gohugoio/hugo/test-exit-code.txt)"
echo " kubernetes/kubernetes: $(cat kubernetes/kubernetes/test-exit-code.txt)"
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ go run -tags dev ./cmd/xgo help
go test -tags dev -run TestHelloWorld -v ./test
```

NOTE: when developing, always add `-tags dev` to tell go that we building with dev mode.
NOTE: when developing, always add `-tags dev` to tell go that we are building in dev mode.

If you want to check instrumented GOROOT, run:
```sh
Expand Down Expand Up @@ -53,7 +53,7 @@ We can also explicitly specify all expected go versions we want to pass:
go run ./script/run-test/ --include go1.17.13 --include go1.18.10 --include go1.19.13 --include go1.20.14 --include go1.21.8 --include go1.22.1
```

If there were testing cache, we can force the test re-run by adding a `-count=1` flag:
If there were testing cache, we can force the test to re-run by adding a `-count=1` flag:
```sh
go run ./script/run-test/ --include go1.17.13 --include go1.18.10 --include go1.19.13 --include go1.20.14 --include go1.21.8 --include go1.22.1 -count=1
```
Expand Down
5 changes: 4 additions & 1 deletion cmd/xgo/patch_compiler_ast_type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ var noderExprPatch = &FilePatch{
},
}

// /Users/xhd2015/.xgo/go-instrument-dev/go1.22.2_Us_xh_Pr_xh_xg_go_go_e9d4e1e8/go1.22.2/src/cmd/compile/internal/syntax/printer.go
var syntaxPrinterPatch = &FilePatch{
FilePath: _FilePath{"src", "cmd", "compile", "internal", "syntax", "printer.go"},
Patches: []*Patch{
Expand All @@ -321,7 +322,7 @@ var syntaxPrinterPatch = &FilePatch{
p.printRawNode(n.X)
`,
CheckGoVersion: func(goVersion *goinfo.GoVersion) bool {
return goVersion.Major == 1 && goVersion.Minor <= 21
return goVersion.Major == 1 && goVersion.Minor <= 22
},
},
},
Expand Down Expand Up @@ -387,6 +388,8 @@ func patchCompilerForConstTrap(goroot string, goVersion *goinfo.GoVersion) error
if err != nil {
return err
}
}
if goVersion.Major == 1 && goVersion.Minor <= 22 {
err = syntaxPrinterPatch.Apply(goroot, goVersion)
if err != nil {
return err
Expand Down
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.37"
const REVISION = "63941402b67d43bbac6c7d3eb342916bff70f2a3+1"
const NUMBER = 233
const REVISION = "cf49f7ce9b237a85a242c245e9846294b3c8347b+1"
const NUMBER = 234

// 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.37"
const REVISION = "63941402b67d43bbac6c7d3eb342916bff70f2a3+1"
const NUMBER = 233
const REVISION = "cf49f7ce9b237a85a242c245e9846294b3c8347b+1"
const NUMBER = 234

func getRevision() string {
revSuffix := ""
Expand Down
13 changes: 13 additions & 0 deletions patch/syntax/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,19 @@ func getConstDeclValueType(expr syntax.Expr) string {
return "bool"
}
// NOTE: nil is not a constant
case *syntax.Operation:
// binary operation
if isBoolOp(expr.Op) {
return "bool"
}
if expr.X != nil {
return getConstDeclValueType(expr.X)
}
if expr.Y != nil {
return getConstDeclValueType(expr.Y)
}
case *syntax.ParenExpr:
return getConstDeclValueType(expr.X)
}
return ""
}
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.37"
const REVISION = "63941402b67d43bbac6c7d3eb342916bff70f2a3+1"
const NUMBER = 233
const REVISION = "cf49f7ce9b237a85a242c245e9846294b3c8347b+1"
const NUMBER = 234

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
13 changes: 13 additions & 0 deletions runtime/test/bugs/hugoio/hugio_math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package hugoio

const (
bias = 1023
shift = 64 - 11 - 1
)

func round(x float64) {
var e uint

if e < bias+shift {
}
}
9 changes: 9 additions & 0 deletions runtime/test/bugs/hugoio/hugio_math_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// see https://github.com/xhd2015/xgo/issues/142#issuecomment-2128262593

package hugoio

import "testing"

func TestRound(t *testing.T) {
round(0)
}
15 changes: 15 additions & 0 deletions runtime/test/bugs/pocketbase/pocketbase_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// see https://github.com/xhd2015/xgo/issues/142
package pocketbase

const DefaultDateLayout = "2006-01-02 15:04:05.000Z"

// from https://github.com/pocketbase/pocketbase/blob/4937acb3e2685273998506b715e2b54e33174172/models/schema/schema_field.go#L591
func chainedConst() {
// the problem only appears when chained const with
// following call with arg
wrap(DefaultDateLayout).Min(nil)
}

func wrap(e string) interface{ Min(t interface{}) } {
return nil
}
23 changes: 5 additions & 18 deletions runtime/test/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,12 @@

package debug

import (
"testing"
const DefaultDateLayout = "2006-01-02 15:04:05.000Z"

"github.com/xhd2015/xgo/runtime/mock"
"github.com/xhd2015/xgo/runtime/test/mock_var/sub"
)

var c sub.Mapping = sub.Mapping{
1: "hello",
func chainedConst() {
wrap(DefaultDateLayout).Min(nil)
}

func TestThirdPartyTypeMethodVar(t *testing.T) {
mock.Patch(&c, func() sub.Mapping {
return sub.Mapping{
1: "mock",
}
})
txt := c.Get(1)
if txt != "mock" {
t.Fatalf("expect c[1] to be %s, actual: %s", "mock", txt)
}
func wrap(e string) interface{ Min(t interface{}) } {
return nil
}
2 changes: 1 addition & 1 deletion script/download-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func downloadGo(cmd string, version string) error {
return err
}

goTmpDir, err := os.MkdirTemp("", "go")
goTmpDir, err := os.MkdirTemp(".", "go")
if err != nil {
return err
}
Expand Down
28 changes: 24 additions & 4 deletions script/run-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ var extraSubTests = []*TestCase{
dir: "runtime/test/trap_stdlib_any",
flags: []string{"--trap-stdlib"},
},
{
// see https://github.com/xhd2015/xgo/issues/142
name: "bugs_regression",
dir: "runtime/test/bugs/...",
flags: []string{},
},
}

func main() {
Expand Down Expand Up @@ -451,17 +457,31 @@ func runRuntimeSubTest(goroot string, args []string, tests []string, names []str
continue
}
dir := tt.dir
hookFile := filepath.Join(dir, "hook_test.go")
_, statErr := os.Stat(hookFile)
hasHook := statErr == nil
var subDirs bool
if strings.HasSuffix(dir, "/...") {
subDirs = true
dir = strings.TrimSuffix(dir, "/...")
}
var hasHook bool
if !subDirs {
hookFile := filepath.Join(dir, "hook_test.go")
_, statErr := os.Stat(hookFile)
hasHook = statErr == nil
}
dotDir := "./" + dir
if hasHook {
err := doRunTest(goroot, testKind_xgoAny, amendArgs([]string{"--project-dir", dotDir, "-run", "TestPreCheck"}, nil), []string{"./hook_test.go"})
if err != nil {
return err
}
}
err := doRunTest(goroot, testKind_xgoAny, amendArgs([]string{"--project-dir", dotDir}, tt.flags), []string{"./"})
var testArgDir string
if !subDirs {
testArgDir = "./"
} else {
testArgDir = "./..."
}
err := doRunTest(goroot, testKind_xgoAny, amendArgs([]string{"--project-dir", dotDir}, tt.flags), []string{testArgDir})
if err != nil {
return err
}
Expand Down
Loading