Skip to content

Commit 2f498f2

Browse files
rscgopherbot
authored andcommitted
[release-branch.go1.19] cmd/go: refuse to build Go 1.22 code
With #60078 accepted, we expect Go 1.22 will have different for loop semantics than Go 1.19 did. Go 1.19 is already unsupported, but add a check anyway, just to help catch some mistakes and usage of old Go toolchains beyond their end-of-support. Note that Go 1.19 can keep being used indefinitely with pre-Go 1.22 code. This change only makes it refuse to build code that says it needs Go 1.22 semantics, because Go 1.19 does not provide those. Cherry-pick of the change from the Go 1.20 branch. For #60078. Change-Id: I75118d6fbd0cc08a6bc309aca54c389a255ba7dc Reviewed-on: https://go-review.googlesource.com/c/go/+/518675 Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/518815 Auto-Submit: Russ Cox <[email protected]> TryBot-Bypass: Russ Cox <[email protected]>
1 parent 0ae54dd commit 2f498f2

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,18 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
546546
return errors.New("binary-only packages are no longer supported")
547547
}
548548

549+
// Go 1.22 is likely to change for loop semantics.
550+
// If we try to build code written for Go 1.22,
551+
// it may have aliasing bugs that it shouldn't have.
552+
// See go.dev/issue/60078.
553+
// Go 1.19 is no longer supported,
554+
// but we are adding this check anyway,
555+
// just to help catch some mistakes and usage of old
556+
// Go toolchains beyond their end-of-support.
557+
if p.Module != nil && !(allowedVersion(p.Module.GoVersion) || p.Module.GoVersion == "1.20" || p.Module.GoVersion == "1.21") {
558+
return errors.New("cannot compile Go " + p.Module.GoVersion + " code")
559+
}
560+
549561
if err := b.Mkdir(a.Objdir); err != nil {
550562
return err
551563
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
! go build
2+
stderr '^m: cannot compile Go 1.22 code$'
3+
4+
cd dep
5+
! go build
6+
stderr '^m: cannot compile Go 1.22 code$'
7+
8+
cd ../dep20
9+
go build
10+
11+
cd ../dep21
12+
go build
13+
14+
-- go.mod --
15+
module m
16+
go 1.22
17+
18+
-- p.go --
19+
package p
20+
21+
-- dep/go.mod --
22+
module dep
23+
go 1.19
24+
require m v1.0.0
25+
replace m v1.0.0 => ../
26+
27+
-- dep/p.go --
28+
package p
29+
30+
import "m"
31+
32+
-- dep20/go.mod --
33+
module dep
34+
go 1.20
35+
36+
-- dep20/p.go --
37+
package p
38+
39+
-- dep21/go.mod --
40+
module dep
41+
go 1.21
42+
43+
-- dep21/p.go --
44+
package p

src/cmd/go/testdata/script/mod_go_version.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ go build sub.1
88
go build subver.1
99
! stderr 'module requires'
1010
! go build badsub.1
11-
stderr '^note: module requires Go 1.11111$'
11+
stderr '^note: module requires Go 1.21$'
1212

1313
go build versioned.1
1414
go mod edit -require [email protected]
1515
! go build versioned.1
16-
stderr '^note: module requires Go 1.99999$'
16+
stderr '^note: module requires Go 1.21$'
1717

1818
[short] stop
1919

2020
# The message should be printed even if the compiler emits no output.
2121
go build -o $WORK/nooutput.exe nooutput.go
2222
! go build -toolexec=$WORK/nooutput.exe versioned.1
23-
stderr '^# versioned.1\nnote: module requires Go 1.99999$'
23+
stderr '^# versioned.1\nnote: module requires Go 1.21$'
2424

2525
-- go.mod --
2626
module m
27-
go 1.999
27+
go 1.21
2828
require (
2929
sub.1 v1.0.0
3030
subver.1 v1.0.0
@@ -51,14 +51,14 @@ package x
5151

5252
-- subver/go.mod --
5353
module m
54-
go 1.11111
54+
go 1.21
5555

5656
-- subver/x.go --
5757
package x
5858

5959
-- badsub/go.mod --
6060
module m
61-
go 1.11111
61+
go 1.21
6262

6363
-- badsub/x.go --
6464
package x
@@ -73,7 +73,7 @@ package x
7373

7474
-- versioned2/go.mod --
7575
module versioned
76-
go 1.99999
76+
go 1.21
7777

7878
-- versioned2/x.go --
7979
package x

0 commit comments

Comments
 (0)