Commit 40f585b2 authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/go: pass go language version to cmd/compile

Pass the Go language version specified in the go.mod file to
cmd/compile.

Also, change the behavior when the go.mod file requests a Go version
that is later than the current one. Previously cmd/go would give a
fatal error in this situation. With this change it attempts the
compilation, and if (and only if) the compilation fails it adds a note
saying that the requested Go version is newer than the known version.
This is as described in https://golang.org/issue/28221.

Updates #28221.

Change-Id: I46803813e7872d4a418a3fd5299880be3b73a971
Reviewed-on: https://go-review.googlesource.com/c/147278
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent 60cf9ac7
......@@ -434,10 +434,6 @@ func (b *Builder) build(a *Action) (err error) {
return fmt.Errorf("missing or invalid binary-only package; expected file %q", a.Package.Target)
}
if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
return fmt.Errorf("module requires Go %s", p.Module.GoVersion)
}
if err := b.Mkdir(a.Objdir); err != nil {
return err
}
......@@ -638,12 +634,19 @@ func (b *Builder) build(a *Action) (err error) {
objpkg := objdir + "_pkg_.a"
ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), len(sfiles) > 0, gofiles)
if len(out) > 0 {
b.showOutput(a, a.Package.Dir, a.Package.Desc(), b.processOutput(out))
output := b.processOutput(out)
if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
output += "note: module requires Go " + p.Module.GoVersion
}
b.showOutput(a, a.Package.Dir, a.Package.Desc(), output)
if err != nil {
return errPrintedOutput
}
}
if err != nil {
if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
b.showOutput(a, a.Package.Dir, a.Package.Desc(), "note: module requires Go "+p.Module.GoVersion)
}
return err
}
if ofile != objpkg {
......
......@@ -53,6 +53,9 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg []byte, a
pkgpath = "main"
}
gcargs := []string{"-p", pkgpath}
if p.Module != nil && p.Module.GoVersion != "" && allowedVersion(p.Module.GoVersion) {
gcargs = append(gcargs, "-lang=go"+p.Module.GoVersion)
}
if p.Standard {
gcargs = append(gcargs, "-std")
}
......
......@@ -3,9 +3,10 @@
env GO111MODULE=on
go list
! go build
stderr 'module requires Go 1.999'
go build
go build sub.1
go build subver.1
! stderr 'module requires'
! go build badsub.1
stderr 'module requires Go 1.11111'
......@@ -19,11 +20,13 @@ module m
go 1.999
require (
sub.1 v1.0.0
subver.1 v1.0.0
badsub.1 v1.0.0
versioned.1 v1.0.0
)
replace (
sub.1 => ./sub
subver.1 => ./subver
badsub.1 => ./badsub
versioned.1 v1.0.0 => ./versioned1
versioned.1 v1.1.0 => ./versioned2
......@@ -39,12 +42,20 @@ go 1.11
-- sub/x.go --
package x
-- subver/go.mod --
module m
go 1.11111
-- subver/x.go --
package x
-- badsub/go.mod --
module m
go 1.11111
-- badsub/x.go --
package x
invalid syntax
-- versioned1/go.mod --
module versioned
......@@ -59,3 +70,4 @@ go 1.99999
-- versioned2/x.go --
package x
invalid syntax
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment