1
0
mirror of https://github.com/golang/go synced 2024-11-21 23:14:40 -07:00

Revert "go/types, types2: only use fileVersion if 1.21 or greater"

This reverts CL 603895

Reason for revert: We've decided to change the logic for how upgrades are done and want to submit the new logic in a self contained CL that can be cherry-picked onto release-branch.go1.23

Change-Id: I366af8e95ce1de7311b0385a23f9dd3df175745a
Reviewed-on: https://go-review.googlesource.com/c/go/+/605675
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Michael Matloob 2024-08-14 20:04:08 +00:00
parent 1419d0e925
commit 2693f77b35
10 changed files with 22 additions and 87 deletions

View File

@ -3036,21 +3036,17 @@ func TestFileVersions(t *testing.T) {
{"", "go1.20", ""}, // file upgrade ignored
{"go1.19", "go1.20", "go1.20"}, // file upgrade permitted
{"go1.20", "go1.19", "go1.20"}, // file downgrade not permitted
{"go1.21", "go1.20", "go1.21"}, // file downgrade not permitted
{"go1.22", "go1.21", "go1.21"}, // file downgrade permitted (file and module version are >= go1.21)
{"go1.21", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
// versions containing release numbers
// (file versions containing release numbers are considered invalid)
{"go1.19.0", "", "go1.19.0"}, // no file version specified
{"go1.20", "go1.20.1", "go1.20"}, // file upgrade ignored
{"go1.20.1", "go1.20", "go1.20.1"}, // file upgrade ignored
{"go1.21.0", "go1.21.1", "go1.21.0"}, // file upgrade ignored
{"go1.21", "go1.21.1", "go1.21"}, // file upgrade ignored
{"go1.20.1", "go1.21", "go1.21"}, // file upgrade permitted
{"go1.21.1", "go1.21", "go1.21.1"}, // file downgrade ignored
{"go1.20.1", "go1.19", "go1.20.1"}, // file downgrade not permitted
{"go1.21.1", "go1.19.1", "go1.21.1"}, // file downgrade not permitted (invalid file version)
{"go1.22.1", "go1.21", "go1.21"}, // file downgrade permitted (file and module version is >= go1.21)
{"go1.21.1", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
} {
var src string
if test.fileVersion != "" {

View File

@ -353,25 +353,12 @@ func (check *Checker) initFiles(files []*syntax.File) {
// To work around this, downgrades are only allowed when the
// module's Go version is Go 1.21 or later.
//
// Downgrades are also only allowed to Go versions Go 1.21 or later.
// In GOPATH mode, there's no way to set a module version and the
// -lang is set to the local toolchain version to allow the use of
// new features in GOPATH mode. But //go:build lines added before go1.21
// weren't intended to downgrade, so code with //go:build lines for
// go versions earlier than 1.21 may use language features added
// in later versions and compile.
//
// We should probably change the downgradeOk condition to capture this
// instead of adding an extra condition, but to make the change simpler,
// we've tried to limit it to one line.
// TODO(gri): simplify this code after 1.23 has shipped
//
// If there is no valid check.version, then we don't really know what
// Go version to apply.
// Legacy tools may do this, and they historically have accepted everything.
// Preserve that behavior by ignoring //go:build constraints entirely in that
// case (!pkgVersionOk).
if cmp > 0 || cmp < 0 && downgradeOk && fileVersion.cmp(go1_21) >= 0 {
if cmp > 0 || cmp < 0 && downgradeOk {
v = file.GoVersion
}
}

View File

@ -3040,21 +3040,17 @@ func TestFileVersions(t *testing.T) {
{"", "go1.20", ""}, // file upgrade ignored
{"go1.19", "go1.20", "go1.20"}, // file upgrade permitted
{"go1.20", "go1.19", "go1.20"}, // file downgrade not permitted
{"go1.21", "go1.20", "go1.21"}, // file downgrade not permitted
{"go1.22", "go1.21", "go1.21"}, // file downgrade permitted (file and module version are >= go1.21)
{"go1.21", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
// versions containing release numbers
// (file versions containing release numbers are considered invalid)
{"go1.19.0", "", "go1.19.0"}, // no file version specified
{"go1.20", "go1.20.1", "go1.20"}, // file upgrade ignored
{"go1.20.1", "go1.20", "go1.20.1"}, // file upgrade ignored
{"go1.21.0", "go1.21.1", "go1.21.0"}, // file upgrade ignored
{"go1.21", "go1.21.1", "go1.21"}, // file upgrade ignored
{"go1.20.1", "go1.21", "go1.21"}, // file upgrade permitted
{"go1.21.1", "go1.21", "go1.21.1"}, // file downgrade ignored
{"go1.20.1", "go1.19", "go1.20.1"}, // file downgrade not permitted
{"go1.21.1", "go1.19.1", "go1.21.1"}, // file downgrade not permitted (invalid file version)
{"go1.22.1", "go1.21", "go1.21"}, // file downgrade permitted (file and module version is >= go1.21)
{"go1.21.1", "go1.19", "go1.19"}, // file downgrade permitted (module version is >= go1.21)
} {
var src string
if test.fileVersion != "" {

View File

@ -374,25 +374,12 @@ func (check *Checker) initFiles(files []*ast.File) {
// To work around this, downgrades are only allowed when the
// module's Go version is Go 1.21 or later.
//
// Downgrades are also only allowed to Go versions Go 1.21 or later.
// In GOPATH mode, there's no way to set a module version and the
// -lang is set to the local toolchain version to allow the use of
// new features in GOPATH mode. But //go:build lines added before go1.21
// weren't intended to downgrade, so code with //go:build lines for
// go versions earlier than 1.21 may use language features added
// in later versions and compile.
//
// We should probably change the downgradeOk condition to capture this
// instead of adding an extra condition, but to make the change simpler,
// we've tried to limit it to one line.
// TODO(gri): simplify this code after 1.23 has shipped
//
// If there is no valid check.version, then we don't really know what
// Go version to apply.
// Legacy tools may do this, and they historically have accepted everything.
// Preserve that behavior by ignoring //go:build constraints entirely in that
// case (!pkgVersionOk).
if cmp > 0 || cmp < 0 && downgradeOk && fileVersion.cmp(go1_21) >= 0 {
if cmp > 0 || cmp < 0 && downgradeOk {
v = file.GoVersion
}
}

View File

@ -14,4 +14,4 @@ type Slice []byte
type Array [8]byte
var s Slice
var p = (Array)(s /* ok because downgrades below 1.21 are ignored */)
var p = (Array)(s /* ok because Go 1.20 ignored the //go:build go1.19 */)

View File

@ -14,4 +14,4 @@ type Slice []byte
type Array [8]byte
var s Slice
var p = (Array)(s /* ok because downgrades below 1.21 are ignored */)
var p = (Array)(s /* ERROR "requires go1.20 or later" */)

View File

@ -1,16 +0,0 @@
// -lang=go1.21
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Check Go language version-specific errors.
//go:build go1.22
package p
func f() {
for _ = range /* ok because of upgrade to 1.22 */ 10 {
}
}

View File

@ -1,16 +0,0 @@
// -lang=go1.22
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Check Go language version-specific errors.
//go:build go1.21
package p
func f() {
for _ = range 10 /* ERROR "requires go1.22 or later" */ {
}
}

View File

@ -1,9 +1,14 @@
// -lang=go1.13
// -lang=go1.21
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Note: Downgrading to go1.13 requires at least go1.21,
// hence the need for -lang=go1.21 at the top.
//go:build go1.13
package p
import "io"

View File

@ -1,20 +1,16 @@
// errorcheck -lang=go1.22
// errorcheck -lang=go1.21
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file has been changed from its original version as
// //go:build language downgrades below go1.21 are no longer
// supported. The original tested a downgrade from go1.21 to
// go1.4 while this new version tests a downgrade from go1.22
// to go1.21
//go:build go1.21
//go:build go1.4
package p
func f() {
for _ = range 10 { // ERROR "file declares //go:build go1.21"
}
}
const c = 0o123 // ERROR "file declares //go:build go1.4"
// ERROR "file declares //go:build go1.4"
//line issue63489a.go:13:1
const d = 0o124