Skip to content

Commit 201f8b4

Browse files
committed
[release-branch.go1.20] 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.20 did. Once Go 1.22 is released, Go 1.20 will be 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.20 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.20 does not provide those. 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]>
1 parent adb775e commit 201f8b4

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,19 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
531531
return errors.New("binary-only packages are no longer supported")
532532
}
533533

534+
// Go 1.22 is likely to change for loop semantics.
535+
// If we try to build code written for Go 1.22,
536+
// it may have aliasing bugs that it shouldn't have.
537+
// See go.dev/issue/60078.
538+
// When Go 1.22 is released, Go 1.20 will no longer
539+
// be supported, so no one should ever run into this condition,
540+
// but we are adding this check anyway,
541+
// just to help catch some mistakes and usage of old
542+
// Go toolchains beyond their end-of-support.
543+
if p.Module != nil && !(allowedVersion(p.Module.GoVersion) || p.Module.GoVersion == "1.21") {
544+
return errors.New("cannot compile Go " + p.Module.GoVersion + " code")
545+
}
546+
534547
if err := b.Mkdir(a.Objdir); err != nil {
535548
return err
536549
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
-- go.mod --
9+
module m
10+
go 1.22
11+
12+
-- p.go --
13+
package p
14+
15+
-- dep/go.mod --
16+
module dep
17+
go 1.19
18+
require m v1.0.0
19+
replace m v1.0.0 => ../
20+
21+
-- dep/p.go --
22+
package p
23+
24+
import "m"

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)