]> Cypherpunks repositories - gostls13.git/commitdiff
types2: flip the default value of GODEBUG=gotypesalias=1
authorRobert Griesemer <gri@golang.org>
Mon, 15 Apr 2024 21:10:40 +0000 (14:10 -0700)
committerRobert Griesemer <gri@google.com>
Tue, 16 Apr 2024 21:06:56 +0000 (21:06 +0000)
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 <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/noder/irgen.go
src/cmd/compile/internal/types2/check.go
src/cmd/compile/internal/types2/check_test.go
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/named_test.go
src/go/types/api_test.go
src/go/types/check.go
src/go/types/check_test.go
src/go/types/issues_test.go

index e0b7bb946d28fd88471401602c6b37cb60148be9..c159e4823ea84831cf819d392921741f4e740609 100644 (file)
@@ -8,6 +8,7 @@ import (
        "fmt"
        "internal/buildcfg"
        "internal/types/errors"
+       "os"
        "regexp"
        "sort"
 
@@ -85,6 +86,10 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
                base.ErrorfAt(m.makeXPos(terr.Pos), terr.Code, "%s", msg)
        }
 
+       // Currently, the compiler panics when using Alias types.
+       // Use the non-default setting for now.
+       // TODO(gri) set this to gotypesalias=1 or remove this call.
+       os.Setenv("GODEBUG", "gotypesalias=0")
        pkg, err := conf.Check(base.Ctxt.Pkgpath, files, info)
        base.ExitIfErrors()
        if err != nil {
index b59e471e1566a5bf8262a56bc389dfbf1b030d33..cc723f20127e2c1af7270aa12c2eeb8c2f0df089 100644 (file)
@@ -21,7 +21,10 @@ var nopos syntax.Pos
 const debug = false // leave on during development
 
 // gotypesalias controls the use of Alias types.
-var gotypesalias = godebug.New("#gotypesalias")
+// As of Apr 16 2024 they are used by default.
+// To disable their use, set GODEBUG to gotypesalias=0.
+// This GODEBUG flag will be removed in the near future (tentatively Go 1.24).
+var gotypesalias = godebug.New("gotypesalias")
 
 // exprInfo stores information about an untyped expression.
 type exprInfo struct {
@@ -255,7 +258,7 @@ func NewChecker(conf *Config, pkg *Package, info *Info) *Checker {
        // (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
 
        return &Checker{
-               enableAlias: gotypesalias.Value() == "1",
+               enableAlias: gotypesalias.Value() != "0",
                conf:        conf,
                ctxt:        conf.Context,
                pkg:         pkg,
index 8b309898d2df39b7187b57de29be829ab614912d..066218772ea97f4d236a06e5c36b4b8abd704a9d 100644 (file)
@@ -122,10 +122,10 @@ 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 disabled by default
+       // Alias types are enabled by default
        testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
        if !manual {
-               t.Setenv("GODEBUG", "gotypesalias=1")
+               t.Setenv("GODEBUG", "gotypesalias=0")
                testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
        }
 }
index 8bf9c5830742e4736777837dbeb5e3c24f5a4eb8..26ce49d87a7abd1c27577edf12a9d8bdb2480b89 100644 (file)
@@ -533,7 +533,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
                        Unalias(alias) // resolve alias.actual
                } else {
                        if !versionErr && tparam0 != nil {
-                               check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1")
+                               check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1 or unset")
                                versionErr = true
                        }
 
index 25aea267927b5f1f674297bae0842f78d7599c47..760930609948078569a97fb86179c2e7b38e888a 100644 (file)
@@ -102,7 +102,7 @@ type Inst = *Tree[int]
                return n.Underlying().(*Struct).Field(0).Type().(*Pointer).Elem().(*Named)
        }
 
-       Inst := pkg.Scope().Lookup("Inst").Type().(*Pointer).Elem().(*Named)
+       Inst := Unalias(pkg.Scope().Lookup("Inst").Type()).(*Pointer).Elem().(*Named)
        Node := firstFieldType(Inst)
        Tree := firstFieldType(Node)
        if !Identical(Inst, Tree) {
index 7ab695d365faff51b5735cb370de2385955e36d1..5bc4e8a61f04dba434451d178cca0ddb3a3df4ce 100644 (file)
@@ -2997,6 +2997,7 @@ func TestTooNew(t *testing.T) {
 
 // This is a regression test for #66704.
 func TestUnaliasTooSoonInCycle(t *testing.T) {
+       t.Setenv("GODEBUG", "gotypesalias=1")
        const src = `package a
 
 var x T[B] // this appears to cause Unalias to be called on B while still Invalid
index 74849171f24baa0ba2de3e3d49810168956224aa..be990eabfe58d4acc9f688376abaff290669672d 100644 (file)
@@ -24,8 +24,9 @@ var noposn = atPos(nopos)
 const debug = false // leave on during development
 
 // gotypesalias controls the use of Alias types.
-// As of Apr 12 2024 it is on by default.
-// It will be removed soon.
+// As of Apr 16 2024 they are used by default.
+// To disable their use, set GODEBUG to gotypesalias=0.
+// This GODEBUG flag will be removed in the near future (tentatively Go 1.24).
 var gotypesalias = godebug.New("gotypesalias")
 
 // exprInfo stores information about an untyped expression.
@@ -260,14 +261,8 @@ func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Ch
        //
        // (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
 
-       enableAlias := false
-       switch gotypesalias.Value() {
-       case "", "1":
-               enableAlias = true
-       }
-
        return &Checker{
-               enableAlias: enableAlias,
+               enableAlias: gotypesalias.Value() != "0",
                conf:        conf,
                ctxt:        conf.Context,
                fset:        fset,
index 63891e905600bce087f90d2159e8f9ca016023ac..6ad7ef3a27b6810fafb3c23e538a2807c263a226 100644 (file)
@@ -124,6 +124,8 @@ func parseFlags(src []byte, flags *flag.FlagSet) error {
 
 // testFiles type-checks the package consisting of the given files, and
 // compares the resulting errors with the ERROR annotations in the source.
+// Except for manual tests, each package is type-checked twice, once without
+// use of Alias types, and once with Alias types.
 //
 // The srcs slice contains the file content for the files named in the
 // filenames slice. The colDelta parameter specifies the tolerance for position
@@ -132,6 +134,15 @@ 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
+       testFilesImpl(t, filenames, srcs, manual, opts...)
+       if !manual {
+               t.Setenv("GODEBUG", "gotypesalias=0")
+               testFilesImpl(t, filenames, srcs, manual, opts...)
+       }
+}
+
+func testFilesImpl(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
        if len(filenames) == 0 {
                t.Fatal("no source files")
        }
index eb627eaee7a5ffde7b0319bd1e0e1d4ea7e553a9..4f4bf6f07704135fa19f4f6c4ba11911cdd65f0a 100644 (file)
@@ -998,7 +998,7 @@ type A = []int
 type S struct{ A }
 `
 
-       // t.Setenv("GODEBUG", "gotypesalias=1") // now on by default
+       t.Setenv("GODEBUG", "gotypesalias=1")
        pkg := mustTypecheck(src, nil, nil)
 
        S := pkg.Scope().Lookup("S")