mirror of https://github.com/tinygo-org/tinygo.git
Browse Source
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.pull/4100/head
Ayke van Laethem
10 months ago
committed by
Ron Evans
10 changed files with 78 additions and 10 deletions
@ -0,0 +1,17 @@ |
|||
//go:build go1.22
|
|||
|
|||
// types.Info.FileVersions was added in Go 1.22, so we can only initialize it
|
|||
// when built with Go 1.22.
|
|||
|
|||
package loader |
|||
|
|||
import ( |
|||
"go/ast" |
|||
"go/types" |
|||
) |
|||
|
|||
func init() { |
|||
initFileVersions = func(info *types.Info) { |
|||
info.FileVersions = make(map[*ast.File]string) |
|||
} |
|||
} |
@ -0,0 +1,3 @@ |
|||
module github.com/tinygo-org/tinygo/testdata/go1.22 |
|||
|
|||
go 1.22 |
@ -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 |
@ -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") |
|||
} |
|||
} |
@ -0,0 +1 @@ |
|||
loops behave like Go 1.21 |
Loading…
Reference in new issue