"fmt"
"internal/buildcfg"
"internal/types/errors"
- "os"
"regexp"
"sort"
IgnoreBranchErrors: true, // parser already checked via syntax.CheckBranches mode
Importer: &importer,
Sizes: types2.SizesFor("gc", buildcfg.GOARCH),
+ // Currently, the compiler panics when using Alias types.
+ // TODO(gri) set to true once this is fixed (issue #66873)
+ EnableAlias: false,
}
if base.Flag.ErrorURL {
conf.ErrorURL = " [go.dev/e/%s]"
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 {
// of an error message. ErrorURL must be a format string containing
// exactly one "%s" format, e.g. "[go.dev/e/%s]".
ErrorURL string
+
+ // 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.
+ // This flag will eventually be removed (with Go 1.24 at the earliest).
+ EnableAlias bool
}
func srcimporter_setUsesCgo(conf *Config) {
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.
- // Otherwise the alias information is only in the type name, which
- // points directly to the actual (aliased) type.
- enableAlias bool
-
conf *Config
ctxt *Context // context for de-duplicating instances
pkg *Package
// brokenAlias records that alias doesn't have a determined type yet.
// It also sets alias.typ to Typ[Invalid].
-// Not used if check.enableAlias is set.
+// Not used if check.conf.EnableAlias is set.
func (check *Checker) brokenAlias(alias *TypeName) {
- assert(!check.enableAlias)
+ assert(!check.conf.EnableAlias)
if check.brokenAliases == nil {
check.brokenAliases = make(map[*TypeName]bool)
}
// validAlias records that alias has the valid type typ (possibly Typ[Invalid]).
func (check *Checker) validAlias(alias *TypeName, typ Type) {
- assert(!check.enableAlias)
+ assert(!check.conf.EnableAlias)
delete(check.brokenAliases, alias)
alias.typ = typ
}
// isBrokenAlias reports whether alias doesn't have a determined type yet.
func (check *Checker) isBrokenAlias(alias *TypeName) bool {
- assert(!check.enableAlias)
+ assert(!check.conf.EnableAlias)
return check.brokenAliases[alias]
}
// (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
return &Checker{
- enableAlias: gotypesalias.Value() != "0",
- conf: conf,
- ctxt: conf.Context,
- pkg: pkg,
- Info: info,
- version: asGoVersion(conf.GoVersion),
- objMap: make(map[Object]*declInfo),
- impMap: make(map[importKey]*Package),
+ conf: conf,
+ ctxt: conf.Context,
+ pkg: pkg,
+ Info: info,
+ version: asGoVersion(conf.GoVersion),
+ objMap: make(map[Object]*declInfo),
+ impMap: make(map[importKey]*Package),
}
}
//
// 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
+ enableAlias := true
+ opts = append(opts, func(conf *Config) { conf.EnableAlias = enableAlias })
testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
if !manual {
- t.Setenv("GODEBUG", "gotypesalias=0")
+ enableAlias = false
testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
}
}
// By default, gotypesalias is not set.
if gotypesalias != "" {
- t.Setenv("GODEBUG", "gotypesalias="+gotypesalias)
+ conf.EnableAlias = gotypesalias != "0"
}
// Provide Config.Info with all maps so that info recording is tested.
// the syntactic information. We should consider storing
// this information explicitly in the object.
var alias bool
- if check.enableAlias {
+ if check.conf.EnableAlias {
alias = obj.IsAlias()
} else {
if d := check.objMap[obj]; d != nil {
if tname != nil && tname.IsAlias() {
// If we use Alias nodes, it is initialized with Typ[Invalid].
// TODO(gri) Adjust this code if we initialize with nil.
- if !check.enableAlias {
+ if !check.conf.EnableAlias {
check.validAlias(tname, Typ[Invalid])
}
}
versionErr = true
}
- if check.enableAlias {
+ if check.conf.EnableAlias {
// TODO(gri) Should be able to use nil instead of Typ[Invalid] to mark
// the alias as incomplete. Currently this causes problems
// with certain cycles. Investigate.
type S struct{ A }
`
- t.Setenv("GODEBUG", "gotypesalias=1")
- pkg := mustTypecheck(src, nil, nil)
+ conf := Config{EnableAlias: true}
+ pkg := mustTypecheck(src, &conf, nil)
S := pkg.Scope().Lookup("S")
if S == nil {
for i, test := range testObjects {
t.Run(fmt.Sprint(i), func(t *testing.T) {
- if test.alias {
- t.Setenv("GODEBUG", "gotypesalias=1")
- }
-
src := "package p; " + test.src
- pkg, err := typecheck(src, nil, nil)
+ conf := Config{Error: func(error) {}, Importer: defaultImporter(), EnableAlias: test.alias}
+ pkg, err := typecheck(src, &conf, nil)
if err != nil {
t.Fatalf("%s: %s", src, err)
}
}
}
- if false && check.enableAlias {
+ if false && check.conf.EnableAlias {
// With Alias nodes we can process declarations in any order.
//
// TODO(adonovan): unfortunately, Alias nodes
x.mode = constant_
case *TypeName:
- if !check.enableAlias && check.isBrokenAlias(obj) {
+ if !check.conf.EnableAlias && check.isBrokenAlias(obj) {
check.errorf(e, InvalidDeclCycle, "invalid use of type alias %s in recursive type (see go.dev/issue/50729)", quote(obj.name))
return
}
// of an error message. ErrorURL must be a format string containing
// exactly one "%s" format, e.g. "[go.dev/e/%s]".
_ErrorURL string
+
+ // 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.
+ // This flag will eventually be removed (with Go 1.24 at the earliest).
+ _EnableAlias bool
}
func srcimporter_setUsesCgo(conf *Config) {
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.
- // Otherwise the alias information is only in the type name, which
- // points directly to the actual (aliased) type.
- enableAlias bool
-
conf *Config
ctxt *Context // context for de-duplicating instances
fset *token.FileSet
// brokenAlias records that alias doesn't have a determined type yet.
// It also sets alias.typ to Typ[Invalid].
-// Not used if check.enableAlias is set.
+// Not used if check.conf._EnableAlias is set.
func (check *Checker) brokenAlias(alias *TypeName) {
- assert(!check.enableAlias)
+ assert(!check.conf._EnableAlias)
if check.brokenAliases == nil {
check.brokenAliases = make(map[*TypeName]bool)
}
// validAlias records that alias has the valid type typ (possibly Typ[Invalid]).
func (check *Checker) validAlias(alias *TypeName, typ Type) {
- assert(!check.enableAlias)
+ assert(!check.conf._EnableAlias)
delete(check.brokenAliases, alias)
alias.typ = typ
}
// isBrokenAlias reports whether alias doesn't have a determined type yet.
func (check *Checker) isBrokenAlias(alias *TypeName) bool {
- assert(!check.enableAlias)
+ assert(!check.conf._EnableAlias)
return check.brokenAliases[alias]
}
//
// (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
+ // In go/types, conf._EnableAlias is controlled by gotypesalias.
+ conf._EnableAlias = gotypesalias.Value() != "0"
+
return &Checker{
- enableAlias: gotypesalias.Value() != "0",
- conf: conf,
- ctxt: conf.Context,
- fset: fset,
- pkg: pkg,
- Info: info,
- version: asGoVersion(conf.GoVersion),
- objMap: make(map[Object]*declInfo),
- impMap: make(map[importKey]*Package),
+ conf: conf,
+ ctxt: conf.Context,
+ fset: fset,
+ pkg: pkg,
+ Info: info,
+ version: asGoVersion(conf.GoVersion),
+ objMap: make(map[Object]*declInfo),
+ impMap: make(map[importKey]*Package),
}
}
// the syntactic information. We should consider storing
// this information explicitly in the object.
var alias bool
- if check.enableAlias {
+ if check.conf._EnableAlias {
alias = obj.IsAlias()
} else {
if d := check.objMap[obj]; d != nil {
if tname != nil && tname.IsAlias() {
// If we use Alias nodes, it is initialized with Typ[Invalid].
// TODO(gri) Adjust this code if we initialize with nil.
- if !check.enableAlias {
+ if !check.conf._EnableAlias {
check.validAlias(tname, Typ[Invalid])
}
}
versionErr = true
}
- if check.enableAlias {
+ if check.conf._EnableAlias {
// TODO(gri) Should be able to use nil instead of Typ[Invalid] to mark
// the alias as incomplete. Currently this causes problems
// with certain cycles. Investigate.
insertImportPath(f, `"go/ast"`)
renameSelectorExprs(f, "syntax.Expr->ast.Expr")
},
- "named.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
- "object.go": func(f *ast.File) { fixTokenPos(f); renameIdents(f, "NewTypeNameLazy->_NewTypeNameLazy") },
- "object_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
- "objset.go": nil,
+ "named.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
+ "object.go": func(f *ast.File) { fixTokenPos(f); renameIdents(f, "NewTypeNameLazy->_NewTypeNameLazy") },
+ // TODO(gri) needs adjustments for TestObjectString - disabled for now
+ // "object_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
+ "objset.go": nil,
"operand.go": func(f *ast.File) {
insertImportPath(f, `"go/token"`)
renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
-// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
-// Source: ../../cmd/compile/internal/types2/object_test.go
-
// Copyright 2016 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.
}
}
- if check.enableAlias {
+ if check.conf._EnableAlias {
// With Alias nodes we can process declarations in any order.
for _, obj := range objList {
check.objDecl(obj, nil)
x.mode = constant_
case *TypeName:
- if !check.enableAlias && check.isBrokenAlias(obj) {
+ if !check.conf._EnableAlias && check.isBrokenAlias(obj) {
check.errorf(e, InvalidDeclCycle, "invalid use of type alias %s in recursive type (see go.dev/issue/50729)", quote(obj.name))
return
}