aboutsummaryrefslogtreecommitdiff
path: root/src/go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2024-04-17 17:44:55 -0700
committerGopher Robot <gobot@golang.org>2024-04-18 14:10:44 +0000
commit9101bf19165ecde1967a0163d2fafa168e40ac6d (patch)
tree20a5df146ea3ff0c4db8a82818bdcd7f2e256d21 /src/go
parent334ce510046ad30b1be466634cf313aad3040892 (diff)
downloadgo-9101bf19165ecde1967a0163d2fafa168e40ac6d.tar.gz
go-9101bf19165ecde1967a0163d2fafa168e40ac6d.zip
go/types, types2: use types2.Config flag to control Alias node creation
Move Checker.enableAlias to Config.EnableAlias (for types2) and Config._EnableAlias (for go/types), and adjust all uses. Use Config.EnableAlias to control Alias creation for types2 and with that remove dependencies on the gotypesalias GODEBUG setting and problems during bootstrap. The only client is the compiler and there we simply use the desired configuration; it is undesirable for the compiler to be dependent on gotypesalias. Use the gotypesalias GODEBUG setting to control Config._EnableAlias for go/types (similar to before). Adjust some related code. We plan to remove gotypesalias eventually which will remove some of the new discrepancies between types2 and go/types again. Fixes #66874. Change-Id: Id7cc4805e7ea0697e0d023c7f510867e59a24871 Reviewed-on: https://go-review.googlesource.com/c/go/+/579935 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Robert Griesemer <gri@google.com>
Diffstat (limited to 'src/go')
-rw-r--r--src/go/types/api.go6
-rw-r--r--src/go/types/check.go34
-rw-r--r--src/go/types/decl.go6
-rw-r--r--src/go/types/generate_test.go9
-rw-r--r--src/go/types/object_test.go3
-rw-r--r--src/go/types/resolver.go2
-rw-r--r--src/go/types/typexpr.go2
7 files changed, 31 insertions, 31 deletions
diff --git a/src/go/types/api.go b/src/go/types/api.go
index 5b4f59c94e..cfe86f9dd6 100644
--- a/src/go/types/api.go
+++ b/src/go/types/api.go
@@ -180,6 +180,12 @@ type Config struct {
// 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) {
diff --git a/src/go/types/check.go b/src/go/types/check.go
index be990eabfe..87106c4d01 100644
--- a/src/go/types/check.go
+++ b/src/go/types/check.go
@@ -101,12 +101,6 @@ type actionDesc struct {
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
@@ -173,9 +167,9 @@ func (check *Checker) addDeclDep(to Object) {
// 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)
}
@@ -185,14 +179,14 @@ func (check *Checker) brokenAlias(alias *TypeName) {
// 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]
}
@@ -261,16 +255,18 @@ func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Ch
//
// (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),
}
}
diff --git a/src/go/types/decl.go b/src/go/types/decl.go
index 937163fc75..679dc1a136 100644
--- a/src/go/types/decl.go
+++ b/src/go/types/decl.go
@@ -242,7 +242,7 @@ loop:
// 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 {
@@ -314,7 +314,7 @@ func (check *Checker) cycleError(cycle []Object, start int) {
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])
}
}
@@ -583,7 +583,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName
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.
diff --git a/src/go/types/generate_test.go b/src/go/types/generate_test.go
index 2b0e4a4289..f3047b2846 100644
--- a/src/go/types/generate_test.go
+++ b/src/go/types/generate_test.go
@@ -144,10 +144,11 @@ var filemap = map[string]action{
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"`)
diff --git a/src/go/types/object_test.go b/src/go/types/object_test.go
index a9f7eed69c..43ff5b35e5 100644
--- a/src/go/types/object_test.go
+++ b/src/go/types/object_test.go
@@ -1,6 +1,3 @@
-// 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.
diff --git a/src/go/types/resolver.go b/src/go/types/resolver.go
index 69cc6ba154..f336057c53 100644
--- a/src/go/types/resolver.go
+++ b/src/go/types/resolver.go
@@ -665,7 +665,7 @@ func (check *Checker) packageObjects() {
}
}
- 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)
diff --git a/src/go/types/typexpr.go b/src/go/types/typexpr.go
index 79e4c0ab66..4bbc8b2448 100644
--- a/src/go/types/typexpr.go
+++ b/src/go/types/typexpr.go
@@ -109,7 +109,7 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *TypeName, wantType bo
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
}