Skip to content

Commit c51f6c6

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: track gotypesalias non-default behavior
Fixes #66216. Change-Id: I2750a744d0dcf636a00d388299e1f2f993e5ac26 Reviewed-on: https://go-review.googlesource.com/c/go/+/572575 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent 15cec43 commit c51f6c6

File tree

8 files changed

+40
-4
lines changed

8 files changed

+40
-4
lines changed

src/cmd/compile/internal/types2/check.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ type Checker struct {
102102
// If enableAlias is set, alias declarations produce an Alias type.
103103
// Otherwise the alias information is only in the type name, which
104104
// points directly to the actual (aliased) type.
105+
// Starting with Go 1.23, enableAlias is set by default.
106+
// Non-default behavior is tracked with gotypesalias.IncNonDefault()
107+
// for each declaration of an alias type where enableAlias is not set.
108+
//
109+
// TODO(gri) Testing runs tests in both modes. Do we need to exclude
110+
// tracking of non-default behavior for tests?
105111
enableAlias bool
106112

107113
conf *Config

src/cmd/compile/internal/types2/check_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func parseFlags(src []byte, flags *flag.FlagSet) error {
122122
//
123123
// If provided, opts may be used to mutate the Config before type-checking.
124124
func testFiles(t *testing.T, filenames []string, srcs [][]byte, colDelta uint, manual bool, opts ...func(*Config)) {
125-
// Alias types are enabled by default
125+
// Alias types are enabled by default.
126126
testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
127127
if !manual {
128128
t.Setenv("GODEBUG", "gotypesalias=0")

src/cmd/compile/internal/types2/decl.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,22 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
532532
alias.fromRHS = rhs
533533
Unalias(alias) // resolve alias.actual
534534
} else {
535+
// With Go1.23, the default behavior is to use Alias nodes,
536+
// reflected by check.enableAlias. Signal non-default behavior
537+
// by calling gotypesalias.IncNonDefault().
538+
//
539+
// Note: As of Go 1.23, Settings.IncNonDefault is not present
540+
// in internal/godebug/godebug.go used during bootstrapping,
541+
// only after the tool chain is built and recompiles itself.
542+
// Check dynamically for the presence of IncNonDefault.
543+
// (This is not an issue for go/types because it is not used
544+
// during bootstrap.)
545+
//
546+
// TODO(gri) replace with direct call when we bootstrap with Go 1.20
547+
if s, ok := any(gotypesalias).(interface{ IncNonDefault() }); ok {
548+
s.IncNonDefault()
549+
}
550+
535551
if !versionErr && tparam0 != nil {
536552
check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1 or unset")
537553
versionErr = true

src/go/types/check.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,15 @@ type Checker struct {
102102
// package information
103103
// (initialized by NewChecker, valid for the life-time of checker)
104104

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

110116
conf *Config

src/go/types/check_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func parseFlags(src []byte, flags *flag.FlagSet) error {
134134
//
135135
// If provided, opts may be used to mutate the Config before type-checking.
136136
func testFiles(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
137-
// Alias types are enabled by default
137+
// Alias types are enabled by default.
138138
testFilesImpl(t, filenames, srcs, manual, opts...)
139139
if !manual {
140140
t.Setenv("GODEBUG", "gotypesalias=0")

src/go/types/decl.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName
607607
alias.fromRHS = rhs
608608
Unalias(alias) // resolve alias.actual
609609
} else {
610+
// With Go1.23, the default behavior is to use Alias nodes,
611+
// reflected by check.enableAlias. Signal non-default behavior.
612+
gotypesalias.IncNonDefault()
613+
610614
if !versionErr && tparam0 != nil {
611615
check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1 or unset")
612616
versionErr = true

src/internal/godebugs/table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var All = []Info{
3030
{Name: "gocachehash", Package: "cmd/go"},
3131
{Name: "gocachetest", Package: "cmd/go"},
3232
{Name: "gocacheverify", Package: "cmd/go"},
33-
{Name: "gotypesalias", Package: "go/types", Changed: 23, Old: "0", Opaque: true}, // bug #66216: remove Opaque
33+
{Name: "gotypesalias", Package: "go/types", Changed: 23, Old: "0"},
3434
{Name: "http2client", Package: "net/http"},
3535
{Name: "http2debug", Package: "net/http", Opaque: true},
3636
{Name: "http2server", Package: "net/http"},

src/runtime/metrics/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ Below is the full list of supported metrics, ordered lexicographically.
246246
The number of non-default behaviors executed by the cmd/go
247247
package due to a non-default GODEBUG=gocacheverify=... setting.
248248
249+
/godebug/non-default-behavior/gotypesalias:events
250+
The number of non-default behaviors executed by the go/types
251+
package due to a non-default GODEBUG=gotypesalias=... setting.
252+
249253
/godebug/non-default-behavior/http2client:events
250254
The number of non-default behaviors executed by the net/http
251255
package due to a non-default GODEBUG=http2client=... setting.

0 commit comments

Comments
 (0)