Skip to content

Commit 15cec43

Browse files
griesemerRobert Griesemer
authored and
Robert Griesemer
committed
types2: flip the default value of GODEBUG=gotypesalias=1
This CL changes the interpretation of the unset value of gotypesalias to not equal "0". This is a port of CL 577715 from go/types to types2, with adjustments to go/types to keep the source code in sync. Specifically: - Re-introduce testing of both modes (gotypesalias=0, gotypesalias=1) in go/types. - Re-introduce setting of gotypesalias in some of the tests for explicit documentation in go/types. The compiler still uses the (now) non-default setting due to a panic with the default setting that needs to be debugged. Also, the type checkers still don't call IncNonDefault when the non-default setting of gotypesalias is used. Change-Id: I1feed3eb334c202950ac5aadf49a74adcce0d8c3 Reviewed-on: https://go-review.googlesource.com/c/go/+/579076 TryBot-Bypass: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent 661f981 commit 15cec43

File tree

9 files changed

+31
-16
lines changed

9 files changed

+31
-16
lines changed

src/cmd/compile/internal/noder/irgen.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"internal/buildcfg"
1010
"internal/types/errors"
11+
"os"
1112
"regexp"
1213
"sort"
1314

@@ -85,6 +86,10 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
8586
base.ErrorfAt(m.makeXPos(terr.Pos), terr.Code, "%s", msg)
8687
}
8788

89+
// Currently, the compiler panics when using Alias types.
90+
// Use the non-default setting for now.
91+
// TODO(gri) set this to gotypesalias=1 or remove this call.
92+
os.Setenv("GODEBUG", "gotypesalias=0")
8893
pkg, err := conf.Check(base.Ctxt.Pkgpath, files, info)
8994
base.ExitIfErrors()
9095
if err != nil {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ var nopos syntax.Pos
2121
const debug = false // leave on during development
2222

2323
// gotypesalias controls the use of Alias types.
24-
var gotypesalias = godebug.New("#gotypesalias")
24+
// As of Apr 16 2024 they are used by default.
25+
// To disable their use, set GODEBUG to gotypesalias=0.
26+
// This GODEBUG flag will be removed in the near future (tentatively Go 1.24).
27+
var gotypesalias = godebug.New("gotypesalias")
2528

2629
// exprInfo stores information about an untyped expression.
2730
type exprInfo struct {
@@ -255,7 +258,7 @@ func NewChecker(conf *Config, pkg *Package, info *Info) *Checker {
255258
// (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
256259

257260
return &Checker{
258-
enableAlias: gotypesalias.Value() == "1",
261+
enableAlias: gotypesalias.Value() != "0",
259262
conf: conf,
260263
ctxt: conf.Context,
261264
pkg: pkg,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ 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 disabled by default
125+
// Alias types are enabled by default
126126
testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
127127
if !manual {
128-
t.Setenv("GODEBUG", "gotypesalias=1")
128+
t.Setenv("GODEBUG", "gotypesalias=0")
129129
testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
130130
}
131131
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
533533
Unalias(alias) // resolve alias.actual
534534
} else {
535535
if !versionErr && tparam0 != nil {
536-
check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1")
536+
check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1 or unset")
537537
versionErr = true
538538
}
539539

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type Inst = *Tree[int]
102102
return n.Underlying().(*Struct).Field(0).Type().(*Pointer).Elem().(*Named)
103103
}
104104

105-
Inst := pkg.Scope().Lookup("Inst").Type().(*Pointer).Elem().(*Named)
105+
Inst := Unalias(pkg.Scope().Lookup("Inst").Type()).(*Pointer).Elem().(*Named)
106106
Node := firstFieldType(Inst)
107107
Tree := firstFieldType(Node)
108108
if !Identical(Inst, Tree) {

src/go/types/api_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2997,6 +2997,7 @@ func TestTooNew(t *testing.T) {
29972997

29982998
// This is a regression test for #66704.
29992999
func TestUnaliasTooSoonInCycle(t *testing.T) {
3000+
t.Setenv("GODEBUG", "gotypesalias=1")
30003001
const src = `package a
30013002
30023003
var x T[B] // this appears to cause Unalias to be called on B while still Invalid

src/go/types/check.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ var noposn = atPos(nopos)
2424
const debug = false // leave on during development
2525

2626
// gotypesalias controls the use of Alias types.
27-
// As of Apr 12 2024 it is on by default.
28-
// It will be removed soon.
27+
// As of Apr 16 2024 they are used by default.
28+
// To disable their use, set GODEBUG to gotypesalias=0.
29+
// This GODEBUG flag will be removed in the near future (tentatively Go 1.24).
2930
var gotypesalias = godebug.New("gotypesalias")
3031

3132
// exprInfo stores information about an untyped expression.
@@ -260,14 +261,8 @@ func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Ch
260261
//
261262
// (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
262263

263-
enableAlias := false
264-
switch gotypesalias.Value() {
265-
case "", "1":
266-
enableAlias = true
267-
}
268-
269264
return &Checker{
270-
enableAlias: enableAlias,
265+
enableAlias: gotypesalias.Value() != "0",
271266
conf: conf,
272267
ctxt: conf.Context,
273268
fset: fset,

src/go/types/check_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ func parseFlags(src []byte, flags *flag.FlagSet) error {
124124

125125
// testFiles type-checks the package consisting of the given files, and
126126
// compares the resulting errors with the ERROR annotations in the source.
127+
// Except for manual tests, each package is type-checked twice, once without
128+
// use of Alias types, and once with Alias types.
127129
//
128130
// The srcs slice contains the file content for the files named in the
129131
// filenames slice. The colDelta parameter specifies the tolerance for position
@@ -132,6 +134,15 @@ func parseFlags(src []byte, flags *flag.FlagSet) error {
132134
//
133135
// If provided, opts may be used to mutate the Config before type-checking.
134136
func testFiles(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
137+
// Alias types are enabled by default
138+
testFilesImpl(t, filenames, srcs, manual, opts...)
139+
if !manual {
140+
t.Setenv("GODEBUG", "gotypesalias=0")
141+
testFilesImpl(t, filenames, srcs, manual, opts...)
142+
}
143+
}
144+
145+
func testFilesImpl(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
135146
if len(filenames) == 0 {
136147
t.Fatal("no source files")
137148
}

src/go/types/issues_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ type A = []int
998998
type S struct{ A }
999999
`
10001000

1001-
// t.Setenv("GODEBUG", "gotypesalias=1") // now on by default
1001+
t.Setenv("GODEBUG", "gotypesalias=1")
10021002
pkg := mustTypecheck(src, nil, nil)
10031003

10041004
S := pkg.Scope().Lookup("S")

0 commit comments

Comments
 (0)