]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/go/types/check.go
go/types, types2: implement Alias proposal (export API)
[gostls13.git] / src / go / types / check.go
index f9decbf9a0afc1940352cd43c8cf79ec2d52dea7..0feea6dfeba678815a8559497e643152439a79a1 100644 (file)
@@ -12,6 +12,7 @@ import (
        "go/ast"
        "go/constant"
        "go/token"
+       "internal/godebug"
        "internal/goversion"
        . "internal/types/errors"
 )
@@ -22,6 +23,9 @@ var nopos token.Pos
 // debugging/development support
 const debug = false // leave on during development
 
+// gotypesalias controls the use of Alias types
+var gotypesalias = godebug.New("gotypesalias")
+
 // exprInfo stores information about an untyped expression.
 type exprInfo struct {
        isLhs bool // expression is lhs operand of a shift with delayed type-check
@@ -94,6 +98,12 @@ 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
@@ -155,13 +165,13 @@ func (check *Checker) addDeclDep(to Object) {
 }
 
 // Note: The following three alias-related functions are only used
-//       when _Alias types are not enabled.
+//       when Alias types are not enabled.
 
 // brokenAlias records that alias doesn't have a determined type yet.
 // It also sets alias.typ to Typ[Invalid].
-// Not used if check.conf._EnableAlias is set.
+// Not used if check.enableAlias is set.
 func (check *Checker) brokenAlias(alias *TypeName) {
-       assert(!check.conf._EnableAlias)
+       assert(!check.enableAlias)
        if check.brokenAliases == nil {
                check.brokenAliases = make(map[*TypeName]bool)
        }
@@ -171,14 +181,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.conf._EnableAlias)
+       assert(!check.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.conf._EnableAlias)
+       assert(!check.enableAlias)
        return check.brokenAliases[alias]
 }
 
@@ -248,13 +258,14 @@ func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Ch
        // (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
 
        return &Checker{
-               conf:   conf,
-               ctxt:   conf.Context,
-               fset:   fset,
-               pkg:    pkg,
-               Info:   info,
-               objMap: make(map[Object]*declInfo),
-               impMap: make(map[importKey]*Package),
+               enableAlias: gotypesalias.Value() == "1",
+               conf:        conf,
+               ctxt:        conf.Context,
+               fset:        fset,
+               pkg:         pkg,
+               Info:        info,
+               objMap:      make(map[Object]*declInfo),
+               impMap:      make(map[importKey]*Package),
        }
 }
 
@@ -296,7 +307,7 @@ func (check *Checker) initFiles(files []*ast.File) {
 
        // collect file versions
        for _, file := range check.files {
-               check.recordFileVersion(file, check.conf.GoVersion)
+               check.recordFileVersion(file, check.conf.GoVersion) // record package version (possibly zero version)
                if v, _ := parseGoVersion(file.GoVersion); v.major > 0 {
                        if v.equal(check.version) {
                                continue