Skip to content

Commit

Permalink
go/types, types2: track gotypesalias non-default behavior
Browse files Browse the repository at this point in the history
Fixes #66216.

Change-Id: I2750a744d0dcf636a00d388299e1f2f993e5ac26
Reviewed-on: https://go-review.googlesource.com/c/go/+/572575
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
  • Loading branch information
griesemer authored and gopherbot committed Apr 16, 2024
1 parent 15cec43 commit c51f6c6
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/cmd/compile/internal/types2/check.go
Expand Up @@ -102,6 +102,12 @@ type Checker struct {
// If enableAlias is set, alias declarations produce an Alias type.
// Otherwise the alias information is only in the type name, which
// points directly to the actual (aliased) type.
// Starting with Go 1.23, enableAlias is set by default.
// Non-default behavior is tracked with gotypesalias.IncNonDefault()
// for each declaration of an alias type where enableAlias is not set.
//
// TODO(gri) Testing runs tests in both modes. Do we need to exclude
// tracking of non-default behavior for tests?
enableAlias bool

conf *Config
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types2/check_test.go
Expand Up @@ -122,7 +122,7 @@ func parseFlags(src []byte, flags *flag.FlagSet) error {
//
// If provided, opts may be used to mutate the Config before type-checking.
func testFiles(t *testing.T, filenames []string, srcs [][]byte, colDelta uint, manual bool, opts ...func(*Config)) {
// Alias types are enabled by default
// Alias types are enabled by default.
testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
if !manual {
t.Setenv("GODEBUG", "gotypesalias=0")
Expand Down
16 changes: 16 additions & 0 deletions src/cmd/compile/internal/types2/decl.go
Expand Up @@ -532,6 +532,22 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
alias.fromRHS = rhs
Unalias(alias) // resolve alias.actual
} else {
// With Go1.23, the default behavior is to use Alias nodes,
// reflected by check.enableAlias. Signal non-default behavior
// by calling gotypesalias.IncNonDefault().
//
// Note: As of Go 1.23, Settings.IncNonDefault is not present
// in internal/godebug/godebug.go used during bootstrapping,
// only after the tool chain is built and recompiles itself.
// Check dynamically for the presence of IncNonDefault.
// (This is not an issue for go/types because it is not used
// during bootstrap.)
//
// TODO(gri) replace with direct call when we bootstrap with Go 1.20
if s, ok := any(gotypesalias).(interface{ IncNonDefault() }); ok {
s.IncNonDefault()
}

if !versionErr && tparam0 != nil {
check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1 or unset")
versionErr = true
Expand Down
8 changes: 7 additions & 1 deletion src/go/types/check.go
Expand Up @@ -102,9 +102,15 @@ type Checker struct {
// package information
// (initialized by NewChecker, valid for the life-time of checker)

// If EnableAlias is set, alias declarations produce an Alias type.
// If enableAlias is set, alias declarations produce an Alias type.
// Otherwise the alias information is only in the type name, which
// points directly to the actual (aliased) type.
// Starting with Go 1.23, enableAlias is set by default.
// Non-default behavior is tracked with gotypesalias.IncNonDefault()
// for each declaration of an alias type where enableAlias is not set.
//
// TODO(gri) Testing runs tests in both modes. Do we need to exclude
// tracking of non-default behavior for tests?
enableAlias bool

conf *Config
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/check_test.go
Expand Up @@ -134,7 +134,7 @@ func parseFlags(src []byte, flags *flag.FlagSet) error {
//
// If provided, opts may be used to mutate the Config before type-checking.
func testFiles(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
// Alias types are enabled by default
// Alias types are enabled by default.
testFilesImpl(t, filenames, srcs, manual, opts...)
if !manual {
t.Setenv("GODEBUG", "gotypesalias=0")
Expand Down
4 changes: 4 additions & 0 deletions src/go/types/decl.go
Expand Up @@ -607,6 +607,10 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName
alias.fromRHS = rhs
Unalias(alias) // resolve alias.actual
} else {
// With Go1.23, the default behavior is to use Alias nodes,
// reflected by check.enableAlias. Signal non-default behavior.
gotypesalias.IncNonDefault()

if !versionErr && tparam0 != nil {
check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1 or unset")
versionErr = true
Expand Down
2 changes: 1 addition & 1 deletion src/internal/godebugs/table.go
Expand Up @@ -30,7 +30,7 @@ var All = []Info{
{Name: "gocachehash", Package: "cmd/go"},
{Name: "gocachetest", Package: "cmd/go"},
{Name: "gocacheverify", Package: "cmd/go"},
{Name: "gotypesalias", Package: "go/types", Changed: 23, Old: "0", Opaque: true}, // bug #66216: remove Opaque
{Name: "gotypesalias", Package: "go/types", Changed: 23, Old: "0"},
{Name: "http2client", Package: "net/http"},
{Name: "http2debug", Package: "net/http", Opaque: true},
{Name: "http2server", Package: "net/http"},
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/metrics/doc.go
Expand Up @@ -246,6 +246,10 @@ Below is the full list of supported metrics, ordered lexicographically.
The number of non-default behaviors executed by the cmd/go
package due to a non-default GODEBUG=gocacheverify=... setting.
/godebug/non-default-behavior/gotypesalias:events
The number of non-default behaviors executed by the go/types
package due to a non-default GODEBUG=gotypesalias=... setting.
/godebug/non-default-behavior/http2client:events
The number of non-default behaviors executed by the net/http
package due to a non-default GODEBUG=http2client=... setting.
Expand Down

0 comments on commit c51f6c6

Please sign in to comment.