diff options
author | Ayke van Laethem <[email protected]> | 2024-01-19 16:57:04 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-01-19 21:23:58 +0100 |
commit | 57f49af7267d8a3a299d93e035108590f88bbf66 (patch) | |
tree | df80f0017529672d5adb4262eeb741fc9606ba4a /testdata | |
parent | 38a80b45d3f5eb07670a4d8fe075315920f2c712 (diff) | |
download | tinygo-57f49af7267d8a3a299d93e035108590f88bbf66.tar.gz tinygo-57f49af7267d8a3a299d93e035108590f88bbf66.zip |
loader: make sure Go version is plumbed through
This fixes the new loop variable behavior in Go 1.22.
Specifically:
* The compiler (actually, the x/tools/go/ssa package) now correctly
picks up the Go version.
* If a module doesn't specify the Go version, the current Go version
(from the `go` tool and standard library) is used. This fixes
`go run`.
* The tests in testdata/ that use a separate directory are now
actually run in that directory. This makes it possible to use a
go.mod file there.
* There is a test to make sure old Go modules still work with the old
Go behavior, even on a newer Go version.
Diffstat (limited to 'testdata')
-rw-r--r-- | testdata/go1.22/go.mod | 3 | ||||
-rw-r--r-- | testdata/go1.22/main.go (renamed from testdata/go1.22.go) | 10 | ||||
-rw-r--r-- | testdata/go1.22/out.txt (renamed from testdata/go1.22.txt) | 2 | ||||
-rw-r--r-- | testdata/oldgo/go.mod | 5 | ||||
-rw-r--r-- | testdata/oldgo/main.go | 26 | ||||
-rw-r--r-- | testdata/oldgo/out.txt | 1 |
6 files changed, 40 insertions, 7 deletions
diff --git a/testdata/go1.22/go.mod b/testdata/go1.22/go.mod new file mode 100644 index 000000000..fd97e8b22 --- /dev/null +++ b/testdata/go1.22/go.mod @@ -0,0 +1,3 @@ +module github.com/tinygo-org/tinygo/testdata/go1.22 + +go 1.22 diff --git a/testdata/go1.22.go b/testdata/go1.22/main.go index 2b02a18e1..80f6c887b 100644 --- a/testdata/go1.22.go +++ b/testdata/go1.22/main.go @@ -19,15 +19,13 @@ func testLoopVar() { f = func() int { return i } } } - // Prints 1 in Go 1.21, or 0 in Go 1.22. - // TODO: this still prints Go 1.21 even in Go 1.22. We probably need to - // specify the Go version somewhere. + // Variable n is 1 in Go 1.21, or 0 in Go 1.22. n := f() if n == 0 { - println("behaves like Go 1.22") + println("loops behave like Go 1.22") } else if n == 1 { - println("behaves like Go 1.21") + println("loops behave like Go 1.21") } else { - println("unknown behavior") + println("unknown loop behavior") } } diff --git a/testdata/go1.22.txt b/testdata/go1.22/out.txt index 2695c7846..ac9836aff 100644 --- a/testdata/go1.22.txt +++ b/testdata/go1.22/out.txt @@ -9,4 +9,4 @@ 2 1 go1.22 has lift-off! -behaves like Go 1.21 +loops behave like Go 1.22 diff --git a/testdata/oldgo/go.mod b/testdata/oldgo/go.mod new file mode 100644 index 000000000..f3c7277a9 --- /dev/null +++ b/testdata/oldgo/go.mod @@ -0,0 +1,5 @@ +module github.com/tinygo-org/tinygo/testdata/oldgo + +// Go version doesn't matter much, as long as it's old. + +go 1.15 diff --git a/testdata/oldgo/main.go b/testdata/oldgo/main.go new file mode 100644 index 000000000..4bc030444 --- /dev/null +++ b/testdata/oldgo/main.go @@ -0,0 +1,26 @@ +package main + +// This package verifies that the Go language version is correctly picked up +// from the go.mod file. + +func main() { + testLoopVar() +} + +func testLoopVar() { + var f func() int + for i := 0; i < 1; i++ { + if i == 0 { + f = func() int { return i } + } + } + // Variable n is 1 in Go 1.21, or 0 in Go 1.22. + n := f() + if n == 0 { + println("loops behave like Go 1.22") + } else if n == 1 { + println("loops behave like Go 1.21") + } else { + println("unknown loop behavior") + } +} diff --git a/testdata/oldgo/out.txt b/testdata/oldgo/out.txt new file mode 100644 index 000000000..57ef14f10 --- /dev/null +++ b/testdata/oldgo/out.txt @@ -0,0 +1 @@ +loops behave like Go 1.21 |